def converttopo(infile, outfile): """ convert from 3-column, x,y,z, to single column with header for spacing """ if (os.path.exists(outfile)): print(' ' + outfile + ' already exists...skipping') print ' To recreate delete file and then rerun maketopo.py\n' else: print(' converting ' + (infile) + ' to ' + (outfile)) print ' output format contains a header and single column of data' a = np.loadtxt(infile, skiprows=9) 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)) Y = np.flipud(Y) Z = np.flipud(Z) gt.griddata2topofile(X, Y, Z, outfile) print ' file conversion complete\n'
def converttopo(infile,outfile): """ convert from 3-column, x,y,z, to single column with header for spacing """ if (os.path.exists(outfile)): print (' '+outfile+' already exists...skipping') print ' To recreate delete file and then rerun maketopo.py\n' else: print (' converting '+(infile)+' to '+(outfile)) print ' output format contains a header and single column of data' a = np.loadtxt(infile,skiprows=9) 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)) Y=np.flipud(Y) Z=np.flipud(Z) gt.griddata2topofile(X,Y,Z,outfile) print ' file conversion complete\n'
def dem2(): framenumber = 0 outfile = 'depth_t0s_ft.tt2' (X,Y,Q)=cf.fort2griddata(framenumber,xll,yll,cellsize,ncols,nrows) print 'converting file to feet' X = X*m2ft Y = Y*m2ft Q = Q*m2ft gt.griddata2topofile(X,Y,Q,outfile) infile = 'depth_t0s_ft.tt2' outfile = 'depth_t0s_ft.asc' gt.esriheader(infile,outfile)
def expandinit(ncomp): """ x1=6.145e5 - 200. x2=6.194e5 y1=4.899e6 -260. -500. y2=4.907e6 + 1300.0 mx = int(x2-x1) + 2 my = int(y2-y1) + 2 #nplots = [44] """ dirname = 'sisters_subdomain_init' comps = [1, ncomp] for comp in comps: outfilename = 'topo_' + str(comp) + '_aug2.0080' outfilename2 = 'topo_' + str(comp) + '_aug2_div.0080' infilename = 'topo_' + str(comp) + '_aug.0080' outname = os.path.join(dirname, outfilename) outname2 = os.path.join(dirname, outfilename2) inname = os.path.join(dirname, infilename) print('reading: ' + inname) (X, Y, Z) = gt.topofile2griddata(inname) (nrows, ncols) = np.shape(Z) print('expanding grid') x0 = X[0, 0] xu = X[0, -1] y0 = Y[-1, 0] yu = Y[0, 0] dx = X[0, 1] - X[0, 0] dy = Y[0, 0] - Y[1, 0] xa0 = np.array(x0 - dx).reshape(1, 1) xau = np.array(xu + dx).reshape(1, 1) ya0 = np.array(y0 - dy).reshape(1, 1) yau = np.array(yu + dy).reshape(1, 1) YL = Y[:, 0] YL = YL.reshape(nrows, 1) YR = Y[:, -1] YR = YR.reshape(nrows, 1) YU = Y[0, :] + dy YU = YU.reshape(1, ncols) YU = np.hstack((yau, YU, yau)) Y0 = Y[-1, :] - dy Y0 = Y0.reshape(1, ncols) Y0 = np.hstack((ya0, Y0, ya0)) XL = X[:, 0] - dx XL = XL.reshape(nrows, 1) XR = X[:, -1] + dx XR = XR.reshape(nrows, 1) XU = X[0, :] XU = XU.reshape(1, ncols) XU = np.hstack((xa0, XU, xau)) X0 = XU ZL = 0.0 * XL ZR = 0.0 * XR ZU = 0.0 * YU Z0 = 0.0 * Y0 print 'shapes:' print np.shape(X) print np.shape(XL) print np.shape(XR) print np.shape(XU) print np.shape(X0) print '-----------' X = np.hstack((XL, X, XR)) X = np.vstack((XU, X, X0)) Y = np.hstack((YL, Y, YR)) Y = np.vstack((YU, Y, Y0)) Z = np.hstack((ZL, Z, ZR)) Z = np.vstack((ZU, Z, Z0)) if comp == 1: H = np.copy(Z) for i in xrange(nrows): for j in xrange(ncols): if H[i, j] <= 1.e-3: Z[i, j] = 0.0 print('writing: ' + outfilename) gt.griddata2topofile(X, Y, Z, outname) for i in xrange(nrows): for j in xrange(ncols): if H[i, j] > 1.e-3: Z[i, j] = Z[i, j] / H[i, j] print('writing: ' + outfilename2) gt.griddata2topofile(X, Y, Z, outname2)
def fort2topotype(framenumber, outfile, fortdir, xll, yll, cellsize, ncols, nrows, m=1, topotype=2): """ convert data in a fort file of framenumber = XXXX, ie fort.qXXXX to a DEM style file of topotype=1,2 or 3, or gdal (gdal/esri header) with uniform spacing. m is the component of q, ie. the column in the fort.qXXXX file for all components of q, see fort2xyscattered or amr2single specifying xll, yll, cellsize, ncols, nrows will result in interpolation of AMR data without xll, yll, cellsize, ncols, nrows...the domain will be the same as input frame and coarse resolution. """ try: import geotools.topotools as gt except: import clawpack.geoclaw.topotools as gt numstring = str(10000 + framenumber) framenostr = numstring[1:] forttname = os.path.join(fortdir, 'fort.t' + framenostr) fortqname = os.path.join(fortdir, 'fort.q' + framenostr) nodata_value = -9999.0 solutionlist = fort2list(fortqname, forttname) if (not xll): dx = solutionlist[0]['dx'] dy = solutionlist[0]['dy'] xll = solutionlist[0]['xlowdomain'] yll = solutionlist[0]['ylowdomain'] xhi = solutionlist[0]['xhidomain'] yhi = solutionlist[0]['yhidomain'] cellsize = min(dx, dy) ncols = int(np.floor((xhi - xll) / cellsize)) nrows = int(np.floor((yhi - yll) / cellsize)) xll = xll + 0.5 * cellsize yll = yll + 0.5 * cellsize xhi = xhi - 0.5 * cellsize yhi = yhi - 0.5 * cellsize meqn = solutionlist[0]['meqn'] xv = np.array(xll + cellsize * np.arange(ncols)) yv = np.array(yll + cellsize * np.arange(nrows)) (X, Y) = np.meshgrid(xv, yv) Y = np.flipud(Y) Q = np.zeros(np.shape(X)) if (m == 'topo'): for j in xrange(ncols): xp = X[0, j] for i in xrange(nrows): yp = Y[i, 0] qv = pointfromfort((xp, yp), solutionlist) Q[i, j] = qv[meqn - 1] - qv[0] elif (m == 'depth'): for j in xrange(ncols): xp = X[0, j] for i in xrange(nrows): yp = Y[i, 0] qv = pointfromfort((xp, yp), solutionlist) depth = qv[0] if (depth <= 1.e-3): depth = nodata_value Q[i, j] = depth elif (m == 'eta'): for j in xrange(ncols): xp = X[0, j] for i in xrange(nrows): yp = Y[i, 0] qv = pointfromfort((xp, yp), solutionlist) eta = qv[meqn - 1] if (qv[0] <= 1.e-3): eta = nodata_value Q[i, j] = eta elif (m == 'all'): Q = np.zeros((ncols * nrows, meqn)) for i in xrange(nrows): yp = Y[i, 0] for j in xrange(ncols): xp = X[0, j] k = i * ncols + j qv = pointfromfort((xp, yp), solutionlist) Q[k] = qv else: for j in xrange(ncols): xp = X[0, j] for i in xrange(nrows): yp = Y[i, 0] qv = pointfromfort((xp, yp), solutionlist) Q[i, j] = qv[m - 1] if m == 'all': headerstring = """ """ np.savetxt(outfile, Q) #,header=headerstring) save for new numpy version elif (topotype == 'gdal'): gt.griddata2topofile(X, Y, Q, '.tmpfile', nodata_value_out=nodata_value) infile = '.tmpfile' gt.esriheader(infile, outfile) os.system('rm .tmpfile') else: gt.griddata2topofile(X, Y, Q, outfile, topotype, nodata_value_out=nodata_value)
def fort2topotype(framenumber,outfile,fortdir,xll,yll,cellsize,ncols,nrows,m=1,topotype=2): """ convert data in a fort file of framenumber = XXXX, ie fort.qXXXX to a DEM style file of topotype=1,2 or 3, or gdal (gdal/esri header) with uniform spacing. m is the component of q, ie. the column in the fort.qXXXX file for all components of q, see fort2xyscattered or amr2single specifying xll, yll, cellsize, ncols, nrows will result in interpolation of AMR data without xll, yll, cellsize, ncols, nrows...the domain will be the same as input frame and coarse resolution. """ try: import geotools.topotools as gt except: import clawpack.geoclaw.topotools as gt numstring = str(10000 + framenumber) framenostr = numstring[1:] forttname = os.path.join(fortdir,'fort.t'+framenostr) fortqname = os.path.join(fortdir,'fort.q'+framenostr) nodata_value = -9999.0 solutionlist=fort2list(fortqname,forttname) if (not xll): dx = solutionlist[0]['dx'] dy = solutionlist[0]['dy'] xll = solutionlist[0]['xlowdomain'] yll = solutionlist[0]['ylowdomain'] xhi = solutionlist[0]['xhidomain'] yhi = solutionlist[0]['yhidomain'] cellsize = min(dx,dy) ncols = int(np.floor((xhi-xll)/cellsize)) nrows = int(np.floor((yhi-yll)/cellsize)) xll = xll + 0.5*cellsize yll = yll + 0.5*cellsize xhi = xhi - 0.5*cellsize yhi = yhi - 0.5*cellsize meqn = solutionlist[0]['meqn'] xv = np.array(xll + cellsize*np.arange(ncols)) yv = np.array(yll + cellsize*np.arange(nrows)) (X,Y) = np.meshgrid(xv,yv) Y = np.flipud(Y) Q = np.zeros(np.shape(X)) if (m=='topo'): for j in xrange(ncols): xp = X[0,j] for i in xrange(nrows): yp = Y[i,0] qv = pointfromfort((xp,yp),solutionlist) Q[i,j] = qv[meqn-1]-qv[0] elif (m=='depth'): for j in xrange(ncols): xp = X[0,j] for i in xrange(nrows): yp = Y[i,0] qv = pointfromfort((xp,yp),solutionlist) depth = qv[0] if (depth<=1.e-3): depth = nodata_value Q[i,j] = depth elif (m=='eta'): for j in xrange(ncols): xp = X[0,j] for i in xrange(nrows): yp = Y[i,0] qv = pointfromfort((xp,yp),solutionlist) eta = qv[meqn-1] if (qv[0]<=1.e-3): eta = nodata_value Q[i,j] = eta elif (m=='all'): Q = np.zeros((ncols*nrows,meqn)) for i in xrange(nrows): yp = Y[i,0] for j in xrange(ncols): xp = X[0,j] k = i*ncols + j qv = pointfromfort((xp,yp),solutionlist) Q[k]=qv else: for j in xrange(ncols): xp = X[0,j] for i in xrange(nrows): yp = Y[i,0] qv = pointfromfort((xp,yp),solutionlist) Q[i,j] = qv[m-1] if m=='all': headerstring=""" """ np.savetxt(outfile,Q)#,header=headerstring) save for new numpy version elif (topotype=='gdal'): gt.griddata2topofile(X,Y,Q,'.tmpfile',nodata_value_out=nodata_value) infile = '.tmpfile' gt.esriheader(infile,outfile) os.system('rm .tmpfile') else: gt.griddata2topofile(X,Y,Q,outfile,topotype,nodata_value_out=nodata_value)