def read_hgrid(self, fid): "Read horizontal grid info from SELFE binary output filefile" #read dimensions self.np = io.fread(fid, 1, 'i') self.ne = io.fread(fid, 1, 'i') #read grid and bathymetry pos = fid.tell() hgridtmp = io.fread(fid, 4 * self.np, 'f') self.x, self.y, self.dp, tmp1 = hgridtmp.reshape(self.np, 4).transpose() #read bottom index fid.seek(pos) hgridtmp = io.fread(fid, 4 * self.np, 'i') tmp1, tmp2, tmp3, self.bot_idx = hgridtmp.reshape(self.np, 4).transpose() #read element connectivity list self.elem = io.fread(fid, 4 * self.ne, 'i') self.elem = self.elem.reshape(self.ne, 4)[:, 1:4] #create kdtree self.kdtree = KDTree(zip(self.x, self.y))
def subReadWrite(a, size, fn, fread, fwrite, what='Some'): import os print " %s testing .." % what, # Speed tests @timed def innerSpeedTestWrite(): fwrite(fid, size, a) @timed def innerSpeedTestRead(): return fread(fid, size, 'd') with open(fn, 'w+b') as fid: tmp, dtW = innerSpeedTestWrite() with open(fn, 'rb') as fid: a1, dtR = innerSpeedTestRead() os.remove(fn) # Test more options b = a[:10] # only use a few values for these tests with open(fn, 'w+b') as fid: fwrite(fid, b.size, b, 'd', 1) fid.seek(0) # all data types: 'bBcS1hHfiIlu4dFD' b2 = fread(fid, b.size, 'd', 'f', 1) # convert to floats fid.seek(0) b3 = fread(fid, b.size, 'd', 'i', 1) # convert to int32 fid.seek(0) b4 = fread(fid, b.size, 'd', 'b', 1) # convert to int8 fid.seek(0) b5 = fread(fid, b.size, 'd', 'h', 1) # convert to int16 os.remove(fn) # Finalize stuff if all(a == a1) and all(abs(b-b2) < 0.01) and \ all(abs(b-b3) < 1) and all(abs(b-b4) < 1) and all(abs(b-b5) < 1): print ".. passed" else: print ".. failed" print " a=%s\na1=%s\nb2=%s\nb3=%s\nb4=%s\nb5=%s" % (a, a1, b2, b3, b4, b5) return dtW + dtR
def read_hgrid(self,fid): "Read horizontal grid info from SELFE binary output filefile" #read dimensions self.np = io.fread(fid,1,'i') self.ne = io.fread(fid,1,'i') #read grid and bathymetry pos=fid.tell() hgridtmp = io.fread(fid,4*self.np,'f') self.x, self.y, self.dp, tmp1 = hgridtmp.reshape(self.np,4).transpose() #read bottom index fid.seek(pos) hgridtmp = io.fread(fid,4*self.np,'i') tmp1, tmp2, tmp3, self.bot_idx = hgridtmp.reshape(self.np,4).transpose() #read element connectivity list self.elem = io.fread(fid,4*self.ne,'i') self.elem = self.elem.reshape(self.ne,4)[:,1:4]
def read_hgrid(self, fid): """Read horizontal grid info from SELFE binary output file.""" # Read dimensions. self.np = io.fread(fid, 1, 'i') self.ne = io.fread(fid, 1, 'i') # Read grid and bathymetry. pos = fid.tell() hgridtmp = io.fread(fid, 4*self.np, 'f') self.x, self.y, self.dp, tmp1 = hgridtmp.reshape(self.np, 4).T # Read bottom index. fid.seek(pos) hgridtmp = io.fread(fid, 4*self.np, 'i') tmp1, tmp2, tmp3, self.bot_idx = hgridtmp.reshape(self.np, 4).T # Read element connectivity list. self.elem = io.fread(fid, 4*self.ne, 'i') self.elem = self.elem.reshape(self.ne, 4)[:, 1:4] # Create kdtree. self.kdtree = KDTree(list(zip(self.x, self.y)))
def testAutoNum(): import os print "Testing auto num feature of numpyIO ..", a = np.array((1, 2, 3), 'd') fn = 'temp2.bin' with open(fn, 'w+b') as fid: fwrite(fid, -1, a) fid.seek(0) a1 = fread(fid, -1, 'd') os.remove(fn) passed = all(a == a1) assert passed if passed: print "passed" else: print "failed" print " a=%s\na1=%s" % (a, a1)
def innerSpeedTestRead(): return fread(fid, size, 'd')
def read_header(self,fid): "Read header information from SELFE binary output file" #read misc header info self.data_format = fid.read(48) self.version = fid.read(48) self.start_time = fid.read(48) self.var_type = fid.read(48) self.var_dimension = fid.read(48) self.nsteps = io.fread(fid,1,'i') self.dt = io.fread(fid,1,'f') self.skip = io.fread(fid,1,'i') self.flag_sv = io.fread(fid,1,'i') self.flag_dm = io.fread(fid,1,'i') #@todo check when zDes needs to be read #self.zDes = io.fread(fid,1,'f') #read vert grid info self.nlevels = io.fread(fid,1,'i') self.kz = io.fread(fid,1,'i') self.h0 = io.fread(fid,1,'f') self.hs = io.fread(fid,1,'f') self.hc = io.fread(fid,1,'f') self.theta_b = io.fread(fid,1,'f') self.theta = io.fread(fid,1,'f') self.zlevels = io.fread(fid,self.kz,'f') self.slevels = io.fread(fid,self.nlevels-self.kz,'f')
def read_time_series(self,fname,nodes=None,levels=None,xy=N.array([]),nfiles=3,sfile=1,datadir=None): """ Main function to extract a spatial and temporal slice of entire 3D Time series. Returns [t,t_iter,eta,dp,data] where t : time in seconds from simulation start t_iter : iteration number from simulation start eta : Surface water elevation time series dp : Bathymetry (depth of sea bed from MSL) data[t,nodes,levels,vars] : extracted data slice (ie Salinity, Temp, Velocity etc) Options: nodes : list of nodes to extract (default is all nodes) level : list of levels to extract (default is all levels) xy : array of x,y coordinates to extract (default is none) sfile : serial number of starting file (default is one) nfiles : number of files in data sequence (default is one) NOTE : node index starts at zero so add one to match up with node numbers in SELFE hgrid.gr3 file """ #initialize vars t = N.array([]) t_iter = N.array([]) eta = [] data = [] if nfiles is None: nfiles = self.nfiles if datadir is None: datadir = self.datadir #convert xy points to list of nodes, #find parent elements & calculate interpolation weights if xy.size!=0: if xy.shape[1]!=2: sys.exit('xy array shape wrong') nodes=N.array([],dtype='int32') arco=N.array([]) for xy00 in xy: parent, tmparco, node3 = self.find_parent_element(xy00[0],xy00[1]) nodes = N.append(nodes,node3-1) arco = N.append(arco,tmparco) #set default for nodes to be all nodes #node index starts at zero elif nodes is None: nodes = N.arange(self.np) #set default for level to be all levels if levels is None: levels = N.arange(self.nlevels) #check whether 2D or 3D variable is being read if self.flag_dm==2: nlevs = 1 levels = N.array([0]) else: nlevs = self.nlevels #read time series slice for files in N.arange(sfile, sfile + nfiles): try: fname1 = datadir + '/' + str(files) + '_' + fname fid = open(fname1,'rb') fid.seek(self.data_start_pos) for i in N.arange(self.nsteps): t = N.append(t, io.fread(fid, 1, 'f')) t_iter = N.append(t_iter, io.fread(fid, 1, 'i')) eta.append(io.fread(fid, self.np, 'f')) tmpdata = io.fread(fid, self.flag_sv*self.grid_size, 'f') tmpdata = tmpdata.reshape(self.np, nlevs, self.flag_sv) #only keep requested slice of tmpdata #i.e. tmpdata[nodes,levels,var] tmpdata = tmpdata[nodes,:,:] tmpdata = tmpdata[:,levels,:] data.append(tmpdata) except: continue # import pdb; pdb.set_trace() eta = N.column_stack(eta[:]).T eta = eta[:,nodes] data = N.array(data) dp = self.dp[nodes] #convert nodal values back to xy point values if needed if xy.size!=0: #try:#not sure about this. need to look at it ion more detail put in to remove shape error tmpdata = N.zeros((data.shape[0],data.shape[1]//3,data.shape[2],data.shape[3]))/0. #except: # tmpdata = N.zeros((data.shape[0],data.shape[1]//3,data.shape[2]))/0. tmpeta = N.zeros((eta.shape[0],eta.shape[1]//3))/0. tmpdp = N.zeros(dp.shape[0]//3)/0. for i in range(xy.shape[0]): n1 = i*3 n2 = n1+1 n3 = n2+1 tmpdata[:,i,:,:] = data[:,n1,:,:]*arco[n1] + data[:,n2,:,:]*arco[n2] + data[:,n3,:,:]*arco[n3] tmpeta[:,i] = eta[:,n1]*arco[n1] + eta[:,n2]*arco[n2] + eta[:,n3]*arco[n3] tmpdp[i] = dp[n1]*arco[n1] + dp[n2]*arco[n2] + dp[n3]*arco[n3] data = tmpdata eta = tmpeta dp = tmpdp return [t,t_iter,eta,dp,data]
def readArray(fileName, type='float'): """Read in a matrix of floats or doubles from a data file. If type = 'float', then use single precision (32 bit) float. This is the default. If type = 'double', then use double precision (64 bit) float. """ inFile = open(fileName, 'rb') dimPacked = inFile.read(4) dimensionsTuple = unpack('<i', dimPacked) dimensions = dimensionsTuple[0] if (dimensions == 1): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] num = dim1 if type == 'float': flatArray = numpyIO.fread(inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread(inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1) arrayData = flatArray.reshape(shape) elif (dimensions == 2): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] dim2Packed = inFile.read(4) dim2Tuple = unpack('<i', dim2Packed) dim2 = dim2Tuple[0] num = dim1 * dim2 if type == 'float': flatArray = numpyIO.fread(inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread(inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1, dim2) arrayData = flatArray.reshape(shape) elif (dimensions == 3): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] dim2Packed = inFile.read(4) dim2Tuple = unpack('<i', dim2Packed) dim2 = dim2Tuple[0] dim3Packed = inFile.read(4) dim3Tuple = unpack('<i', dim3Packed) dim3 = dim3Tuple[0] num = dim1 * dim2 * dim3 if type == 'float': flatArray = numpyIO.fread(inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread(inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1, dim2, dim3) arrayData = flatArray.reshape(shape) elif (dimensions == 4): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] dim2Packed = inFile.read(4) dim2Tuple = unpack('<i', dim2Packed) dim2 = dim2Tuple[0] dim3Packed = inFile.read(4) dim3Tuple = unpack('<i', dim3Packed) dim3 = dim3Tuple[0] dim4Packed = inFile.read(4) dim4Tuple = unpack('<i', dim4Packed) dim4 = dim4Tuple[0] num = dim1 * dim2 * dim3 * dim4 if type == 'float': flatArray = numpyIO.fread(inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread(inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1, dim2, dim3, dim4) arrayData = flatArray.reshape(shape) elif (dimensions == 5): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] dim2Packed = inFile.read(4) dim2Tuple = unpack('<i', dim2Packed) dim2 = dim2Tuple[0] dim3Packed = inFile.read(4) dim3Tuple = unpack('<i', dim3Packed) dim3 = dim3Tuple[0] dim4Packed = inFile.read(4) dim4Tuple = unpack('<i', dim4Packed) dim4 = dim4Tuple[0] dim5Packed = inFile.read(4) dim5Tuple = unpack('<i', dim5Packed) dim5 = dim5Tuple[0] num = dim1 * dim2 * dim3 * dim4 * dim5 if type == 'float': flatArray = numpyIO.fread(inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread(inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1, dim2, dim3, dim4, dim5) arrayData = flatArray.reshape(shape) else: raise "Error. Only 1 to 4-dimensional arrays (i.e., matrices) are supported!" inFile.close return arrayData
fig.subplots_adjust(top = 0.9,wspace = 0.12,hspace = 0.2,left = 0.05, right = 0.96) #Xgr = range(0,xsize) #Zgr = range(0,zsize) Xgr,Zgr = np.mgrid[0:zsize,0:xsize] #data = array.array('d') #data.read(fileobj,65*xsize*zsize*3) for i in range (1,Nt,1): for j in range (0,N): spsize = xsizes[j]*zsizes[j] shape = (xsizes[j],zsizes[j]) fileobj[j].seek(Header*4 + 4*(i)*spsize) data = fread(fileobj[j],spsize,'f') data = data.reshape(shape).transpose() pres[zstart[j]/zstep[j]:zend[j]/zstep[j]+1,xstart[j]/xstep[j]:xend[j]/xstep[j]+1] = data # print pres[:,xstart[i]/xstep[i]:xend[i]/xstep[i]+1].shape # print data.shape # outfile = open("%s\\data.dat"%(outdir),mode='w') # c=0 print amax(pres) fig.clear() ax = fig.add_subplot(111) ax.clear() cax = ax.imshow(pres[:,:], interpolation='nearest') axis('off') cb = colorbar(cax,orientation='horizontal',format ='%.3f',pad=0.01)
fig.subplots_adjust(top=0.9, wspace=0.12, hspace=0.2, left=0.05, right=0.96) # Xgr = range(0,xsize) # Zgr = range(0,zsize) Xgr, Zgr = np.mgrid[0:zsize, 0:xsize] # data = array.array('d') # data.read(fileobj,65*xsize*zsize*3) for i in range(1, Nt, 1): for j in range(0, N): spsize = xsizes[j] * zsizes[j] shape = (xsizes[j], zsizes[j]) fileobj[j].seek(Header * 4 + 4 * (i) * spsize) data = fread(fileobj[j], spsize, "f") data = data.reshape(shape).transpose() pres[zstart[j] / zstep[j] : zend[j] / zstep[j] + 1, xstart[j] / xstep[j] : xend[j] / xstep[j] + 1] = data # print pres[:,xstart[i]/xstep[i]:xend[i]/xstep[i]+1].shape # print data.shape # outfile = open("%s\\data.dat"%(outdir),mode='w') # c=0 print amax(pres) fig.clear() ax = fig.add_subplot(111) ax.clear() cax = ax.imshow(pres[:, :], interpolation="nearest") axis("off") cb = colorbar(cax, orientation="horizontal", format="%.3f", pad=0.01)
def readArray(fileName, type='float'): """Read in a matrix of floats or doubles from a data file. If type = 'float', then use single precision (32 bit) float. This is the default. If type = 'double', then use double precision (64 bit) float. """ inFile = open(fileName, 'rb') dimPacked = inFile.read(4) dimensionsTuple = unpack('<i', dimPacked) dimensions = dimensionsTuple[0] if (dimensions == 1): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] num = dim1 if type == 'float': flatArray = numpyIO.fread( inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread( inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1) arrayData = flatArray.reshape(shape) elif (dimensions == 2): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] dim2Packed = inFile.read(4) dim2Tuple = unpack('<i', dim2Packed) dim2 = dim2Tuple[0] num = dim1*dim2 if type == 'float': flatArray = numpyIO.fread( inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread( inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1, dim2) arrayData = flatArray.reshape(shape) elif (dimensions == 3): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] dim2Packed = inFile.read(4) dim2Tuple = unpack('<i', dim2Packed) dim2 = dim2Tuple[0] dim3Packed = inFile.read(4) dim3Tuple = unpack('<i', dim3Packed) dim3 = dim3Tuple[0] num = dim1*dim2*dim3 if type == 'float': flatArray = numpyIO.fread( inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread( inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1, dim2, dim3) arrayData = flatArray.reshape(shape) elif (dimensions == 4): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] dim2Packed = inFile.read(4) dim2Tuple = unpack('<i', dim2Packed) dim2 = dim2Tuple[0] dim3Packed = inFile.read(4) dim3Tuple = unpack('<i', dim3Packed) dim3 = dim3Tuple[0] dim4Packed = inFile.read(4) dim4Tuple = unpack('<i', dim4Packed) dim4 = dim4Tuple[0] num = dim1*dim2*dim3*dim4 if type == 'float': flatArray = numpyIO.fread( inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread( inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1, dim2, dim3, dim4) arrayData = flatArray.reshape(shape) elif (dimensions == 5): dim1Packed = inFile.read(4) dim1Tuple = unpack('<i', dim1Packed) dim1 = dim1Tuple[0] dim2Packed = inFile.read(4) dim2Tuple = unpack('<i', dim2Packed) dim2 = dim2Tuple[0] dim3Packed = inFile.read(4) dim3Tuple = unpack('<i', dim3Packed) dim3 = dim3Tuple[0] dim4Packed = inFile.read(4) dim4Tuple = unpack('<i', dim4Packed) dim4 = dim4Tuple[0] dim5Packed = inFile.read(4) dim5Tuple = unpack('<i', dim5Packed) dim5 = dim5Tuple[0] num = dim1*dim2*dim3*dim4*dim5 if type == 'float': flatArray = numpyIO.fread( inFile, num, 'f') elif type == 'double': flatArray = numpyIO.fread( inFile, num, 'd') else: raise 'Error: Invalid type.' shape = (dim1, dim2, dim3, dim4, dim5) arrayData = flatArray.reshape(shape) else: raise "Error. Only 1 to 4-dimensional arrays (i.e., matrices) are supported!" inFile.close return arrayData