def read_input_rescale(dir_nm, num_mode, fl_nm): f = FortranFile("../ML/%s/%s.q" % (dir_nm, fl_nm)) f.read_ints() x, _, y, tot_num_mode = f.read_ints() X = f.read_reals() #first 3 dimension are shape of grid. last is the number of modes #grids are of shape 286,1,301 #fortran format - first index changing fastest X = X.reshape(x, y, tot_num_mode, order='F') #1 is for channel X = X.reshape(1, x, y, tot_num_mode) X = X[:, :, :, 0:num_mode] X = np.moveaxis(X, 3, 0) #only consider 76-226 for vertical grid which is necessary # and 76:676 for horizontal X = X[:, :, 76:226, 76:676] X_new = np.zeros((X.shape[0], 1, 150, 150)) #normalize and rescale for i in range(X.shape[0]): img = Image.fromarray(X[i][0]) img_rescaled = img.resize((150, 150)) X_new[i][0] = np.asarray(img_rescaled) for i in range(X.shape[0]): X_new[i] = (X_new[i] - X_new[i].mean()) / X_new[i].std() return X_new
def read_output(path, header_only=True): f = FortranFile(path, 'r') ncpu = f.read_ints() dim = f.read_ints() nparts = f.read_ints() if header_only: f.close() return ncpu, dim, nparts f.read_ints() f.read_ints() f.read_ints() f.read_ints() f.read_ints() x = f.read_reals(dtype=np.float64) y = f.read_reals(dtype=np.float64) z = f.read_reals(dtype=np.float64) vx = f.read_reals(dtype=np.float64) vy = f.read_reals(dtype=np.float64) vz = f.read_reals(dtype=np.float64) m = f.read_reals(dtype=np.float64) part_ids = f.read_ints() birth = f.read_reals(dtype=np.float32) f.close() return ncpu, dim, nparts, x, y, z, part_ids
def getgrid(DIR=' '): global t_max, dt, nx, ny, nz, xb, yb, zb, xc, yc, zc, \ alpha, rho_bcc, rho_bbb, rho_ccb, rho_cbc if DIR == ' ': filename = 'Data/grid.dat' else: filename = DIR + 'grid.dat' file = FortranFile(filename, 'r') t_max = file.read_reals(float)[0] dt = file.read_reals(float)[0] nx = file.read_ints(np.int32)[0] ny = file.read_ints(np.int32)[0] nz = file.read_ints(np.int32)[0] xb = file.read_reals(float) yb = file.read_reals(float) zb = file.read_reals(float) xc = file.read_reals(float) yc = file.read_reals(float) zc = file.read_reals(float) alpha = file.read_reals(float)[0] rho_bcc = file.read_reals(float).reshape((nz + 2, ny + 2, nx + 1), order="F") rho_bbb = file.read_reals(float).reshape((nz + 2, ny + 2, nx + 1), order="F") rho_ccb = file.read_reals(float).reshape((nz + 2, ny + 2, nx), order="F") rho_cbc = file.read_reals(float).reshape((nz + 2, ny + 2, nx), order="F")
def read_input(dir_nm, num_mode, fl_nm, is_normalize=True): f = FortranFile("../ML/%s/%s.q" % (dir_nm, fl_nm)) f.read_ints() f.read_ints() X = f.read_reals() #first 3 dimension are shape of grid. last is the number of modes #grids are of shape 286,1,301 #fortran format - first index changing fastest X = X.reshape(286, 301, 10, order='F') X = X.reshape(1, 286, 301, 10) #1 is for channel X = X[:, :, :, 0:num_mode] X = np.moveaxis(X, 3, 0) if (fl_nm == "plate_1"): xlow, ylow, xhigh, yhigh = 76, 76, 226, 226 elif (fl_nm == "plate_2"): xlow, ylow, xhigh, yhigh = 76, 132, 226, 282 else: print("not known file") #only consider 76-226 grid which is necessary X = X[:, :, ylow:yhigh, xlow:xhigh] #normalize if (is_normalize): for i in range(X.shape[0]): X[i] = (X[i] - X[i].mean()) / X[i].std() return X
def read_binary_fortran_file(name, datatype, dim0, dim1=1 ): input_array=np.ndarray((dim0*dim1)) if dim1 == 1 : fmat = FortranFile(name, 'r') if (datatype == "real"): input_array = fmat.read_reals(dtype=np.float64) if (datatype == "int"): input_array = fmat.read_ints(dtype=np.int32) print( name+"shape = " , input_array.shape) fmat.close() else : if (datatype == "real"): fmat = FortranFile(name, 'r') input_array = fmat.read_reals(dtype=np.float64) input_array = input_array.reshape((dim0,dim1)).transpose() fmat.close() elif (datatype == "int"): fmat = FortranFile(name, 'r') input_array = fmat.read_ints(dtype=np.int32) input_array = input_array.reshape((dim0,dim1)).transpose() fmat.close() elif ( datatype == "complex" ): sys.exit("reading of complex matrices is not directly possible, must write out real and imag parts " \ "as two seperate real matrices\n") else : sys.exit("reading of datatype \"" + datatype + "\" is not implemented\n ") return input_array
def __init__(self, normalcar='NormalCAR', wavecar='WAVECAR'): """ Initialize the class with supplied file name (including path) Only the records untile projector coefficients are read for initialize projector coefficients will only be read when explicitly invoked. Args: fnm (optional): NormalCAR filename (including path) Returns: None Raise: IOError if encounter problems reading NormalCAR ValueError if inconsistent record/info read Note: All 'private' members or attributes (starts with single underline) have names following the nice VASP style... with each has a corresponding a callable getXXX() method, where XXX might be easily understood... """ self._normalcar = normalcar self._wavecar = wavecar try: self._fh = FF(self._normalcar, 'r') except: raise IOError("Failed to open NormalCAR file: {}".format( self._fnm)) # rec 1 self._LMDIM, self._NIONS, self._NRSPINORS = FF.read_ints(self._fh) # rec 2 self._CQIJ = FF.read_reals(self._fh) dump = self._LMDIM * self._LMDIM * self._NIONS * self._NRSPINORS if len(self._CQIJ) != dump: raise ValueError( "Inconsistent dimension for CQIJ:\n" "\t# floats intend to read: {}\n\t# floats in this record". format(dump, len(self._CQIJ))) self._CQIJ = self._CQIJ.reshape( [self._LMDIM, self._LMDIM, self._NIONS, self._NRSPINORS], order='F') del dump # rec 3 self._NPROD, self._NPRO, self._NTYP = FF.read_ints(self._fh) # rec 4 ... 3 + NTypes self._LMMAX = [] for i in range(self._NTYP): self._LMMAX.append(FF.read_ints(self._fh)) self._LMMAX = np.array(self._LMMAX) self._NPROJ = np.sum(self._LMMAX[:, 0] * self._LMMAX[:, 1]) # read wavefunction stuff, number of kpts and bands _WC = wf.WAVECAR(self._wavecar) self._NK = _WC.getNKpts() self._NB = _WC.getNBands()
def siesta_read_coefficients(filename, debug=0): """ This routine reads siesta eigenvectors from MyM.WFSX binary using the FortranFile magical binary reader and returns an array with the following dimensions: 1) the k-point index 2) the spin index 3) the state index (nwflist) 4) the coefficient for each basis set index (nuotot) """ f = FortranFile("%s.WFSX" % filename) nk, gamma = f.read_ints() if debug: print "nk, gamma = ", nk, gamma nspin = int(f.read_ints()) if debug: print "nspin =", nspin nuotot = int(f.read_ints()) if debug: print "nuotot =", nuotot bla = f.read_record([('orbital_pos', 'i4'), ('b', '20S'), ('c', 'i4'), ('d', 'i4'), ('e', '20S')]) orbital_pos = np.array(bla['orbital_pos'] - 1, dtype=np.int) psi = np.zeros((nk, nspin, nuotot, nuotot)) psi = psi + 0j #separate condition for gamma point since the array is real if bool(gamma) == True: for iispin in range(1, nspin + 1): #for each spin f.read_record('f') ispin = int(f.read_ints()) nwflist = int(f.read_ints()) for iw in range(1, nwflist + 1): # for each state (nwflist = total number of states) indwf = f.read_ints() # we first read the energy of the state energy = f.read_reals('d') # and all the orbital coefficients read_psi = f.read_reals('f') read_psi = np.reshape(read_psi, (nuotot, 1)) psi[0, iispin - 1, iw - 1, :] = read_psi[:, 0] else: for iik in range(1, nk + 1): #for each k-point for iispin in range(1, nspin + 1): #for each spin f.read_record('f') ispin = int(f.read_ints()) nwflist = int(f.read_ints()) for iw in range(1, nwflist + 1): # for each state (nwflist = total number of states) indwf = f.read_ints() # we first read the energy of the state energy = f.read_reals('d') # and all the orbital coefficients (real value, followed by the imaginary value read_psi = f.read_reals('f') if debug: print "nutot", nuotot, 'len(read_psi)', len(read_psi) read_psi = np.reshape(read_psi, (nuotot, 2)) # reshape it # and make a row of complex numbers psi[iik - 1, iispin - 1, iw - 1, :] = read_psi[:, 0] + 1j * read_psi[:, 1] return psi, orbital_pos
def read(self): """ read data fortran binary. """ f = FortranFile(self.fname) nchan, type = f.read_ints(np.int32) self.channelIdx = f.read_ints(np.int32) if(type == 8): self.R = f.read_reals(np.float64).reshape((nchan,nchan), order="F") else: self.R = f.read_reals(np.float32).reshape((nchan,nchan), order="F") f.close()
def getdata(filenumber, variable, DIR=' '): if DIR == ' ': filename = 'Data/' + variable + '{:03d}'.format(filenumber) + '.dat' else: filename = DIR + variable + '{:03d}'.format(filenumber) + '.dat' file = FortranFile(filename, 'r') nx = file.read_ints(np.int32)[0] ny = file.read_ints(np.int32)[0] nz = file.read_ints(np.int32)[0] return file.read_reals(float).reshape((nz, ny, nx), order="C")
def read_cproj_NormalCar(inf='NormalCAR', save_cproj=True): ''' Read NormalCAR of VASP output, which contains the coefficients of the PAW projector functions. Data stored in NormalCAR: WRITE(IU) LMDIM,WDES%NIONS,WDES%NRSPINORS WRITE(IU) CQIJ(1:LMDIM,1:LMDIM,1:WDES%NIONS,1:WDES%NRSPINORS) WRITE(IU) WDES%NPROD, WDES%NPRO, WDES%NTYP DO NT = 1, WDES%NTYP WRITE(IU) WDES%LMMAX(NT), WDES%NITYP(NT) END DO DO ISPIN=1,WDES%ISPIN DO NK=1,WDES%NKPTS DO N=1,WDES%NB_TOT WRITE(IU) CPROJ(1:WDES1%NPRO_TOT) END DO END DO END DO ''' from scipy.io import FortranFile ncr = FortranFile(inf, 'r') # rec1 = ncr.read_record(dtype=np.int32) rec1 = ncr.read_ints() lmdim, nions, nrspinors = rec1 # rec2 = ncr.read_record(dtype=np.complex) cqij = np.array(ncr.read_reals()).reshape((lmdim, lmdim, nions, nrspinors), order='F') rec3 = ncr.read_ints() nprod, npro, ntyp = rec3 # lmmax, natoms for each type of elements lmmax_per_typ = np.array([ncr.read_ints() for ii in range(ntyp)]) cproj = [] while True: try: rec = ncr.read_record(dtype=np.complex) cproj.append(rec) except: break cproj = np.array(cproj) if save_cproj: np.save('cproj', cproj) ncr.close() return cproj
def read_data_from_file(filepath): f = FortranFile(filepath, 'r') f.read_ints(np.int32)[0] # ng: number of geometries (jd, kd, ld, nq, nqc) = tuple(f.read_ints(np.int32)) type = np.array([np.dtype('<f8')] * 16) type[7] = np.dtype('<i4') (fm, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) = tuple(f.read_record(*type)) # fm, a, re, t, gamma, beta, tinf, igamma, htinf, ht1, ht2, rgas1, rgas2, refmach, tvref, dtvref)) data = f.read_reals(dtype=np.float64).reshape((nq, ld, kd, jd)) return (jd, kd, ld), fm, data[:, :, 1, :] # taking 2D data (middle in y direction)
def read_one_cpu(output): f = FF(output) ncpu = f.read_ints() ndim = f.read_ints() npart = f.read_ints() localseed = f.read_ints() nstart_tot = f.read_ints() mstar_tot = f.read_ints() mstart_lost = f.read_ints() nsink = f.read_ints() x = np.zeros((ndim, npart), dtype=float) v = np.zeros((ndim, npart), dtype=float) m = np.zeros((npart), dtype=float) ind = np.zeros((npart), dtype=int) for dim in range(ndim): x[dim] = f.read_reals() for dim in range(ndim): v[dim] = f.read_reals() m = f.read_reals() ind = f.read_ints() lvl = f.read_ints() try: tp = f.read_reals() Z = f.read_reals() except TypeError: tp = np.zeros((npart)) Z = np.zeros((npart)) return ind, ndim, npart, x, v, m, lvl, tp, Z
def split_positions(centres_filename, filter_filename, handle, quantiles): ''' Splits a set of input positions in different quantiles, according to the local galaxy density. ''' print('\nSplitting positions using the following arguments:') print('centres: {}'.format(centres_filename)) print('filter_filename: {}'.format(filter_filename)) print('quantiles: {}'.format(quantiles)) # open centres file and get dimensions f = FortranFile(centres_filename, 'r') nrows = f.read_ints()[0] ncols = f.read_ints()[0] print('nrows, ncols= ({}, {})'.format(nrows, ncols)) # read raw data and close file centres = f.read_reals(dtype=np.float64).reshape(nrows, ncols) f.close() # open filter file f = FortranFile(filter_filename, 'r') ncentres = f.read_ints()[0] print('ncentres: {}'.format(ncentres)) smoothed_delta = f.read_reals(dtype=np.float64) idx = np.argsort(smoothed_delta) f.close() # sort profiles according to Delta(r=20mpc/h) sorted_centres = centres[idx] # divide profiles by their Delta(r=20mpc/h) binned_centres = {} for i in range(1, quantiles + 1): binned_centres['den{}'.format(i)] = sorted_centres[int( (i - 1) * ncentres / quantiles):int(i * ncentres / quantiles)] if handle != None: output_file = handle + '_DS{}'.format(i) + '.unf' else: output_file = centres_filename.split('.unf')[0] + '_DS{}'.format( i) + '.unf' cout = binned_centres['den{}'.format(i)] print('Shape of cout: {}'.format(np.shape(cout))) f = FortranFile(output_file, 'w') f.write_record(np.shape(cout)[0]) f.write_record(np.shape(cout)[1]) f.write_record(cout) f.close()
def read_fortFFT(file=None): ''' Read FFT grid from fortran output and return delta[i_k,j_k,l_k] ''' f = FortranFile(file, 'r') Ngrid = f.read_ints(dtype=np.int32)[0] delt = f.read_reals(dtype=np.complex64) delt = np.reshape(delt, (Ngrid / 2 + 1, Ngrid, Ngrid), order='F') # reflect half field delta = np.zeros((Ngrid, Ngrid, Ngrid), dtype=np.complex64) # i = 0 - Ngrid // 2 (confirmed correct) delta[:Ngrid // 2 + 1, :, :] = delt[:, :, :] # i = Ngrid // 2 - Ngrid (confirmed corect) delta[:Ngrid // 2:-1, Ngrid:0:-1, Ngrid:0:-1] = np.conj(delt[1:Ngrid // 2, 1:Ngrid, 1:Ngrid]) delta[:Ngrid // 2:-1, Ngrid:0:-1, 0] = np.conj(delt[1:Ngrid // 2, 1:Ngrid, 0]) delta[:Ngrid // 2:-1, 0, Ngrid:0:-1] = np.conj(delt[1:Ngrid // 2, 0, 1:Ngrid]) # reflect the x-axis delta[:Ngrid // 2:-1, 0, 0] = np.conj(delt[1:Ngrid // 2, 0, 0]) hg = Ngrid // 2 delta[hg, 0, 0] = np.real(delt[hg, 0, 0]) delta[0, hg, 0] = np.real(delt[0, hg, 0]) delta[0, 0, hg] = np.real(delt[0, 0, hg]) delta[0, hg, hg] = np.real(delt[0, hg, hg]) delta[hg, 0, hg] = np.real(delt[hg, 0, hg]) delta[hg, hg, 0] = np.real(delt[hg, hg, 0]) delta[hg, hg, hg] = np.real(delt[hg, hg, hg]) return delta
def getstep(filenumber, DIR=' '): if DIR == ' ': filename = 'Data/step' '{:03d}'.format(filenumber) + '.dat' else: filename = DIR + 'step' + '{:03d}'.format(filenumber) + '.dat' file = FortranFile(filename, 'r') return file.read_ints(np.int32)[0]
def ReadArray_FortranBinary(filename, D): """ This function allows the user to read in a D-dimensional array from unformatted Fortran binary. Example input file format for 2D array: Line | Entry | Data Type ------------------------------------ 1 | nrows | int32 2 | ncols | int32 3 | A(1,1) | double 4 | A(2,1) | double . | . | . . | . | . . | . | . end | A(nrows,ncols)| double """ import numpy as np from scipy.io import FortranFile f = FortranFile(filename, 'r') sz = np.zeros(D) for i in range(0, D): sz[i] = f.read_ints(dtype=np.int32) A = f.read_reals(dtype=np.float64) f.close() A = A.reshape(sz.astype(int), order='F') A = np.transpose(A) return (A)
def generate_positions(data_filename, centres_filename, npositions, sampling, boxsize): ''' Generates random points on a box writes them to an unformatted Fortran 90 file. ''' np.random.seed(0) if sampling == 'tracers': print('Randoms will be generated from galaxy positions.') fin = FortranFile(data_filename, 'r') nrows = fin.read_ints()[0] ncols = fin.read_ints()[0] pos = fin.read_reals(dtype=np.float64).reshape(nrows, ncols) idx = np.random.choice(nrows, size=npositions, replace=False) cout = pos[idx] elif sampling == 'uniform': print('Randoms will be generated from a uniform distribution.') x = np.random.uniform(0, boxsize, npositions) y = np.random.uniform(0, boxsize, npositions) z = np.random.uniform(0, boxsize, npositions) cout = np.c_[x, y, z] else: sys.exit('Sampling type not recognized') cout = cout.astype('float64') f = FortranFile(centres_filename, 'w') nrows, ncols = np.shape(cout) f.write_record(nrows) f.write_record(ncols) f.write_record(cout) f.close()
def read_ibool_from_folder(folder, rank=0, ngll=5): filename = data_filename(folder, "NSPEC_ibool", rank) f = FortranFile(filename, "r") nspec = f.read_ints()[0] d = f.read_record("({},{},{})i4".format(nspec, ngll, ngll)) f.close() return d
def read_binary_fortran_file(name, datatype, dim0, dim1=1, real_precision=np.float64): if dim1 == 1: fmat = FortranFile(name, 'r') input_array = fmat.read_reals(dtype=real_precision) fmat.close() else: if datatype == "real": fmat = FortranFile(name, 'r') input_array = fmat.read_reals(dtype=real_precision) input_array = input_array.reshape((dim0, dim1)).transpose() fmat.close() elif datatype == "int": fmat = FortranFile(name, 'r') input_array = fmat.read_ints(dtype=np.int32) input_array = input_array.reshape((dim0, dim1)).transpose() # fmat.close() elif datatype == "complex": sys.exit( "reading of complex matrices is not directly possible, must write out real and imag parts " "as two separate real matrices\n") else: sys.exit("reading of datatype \"" + datatype + "\" is not implemented\n ") return input_array
def dacget1d(file): ff = FortranFile(file,"r") unity = ff.read_ints() mver = ff.read_ints() mtype = ff.read_ints() mndim = ff.read_ints() mdim = ff.read_ints() if mtype == 4: data = ff.read_ints(dtype=np.int32) if mtype == 5: data = ff.read_reals(dtype=np.float32) if mtype == 6: data = ff.read_reals(dtype=np.float64) return data
def read_halo_list(listfile): haloFile = FortranFile(listfile, 'r') nhalos, columns = haloFile.read_ints() _tmp = (haloFile.read_reals(dtype=np.float32)).reshape((columns, nhalos)).transpose() halos = pd.DataFrame(_tmp, columns=['id', 'level', 'mass', 'x', 'y', 'z', 'rvir']) halos[['id', 'level']] = halos[['id', 'level']].astype(int) return halos
def read_association(listfile): assocFile = FortranFile(listfile, 'r') nassoc, columns = assocFile.read_ints() _tmp = (assocFile.read_reals(dtype=np.float32)).reshape((columns, nassoc)).transpose() assoc = pd.DataFrame(_tmp, columns=['halo_id', 'level', 'halo_mass', 'gal_id', 'gal_mass']) assoc[['halo_id', 'level', 'gal_id']] = assoc[['halo_id', 'level', 'gal_id']].astype(np.int32) return assoc
def filtered_density(data_filename1, data_filename2, output_filename, filter_type, filter_size, ngrid, box_size, nthreads=1, dim1_min=0, dim1_max=None, output_format='unformatted'): # check if files exist if not path.isfile(data_filename1): raise FileNotFoundError(f'{data_filename1} does not exist.') if not path.isfile(data_filename2): raise FileNotFoundError(f'{data_filename2} does not exist.') if dim1_max == None: if filter_type == 'tophat': dim1_max = filter_size elif filter_type == 'gaussian': dim1_max = 5 * filter_size binpath = path.join(path.dirname(__file__), 'bin', '{}_filter.exe'.format(filter_type)) cmd = [ binpath, data_filename1, data_filename2, output_filename, str(box_size), str(dim1_min), str(dim1_max), str(filter_size), str(ngrid), str(nthreads) ] subprocess.call(cmd) # open filter file f = FortranFile(output_filename, 'r') smoothed_delta = f.read_ints()[0] smoothed_delta = f.read_reals(dtype=np.float64) f.close() if output_format != 'unformatted': if output_format == 'npy': subprocess.call(['rm', output_filename]) np.save(output_filename, smoothed_delta) elif output_format == 'ascii': np.savetxt(output_filename, smoothed_delta) else: print('Output format not recognized. Using unformatted F90 file.') return smoothed_delta
def dacget3s(file): ff = FortranFile(file,"r") unity = ff.read_ints() mver = ff.read_ints() mtype = ff.read_ints() mndim = ff.read_ints() mdim = ff.read_ints() if mtype == 4: data = ff.read_ints(dtype=np.int32) if mtype == 5: data = ff.read_reals(dtype=np.float32) if mtype == 6: data = ff.read_reals(dtype=np.float64) ff.close() return data.reshape(mdim[3],mdim[2],mdim[1],mdim[0]), mdim[0], mdim[1], mdim[2]
def tmp(): ff = FortranFile(filename) h = {} h["nbodies"] = ff.read_ints() h["massp"] = ff.read_ints() h["aexp"] = ff.read_reals(dtype=np.int32) h["omega_t"] = ff.read_reals(dtype=np.int32) h["age_univ"] = ff.read_reals(dtype=np.int32) h["n_halos"], h["n_subhalos"] = ff.read_ints() for i in tqdm(range(h["n_halos"] + h["n_subhalos"])): infos = { "header": h } infos["nparts"] = ff.read_ints() infos["members"] = ff.read_ints() infos["idh"] = ff.read_ints() infos["timestep"] = ff.read_ints() infos["hlevel"], infos["hosthalo"], infos["hostsub"], infos["nbsub"], infos["nextsub"] = ff.read_ints() infos["mhalo"] = ff.read_reals(dtype=np.int32) infos["pos"] = ff.read_reals(dtype=np.int32) infos["speed"] = ff.read_reals(dtype=np.int32) infos["L"] = ff.read_reals(dtype=np.int32) infos["r"], infos["a"], infos["b"], infos["c"] = ff.read_reals(dtype=np.int32) infos["ek"], infos["ep"], infos["et"] = ff.read_reals(dtype=np.int32) infos["spin"] = ff.read_reals(dtype=np.int32) if not dm_type: ff.read_reals() infos["rvir"],infos["mvir"], infos["tvir"], infos["cvel"] = ff.read_reals(dtype=np.int32) ff.read_reals() if not dm_type: infos["npoints"] = ff.read_ints() infos["rdum"] = ff.read_reals(dtype=np.int32) infos["density"] = ff.read_reals(dtype=np.int32) if low_mem != None: try: keys = list(low_mem) except: keys = ['nparts', 'members'] tmp = {} for key in keys: try: tmp[key] = infos[key] except KeyError: print('Invalid key {}, can be any of', infos['keys']) yield tmp else: yield infos ff.close()
def readtmx(fname='triples.tmx',numTJ=60000): """ Parameter: numTJ: int number of triple junctions Returns: huge_kcell: ndarray (numTJ,3,36) Cell indices that have non-zero coefficient in A, data type in this array is integer. First dimension is the TJ index, second is the boundary index, third is the equivalent point. Using notations in paper, it is (J,s,?) huge_coeff: ndarray (numTJ,3,36,3,3) Coefficients in A, data type is float. Using notations in paper, it is (J,s,?,i,l) """ inttype=np.int32 floattype=np.float32 #headertype=np.uint32 headertype=np.uint64 f = FortranFile(fname, 'r',header_dtype=headertype) number_of_points=numTJ huge_coeff=np.empty((number_of_points,3,36,3,3)).astype(floattype) huge_kcell=np.empty((number_of_points,3,36)).astype(inttype) for nn in range(number_of_points): first=f.read_ints(dtype=inttype) for ss in range(3): huge_kcell[nn,ss]=f.read_ints(dtype=inttype) for kk in range(36): #Tested: Fortran is the column first order, python is row first. #The transpose makes huge_coeff[nn,ss,kk,i,l] equals the xcoeffo(ss,kk,i,l) in Morawiec's reconstruction code huge_coeff[nn,ss,kk]=f.read_record(dtype=floattype).reshape((3,3)).transpose() for kk in range(36): for ii in range(3): huge_coeff[nn,:,kk,ii,:]=huge_coeff[nn,:,kk,ii,:]/(np.sum(huge_coeff[nn,:,kk,ii,:]**2))**0.5 return huge_kcell,huge_coeff
def read_fortran_FFTfield(infile): """ Read a Half-Field with FFTW indexing from a Fortran Unformatted Binary file. The first entry is a single integer. """ f=FortranFile(infile,'r') Ngrid=f.read_ints(dtype=np.int32)[0] print('Fortran file Ngrid='+str(Ngrid)) dcr=f.read_reals(dtype=np.complex64) dcr=np.reshape(dcr,(Ngrid//2+1,Ngrid,Ngrid),order='F') dcr.dump(infile+'.pickle') # Save infile as a pickle return dcr
def read_fortran_FFTfield(self): """ Read a fortran binary file from FFTW assert all Nyquist entries to be real. """ f=FortranFile(self.infile,'r') Ng=f.read_ints(dtype=np.int32)[0] print('Fortran file Ngrid='+str(Ng)) if (Ng != self.Ngrid): print('Ngrid values are not equal!') dcr=f.read_reals(dtype=np.complex64) dcr=np.reshape(dcr,(Ng//2+1,Ng,Ng),order='F') return dcr
def particles_in_halo(tree_brick, start=0, end=None, fun_filter=lambda x: True): ''' Open a tree bricks file and associate to each halo the corresponding particles. ''' # Open file f = FortranFile(tree_brick, 'r') # Give a value to end, by default start + 1 if end == None: end = start + 1 # Read headers nbodies = f.read_ints()[0] f.read_reals(dtype=np.float32) aexp = f.read_reals(dtype=np.float32) f.read_reals(dtype=np.float32) age = f.read_reals(dtype=np.float32) nhalo, nsubhalo = f.read_ints() halo_tot = nhalo + nsubhalo halos = {} for i in tqdm(range(halo_tot)): parts = f.read_ints()[0] members = f.read_ints() this_id = f.read_ints()[0] if (start <= this_id and this_id < end and fun_filter(this_id)): for dm_particle_id in members: if not halos.has_key(this_id): halos[this_id] = [] halos[this_id].append(dm_particle_id) elif this_id >= end: break f.read_ints() # Irrelevant level, hosthalo, hostsub, nbsub, nextsub = f.read_ints() mstar = 1e11 * f.read_reals(dtype=np.float32) px, py, pz = f.read_reals(dtype=np.float32) f.read_reals(dtype=np.float32) f.read_reals(dtype=np.float32) rad = f.read_reals(dtype=np.float32)[0] f.read_reals(dtype=np.float32) f.read_reals(dtype=np.float32) rvir, mvir, tvir, cvel = f.read_reals(dtype=np.float32) f.read_reals(dtype=np.float32) f.close() return halos
def read_galaxy_list(listfile): galFile = FortranFile(listfile, 'r') print(listfile) ngal, columns = galFile.read_ints() _tmp = (galFile.read_reals(dtype=np.float32)).reshape((columns, ngal)).transpose() galaxies = pd.DataFrame(_tmp, columns=['id', 'vt', 'dvz', 'dvr', 'dvtheta', 'mass', 'x', 'y', 'z']) galaxies.id.astype(int) galaxies['sigma'] = 1/3.*np.sqrt(galaxies.dvz**2 + galaxies.dvtheta**2 + galaxies.dvr**2) galaxies['sigmaoverv'] = galaxies.sigma / galaxies.vt galaxies['elliptic'] = galaxies.sigmaoverv > 1.5 galaxies['spiral'] = galaxies.sigmaoverv < 0.8 return galaxies
def read_input_full(dir_nm, fl_nm, is_normalize=True): f = FortranFile("../ML/%s/%s.q" % (dir_nm, fl_nm)) f.read_ints() f.read_ints() X = f.read_reals() #first 3 dimension are shape of grid. last is the number of modes #grids are of shape 286,1,301 #for plate_1 and plate_2 there are 10 modes while plate_0 has grid for # 2 variables - deflection and pressure #fortran format - first index changing fastest if (fl_nm == "plate_1" or fl_nm == "plate_2"): X = X.reshape(286, 301, 10, order='F') X = X.reshape(1, 286, 301, 10) elif (fl_nm == "plate_0"): X = X.reshape(286, 301, 2, order='F') X = X.reshape(1, 286, 301, 2) X = np.moveaxis(X, 3, 0) #normalize if (is_normalize): for i in range(X.shape[0]): X[i] = (X[i] - X[i].mean()) / X[i].std() return X
def read_unformatted(filename): ''' Reads an unformatted Fortran 90 files as a numpy array. Parameters: filename: str Name of the unformatted file. ''' fin = FortranFile(filename, 'r') nrows = fin.read_ints()[0] ncols = fin.read_ints()[0] data = fin.read_reals(dtype=np.float64).reshape(nrows, ncols) return data, nrows, ncols
class SIMRA3DMeshReader(SIMRAMeshReader): reader_name = "SIMRA-3D" filename: Path mesh: FortranFile nodeshape: Shape @classmethod def applicable(cls, filename: Path) -> bool: _, u4_type = dtypes(config.input_endianness) try: with FortranFile(filename, 'r', header_dtype=u4_type) as f: assert f._read_size() == 6 * 4 return True except: return False def __init__(self, filename: Path): super().__init__() self.filename = filename def __enter__(self): self.mesh = FortranFile(self.filename, 'r', header_dtype=self.u4_type).__enter__() with save_excursion(self.mesh._fp): _, _, imax, jmax, kmax, _ = self.mesh.read_ints(self.u4_type) if config.fix_orientation: self.nodeshape = (jmax, imax, kmax) else: self.nodeshape = (imax, jmax, kmax) return self def __exit__(self, *args): self.mesh.__exit__(*args) @cache(1) def patch(self) -> Patch: i, j, k = self.nodeshape return Patch(('geometry',), StructuredTopology((j-1, i-1, k-1), celltype=Hex())) @cache(1) def nodes(self) -> Array2D: with save_excursion(self.mesh._fp): fortran_skip_record(self.mesh) nodes = transpose(self.mesh.read_reals(self.f4_type), self.nodeshape) nodes = ensure_native(nodes) return translate(self.filename.parent, nodes)
def getSimulationData(dotDatFile): try: f = FortranFile(dotDatFile, 'r') except: raise SystemExit # get simulation parameters [Te_sta, Te_end, ne] = f.read_reals(dtype=np.float64) simParams = [Te_sta, Te_end, ne] # get initial and final fractions for equilibrium ionization calculation fraction_initial_ei = f.read_reals(dtype=np.float64).reshape( 30, 30) # reshape for 30 elements by 30 ions grid fraction_ei_final = f.read_reals(dtype=np.float64).reshape(30, 30) n_timeSteps = f.read_ints() times = [] NEI_fractions = [] # add initial fractions to lists times.append(0.0) NEI_fractions.append(fraction_initial_ei) # loop through the data and append to our lists for i in range( n_timeSteps[0] ): # unfortunately n_timeSteps in an array with a single element time = f.read_reals(dtype=np.float64) current_nei_fractions = f.read_reals(dtype=np.float64).reshape(30, 30) times.append(time[0]) NEI_fractions.append(current_nei_fractions) # add the final ei fractions to the list NEI_fractions.append(fraction_ei_final) #*** note that the NEI_fractions list now has one extra element as it isn't reasonable to attach a time to the #*** final equilibrium ionization fractions simData = { 'times': times, 'fractions': NEI_fractions, 'simParams': simParams } return simData
def readRamsesSEDs(sedDir): """Read SED in ramses format and return Parameters: ---------------------------------------------------------------------- sedDir: Directory containing the SED tables """ from scipy.io import FortranFile # Read metallicity bins ZFile = open(sedDir + '/metallicity_bins.dat', 'r') nZ = eval(ZFile.readline()) ZBins = [] for Z in range(0, nZ): ZBins.append(eval(ZFile.readline())) ZFile.close() # Read age bins ageFile = open(sedDir + '/age_bins.dat', 'r') nAge = eval(ageFile.readline()) ageBins = [] for age in range(0, nAge): ageBins.append(eval(ageFile.readline())) ageFile.close() # Read wavelength bins and spectra sedFile = FortranFile(sedDir + '/all_seds.dat', 'r') nLambda = sedFile.read_ints()[0] lambdaBins = sedFile.read_reals() spectra = np.empty([nLambda, nAge, nZ]) for iZ in range(0, nZ): for iAge in range(0, nAge): spectrum = sedFile.read_reals() spectra[:, iAge, iZ] = c.Lsun_cgs * spectrum return { 'ZBins': ZBins, 'ageBins': ageBins, 'lambdaBins': lambdaBins, 'spectra': spectra, 'nLambda': nLambda } #spectra are in erg/s/A/Msun
def __init__(self, element='Fe', temperature=None): """Read in the """ self._element = element self._temperature = temperature if self._temperature: self._index = self._get_temperature_index(temperature) data_dir = __path__[0] + '/data/eigenvaluetables/chianti8/' filename = data_dir + element.lower() + 'eigen.dat' eigenfile = FortranFile(filename, 'r') ntemp, atomic_numb = eigenfile.read_ints(np.int32) nstates = atomic_numb + 1 self._ntemp = ntemp self._atomic_numb = atomic_numb self._nstates = nstates self._temperature_grid = eigenfile.read_reals(np.float64) self._equilibrium_states = \ eigenfile.read_reals(np.float64).reshape((ntemp, nstates)) self._eigenvalues = \ eigenfile.read_reals(np.float64).reshape((ntemp, nstates)) self._eigenvectors = \ eigenfile.read_reals(np.float64).reshape(ntemp, nstates, nstates) self._eigenvector_inverses = \ eigenfile.read_reals(np.float64).reshape(ntemp, nstates, nstates) self._ionization_rate = \ eigenfile.read_reals(np.float64).reshape((ntemp, nstates)) self._recombination_rate = \ eigenfile.read_reals(np.float64).reshape((ntemp, nstates))
def fileHandling(fileName): """Define Variables of Interest from a given fortran file. Keyword arguments: fileName -- Name of fortran file for unpacking (string) [MAXVIEW,MAXLAYER,MAXKERN,NVIEW,NLAYER,NKERN,IINT,ISRF_PERT,PHI0,XMU0,THETAV,C22RA,S22RA,RV11,RV21,RV31] -- All desired variables packed into a single list (list) """ f = FortranFile(fileName, 'r') # CALLING THE read_ints AND read_reals FUNCTIONS IS A WAY OF READING THE FORTRAN FILE LINE BY LINE # WE THEN UNPACK EACH LINE BASED ON THE VARIABLES CONTAINED IN THAT LINE (AS OUTLINED IN README) firstLine = f.read_ints( np.int32 ) # CONTAINS MAXVIEW,MAXLAYER,MAXKERN,NVIEW,NLAYER,NKERN,IINT,ISRF_PERT secondLine = f.read_reals(np.float64) # CONTAINS PHI0,XMU0 thirdLine = f.read_reals(np.float64) # CONTAINS THETAV,C22RA,S22RA fourthLine = f.read_reals(np.float64) # CONTAINS RV11,RV21,RV31 # Note that we must use extra array slicing when defining THETAV, C22RA, S22RA, RV11, RV21, and RV31, as # these variables have trailing zeros at the end of them due to a difference in the true number of angles # used and the space allotted to the array based on MAXVIEW. MAXVIEW, MAXLAYER, MAXKERN, NVIEW, NLAYER, NKERN, IINT, ISRF_PERT = firstLine PHI0, XMU0 = secondLine THETAV = np.trim_zeros(thirdLine[:MAXVIEW], "b") C22RA = thirdLine[MAXVIEW:2 * MAXVIEW] C22RA = C22RA[:len(THETAV)] S22RA = thirdLine[2 * MAXVIEW:3 * MAXVIEW] S22RA = S22RA[:len(THETAV)] RV11 = fourthLine[:MAXVIEW] RV11 = RV11[:len(THETAV)] RV21 = fourthLine[MAXVIEW:2 * MAXVIEW] RV21 = RV21[:len(THETAV)] RV31 = fourthLine[2 * MAXVIEW:3 * MAXVIEW] RV31 = RV31[:len(THETAV)] return MAXVIEW, MAXLAYER, MAXKERN, NVIEW, NLAYER, NKERN, IINT, ISRF_PERT, PHI0, XMU0, THETAV, C22RA, S22RA, RV11, RV21, RV31
def read_atomic_data(elements=['H', 'He', 'C', # twelve most abundant elements 'N', 'O', 'Ne', 'Mg', 'Si', 'S', 'Ar', 'Ca', 'Fe', ] , data_directory= 'sunnei/AtomicData', # not robust! Works when calling from the directory that sunnei is in screen_output=False): ''' This routine reads in the atomic data to be used for the non-equilibrium ionization calculations. Instructions for generating atomic data files ============================================= The atomic data files are generated from the routines described by Shen et al. (2015) and are available at: https://github.com/ionizationcalc/time_dependent_fortran First, run the IDL routine 'pro_write_ionizrecomb_rate.pro' in the subdirectory sswidl_read_chianti with optional parameters: nte (number of temperature bins, default=501), te_low (low log temperature, default=4.0), and te_high (high log temperature, default=9.0) to get an ionization rate table. The routine outputs the file "ionrecomb_rate.dat" which is a text file containing the ionization and recombination rates as a function of temperature. This routine requires the atomic database Chianti to be installed in IDL. Second, compile the Fortran routine 'create_eigenvmatrix.f90'. With the Intel mkl libraries it is compiled as: "ifort -mkl create_eigenvmatrix.f90 -o create.out" which can then be run with the command "./create.out". This routine outputs all the eigenvalue tables for the first 28 elements (H to Ni). As of 2016 April 7, data from Chianti 8 is included in the CMEheat/AtomicData subdirectory. ''' if screen_output: print('read_atomic_data: beginning program') from scipy.io import FortranFile ''' Begin a loop to read in the atomic data files needed for the non-equilibrium ionization modeling. The information will be stored in the atomic_data dictionary. For the first element in the loop, the information that should be the same for each element will be stored at the top level of the dictionary. This includes the temperature grid, the number of temperatures, and the number of elements. For all elements, read in and store the arrays containing the equilibrium state, the eigenvalues, the eigenvectors, and the eigenvector inverses. ''' atomic_data = {} first_element_in_loop = True for element in elements: if screen_output: print('read_atomic_data: '+element) AtomicNumber = AtomicNumbers[element] nstates = AtomicNumber + 1 filename = data_directory + '/' + element.lower() + 'eigen.dat' H = FortranFile(filename, 'r') nte, nelems = H.read_ints(np.int32) temperatures = H.read_reals(np.float64) equistate = H.read_reals(np.float64).reshape((nte,nstates)) eigenvalues = H.read_reals(np.float64).reshape((nte,nstates)) eigenvector = H.read_reals(np.float64).reshape((nte,nstates,nstates)) eigenvector_inv = H.read_reals(np.float64).reshape((nte,nstates,nstates)) c_rate = H.read_reals(np.float64).reshape((nte,nstates)) r_rate = H.read_reals(np.float64).reshape((nte,nstates)) if first_element_in_loop: atomic_data['nte'] = nte atomic_data['nelems'] = nelems # Probably not used but store anyway atomic_data['temperatures'] = temperatures first_element_in_loop = False else: assert nte == atomic_data['nte'], 'Atomic data files have different number of temperature levels: '+element assert nelems == atomic_data['nelems'], 'Atomic data files have different number of elements: '+element assert np.allclose(atomic_data['temperatures'],temperatures), 'Atomic data files have different temperature bins' atomic_data[element] = {'element':element, 'AtomicNumber':AtomicNumber, 'nstates':nstates, 'equistate':equistate, 'eigenvalues':eigenvalues, 'eigenvector':eigenvector, 'eigenvector_inv':eigenvector_inv, 'ionization_rate':c_rate, 'recombination_rate':r_rate, } if screen_output: print('read_atomic_data: '+str(len(elements))+' elements read in') print('read_atomic_data: complete') return atomic_data
#------------------------------------------------------------------------------ # open file and read #------------------------------------------------------------------------------ # Parameters path_eigentb = '/Users/ccai/Works/Project/Ionization_calc/Code_develop/\ ionization_code_reu2016/python_script/chianti_8/' element = 'o' file_name = element+'eigen.dat' # Open file file_eigentb = path_eigentb + file_name f = FortranFile(file_eigentb, 'r') # Read file [nte, natom]=f.read_ints(dtype=np.int32) te_arr = f.read_reals(dtype=np.float64) eqistate = f.read_reals(dtype=np.float64).reshape((natom+1, nte), order='F') eigenvals = f.read_reals(dtype=np.float64).reshape((natom+1, nte), order='F') eigenvector = f.read_reals(dtype=np.float64).reshape((natom+1, natom+1, nte), order='F') eigenvector_invers = f.read_reals(dtype=np.float64).reshape((natom+1, natom+1, nte), order='F') # Close file f.close() # Note from Nick: I copied the next three routines to time_advance.py # but am leaving these also here for now. #------------------------------------------------------------------------------
parser.add_argument('--in', dest='infile', type=str, help='Path to the output file of compute_extrema (in hdf format)', required=True) parser.add_argument('--out', '-o', type=str, help='Prefix of the outputs') parser.add_argument('--infofile', type=str, help='Path to the information file (the one containing units of RAMSES, …)') args = parser.parse_args() # read the info file infos = dict() infos['headers'] = pd.read_csv(args.infofile, sep=' *= *', nrows=19, names=['key', 'value'], index_col='key').T infos['domain'] = pd.read_csv(args.infofile, delim_whitespace=True, skiprows=20) # read the center from scipy.io import FortranFile ff = FortranFile('data/halo_536-centers.bin') ff.read_ints() # don't care outputs = ff.read_ints() centers = ff.read_reals().reshape(len(outputs), 3) mins = ff.read_reals().reshape(len(outputs), 3) span = ff.read_reals().reshape(len(outputs), 3) maxs = ff.read_reals().reshape(len(outputs), 3) # create the output dir if required # if not os.path.isdir(args.out): # os.mkdir(args.out) HDF = pd.HDFStore(args.infile) # read the data df = HDF['extremas'] dens = HDF['dens']
def main(sourcedir, sourcefile, savedir, saveheader, moving=0, incr=1, imin=0, imax=-1 ): """ imin --> minimum iteration above which files will be written incr --> iteration interval to write files at """ # ogdir = os.getcwd() # os.chdir(savedir) MakeOutputDir(savedir) #link in source file if not os.path.isfile('{}/{}'.format(savedir, sourcefile)): # cmd('ln -s {}/{} {}'.format(sourcedir, sourcefile, sourcefile)) cmd('ln -s {}/{} {}/{}'.format(sourcedir, sourcefile, savedir, sourcefile)) #GET GRID/FILE DIMENSIONS f = FortranFile('{}/{}'.format(savedir, sourcefile), 'r') dims = f.read_ints('uint32')[2:6] #Number of iterations, Number of points in each direction, number of parameters, needed to read whole file nj, nk, nl, nq = dims #FORMATS IN BC201 FILE bc201formats = [ ('ints', '<i4', (1,7)), #IGRID,ISTEP,NJ,NK,NL,NQ,NQC ('tvr', 'float64'), #TVREF ('dtr', 'float64'), #DTVREF ('xyz', 'float64', (3,nj,nk,nl)), #XYZ points ('q', 'float64', (nq,nj,nk,nl)), #Q variables (nq variables, njXnkXnl values for each) ('iblank', 'int32', (nj,nk,nl)), #IBLANK status of each grid point ] ######################### #Save pressure coeff history at certian locations #! I want to write out a subset through time (every iteration!) ports = [8,13,30,45] out = open(savedir+"/cp_history.dat","w") out.write("ITER") for p in ports: out.write(" {}_x {}_cp".format(p,p)) out.write("\n") ############################# #RESTART FILE AND READ EACH RECORD f = FortranFile('{}/{}'.format(savedir, sourcefile), 'r') keep_reading = True irecord = -1 while (keep_reading is True): irecord += 1 #READ CURRENT RECORD b = f.read_record(bc201formats) #GET CURRENT ITERATION istep = b['ints'][0][0][1] # print(istep) #DONT WRITE UNDESIRED FILES (SKIP) #only write files at specified interval if (istep % incr != 0): continue #Don't write files below start iter if (istep < imin): continue #Stop reading when max desired iteration is reached if istep > imax - incr: keep_reading = False #WRITE GRID POINTS TO PLOT3D FORMATTED FILE if moving: #Grid is moving, write to file at each iteration savename = '{}/{}.x.{}'.format(savedir, saveheader, istep) WriteGrid(savename, b, nj, nk, nl) elif irecord == 0: #Grid is stationary, write to file only once savename = '{}/{}.x'.format(savedir, saveheader) WriteGrid(savename, b, nj, nk, nl) #CALCULATE CP FOR EACH POINT cps = np.zeros((nj,nk,nl)) # print(cps.shape) for j in range(nj): for k in range(nk): for l in range(nl): #print(j,k,l,b['q'].shape) q = np.zeros(nq) for iq in range(nq): q[iq] = b['q'][0][iq][j][k][l] #print(q) u = q[1] / q[0] v = q[2] / q[0] w = q[3] / q[0] v2 = (u*u + v*v + w*w) dp = 0.5 * q[0] * v2 # Dynamic Pressure dpinf = 0.5 * 1.0 * 0.107**2.0 # DP_oo 0.107=Mach p = (q[5] - 1.0)*(q[4] - dp) cp = (p - 1.0/1.4)/dpinf # 1.4 = gamma_oo cps[j][k][l] = cp #WRITE CP TO PLOT3D FILE # #If horizontal line, the pressure at the gap behind the ramp is at this location to use a reference pressure # vent_cp = cps[45][0][0] #get biggest pressure aft of gap to use as reference pressure vent_cps = [] for j in range(15): vent_cps.append(cps[49+j][0][0]) vent_cp = max(vent_cps) ofile = open('{}/cp.{}.{}'.format(savedir, saveheader, istep), 'w') #write header line with grid dimensions ofile.write('X Y Z cp cp_ref\n') #for the three dimensions, x,y,z... for j in range(nj): for k in range(nk): for l in range(nl): for x in range(3): ofile.write( ' {}'.format(b['xyz'][0][x][j][k][l]) ) ofile.write( ' {}'.format(cps[j][k][l]) ) #if horizontal line, also write gap cp for reference ofile.write( ' {}'.format(vent_cp)) ofile.write( '\n') # ofile.close() ##################### #Save pressure coeff history at certian locations out.write("{:10d}".format(istep)) for p in ports: out.write(" {:12g} {:12g}".format(b['xyz'][0][0][p][0][0],cps[p][0][0])) out.write("\n")
def read(): f=FortranFile(BinaryData,'r') n_vals=f.read_record('i4,i4,i4,i4,i4,(12,)i4') d_vals=f.read_reals('f4') xyz_vals=f.read_record('(500,)f4,(500,)f4,(500,)f4') a_vals=f.read_reals('i8') ndr_vals=f.read_ints('i4') ag_vals=f.read_record('(3,)i8') f.close() return n_vals,d_vals,xyz_vals,a_vals,ndr_vals,ag_vals