def topoheaderread(inputfile, closefile=True): """ topoheaderread (inputfile): read the header in inputfile and place in dictionary topoheader to be returned. The header is of the following form with columns containing the topoheader value and keyword respectively. int ncols int nrows float xll float yll float cellsize float nodata_value """ topoheader = { 'ncols': 0, 'nrows': 0, 'xll': 0.0, 'yll': 0.0, 'cellsize': 0.0, 'nodata_value': 0 } keylist = topoheader.keys() keymap = {'ncols':'ncols','nrows':'nrows','xll':'xll','yll':'yll','cellsize':'cellsize','nodata_value':'nodata_value', \ 'xllcenter':'xll','yllcenter':'yll','xllcorner':'xll','yllcorner':'yll'} fid = open(inputfile, 'r') keyleft = len(keylist) while keyleft > 0: line = string.split(fid.readline()) if line: if line[0].lower() in keymap.keys(): topoheader[keymap[line[0].lower()]] = iotools.convertd2e( line[1]) keyleft = keyleft - 1 if line[1].lower() in keymap.keys(): topoheader[keymap[line[1].lower()]] = iotools.convertd2e( line[0]) keyleft = keyleft - 1 #check if passes convert strings values to numeric for key in keylist: if not key in topoheader: print('ERROR: topoheader not fully specified in %s' % (inputfile)) exit else: if '.' in topoheader[key] or 'nan' in topoheader[key].lower( ) or 'e' in topoheader[key].lower(): topoheader[key] = float(topoheader[key]) else: topoheader[key] = int(topoheader[key]) if closefile: fid.close() return topoheader else: return (fid, topoheader)
def topoheaderread (inputfile, closefile=True): """ topoheaderread (inputfile): read the header in inputfile and place in dictionary topoheader to be returned. The header is of the following form with columns containing the topoheader value and keyword respectively. int ncols int nrows float xll float yll float cellsize float nodata_value """ topoheader={'ncols':0,'nrows':0,'xll':0.0,'yll':0.0,'cellsize':0.0,'nodata_value':0} keylist=topoheader.keys() keymap = {'ncols':'ncols','nrows':'nrows','xll':'xll','yll':'yll','cellsize':'cellsize','nodata_value':'nodata_value', \ 'xllcenter':'xll','yllcenter':'yll','xllcorner':'xll','yllcorner':'yll'} fid=open(inputfile,'r') keyleft=len(keylist) while keyleft> 0 : line=string.split(fid.readline()) if line: if line[0].lower() in keymap.keys(): topoheader[keymap[line[0].lower()]]= iotools.convertd2e(line[1]) keyleft=keyleft-1 if line[1].lower() in keymap.keys(): topoheader[keymap[line[1].lower()]]= iotools.convertd2e(line[0]) keyleft=keyleft-1 #check if passes convert strings values to numeric for key in keylist : if not key in topoheader: print('ERROR: topoheader not fully specified in %s' % (inputfile)) exit else: if '.' in topoheader[key] or 'nan' in topoheader[key].lower() or 'e' in topoheader[key].lower(): topoheader[key]=float(topoheader[key]) else: topoheader[key]=int(topoheader[key]) if closefile: fid.close() return topoheader else: return (fid,topoheader)
def topofile2griddata (inputfile,topotype=2): """ topofile2griddata (inputfile): read topofile into a numpy array. read data in topo files of type 1, 2 or 3 into numpy arrays X,Y, Z, each with shape=(nrows,ncols) holding x,y and z coords. """ import pylab if abs(topotype)>1: (fin,topoheader)=topoheaderread(inputfile,closefile=False) zdata=fin.readlines() fin.close() for row in xrange(len(zdata)): zdata[row]=iotools.convertd2e(zdata[row]) zdata[row]=string.split(zdata[row]) for col in xrange(len(zdata[row])) : zdata[row][col]=float(zdata[row][col]) Z=np.array(zdata) Z=np.reshape(Z,(topoheader['nrows'],topoheader['ncols'])) xlower=topoheader['xll'] xupper=xlower+ topoheader['cellsize']*(topoheader['ncols']-1) ylower = topoheader['yll'] yupper = ylower+ topoheader['cellsize']*(topoheader['nrows']-1) x=np.linspace(xlower,xupper,topoheader['ncols']) y=np.linspace(ylower,yupper,topoheader['nrows']) [X,Y]=np.meshgrid(x,y) #Y=np.flipud(Y) else: a=iotools.datafile2array(inputfile) xdiff=np.diff(a[:,0]) inddiff=pylab.find(xdiff<0) xlength=inddiff[0]+1 ylength=len(a[:,0])/xlength x=a[:,0] y=a[:,1] z=a[:,2] X=np.reshape(x,(ylength,xlength)) Y=np.reshape(y,(ylength,xlength)) Z=np.reshape(z,(ylength,xlength)) x = X[0,:] y = Y[:,0] #y = np.flipud(y) if topotype < 0: Z = -Z Z = np.flipud(Z) return X,Y,Z,x,y
def topofile2griddata(inputfile, topotype=2): """ topofile2griddata (inputfile): read topofile into a numpy array. read data in topo files of type 1, 2 or 3 into numpy arrays X,Y, Z, each with shape=(nrows,ncols) holding x,y and z coords. """ import pylab if abs(topotype) > 1: (fin, topoheader) = topoheaderread(inputfile, closefile=False) zdata = fin.readlines() fin.close() for row in xrange(len(zdata)): zdata[row] = iotools.convertd2e(zdata[row]) zdata[row] = string.split(zdata[row]) for col in xrange(len(zdata[row])): zdata[row][col] = float(zdata[row][col]) Z = np.array(zdata) Z = np.reshape(Z, (topoheader['nrows'], topoheader['ncols'])) xlower = topoheader['xll'] xupper = xlower + topoheader['cellsize'] * (topoheader['ncols'] - 1) ylower = topoheader['yll'] yupper = ylower + topoheader['cellsize'] * (topoheader['nrows'] - 1) x = np.linspace(xlower, xupper, topoheader['ncols']) y = np.linspace(ylower, yupper, topoheader['nrows']) [X, Y] = np.meshgrid(x, y) #Y=np.flipud(Y) else: a = iotools.datafile2array(inputfile) xdiff = np.diff(a[:, 0]) inddiff = pylab.find(xdiff < 0) xlength = inddiff[0] + 1 ylength = len(a[:, 0]) / xlength x = a[:, 0] y = a[:, 1] z = a[:, 2] X = np.reshape(x, (ylength, xlength)) Y = np.reshape(y, (ylength, xlength)) Z = np.reshape(z, (ylength, xlength)) x = X[0, :] y = Y[:, 0] #y = np.flipud(y) if topotype < 0: Z = -Z Z = np.flipud(Z) return X, Y, Z, x, y