def getSliceDF(NN=32, sliceNum=0): if sliceNum==0: f = fortranfile.FortranFile(DFfile) gridx, gridy, gridz = f.readInts() #DF_sl1 = n.zeros((NN, gridx, gridy)) DF_sl1 = n.array([f.readReals().reshape(2048,2048) for ii in range(NN)]) return n.mean(DF_sl1, axis=0) if sliceNum>0: f = fortranfile.FortranFile(DFfile) gridx, gridy, gridz = f.readInts() for sli in n.arange(sliceNum): for ii in range(NN): f.readReals() DF_sl1 = n.array([f.readReals().reshape(2048,2048) for ii in range(NN)]) return n.mean(DF_sl1, axis=0)
def readbin3d(neq,neqs,nxtot,nytot,nztot,mpiX,mpiY,mpiZ,nout,\ path='',kind='d',nghost=2,endian='<',base='points'): nx = nxtot / mpiX ny = nytot / mpiY nz = nztot / mpiZ proc = 0 map = np.zeros(shape=(nxtot, nytot, nztot)) print 'Retrieving 3D map for eqn', neq for ip in range(mpiX): for jp in range(mpiY): for kp in range(mpiZ): filein = path + base + str(proc).zfill(3) + '.' + str( nout).zfill(3) + '.bin' print filein f = fortranfile.FortranFile(filein, endian=endian) data = f.readReals(kind).reshape(neqs, nx + 2 * nghost, ny + 2 * nghost, nz + 2 * nghost, order='F') map[ip*nx:(ip+1)*nx,jp*ny:(jp+1)*ny,kp*nz:(kp+1)*nz]=\ data[neq,nghost:(nx+nghost),nghost:(ny+nghost),nghost:(nz+nghost)] proc = proc + 1 return map.T
def readDrimmelAll(): out = {} allFilenames = [ 'avgrid.dat', 'avori2.dat', 'rf_allsky.dat', 'avdisk.dat', 'avloc.dat', 'avspir.dat', 'avdloc.dat', 'avori.dat' ] for filename in allFilenames: f = fortranfile.FortranFile(os.path.join(_DRIMMELDIR, filename)) nx, ny, nz = 151, 151, 51 if 'avori2' in filename: nx, ny, nz = 101, 201, 51 elif 'avori' in filename: nx, ny, nz = 76, 151, 51 elif 'avloc' in filename: nx, ny, nz = 101, 201, 51 elif 'avdloc' in filename: nx, ny, nz = 31, 31, 51 if not 'rf_allsky' in filename: out[filename.split('.')[0]]=\ f.readReals(prec='f').reshape((nz,ny,nx)).T else: #Need to do more work to read this file rec = f._read_exactly(4) #Read the header rec = f._read_exactly(4 * 393216) num = len(rec) / struct.calcsize('i') out_rf_pixnum = numpy.array(struct.unpack( f.ENDIAN + str(num) + 'i', rec), dtype='int') rec = f._read_exactly(4 * 393216) num = len(rec) / struct.calcsize('i') out_rf_comp = numpy.array(struct.unpack(f.ENDIAN + str(num) + 'i', rec), dtype='int') rec = f._read_exactly(4 * 393216) num = len(rec) / struct.calcsize('f') out_rf_glon = numpy.array(struct.unpack(f.ENDIAN + str(num) + 'f', rec), dtype=numpy.float32) rec = f._read_exactly(4 * 393216) num = len(rec) / struct.calcsize('f') out_rf_glat = numpy.array(struct.unpack(f.ENDIAN + str(num) + 'f', rec), dtype=numpy.float32) rec = f._read_exactly(4 * 393216) num = len(rec) / struct.calcsize('f') out_rf = numpy.array(struct.unpack(f.ENDIAN + str(num) + 'f', rec), dtype=numpy.float32) out['rf_pixnum'] = out_rf_pixnum out['rf_comp'] = out_rf_comp out['rf_glon'] = out_rf_glon out['rf_glat'] = out_rf_glat out['rf'] = out_rf f.close() return out
def readDM(fname): ''' Read unformatted SIESTA sparse matrix file Parameters : - nb : Number of basis - ns : Number of spins - ndmax : Total number of nonzero elements - numd(nb) : Number of nonzero elements of each row of hamiltonian matrix - listhptr(nao) : Pointer to the start of each row of hamiltonian matrix - listh(numh) : Nonzero hamiltonian-matrix element column indexes for each matrix row - dm(ndmax, ns) ''' f = fortran.FortranFile(fname) # basis, spin tmp = f.readInts('i') nb = tmp[0] ns = tmp[1] numd = np.zeros(nb, dtype=int) listdptr = np.zeros(nb, dtype=int) numd = f.readInts('i') ndmax = 0 for m in range(nb): ndmax = ndmax + numd[m] if (m == 0): listdptr[m] = 0 else: listdptr[m] = listdptr[m - 1] + numd[m - 1] listd = np.zeros(ndmax, dtype=int) dm = np.zeros((ndmax, ns), dtype=float) for m in range(nb): n = numd[m] listd[listdptr[m]:listdptr[m] + n] = f.readInts('i') for isp in range(ns): for m in range(nb): n = numd[m] dm[listdptr[m]:listdptr[m] + n, isp] = f.readReals('d') f.close() return nb, ns, numd, listdptr, listd, dm
def load_map(args, k, i): kind = [item for item in args.kind.split(' ')] # define map path map_file = "%s/movie%d/%s_%05d.map" % (args.dir, int( args.proj), kind[k], i) # read image data f = fortranfile.FortranFile(map_file) [t, dx, dy, dz] = f.readReals('d') [nx, ny] = f.readInts() dat = f.readReals() f.close() return dat
def writeDM(fname, nb, ns, numd, listdptr, listd, dm): f = fortran.FortranFile(fname, mode='wb') f.writeInts([nb, ns], 'i') f.writeInts(numd, 'i') for m in range(nb): n = numd[m] f.writeInts(listd[listdptr[m]:listdptr[m] + n], 'i') for isp in range(ns): for m in range(nb): n = numd[m] f.writeReals(dm[listdptr[m]:listdptr[m] + n, isp], 'd') f.close()
def write_file(self, filename, write2D=False, endian='>', header_prec='i', data_prec='d'): """ Writes grid and variable data to a PLOT3D file 'filename' on disk Parameters ---------- filename : string Name of the file to read write2D : boolean [optional] Is the data in a 2d format? endian : character [optional]endianness of the file. '>' for little-endian; '<' for big-endian header_prec : character [optional] precision of integer data types. 'i' for 4-byte; 'l' for 8-byte data_prec : character [optional] precision of floating-point data types. 'f' for 4-byte; 'd' for 8-byte """ f = fofi.FortranFile(filename, endian, header_prec, "w") # the file header f.writeInts([self.ngrids]) #write the grid dims f.writeInts([ii for ii in self.dims.ravel()]) # write the grid data for i in xrange(self.ngrids): #read scalars #TODO: Pull from scalar array f.writeReals([self.mach, self.alpha, self.renolds, self.time], data_prec) data = self.grids[i] shape = np.prod(data.shape) f.writeReals([ii for ii in np.reshape(data, shape, order='F')], data_prec) # complete the write to disk f.close()
def readDIM(fname): ''' read unformatted SIESTA DIM file ''' f = fortran.FortranFile(fname) MAXA = f.readInts('i')[0] MAXO = f.readInts('i')[0] MAXUO = f.readInts('i')[0] NSPIN = f.readInts('i')[0] MAXNH = f.readInts('i')[0] MAXNA = f.readInts('i')[0] f.close() return MAXA, MAXO, MAXUO, NSPIN, MAXNH, MAXNA
def writeDFMock(dataCat, DFfile, Lbox=1000.): print dataCat, DFfile md = fits.open(dataCat)[1].data path_to_outputCat = dataCat[:-4] + "DF.fits.gz" # opens the DF file f = fortranfile.FortranFile(DFfile) gridx, gridy, gridz = f.readInts() dx = Lbox / gridx # convert QSO positions into indexes i = ((md['x'] / dx) // 1).astype('int') j = ((md['y'] / dx) // 1).astype('int') k = ((md['z'] / dx) // 1).astype('int') #print md['x'] #print md['x'] / dx #print ( md['x'] / dx ) // 1 #print i, j, k #init the output array : delta = n.ones_like(i) * -1. #delta1 = n.empty_like(x) #delta2 = n.empty_like(x) # now loops over k (every line of the file) and assigns delta values. for kk in range(gridx): print kk sel = (k == kk) if len(sel.nonzero()[0]) > 0: N = i[sel] + gridx * j[sel] DF = f.readReals() print DF[N] delta[sel] = DF[N] print delta[sel] f.close() c0 = fits.Column(name="DF", format='D', array=delta) hducols = md.columns + c0 # now writes the catalog hdu = fits.BinTableHDU.from_columns(hducols) os.system("rm -rf " + path_to_outputCat) hdu.writeto(path_to_outputCat)
def readPLD(fname, MAXA, MAXO): ''' read unformatted SIESTA PLD file Input : - MAXA - MAXO Output : - IPHORB : Orbital index (within atom) of each orbital - INDXUO : Equivalent orbital in unit cell - DATM : Occupations of basis orbitals in free atom - ISA : Species index of each atom in the supercell - LASTO : Last orbital of each atom in array iphorb - CELL : Supercell vectors CELL(IXYZ,IVECT) (Bohr) - NSC : Num. of unit cells in each supercell direction - XA : Atomic positions in cartesian coordinates (Bohr) ''' f = fortran.FortranFile(fname) RMAXO = f.readReals('d')[0] dat_size = struct.calcsize('<iid') IPHORB = np.zeros((MAXO), dtype=int) INDXUO = np.zeros((MAXO), dtype=int) DATM = np.zeros((MAXO), dtype=np.float64) ISA = np.zeros((MAXA), dtype=int) LASTO = np.zeros((MAXA + 1), dtype=int) CELL = np.zeros((3, 3), dtype=np.float64) NSC = np.zeros((3), dtype=int) XA = np.zeros((3, MAXA), dtype=np.float64) for io in range(MAXO): dat = f.readRecord() val_list = struct.unpack('<iid', dat[0:dat_size]) IPHORB[io] = val_list[0] INDXUO[io] = val_list[1] DATM[io] = val_list[2] for ia in range(MAXA): ISA[ia] = f.readInts('i')[0] for ia in range(MAXA + 1): LASTO[ia] = f.readInts('i')[0] for ia in range(3): CELL[:, ia] = f.readReals('d') NSC = f.readInts('i') for ia in range(MAXA): XA[:, ia] = f.readReals('d') f.close() return RMAXO, IPHORB, INDXUO, DATM, ISA, LASTO, ISA, CELL, NSC, XA
def readHSX(fname): ''' Read unformatted SIESTA HSX file Parameters : - nao : Number of basis orbitals per unit cell - no_s : Number of basis orbitals per supercell - nspin : Spin polarization - maxnhtot : non zero - gamma : - indxuo(no_s) : Index of equivalant orbital in unit cell - numh : Number of nonzero elements of each row of hamiltonian matrix - listhptr(nao) : Pointer to the start of each row of hamiltonian matrix - listh(numh) : Nonzero hamiltonian-matrix element column indexes for each matrix row - hamilt(numx, nspin) : Hamiltonian in sparse form - Sover(numx) : Overlap in sparse form - xij(3, numx) : Vectors between orbital centers (sparse) - qtot : Total number of electrons - temp_in_file : Electronic temperature for Fermi smearing - nspecies : Total number of different atomic species - label(nspecies) : Atomic label for given atomic species - zval(nspecies) : Valence charge for given atomic species - no(nspecies) : Total number of Basis orbitals for given atomic specie - nquant(nspecies, no) : Principal quatum number for a given atomic basis - lquant(nspecies, no) : Total angular momentum quantum number of a given basis orbital - zeta(nspecies, no) : Zeta number of a given basis orbital ''' f = fortran.FortranFile(fname) no_u, no_s, nspin, maxnhtot = f.readInts('i') gamma = f.readInts('i')[0] if gamma == 0: indxuo = f.readInts('i') else: indxuo = np.zeros((no_u), dtype=int) for i in range(no_u): indxuo[i] = i + 1 numh = f.readInts('i') listhptr = np.zeros((no_u, ), dtype=int) for io in range(1, no_u): listhptr[io] = listhptr[io - 1] + numh[io - 1] numx = np.max(listhptr) ibuff = np.zeros((numx, ), dtype=int) hbuff = np.zeros((numx, ), dtype=float) buff3 = np.zeros((numx * 3, ), dtype=float) listh = np.zeros((maxnhtot, ), dtype=int) hamilt = np.zeros((maxnhtot, nspin), dtype=float) Sover = np.zeros((maxnhtot, ), dtype=float) xij = np.zeros((maxnhtot, 3), dtype=float) for io in range(no_u): ptr = listhptr[io] n = numh[io] ibuff[0:n] = f.readInts('i') listh[ptr:ptr + n] = ibuff[0:n] # fortran index for isp in range(nspin): for io in range(no_u): ptr = listhptr[io] n = numh[io] hbuff[0:n] = f.readReals('f') hamilt[ptr:ptr + n, isp] = hbuff[0:n] for io in range(no_u): ptr = listhptr[io] n = numh[io] hbuff[0:n] = f.readReals('f') Sover[ptr:ptr + n] = hbuff[0:n] qtot, temp_in_file = f.readReals('d') for io in range(no_u): ptr = listhptr[io] n = numh[io] buff3[0:3 * n] = f.readReals('f') for i in range(n): xij[ptr + i, 0] = buff3[3 * i] xij[ptr + i, 1] = buff3[3 * i + 1] xij[ptr + i, 2] = buff3[3 * i + 2] # Read auxiliary info nspecies = f.readInts('i')[0] label = [] zval = np.zeros((nspecies, ), dtype=np.float64) no = np.zeros((nspecies, ), dtype=int) dat = f.readRecord() dat_size = struct.calcsize('<20sdi') # problem ind_st = 0 ind_fn = dat_size for ispec in range(nspecies): val_list = struct.unpack('<20sdi', dat[ind_st:ind_fn]) label.append(val_list[0].strip()) zval[ispec] = (val_list[1]) no[ispec] = val_list[2] ind_st = ind_st + dat_size ind_fn = ind_fn + dat_size nquant = [] lquant = [] zeta = [] for ispec in range(nspecies): nquant.append([]) lquant.append([]) zeta.append([]) for io in range(no[ispec]): abuff, bbuff, cbuff = f.readInts('i') nquant[-1].append(abuff) lquant[-1].append(bbuff) zeta[-1].append(cbuff) na_u = f.readInts('i')[0] # number of species isa = np.zeros((na_u, ), dtype=int) iaorb = np.zeros((no_u, ), dtype=int) iphorb = np.zeros((no_u, ), dtype=int) isa = f.readInts('i') obuff = np.zeros((2 * no_u), dtype=int) obuff = f.readInts('i') for i in range(no_u): iaorb[i] = obuff[i * 2] iphorb[i] = obuff[i * 2 + 1] f.close() za = np.zeros((no_u), dtype=int) zc = np.zeros((no_u), dtype=int) zn = np.zeros((no_u), dtype=int) zl = np.zeros((no_u), dtype=int) zx = np.zeros((no_u), dtype=int) zz = np.zeros((no_u), dtype=int) nao = 0 for ia in range(na_u): it = isa[ia] - 1 # species io = 0 while (io < no[it]): lorb = lquant[it][io] for ko in range(lorb * 2 + 1): za[nao] = ia + 1 # atomic index zc[nao] = it + 1 # atomic species zn[nao] = nquant[it][io] # principle quantum number zl[nao] = lorb # total angular momentum quantum number zx[nao] = ko + 1 # zz[nao] = zeta[it][io] nao += 1 io = io + 2 * lorb + 1 return numh, listhptr, listh, indxuo, hamilt, Sover, xij, za, zc, zn, zl, zx, zz
def save_emission(self): print "debug: Emission save start" self.progress("Saving Result") emission_name = [ 'e_so2 ', 'e_no ', 'e_ald ', 'e_hcho', 'e_ora2', 'e_nh3 ', 'e_hc3 ', 'e_hc5 ', 'e_hc8 ', 'e_eth ', 'e_co ', 'e_ol2 ', 'e_olt ', 'e_oli ', 'e_tol ', 'e_xyl ', 'e_ket ', 'e_csl ', 'e_iso ', 'e_pm25i', 'e_pm25j', 'e_so4i', 'e_so4j', 'e_no3i', 'e_no3j', 'e_orgi', 'e_orgj', 'e_eci', 'e_ecj', 'e_pm10' ] n_emiss = len(emission_name) emission_name_str = '' for em in emission_name: emission_name_str += '%-9s' % em for n in range(self.maxdom): domain = self.domains[n] filename_1 = opj("{0}/wrfem_00to12z_d{1:0>2}".format( self.save_dir, n + 1)) filename_2 = opj("{0}/wrfem_12to24z_d{1:0>2}".format( self.save_dir, n + 1)) width = domain.w # e_we - 1 -> IX2 height = domain.h # e_sn - 1 -> JX3 n_layer = 1 # 0 < kemit < e_vert -> KX or z-level # file 1 f = fortranfile.FortranFile(filename_1, endian='>', mode='w') f.writeInts([n_emiss]) f.writeString(emission_name_str) for hour in range(1, 13): f.writeInts([hour]) for emission_num in range(1, n_emiss + 1): for p, plt in enumerate(self.pollutant_str): print 'saving frame [1]', hour, emission_num, plt b = numpy.ndarray([], numpy.float32) b.resize((width * n_layer * height, )) current_pollutant = self.pollutant_list[emission_num - 1] if plt.lower() == current_pollutant.strip(): print 'saving ', plt for z in range(n_layer): for y in range(height): for x in range(width): b[x + (n_layer * width * y) + (width * z)] = self.domain_emiss[n][y][x][p] f.writeReals(b) f.close() # file 2 f = fortranfile.FortranFile(filename_2, endian='>', mode='w') f.writeInts([n_emiss]) f.writeString(emission_name_str) for hour in range(13, 25): f.writeInts([hour]) for emission_num in range(1, n_emiss + 1): for p, plt in enumerate(self.pollutant_str): print 'saving frame [2]', hour, emission_num, plt b = numpy.ndarray([], numpy.float32) b.resize((width * n_layer * height, )) current_pollutant = self.pollutant_list[emission_num - 1] if plt.lower() == current_pollutant.strip(): print 'saving ', plt for z in range(n_layer): for y in range(height): for x in range(width): b[x + (n_layer * width * y) + (width * z)] = self.domain_emiss[n][y][x][p] f.writeReals(b) f.close() print "debug: Emission save end" print "------------------------------"
def main(): # Parse command line parser = ArgumentParser( description='Script to create image out of amr2map/part2map outputs') parser.add_argument('-l', '--logscale', dest='logscale', action='store_true', \ help='use log color scaling', default=False) parser.add_argument('-i', '--input', dest='infile', metavar='FILE', \ help='input binary image file', type=str, default=None) parser.add_argument('-o', '--output', dest='outfile', metavar='FILE', \ help='output image file', type=str, default=None) parser.add_argument('-m', '--min', dest='min', metavar='VALUE', \ help='min value', type=float, default=None) parser.add_argument('-M', '--max', dest='max', metavar='VALUE', \ help='max value', type=float, default=None) parser.add_argument('-a', '--autorange', dest='autorange', action='store_true', \ help='use automatic dynamic range (overrides min & max)', default=False) parser.add_argument('-c', '--colormap', dest='cmap_str', metavar='VALUE', \ help='matplotlib color map to use', type=str, default='gray') args = parser.parse_args() if args.infile is None or not os.path.isfile(args.infile): print 'Incorrect input. Exiting' sys.exit() # Read image data fileobj = fortranfile.FortranFile(args.infile) [time, delta_x, delta_y, delta_z] = fileobj.readReals('d') del time, delta_x, delta_y, delta_z [nx, ny] = fileobj.readInts() dat = fileobj.readReals() fileobj.close() rawmin = numpy.amin(dat) rawmax = numpy.amax(dat) # Bounds if args.min is None: plotmin = rawmin else: plotmin = float(args.min) if args.max is None: plotmax = rawmax else: plotmax = float(args.max) # Log scale? if args.logscale: dat = numpy.log10(dat) rawmin = numpy.log10(rawmin) rawmax = numpy.log10(rawmax) plotmin = numpy.log10(plotmin) plotmax = numpy.log10(plotmax) # Auto-adjust dynamic range? if args.autorange: # Overrides any provided bounds nbins = 200 # Compute histogram (hist, bins) = numpy.histogram(dat, nbins, (rawmin, rawmax), normed=True) chist = numpy.cumsum(hist) chist = chist / numpy.amax(chist) # Compute black and white point clip_k = chist.searchsorted(0.05) plotmin = bins[clip_k] plotmax = rawmax # Reshape data to 2d dat = dat.reshape(ny, nx) # Plotting fig = plt.figure(frameon=False) fig.set_size_inches(nx / 100, ny / 100) axis = plt.Axes(fig, [0., 0., 1., 1.]) axis.set_axis_off() fig.add_axes(axis) axis.imshow(dat, interpolation='nearest', cmap=args.cmap_str, vmin=plotmin, vmax=plotmax, aspect='auto') plt.axis('off') # removes axis plt.xlim(0, nx) # trims image to borders plt.ylim(0, ny) # corrects window extent axis.get_window_extent().transformed(fig.dpi_scale_trans.inverted()) plt.savefig(args.outfile, dpi=100) plt.close(fig)
def read_file(self, filename, read2D=False, endian='>', header_prec='i', data_prec='d'): """ Reads a PLOT3D file 'filename' from disk and stores its contents in memory Parameters ---------- filename : string Name of the file to read read2D : boolean [optional] Is the file in a 2d data format? endian : character [optional]endianness of the file. '>' for little-endian; '<' for big-endian header_prec : character [optional] precision of integer data types. 'i' for 4-byte; 'l' for 8-byte data_prec : character [optional] precision of floating-point data types. 'f' for 4-byte; 'd' for 8-byte """ f = fofi.FortranFile(filename, endian, header_prec) self.dim = 3 self.nvar = 5 if read2D == True: self.ndim = 2 self.nvar = 4 # read the file header self.ngrids = f.readInts()[0] self.dims = np.zeros((self.ngrids, self.ndim), dtype=np.int32) #3D data grid_dims = f.readInts() #read the grid dims for i in xrange(self.ngrids): self.dims[i, :] = grid_dims[self.ndim * i:self.ndim * (i + 1)] #clean up from previous file if self.grids != None: for i in xrange(self.ngrids): #if self.grids[i] != None: del self.grids[0] del self.grids self.grids = [] # read the grid data for i in xrange(self.ngrids): #read scalars #TODO: Put into scalar array self.mach, self.alpha, self.renolds, self.time = np.array( f.readReals(data_prec)) data = np.array(f.readReals(data_prec)) if self.ndim == 3: shape = (self.dims[i][0], self.dims[i][1], self.dims[i][2], self.nvar) else: shape = (self.dims[i][0], self.dims[i][1], self.nvar) data = np.reshape(data, shape, order='F') self.grids.append(data)
from astropy.io import fits import time import fortranfile import cPickle DFdir = join("/data2", "users", "gustavo", "BigMD", "1Gpc_3840_Planck1_New", "DENSFIELDS") mockDir = "/data1/DATA/eBOSS/Multidark-box-mocks/v1.0/parts/" inFiles = n.array(["dmdens_cic_104.dat", "dmdens_cic_101.dat", "dmdens_cic_097.dat", "dmdens_cic_087.dat"]) bins = n.hstack((0,n.logspace(-3, 4, 1000))) Nd = 10 for infi in inFiles: print infi DFfile = join(DFdir,infi) f = fortranfile.FortranFile(DFfile) gridx, gridy, gridz = f.readInts() Ntot = gridx/Nd res0 = n.empty((Ntot, len(bins)-1)) NS = n.arange(Ntot) for kk in NS: print kk, time.time() DF0 = f.readReals().reshape((gridx, gridx)) DF1 = f.readReals().reshape((gridx, gridx)) DF2 = f.readReals().reshape((gridx, gridx)) DF3 = f.readReals().reshape((gridx, gridx)) DF4 = f.readReals().reshape((gridx, gridx)) DF5 = f.readReals().reshape((gridx, gridx)) DF6 = f.readReals().reshape((gridx, gridx)) DF7 = f.readReals().reshape((gridx, gridx)) DF8 = f.readReals().reshape((gridx, gridx))
run = '0.3G-G1.01-RD-300' path = '/datos_diable/esquivel/EXO-HUACHO/' + run + '/BIN/' #plt.ion() for nout in range(1, 39): #name of the file to read filein = path + 'LA_tau-' + str(nout).zfill(3) + '.bin' dx = 0.3 * 1.49e13 / float(nxmap) # dx in cm rstar = 1.1 * 6.955e10 * 2. # star radius in cm plt.ion() f = fortranfile.FortranFile(filein, endian=endian) data = f.readReals(kind).reshape(nxmap, nymap, nvmap, order='F').T emtaunu = np.exp(-data) emtau = np.sum(emtaunu, 0) / float(nvmap) Emission = np.zeros(shape=(nxmap, nymap)) Emission[::, ::] = 1e-3 TotalEmission = 0. Ref = 0. for i in range(nxmap): for j in range(nxmap): rad = np.sqrt((float(i - nxmap / 2) + 0.5)**2 + (float(j - nymap / 2) + 0.5)**2) * dx if (rad <= rstar):
inFiles = n.array(["PMcrd.DAT","PMcrs1.0136.DAT"]) bins = n.hstack((-1,n.arange(0,10,0.1),n.arange(10,100,10),n.arange(100,1000,100),n.arange(1000,10000,1000))) #for infi in inFiles: infi = inFiles[1] DFfile = join(DFdir,infi) f=ff(DFfile, 'r') record = f.read_record([('x', 'f4'),('y', 'f4'),('z', 'f4'),('vx', 'f4'),('vy', 'f4'),('vz', 'f4')])# ('b', '<i4', (3,3))]) f = open(DFfile, 'rb') f.close() f = fortranfile.FortranFile(DFfile, endian='=', header_prec='i') 2i7,2a,3x,i9 10x,a,i5,a,4i11 i5 : integer 5 positions 3x : 3 horizontal positionning space ? 2a : 2 characters out = f.readRecord() xlines = f.readlines() gridx, gridy, gridz = f.readInts() res = n.empty((gridx, len(bins)-1))
def coplotTemp(cut,pos,neqs,nxtot,nytot,nztot,mpiX,mpiY,mpiZ,nout, \ path='',kind='d',nghost=2,endian='<', cv=1.5, Tempsc=1.,base='points',neqdyn=5): nx = nxtot / mpiX ny = nytot / mpiY nz = nztot / mpiZ if cut == 1: map = np.zeros(shape=(nytot, nztot)) print 'YZ Temperature cut' elif cut == 2: map = np.zeros(shape=(nxtot, nztot)) print 'XZ Temperature cut' elif cut == 3: map = np.zeros(shape=(nxtot, nytot)) print 'XY Temperature cut' proc = 0 for ip in range(mpiX): for jp in range(mpiY): for kp in range(mpiZ): if cut == 1: if ((pos >= ip * nx) and (pos <= (ip + 1) * nx - 1)): filein = path + base + str(proc).zfill(3) + '.' + str( nout).zfill(3) + '.bin' print filein f = fortranfile.FortranFile(filein, endian=endian) data = f.readReals(kind).reshape(neqs, nx + 2 * nghost, ny + 2 * nghost, nz + 2 * nghost, order='F') offset = pos - ip * nx + nghost data2d = data[::, offset, nghost:(ny + nghost), nghost:(nz + nghost)] pgas = (data2d[4, ::, ::] - 0.5 * (np.square(data2d[1, ::, ::]) + np.square(data2d[2, ::, ::]) + np.square(data2d[3, ::, ::])) / data2d[0, ::, ::]) / cv dentot = 2. * data2d[0, ::, ::] - data2d[ neqdyn, ::, ::] temp = pgas / dentot map[jp * ny:(jp + 1) * ny, kp * nz:(kp + 1) * nz] = temp[::, ::] * Tempsc elif cut == 2: if ((pos >= jp * ny) and (pos <= (jp + 1) * ny - 1)): filein = path + base + str(proc).zfill(3) + '.' + str( nout).zfill(3) + '.bin' print filein f = fortranfile.FortranFile(filein, endian=endian) data = f.readReals(kind).reshape(neqs, nx + 2 * nghost, ny + 2 * nghost, nz + 2 * nghost, order='F') offset = pos - jp * ny + nghost data2d = data[::, nghost:(nx + nghost), offset, nghost:(nz + nghost)] pgas = (data2d[4, ::, ::] - 0.5 * (np.square(data2d[1, ::, ::]) + np.square(data2d[2, ::, ::]) + np.square(data2d[3, ::, ::])) / data2d[0, ::, ::]) / cv dentot = 2. * data2d[0, ::, ::] - data2d[ neqdyn, ::, ::] temp = pgas / dentot map[ip * nx:(ip + 1) * nx, kp * nz:(kp + 1) * nz] = temp[::, ::] * Tempsc elif cut == 3: if ((pos >= kp * nz) and (pos <= (kp + 1) * nz - 1)): filein = path + base + str(proc).zfill(3) + '.' + str( nout).zfill(3) + '.bin' print filein f = fortranfile.FortranFile(filein, endian=endian) data = f.readReals(kind).reshape(neqs, nx + 2 * nghost, ny + 2 * nghost, nz + 2 * nghost, order='F') offset = pos - kp * nz + nghost data2d = data[::, nghost:(nx + nghost), nghost:(ny + nghost), offset] pgas = (data2d[4, ::, ::] - 0.5 * (np.square(data2d[1, ::, ::]) + np.square(data2d[2, ::, ::]) + np.square(data2d[3, ::, ::])) / data2d[0, ::, ::]) / cv dentot = 2. * data2d[0, ::, ::] - data2d[ neqdyn, ::, ::] temp = pgas / dentot map[ip * nx:(ip + 1) * nx, jp * ny:(jp + 1) * ny] = temp[::, ::] * Tempsc proc = proc + 1 return map.T
import fortranfile import numpy from matplotlib import pyplot # path the the file map_file = './Mono/Output/output_00043.00000' # read image data f = fortranfile.FortranFile(map_file) [t, gamma] = f.readReals() [nx, ny, nvar, nstep] = f.readInts() dat = f.readReals() f.close() dat = numpy.array(dat) dat = dat.reshape(nvar, ny, nx) # plot the map fig = pyplot.figure() ax = fig.add_subplot(1, 1, 1) ax.imshow(dat[0, :, :], interpolation='nearest') pyplot.show()
def coplot3d(cut,pos,neq,neqs,nxtot,nytot,nztot,mpiX,mpiY,mpiZ,nout, \ path='',kind='d',nghost=2,endian='<',base='points'): nx = nxtot / mpiX ny = nytot / mpiY nz = nztot / mpiZ if cut == 1: map = np.zeros(shape=(nytot, nztot)) print 'YZ cut, equation: ', neq elif cut == 2: map = np.zeros(shape=(nxtot, nztot)) print 'XZ cut, equation: ', neq elif cut == 3: map = np.zeros(shape=(nxtot, nytot)) print 'XY cut, equation: ', neq proc = 0 for ip in range(mpiX): for jp in range(mpiY): for kp in range(mpiZ): if cut == 1: if ((pos >= ip * nx) and (pos <= (ip + 1) * nx - 1)): filein = path + base + str(proc).zfill(3) + '.' + str( nout).zfill(3) + '.bin' print filein f = fortranfile.FortranFile(filein, endian=endian) data = f.readReals(kind).reshape(neqs, nx + 2 * nghost, ny + 2 * nghost, nz + 2 * nghost, order='F') offset = pos - ip * nx + nghost map[jp * ny:(jp + 1) * ny, kp * nz:(kp + 1) * nz] = data[neq, offset, nghost:(ny + nghost), nghost:(nz + nghost)] elif cut == 2: if ((pos >= jp * ny) and (pos <= (jp + 1) * ny - 1)): filein = path + base + str(proc).zfill(3) + '.' + str( nout).zfill(3) + '.bin' print filein f = fortranfile.FortranFile(filein, endian=endian) data = f.readReals(kind).reshape(neqs, nx + 2 * nghost, ny + 2 * nghost, nz + 2 * nghost, order='F') offset = pos - jp * ny + nghost map[ip * nx:(ip + 1) * nx, kp * nz:(kp + 1) * nz] = data[neq, nghost:(nx + nghost), offset, nghost:(nz + nghost)] elif cut == 3: if ((pos >= kp * nz) and (pos <= (kp + 1) * nz - 1)): filein = path + base + str(proc).zfill(3) + '.' + str( nout).zfill(3) + '.bin' print filein f = fortranfile.FortranFile(filein, endian=endian) data = f.readReals(kind).reshape(neqs, nx + 2 * nghost, ny + 2 * nghost, nz + 2 * nghost, order='F') offset = pos - kp * nz + nghost map[ip * nx:(ip + 1) * nx, jp * ny:(jp + 1) * ny] = data[neq, nghost:(nx + nghost), nghost:(ny + nghost), offset] proc = proc + 1 return map.T
def plot3d_read(file): global b # the endian depends on your machine or you plot3d file. # Tested for icemcfd 18.0 and 19.0 plot3d unforamtted file, 'Big endian'='>' option should be used! f = ff.FortranFile(file, endian='>') header = f.readInts() nblocks = header[0] dims = f.readInts() # put dims into idim jdim kdim idim = np.arange(nblocks + 1, dtype='int32') jdim = np.arange(nblocks + 1, dtype='int32') kdim = np.arange(nblocks + 1, dtype='int32') # set index[0] to be 0, as a placeholder. so the index will begin from 1 as in fortran idim[0] = 0 jdim[0] = 0 kdim[0] = 0 # get the i j k dims from dims i = 0 for n in range(1, nblocks + 1): idim[n] = dims[i] jdim[n] = dims[i + 1] kdim[n] = dims[i + 2] if n < nblocks: i += 3 # create block_mesh class for each block b = [] #create a blank list b.append(block(0, 0, 0, 0)) #placeholder for n in range(1, nblocks + 1): #temp = block(n,idim[n],jdim[n],kdim[n]) b.append(block(n, idim[n], jdim[n], kdim[n])) #b.append(temp) print ' Total blocks:', nblocks print(' Initializing block mesh arrays ') for n in range(1, nblocks + 1): print ' Block ', n print ' Idim:', idim[n], ' Jdim:', jdim[n], ' Kdim:', kdim[n] ptr = list(range(nblocks + 2)) ptr[0] = 0 #placeholder ptr[1] = 0 # first block pointer equals to 0 for n in range(1, nblocks + 1): ptr[n + 1] = ptr[n] + b[n].size * 3 # read the mesh cooridinates into 'data' data = np.arange(ptr[-1], dtype='float32') data = f.readReals('f') for n in range(1, nblocks + 1): temp = np.arange(b[n].size, dtype='float32') start = ptr[n] end = start + b[n].size - 1 temp = data[start:end].copy() #print data[start:end] temp.resize( kdim[n], jdim[n], idim[n]) #its okay if you use b[n].idim. equal with idim[n] b[n].x[1:, 1:, 1:] = temp[0:, 0:, 0:] #print temp start = end + 1 end = start + b[n].size - 1 temp = data[start:end].copy() temp.resize( kdim[n], jdim[n], idim[n]) #its okay if you use b[n].idim. equal with idim[n] b[n].y[1:, 1:, 1:] = temp[0:, 0:, 0:] start = end + 1 end = start + b[n].size - 1 temp = data[start:end].copy() temp.resize( kdim[n], jdim[n], idim[n]) #its okay if you use b[n].idim. equal with idim[n] b[n].z[1:, 1:, 1:] = temp[0:, 0:, 0:] #print b[n].x[12,5,5],b[n].size,ptr[n],b[n].block_no print(' Read block mesh data into arrays ') print(' Plot3d file read complete ')
print 'Time for get final probability routine for ', t, 'inch threshold: ', t5 - t4 print 'max of probfinal: ', np.max(probfinal) probstr = str(t * 2.54) print 'probstr is: ', probstr byte = int(t * 2.54 * 1000) byte44 = 0 byte45 = int(byte / 65536) byte45rem = byte % 65536 byte46 = int(byte45rem / 256) byte47 = byte45rem % 256 # write binary file out of probfinal array, then import it into grib file using WGRIB2 myfort = F.FortranFile('record_out.bin', mode='w') myfort.writeReals(probfinal) myfort.close() string = "0:0:d=" + wgribdate + ":WEASD:surface:" + fhr_range + " hour acc fcst:prob > " + probstr print 'string used: ', string os.system(WGRIB2 + ' ' + template + ' -import_bin record_out.bin -set_metadata_str "' + string + '" -set_grib_type c3 -grib_out premod.grb') os.system( WGRIB2 + ' premod.grb -set_byte 4 12 197 -set_byte 4 17 0 -set_byte 4 24:35 0:0:0:0:0:255:0:0:0:0:0:0 -set_byte 4 36 ' + str(nm_use) + ' -set_byte 4 38:42 0:0:0:0:0 -set_byte 4 43 3 -set_byte 4 44 0 -set_byte 4 45 ' + str(byte45) + ' -set_byte 4 46 ' + str(byte46) + ' -set_byte 4 47 ' + str(byte47) + ' -append -set_grib_type c3 -grib_out ' + outfile)
def readWFSX(fname): ''' Read unformatted SIESTA WFSX file Input : - fname : WFSX file Parameters : - nkp : Number of k-points - nsp : Number of spins - nao : Number of basis orbitals - ia(nao) : Atomic index of each orbital - label(nao) : Atomic labels of each orbital - iao(nao) : Orbital index of each orbital in each atoms - nquant(nao) : Principal quatum number of each orbital - sym(nao) : Symmetry of each orbital - wk(nkp) : Weight of each k-point - pk(3, nkp) : k-point vector - eig(nao, nsp, nkp) : Eigenvalue of - wf(1 or 2, nao, nao, nsp, nkp) : Eigenvector of each ''' f = fortran.FortranFile(fname) # read number of kpoints, spins, atomic orbitals nkp, gamma = f.readInts('i') nsp = f.readInts('i')[0] nao = f.readInts('i')[0] # read index of each orbital ia = np.zeros((nao), dtype=int) label = [] iao = np.zeros((nao), dtype=int) nquant = np.zeros((nao), dtype=int) sym = [] dat = f.readRecord() dat_size = struct.calcsize('<i20sii20s') # five values ind_st = 0 ind_fn = dat_size for io in range(nao): val_list = struct.unpack('<i20sii20s', dat[ind_st:ind_fn]) ia[io] = val_list[0] label.append(val_list[1].decode('ascii').strip()) iao[io] = val_list[2] nquant[io] = val_list[3] sym.append(val_list[4].decode('ascii').strip()) ind_st = ind_st + dat_size ind_fn = ind_fn + dat_size label = np.array(label) sym = np.array(sym) # read k compontents, eigenvalue, eigenvector per each k and e wk = np.zeros((nkp), dtype=np.float64) pk = np.zeros((nkp, 3), dtype=np.float64) eig = np.zeros((nao, nsp, nkp), dtype=np.float64) if (gamma == -1): wf = np.zeros((1, nao, nao, nsp, nkp), dtype=float) else: wf = np.zeros((2, nao, nao, nsp, nkp), dtype=float) dat_size = struct.calcsize('<idddd') # problem for ik in range(nkp): for isp in range(nsp): dat = f.readRecord() val_list = struct.unpack('<idddd', dat[0:dat_size]) dummy = val_list[0] - 1 pk[ik, :] = val_list[1:4] wk[ik] = val_list[4] ispin = f.readInts('i')[0] nwf = f.readInts('i')[0] if (dummy != ik): raise ValueError('ik =! dummy') if (ispin > nsp): raise ValueError('ispin > nsp') if (nwf > nao): raise ValueError('nwf > nao') for iw in range(nwf): iao_ = f.readInts('i')[0] - 1 eig[iw, isp, ik] = f.readReals('d')[0] buff = f.readReals('f') if (gamma == -1): wf[0, :, iw, isp, ik] = buff else: buff = buff.reshape((2, -1), order='F') wf[:, :, iw, isp, ik] = buff f.close() return gamma, pk, wk, wf, eig, ia, label, iao, nquant, sym
def main(): # Parse command line arguments parser = OptionParser() parser.usage = "%prog [options] map_file" parser.add_option('-l', '--logscale', dest='logscale', action='store_true', help='use log color scaling', default=False) parser.add_option("-o", "--output", dest="outfile", metavar="FILE", help='output image file [default: <map_file>.png]', default=None) parser.add_option("-m", "--min", dest="min", metavar="VALUE", help='min value', default=None) parser.add_option("-M", "--max", dest="max", metavar="VALUE", help='max value', default=None) parser.add_option('-a', '--autorange', dest='autorange', action='store_true', help='use automatic dynamic range (no min/max)', default=False) parser.add_option('--big-endian', dest='big_endian', action='store_true', help='input binary data is stored as big endian', default=False) parser.add_option('-c', '--colormap', dest='cmap_str', metavar='CMAP', help='matplotlib color map to use', default="jet") (opts, args) = parser.parse_args() # Parse input and output try: infile = args[0] except: print parser.print_help() return 1 if (opts.outfile == None): outfile = infile + '.tif' else: outfile = opts.outfile # Endianness if (opts.big_endian): endianness = ">" else: endianness = "=" # Read image data #print "Reading raw Fortran data..." f = fortranfile.FortranFile(infile) [nx, ny] = f.read_fortran_record('i4', endian=endianness) dat = f.read_fortran_record('f4', endian=endianness) f.close() if (opts.logscale): dat = numpy.array(dat) + 1e-12 rawmin = numpy.amin(dat) rawmax = numpy.amax(dat) #print ' Image map size : ',(nx, ny) #print ' Data bounds : ',(rawmin,rawmax) #print "Scaling data and processing colormap..." # Bounds if opts.min == None: plotmin = rawmin else: plotmin = float(opts.min) if opts.max == None: plotmax = rawmax else: plotmax = float(opts.max) # Log scale? if (opts.logscale): dat = numpy.log10(dat) rawmin = numpy.log10(rawmin) rawmax = numpy.log10(rawmax) plotmin = numpy.log10(plotmin) plotmax = numpy.log10(plotmax) # Auto-adjust dynamic range? if (opts.autorange): #print "Computing dynamic range..." # Overrides any provided bounds NBINS = 200 # Compute histogram (hist, bins) = numpy.histogram(dat, NBINS, (rawmin, rawmax), normed=True) chist = numpy.cumsum(hist) chist = chist / numpy.amax(chist) # Compute black and white point clip_k = chist.searchsorted(0.05) plotmin = bins[clip_k] plotmax = rawmax if (plotmax - plotmin > 0): dat = numpy.clip((dat - plotmin) / (plotmax - plotmin), 0.0, 1.0) else: dat = 0.5 * dat / plotmax # if(opts.logscale): #print ' Color bounds : ',(10**plotmin,10**plotmax) # else: #print ' Color bounds : ',(plotmin,plotmax) # Apply chosen color map color_map = cm.get_cmap(opts.cmap_str) dat = color_map(dat) * 255 # Convert to int dat = numpy.array(dat, dtype='i') # Output to file #print "Saving image to file..." R_band = Image.new("L", (nx, ny)) R_band.putdata(dat[:, 0]) G_band = Image.new("L", (nx, ny)) G_band.putdata(dat[:, 1]) B_band = Image.new("L", (nx, ny)) B_band.putdata(dat[:, 2]) out_img = Image.merge("RGB", (R_band, G_band, B_band)).transpose( Image.FLIP_TOP_BOTTOM) out_img.save(outfile) print "map2img.py completed"
def main(): # Parse command line arguments parser = OptionParser() parser.usage = "%prog [options] map_file" parser.add_option('-l','--logscale',dest='logscale', action='store_true', \ help='use log color scaling', default=False) parser.add_option("-o","--output", dest="outfile", metavar="FILE", \ help='output image file [default: <map_file>.png]', default=None) parser.add_option("-m","--min", dest="min", metavar="VALUE", \ help='min value', default=None) parser.add_option("-M","--max", dest="max", metavar="VALUE", \ help='max value', default=None) parser.add_option("-f","--fmin", dest="fmin", metavar="VALUE", \ help='frame min value', default=0, type=int) parser.add_option("-F","--fmax", dest="fmax", metavar="VALUE", \ help='frame max value', default=0, type=int) parser.add_option("-d","--dir", dest="dir", \ help='map directory', default=None) parser.add_option("-i","--iter", dest="iter", \ help="iterator index", default=2) parser.add_option("-p","--proj", dest="proj", \ help="proj_nd", default=2) parser.add_option("-s","--step", dest="step", \ help="framing step", default=5, type=int) parser.add_option('-k','--kind', dest="kind", \ help="kind of plot [temp, dens, metal]", default='dens') parser.add_option('-a','--autorange',dest='autorange', action='store_true', \ help='use automatic dynamic range (overrides min & max)', default=False) parser.add_option('--big-endian',dest='big_endian', action='store_true', \ help='input binary data is stored as big endian', default=False) parser.add_option('-c','--colormap',dest='cmap_str', metavar='CMAP', \ help='matplotlib color map to use', default="jet") parser.add_option('-b','--barlen',dest='barlen', metavar='VALUE', \ help='length of the bar (specify unit!)', default=5) parser.add_option('-B','--barlen_unit',dest='barlen_unit', metavar='VALUE', \ help='unit of the bar length [pc/kpc/Mpc]', default='kpc') parser.add_option('-t','--time_unit',dest='time_unit', metavar='VALUE', \ help='unit of time [Myr/Gyr]', default='Myr') (opts,args)=parser.parse_args() if opts.barlen_unit == 'pc': scale_l = 1e0 elif opts.barlen_unit == 'kpc': scale_l = 1e3 elif opts.barlen_unit == 'Mpc': scale_l = 1e6 else: print "Wrong length unit!" sys.exit() if opts.time_unit == 'Myr': scale_t = 1e6 elif opts.time_unit == 'Gyr': scale_t = 1e9 else: print "Wrong time unit!" sys.exit() proj_ind = int(opts.proj)-1 # Searching for namelist for file in os.listdir(opts.dir): if file.endswith(".nml"): namelist = opts.dir + file try: nmlf = open(namelist) except IOError: print "No namelist found! Aborting!" sys.exit() sink_flag = False # Loading parameters from the namelist for i, line in enumerate(nmlf): if line.split('=')[0] == 'xcentre_frame': xcentre_frame = float(line.split('=')[1].split(',')[0+4*proj_ind]) if line.split('=')[0] == 'ycentre_frame': ycentre_frame = float(line.split('=')[1].split(',')[0+4*proj_ind]) if line.split('=')[0] == 'zcentre_frame': zcentre_frame = float(line.split('=')[1].split(',')[0+4*proj_ind]) if line.split('=')[0] == 'deltax_frame': deltax_frame = float(line.split('=')[1].split(',')[0+2*proj_ind]) if line.split('=')[0] == 'deltay_frame': deltay_frame = float(line.split('=')[1].split(',')[0+2*proj_ind]) if line.split('=')[0] == 'deltaz_frame': deltaz_frame = float(line.split('=')[1].split(',')[0+2*proj_ind]) if line.split('=')[0] == 'proj_axis': proj_axis = line.split('=')[1] if line.split('=')[0] == 'imovout': max_iter = int(line.split('=')[1]) if (line.split('=')[0] == 'sink') and (line.split('=')[1][:-1] == '.true.'): sink_flag = True print 'Projection axis: %s' % (proj_axis[proj_ind+1]) # Progressbar imports/inits try: from widgets import Percentage, Bar, ETA from progressbar import ProgressBar progressbar_avail = True except ImportError: progressbar_avail = False if (int(opts.fmax) > 0): max_iter=int(opts.fmax) if progressbar_avail: widgets = ['Working...', Percentage(), Bar(marker='#'),ETA()] pbar = ProgressBar(widgets=widgets, maxval = max_iter+1).start() else: print 'Working!' # Looping over movie snapshots for i in xrange(int(opts.fmin)+int(opts.step),max_iter+1,int(opts.step)): infile = "%smovie%d/%s_%05d.map" % (opts.dir, int(opts.proj), opts.kind, i) infof = open("%smovie%d/info_%05d.txt" % (opts.dir, int(opts.proj), i)) for j, line in enumerate(infof): if j == 7: boxlen = float(line.split()[2]) if j == 8: time = float(line.split()[2]) if j == 15: unit_l = float(line.split()[2]) if j == 16: unit_d = float(line.split()[2]) if j == 17: unit_t = float(line.split()[2]) if j> 18: break unit_m = unit_d*unit_l**3/2e33 # in MSun if sink_flag: sink_file = "%smovie1/sink_%05d.txt" % (opts.dir, i) try: with warnings.catch_warnings(): warnings.simplefilter("ignore") sink_m,sink_x,sink_y,sink_z = numpy.loadtxt(sink_file, delimiter=',',usecols=(1,2,3,4),unpack=True)/float(boxlen) sink_m *= unit_m*float(boxlen) sink_m = numpy.log10(sink_m) plot_sinks = True except ValueError: plot_sinks = False except IOError: print "No sink file" if(opts.outfile==None): if not os.path.exists("%smovie%d/pngs/" % (opts.dir, int(opts.proj))): os.makedirs("%smovie%d/pngs/" % (opts.dir, int(opts.proj))) outfile="%smovie%d/pngs/%s_%05d.png" % (opts.dir, int(opts.proj), opts.kind, i/int(opts.step)-int(opts.fmin)) else: outfile=opts.outfile # Read image data f = fortranfile.FortranFile(infile) [time, dx, dy, dz] = f.readReals('d') [nx,ny] = f.readInts() dat = f.readReals() f.close() if(opts.logscale): dat = numpy.array(dat) rawmin = numpy.amin(dat) rawmax = numpy.amax(dat) # Bounds if opts.min==None: plotmin = rawmin else: plotmin = float(opts.min) if opts.max==None: plotmax = rawmax else: plotmax = float(opts.max) # Log scale? if(opts.logscale): dat = numpy.log10(dat) rawmin = numpy.log10(rawmin) rawmax = numpy.log10(rawmax) plotmin = numpy.log10(plotmin) plotmax = numpy.log10(plotmax) # Auto-adjust dynamic range? if(opts.autorange): # Overrides any provided bounds NBINS = 200 # Compute histogram (hist,bins) = numpy.histogram(dat, NBINS, (rawmin,rawmax), normed=True) chist = numpy.cumsum(hist); chist = chist / numpy.amax(chist) # Compute black and white point clip_k = chist.searchsorted(0.05) plotmin = bins[clip_k] plotmax = rawmax #if(plotmax-plotmin>0): # dat = numpy.clip((dat-plotmin)/(plotmax-plotmin), 0.0, 1.0) #else: # dat = 0.5*dat/plotmax axis = proj_axis[proj_ind+1] # Reshape data to 2d dat = dat.reshape(ny,nx) # Plotting fig = plt.figure(figsize=(8,8*ny/nx),frameon=False) ax = fig.add_subplot(1,1,1) ax.set_axis_off() fig.add_axes(ax) ax.imshow(dat, interpolation = 'nearest', cmap = opts.cmap_str,\ vmin = plotmin, vmax = plotmax) # Plotting sink if (sink_flag and plot_sinks): if axis == 'x': ax.scatter((sink_y-ycentre_frame/boxlen)/(deltay_frame/boxlen/2)*nx/2+nx/2,\ (sink_z-zcentre_frame/boxlen)/(deltaz_frame/boxlen/2)*ny/2+ny/2,\ marker='+',c='r',s=4*sink_m**2) elif axis == 'y': ax.scatter((sink_x-xcentre_frame/boxlen)/(deltax_frame/boxlen/2)*nx/2+nx/2,\ (sink_z-zcentre_frame/boxlen)/(deltaz_frame/boxlen/2)*ny/2+ny/2,\ marker='+',c='r',s=4*sink_m**2) else: ax.scatter((sink_x-xcentre_frame/boxlen)/(deltax_frame/boxlen/2)*nx/2+nx/2,\ (sink_y-ycentre_frame/boxlen)/(deltay_frame/boxlen/2)*ny/2+ny/2,\ marker='+',c='r',s=4*sink_m**2) patches = [] barlen_px = opts.barlen*scale_l*nx/(float(boxlen)*unit_l*3.24e-19*deltax_frame/float(boxlen)) rect = mpatches.Rectangle((nx/20,ny/100), barlen_px, 10) label([nx/20+barlen_px/2,ny/100],"%d %s" % (opts.barlen, opts.barlen_unit)) patches.append(rect) plt.axis('off') # removes axis plt.xlim(0,nx) # trims image to borders plt.ylim(0,ny) ax.text(0.95, 0.95, '%.1f %s' % (time*unit_t/86400/365.25/scale_t, opts.time_unit), verticalalignment='bottom', horizontalalignment='right', transform=ax.transAxes, color='white', fontsize=14) collection = PatchCollection(patches, facecolor='white') ax.add_collection(collection) # corrects window extent extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted()) plt.savefig(outfile,bbox_inches=extent,dpi=100) plt.close(fig) if progressbar_avail: pbar.update(i) # updates progressbar if progressbar_avail: pbar.finish() else: print 'Finished!'
def writeDFMock(dataCat, DFfile, Lbox=1000.): print dataCat, DFfile x, y, z, vx, vy, vz, Vmax, Vpeak, Mvir, parent_id, snap_id, kind, z_dis = n.loadtxt( dataCat, unpack=True) path_to_outputCat = dataCat[:-4] + ".DF.fits.gz" # opens the DF file f = fortranfile.FortranFile(DFfile) gridx, gridy, gridz = f.readInts() dx = Lbox / gridx # convert QSO positions into indexes i = (x / dx).astype(int) j = (y / dx).astype(int) k = (z / dx).astype(int) #init the output array : delta = n.empty_like(x) #delta1 = n.empty_like(x) #delta2 = n.empty_like(x) # now loops over k (every line of the file) and assigns delta values. for kk in range(gridx): sel = (k == kk) N = i[sel] + gridx * j[sel] DF = f.readReals() delta[sel] = DF[N] """ # distance 1 mean density field in the plane sel1 = (sel)&(i>=1)&(i<gridx-1)&(j>=1)&(j<gridx-1) N1 = n.transpose([ (i[sel1]-1) + gridx * (j[sel1] -1), (i[sel1]) + gridx * (j[sel1] -1), (i[sel1]-1) + gridx * (j[sel1]), (i[sel1]+1) + gridx * (j[sel1] +1), (i[sel1]+1) + gridx * (j[sel1] ), (i[sel1]) + gridx * (j[sel1] +1), (i[sel1]+1) + gridx * (j[sel1] -1), (i[sel1]-1) + gridx * (j[sel1] +1) ]) delta1[sel1] = n.array([ n.mean(DF[el]) for el in N1 ]) # assign -1 err value to points on the boundary border1 = (sel)&(sel1==False) delta1[border1] = n.ones_like(delta1[border1])*-1. # distance 2 mean density field in the plane sel2 = (sel)&(i>=2)&(i<gridx-2)&(j>=2)&(j<gridx-2) N2 = n.transpose([ (i[sel2]-2) + gridx * (j[sel2] -2), (i[sel2]-2) + gridx * (j[sel2] -1), (i[sel2]-2) + gridx * (j[sel2] ), (i[sel2]-2) + gridx * (j[sel2] +1), (i[sel2]-2) + gridx * (j[sel2] +2), (i[sel2]-1) + gridx * (j[sel2] + 2), (i[sel2]) + gridx * (j[sel2] +2), (i[sel2]+11) + gridx * (j[sel2] +2), (i[sel2] + 2) + gridx * (j[sel2] +2), (i[sel2] + 2) + gridx * (j[sel2] +1), (i[sel2] + 2) + gridx * (j[sel2] ), (i[sel2] + 2) + gridx * (j[sel2] -1), (i[sel2] + 2) + gridx * (j[sel2] -2), (i[sel2] + 1) + gridx * (j[sel2] -2), (i[sel2] ) + gridx * (j[sel2] -2), (i[sel2] - 1) + gridx * (j[sel2] -2) ]) -1 delta2[sel2] = n.array([ n.mean(DF[el]) for el in N2 ]) # assign -1 err value to points on the boundary border2 = (sel)&(sel2==False) delta2[border2] = n.ones_like(delta2[border2])*-1. """ f.close() c0 = fits.Column(name="DF", format='D', array=delta) #c01 = fits.Column(name="DF_N1",format='D', array=delta1 ) #c02 = fits.Column(name="DF_N2",format='D', array=delta2 ) c1 = fits.Column(name="x", format='D', array=x) c2 = fits.Column(name="y", format='D', array=y) c3 = fits.Column(name="z", format='D', array=z) c4 = fits.Column(name="vx", format='D', array=vx) c5 = fits.Column(name="vy", format='D', array=vy) c6 = fits.Column(name="vz", format='D', array=vz) c7 = fits.Column(name="Vmax", format='D', array=Vmax) c8 = fits.Column(name="Vpeak", format='D', array=Vpeak) c9 = fits.Column(name="Mvir", format='D', array=Mvir) c10 = fits.Column(name="parent_id", format='D', array=parent_id) c11 = fits.Column(name="snap_id", format='D', array=snap_id) c12 = fits.Column(name="kind", format='D', array=kind) c13 = fits.Column(name="z_dis", format='D', array=z_dis) # now writes the catalog cols = fits.ColDefs( [c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c0]) #, c01, c02 ]) hdu = fits.BinTableHDU.from_columns(cols) os.system("rm -rf " + path_to_outputCat) hdu.writeto(path_to_outputCat)
def TdlpackDecode(filename, dates=[], ids=[]): """Read the contents of a TDLPACK file.""" # Open file and set its file size. f = ff.FortranFile(filename, '>') f.seek(0, os.SEEK_END) fsize = f.tell() f.seek(0) # Initialize lists ccalls = [] nstas = [] tdlps = [] # Initialize NumPy arrays is0 = np.zeros(54, dtype='int32', order='F') is1 = np.zeros(54, dtype='int32', order='F') is2 = np.zeros(54, dtype='int32', order='F') is4 = np.zeros(54, dtype='int32', order='F') moctet = np.zeros(1, dtype='int32', order='F') # Iterate through the file cnt = 0 igive = 1 nccall = 0 while 1: isTDLP = False # Check for at EOF if fsize == f.tell(): break # Read using fortranfile.readInts() method. This is the Python equivlent # to Fortran's READ statement. rec contains the entire Fortran record. # ioctet is the 8-byte integer that is the TDLPACK record size in bytes. # ipack becomes the TDLPACK record. rec = f.readInts() ioctet = struct.unpack('>q', rec[0:2])[0] ipack = rec[2:] # When ipack[0] = 0, its the trailer record and there is no need to # do anything with this record. if ipack[0] == 0: ccall = [] continue kwargs = {} # Determine the record type. Above we are already checking for a trailer # record so the below checks are for a TDLPACK record and station CALL # letter record. # # If we come here, then we need to check the first 4-bytes of the TDLPACK # record for the "TDLP" string. Since TDLPACK is big-endian, on a # little-endian system, the TDLPACK identifying string will appear as # "PLDT". tdlpHeader = struct.unpack('<4s', ipack[0])[0] if sys.byteorder == 'big' and tdlpHeader == 'TDLP' or \ sys.byteorder == 'little' and tdlpHeader == 'PLDT': # Before unpacking begins check to see is a date and/or id list has been # passed into this function. If so, then check the date and ID from ipack # against dates and IDs in their lists. cnt = 0 if dates: cnt = dates.count(ipack[4]) if cnt > 0: cnt = ids.count(list(ipack[5:9])) # If cnt == 0, then the TDLPACK record is ted given the dates and/or # IDs -- iterate. if (dates or ids) and cnt == 0: continue # Let the function know this is a TDLPACK record and to set ipack # instance variable. isTDLP = True kwargs['ipack'] = np.copy(ipack) # Unpack sections 0 and 1. iret = tdlpack.unpack0(ipack, moctet, is0) iret = tdlpack.unpack1(ipack, moctet, is1) # Determine whether the data are gridded. If so, then unpack # Section 2 (Grid Definition Section). # # IMPORTANT: nvalues is set here. The value should match # is4[2] (3rd value). This will be checked when method # unpackData() is called. if is1[1] & 1: # TDLPACK Gridded Record iret = tdlpack.unpack2(ipack, moctet, is2) kwargs['nvalues'] = is2[2] * is2[3] kwargs['ccall'] = [] kwargs['nsta'] = 0 kwargs['nccall'] = 0 else: # TDLPACK Vector Record kwargs['nvalues'] = nstas[len(nstas) - 1] kwargs['nsta'] = nstas[len(nstas) - 1] kwargs['ccall'] = ccalls[len(ccalls) - 1] kwargs['nccall'] = nccall # Data section will be unpacked using method unpackData(). Here # we just need to know the beginning position of Section 4. kwargs['_is4Pos'] = np.copy(moctet) # Populate kwargs. Even though we have notunpacked section 4, # we need to save the empty array for each instance. # IMPORTANT: Use np.copy()! kwargs['indicator_section'] = np.copy(is0) kwargs['product_definition_section'] = np.copy(is1) kwargs['grid_definition_section'] = np.copy(is2) kwargs['data_section'] = np.copy(is4) else: # Record contains station call letter records. Catalog them. nsta = ioctet / 8 ccall = [] for n in range(0, len(ipack), 2): ccall.append(struct.unpack( '>8s', ipack[n:n + 2])[0].strip(' ')) nstas.append(nsta) ccalls.append(ccall) nccall += 1 # Append TdlpackRecord instance to return list. if isTDLP: tdlps.append(TdlpackRecord(**kwargs)) f.close() return tdlps # Return from function TdlpackDecode
def main(): tic = time.clock() #1. DECLARE THE SUPEROB PARAMETERS USED FROM THE GSI NAMELIST. anal_time=str(sys.argv[1]) station_id=sys.argv[2] del_azimuth=float(sys.argv[3])#5. del_elev=float(sys.argv[4]) del_range=float(sys.argv[5])#5000. range_max=100000. del_time=float(sys.argv[6])#0.125 minnum=int(sys.argv[7])#50 #2. SET SOME VALUES WHICH CHANGE BASED ON THE SUPEROB PARAMETERS. max_angle_az=360. rows_az=int(max_angle_az/del_azimuth) cols_rw=range_max/del_range dr2=del_range/2. half_del_az=del_azimuth/2. #3. INITIALIZE SOME BASIC LISTS. staid=[]; stalat=[]; stalon=[]; stahgt=[]; dattime=[]; lat=[]; lon=[]; hgt=[]; vr=[] corrected_azimuth=[]; err=[]; corrected_tilt=[]; gamma=[] stalats=[]; stalons=[]; anel=[]; anaz=[]; radii=[]; l2rw=[]; tilt=[]; azimuth=[]; rad=[]; l2=[] #4. GET THE INPUT FILE CONTAINING THE SUPER OBSERVATIONS. fileds=[stalat,stalon,stahgt,dattime,hgt,vr,corrected_azimuth,err,corrected_tilt,gamma] os.system('./exec/read_radar.exe') fname='./output.bin' f=F.FortranFile(fname,endian='>') #5. GET THE NUMBER OF SUPEROB BOXES READ BY READ_RADAR. #os.system('./read_radar.exe') fname_num_supob_boxes='./num_supob_boxes.bin' fnsb=F.FortranFile(fname_num_supob_boxes,endian='>') num_supob_boxes=fnsb.readInts('i') #num_supob_boxes=1 #6. READ THE SUPEROB FILE AND STORE DATA IN 1-D RAD AND L2 ARRAYS. for i in range(num_supob_boxes): fields=f.readReals('d') vr =fields[5] corrected_azimuth =fields[6] corrected_tilt =fields[8] gamma =fields[9] azimuth.append(corrected_azimuth) tilt.append(corrected_tilt) rad.append(int(gamma)) l2.append(vr) #7. INITIALIZE RADIUS AND L2RW DATA ARRAYS WITH ALL ZEROS. radii = np.zeros((rows_az,cols_rw)) l2rw = np.zeros((rows_az,cols_rw)) #8. INITIALIZE ANAZ WITH MID-POINT ANGLES. start_az=0.; end_az=360. anaz = np.linspace(start_az,end_az,360./del_azimuth) print("\n \nYou are using the following settings:") print("del_azimuth :",del_azimuth) print("start_az :",start_az) print("end_az :",end_az) print("number of rows (azimuths) (",max_angle_az,"/",del_azimuth,") :",rows_az) #9. PUT RADIUS AND L2RW VALUES IN 2-D ARRAY ACCORDING TO AZIMUTHS. j=0; daz=del_azimuth-0.2; gates_beam=int(range_max/del_range) print("range_max :",range_max) print("del_range :",del_range) print("gates per beam :",gates_beam) print("number of columns (rw) (",range_max,"/",del_range,") :",cols_rw) print("giving the shape of",np.shape(radii),"\n \n") for i in range(num_supob_boxes): ai=int(azimuth[i]/del_azimuth) aim1=int(azimuth[i-1]/del_azimuth) if(i != 0 and np.abs(ai - aim1) > 0.8): j=0 if(i != 0 and np.abs(ai - aim1) < 0.8): j=j+1 if(j == gates_beam): print('Error Raised at azimuth angle: ',azimuth[i]*180/3.141592) print('np.abs(ai - aim1) = ',np.abs(ai - aim1)) print('ai =',ai) print('aim1 =',aim1) for k in range(gates_beam): if(rad[i] > del_range*k and rad[i] <= del_range*(k+1)): radii[ai-1][j] = dr2 + del_range*k l2rw[ai-1][j] = l2[i] #10. INITIALIZE THE POLAR PLOT AS ALL MISSING DATA (-999). fig = plt.figure(figsize=(8,8)) ax = fig.add_subplot(111,polar=True) #ax.set_theta_zero_location("N"); ax.set_theta_direction(-1) # DO NOT USE THESE!!!! theta,r = np.meshgrid(anaz,np.linspace(0.,range_max,range_max/del_range)) theta=deg2rad(theta) rw = np.zeros(shape=(len(theta),len(r[0]))) rw.fill(-999) r=r.T; rw=rw.T print('Max value should be...',np.max(l2rw)) #11. POPULATE THE EMPTY RW ARRAY WITH ACTUAL VALUES. if(np.max(radii)==0.0): print('Make sure gamma is written out by GSI!') l=0 for i in range(len(anaz)): for j in range(len(radii[i])): for k in range(len(r[i])): if((radii[i][j]-r[i][k]) <= del_range/2.): rw[i][k]=l2rw[i][j] l=l+1 break #### print the index of the single observatios ### #print(np.argmax(rw)) #print(rw[0]) #print(np.shape(rw)) #print(np.shape(radii)) #print(rw[np.argmax(rw)/39][:]) #print(r[np.argmax(rw)/39][:]) ################################################## #if(del_range == 5000): # so_type="default" #if(del_range == 3000): # so_type="tuned" calc_variance=True if(calc_variance): if(del_time == 0.5 and del_range == 5000): f1=open('./'+str(station_id)+'stats'+str(anal_time)+"default",'w+') if(del_time != 0.5 and del_range == 5000): f1=open('./'+str(station_id)+'stats'+str(anal_time)+"default_7pt5min",'w+') if(del_time != 0.5 and del_range == 3000): f1=open('./'+str(station_id)+'stats'+str(anal_time)+"tuned",'w+') if(del_time == 0.5 and del_range == 3000): f1=open('./'+str(station_id)+'stats'+str(anal_time)+"tuned_30min",'w+') rw_stdev=rw[rw>-999].std() rw_mean =rw[rw>-999].mean() rw_var =rw[rw>-999].var() f1.write("The output here describes some statistics for the radial wind obs \n") f1.write("Obs file={} \n".format("./output.bin")) f1.write("date ={} \n".format(anal_time)) f1.write("Station ={} \n".format(station_id)) f1.write("DELAZ ={} \n".format(del_azimuth)) f1.write("DELELV ={} \n".format(del_elev)) f1.write("DELRNG ={} \n".format(del_range)) f1.write("RMAX ={} \n".format(range_max)) f1.write("DELTIME ={} \n".format(del_time)) f1.write("MINNUM ={} \n".format(minnum)) f1.write("STDEV ={} \n".format(rw_stdev)) f1.write("MEAN ={} \n".format(rw_mean)) f1.write("VAR ={} \n".format(rw_var)) f1.close() print('The max rw value is: ',np.max(rw)) if(np.max(rw) == -999): print('There is an error reading in the data.') exit() print(l,' out of ',num_supob_boxes,' super ob boxes read') #12. FINISH MAKING THE POLAR PLOT. if(False): cmap = plt.cm.jet cmap.set_under('white') else: c = mcolors.ColorConverter().to_rgb cmap = make_colormap( [c('deepskyblue'),c('navy') ,0.20, # light blue to dark blue c('#02ff02') ,c('#003500') ,0.47, # bright green to dark green c('#809e80') ,c('white') ,0.50, # gray with green tint to white c('white') ,c('#9e8080') ,0.53, # white to gray with red tint c('#350000') ,c('#ff0000') ,0.80, # dark red to bright red c('salmon') ,c('yellow')]) # salmon to yellow #plt.register_cmap(name=gwr.name, cmap=gwr); cmap= plt.set_cmap(gwr) cmap.set_under('#999999') cmap.set_over('purple') mesh = ax.pcolormesh(theta,r.T,rw.T,shading='flat',cmap=cmap,vmin=-40,vmax=40) cbar = fig.colorbar(mesh,shrink=0.85,pad=0.10,ax=ax) cbar.set_label('$m/s$') lLaTeX=True if(lLaTeX): fig.suptitle('Doppler Velocity Super-Observations '+station_id +'\n $\Delta$r: '+str(int(del_range))+'-m, '\ +' $\Delta \\theta$: '+str(del_azimuth)+' deg, '\ +'\n $\Delta$t: +/-'+str(del_time*60)+' min, '\ +' N: '+str(minnum), fontsize=15,x=0.42,y=0.95) if(not lLaTeX): fig.suptitle('Doppler Velocity Super-Observations '+station_id +'\n del_range: '+str(int(del_range))+'-m, '\ +' del_azimuth: '+str(del_azimuth)+' deg, '\ +'\n del_time: +/-'+str(del_time*60)+' min, '\ +' minnum: '+str(minnum), fontsize=15,x=0.42,y=0.95) ax.grid(True) plt.show() plt.savefig('./'+station_id+'_'+anal_time+'_'\ +str(int(del_range))+'-'+str(del_azimuth)+'-'\ +str("%0.3f" % del_time)+'-'+str(int(minnum))+'.png'\ ,bbox_inches='tight') #13. CALCULATE SOME TIMING STATS. toc = time.clock() # check to see how long it took to run. sec = str(toc-tic) print('time it took to run: '+sec+' seconds.')