def test_grd2array_dem2array(self): '''test the conversion result of grd to array and dem to array. The pts files should be the same''' #ANUGA models from anuga.file_conversion.grd2array import grd2array from anuga.file_conversion.dem2array import dem2array from anuga.file_conversion.asc2dem import asc2dem #Create .asc file. Uses the example from test_grd2array.py """ Format of asc file ncols 11 nrows 12 xllcorner 240000 yllcorner 7620000 cellsize 6000 NODATA_value -9999 """ x0 = 0.0 y0 = 0.0 ncols = 11 # Nx nrows = 12 # Ny xllcorner = x0 yllcorner = y0 cellsize = 1.0 NODATA_value = -9999 #Create .asc file root = 'test_asc' txt_file = root + '.asc' datafile = open(txt_file, "w") datafile.write('ncols ' + str(ncols) + "\n") datafile.write('nrows ' + str(nrows) + "\n") datafile.write('xllcorner ' + str(xllcorner) + "\n") datafile.write('yllcorner ' + str(yllcorner) + "\n") datafile.write('cellsize ' + str(cellsize) + "\n") datafile.write('NODATA_value ' + str(NODATA_value) + "\n") x_ex = num.linspace(xllcorner, xllcorner + (ncols - 1) * cellsize, ncols) y_ex = num.linspace(yllcorner, yllcorner + (nrows - 1) * cellsize, nrows) Z_ex = [[0., 3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33.], [1., 4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34.], [2., 5., 8., 11., 14., 17., 20., 23., 26., 29., 32., 35.], [3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36.], [4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37.], [5., 8., 11., 14., 17., 20., 23., 26., 29., 32., 35., 38.], [6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39.], [7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40.], [8., 11., 14., 17., 20., 23., 26., 29., 32., 35., 38., 41.], [9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39., 42.], [10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40., 43.]] points = axes2points(x_ex, y_ex) datavalues = linear_function(points) datavalues = datavalues.reshape(nrows, ncols) for row in datavalues: #print row datafile.write(" ".join(str(elem) for elem in row) + "\n") datafile.close() #create dem file from asc file txt_file_prj = root + '.prj' fid = open(txt_file_prj, 'w') fid.write("""Projection UTM Zone 56 Datum WGS84 Zunits NO Units METERS Spheroid WGS84 Xshift 0.0000000000 Yshift 10000000.0000000000 Parameters """) fid.close() txt_file_dem = root + '.dem' asc2dem(name_in=txt_file, name_out=root, use_cache=False, verbose=False) #convert grd to array x_grd, y_grd, Z_grd = grd2array(txt_file) #convert dem to array x_dem, y_dem, Z_dem = dem2array(txt_file_dem) #check grd2array and dem2array results are equal assert num.allclose(x_grd, x_dem) assert num.allclose(y_grd, y_dem) assert num.allclose(Z_grd, Z_dem) #check grd2array (dem2array) results are correct assert num.allclose(x_grd, x_ex) assert num.allclose(y_grd, y_ex) assert num.allclose(Z_grd, Z_ex) try: os.remove(root + '.dem') os.remove(root + '.asc') os.remove(root + '.prj') except: # Expect error on windows pass
def test_grd2array_1(self): """ Format of asc file ncols 11 nrows 12 xllcorner 240000 yllcorner 7620000 cellsize 6000 NODATA_value -9999 """ x0 = 0.0 y0 = 0.0 ncols = 11 # Nx nrows = 12 # Ny xllcorner = x0 yllcorner = y0 cellsize = 1.0 NODATA_value = -9999 #Create .asc file #txt_file = tempfile.mktemp(".asc")from anuga.config import netcdf_float root = 'test_asc_1' txt_file = root + '.asc' datafile = open(txt_file, "w") datafile.write('ncols ' + str(ncols) + "\n") datafile.write('nrows ' + str(nrows) + "\n") datafile.write('xllcorner ' + str(xllcorner) + "\n") datafile.write('yllcorner ' + str(yllcorner) + "\n") datafile.write('cellsize ' + str(cellsize) + "\n") datafile.write('NODATA_value ' + str(NODATA_value) + "\n") x_ex = num.linspace(xllcorner, xllcorner + (ncols - 1) * cellsize, ncols) y_ex = num.linspace(yllcorner, yllcorner + (nrows - 1) * cellsize, nrows) points = axes2points(x_ex, y_ex) #print points #print x.shape, x #print y.shape, y datavalues = linear_function(points) #print datavalues datavalues = datavalues.reshape(nrows, ncols) #print datavalues #print datavalues.shape for row in datavalues: #print row datafile.write(" ".join(str(elem) for elem in row) + "\n") datafile.close() #print quantity.vertex_values #print quantity.centroid_values x, y, Z = grd2array(txt_file) #print x #print y #print Z answer = [[0., 3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33.], [1., 4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34.], [2., 5., 8., 11., 14., 17., 20., 23., 26., 29., 32., 35.], [3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36.], [4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37.], [5., 8., 11., 14., 17., 20., 23., 26., 29., 32., 35., 38.], [6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39.], [7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40.], [8., 11., 14., 17., 20., 23., 26., 29., 32., 35., 38., 41.], [9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39., 42.], [10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40., 43.]] #print quantity.vertex_values assert num.allclose(Z, answer) assert num.allclose(x, x_ex) assert num.allclose(y, y_ex) os.remove(root + '.asc')
def test_grd2array_2(self): """ Format of asc file ncols 11 nrows 12 xllcorner 240000 yllcorner 7620000 cellsize 6000 NODATA_value -9999 """ x0 = 240000.0 y0 = 7620000.0 ncols = 11 # Nx nrows = 12 # Ny xllcorner = x0 yllcorner = y0 cellsize = 6000.0 NODATA_value = -9999 #Create .asc file #txt_file = tempfile.mktemp(".asc")from anuga.config import netcdf_float root = 'test_asc_2' txt_file = root + '.asc' datafile = open(txt_file, "w") datafile.write('ncols ' + str(ncols) + "\n") datafile.write('nrows ' + str(nrows) + "\n") datafile.write('xllcorner ' + str(xllcorner) + "\n") datafile.write('yllcorner ' + str(yllcorner) + "\n") datafile.write('cellsize ' + str(cellsize) + "\n") datafile.write('NODATA_value ' + str(NODATA_value) + "\n") x_ex = num.linspace(xllcorner, xllcorner + (ncols - 1) * cellsize, ncols) y_ex = num.linspace(yllcorner, yllcorner + (nrows - 1) * cellsize, nrows) points = axes2points(x_ex, y_ex) #print points #print x_ex.shape, x_ex #print y_ex.shape, y_ex datavalues = linear_function(points) #print datavalues datavalues = datavalues.reshape(nrows, ncols) #print datavalues #print datavalues.shape for row in datavalues: #print row datafile.write(" ".join(str(elem) for elem in row) + "\n") datafile.close() #print quantity.vertex_values #print quantity.centroid_values x, y, Z = grd2array(txt_file) #print x #print y #print Z answer = [[ 23100000., 23118000., 23136000., 23154000., 23172000., 23190000., 23208000., 23226000., 23244000., 23262000., 23280000., 23298000. ], [ 23106000., 23124000., 23142000., 23160000., 23178000., 23196000., 23214000., 23232000., 23250000., 23268000., 23286000., 23304000. ], [ 23112000., 23130000., 23148000., 23166000., 23184000., 23202000., 23220000., 23238000., 23256000., 23274000., 23292000., 23310000. ], [ 23118000., 23136000., 23154000., 23172000., 23190000., 23208000., 23226000., 23244000., 23262000., 23280000., 23298000., 23316000. ], [ 23124000., 23142000., 23160000., 23178000., 23196000., 23214000., 23232000., 23250000., 23268000., 23286000., 23304000., 23322000. ], [ 23130000., 23148000., 23166000., 23184000., 23202000., 23220000., 23238000., 23256000., 23274000., 23292000., 23310000., 23328000. ], [ 23136000., 23154000., 23172000., 23190000., 23208000., 23226000., 23244000., 23262000., 23280000., 23298000., 23316000., 23334000. ], [ 23142000., 23160000., 23178000., 23196000., 23214000., 23232000., 23250000., 23268000., 23286000., 23304000., 23322000., 23340000. ], [ 23148000., 23166000., 23184000., 23202000., 23220000., 23238000., 23256000., 23274000., 23292000., 23310000., 23328000., 23346000. ], [ 23154000., 23172000., 23190000., 23208000., 23226000., 23244000., 23262000., 23280000., 23298000., 23316000., 23334000., 23352000. ], [ 23160000., 23178000., 23196000., 23214000., 23232000., 23250000., 23268000., 23286000., 23304000., 23322000., 23340000., 23358000. ]] #print quantity.vertex_values assert num.allclose(Z, answer) assert num.allclose(x, x_ex) assert num.allclose(y, y_ex) os.remove(root + '.asc')
def test_grd2array_dem2array(self): '''test the conversion result of grd to array and dem to array. The pts files should be the same''' #ANUGA models from anuga.file_conversion.grd2array import grd2array from anuga.file_conversion.dem2array import dem2array from anuga.file_conversion.asc2dem import asc2dem #Create .asc file. Uses the example from test_grd2array.py """ Format of asc file ncols 11 nrows 12 xllcorner 240000 yllcorner 7620000 cellsize 6000 NODATA_value -9999 """ x0 = 0.0 y0 = 0.0 ncols = 11 # Nx nrows = 12 # Ny xllcorner = x0 yllcorner = y0 cellsize = 1.0 NODATA_value = -9999 #Create .asc file root = 'test_asc' txt_file = root+'.asc' datafile = open(txt_file,"w") datafile.write('ncols '+str(ncols)+"\n") datafile.write('nrows '+str(nrows)+"\n") datafile.write('xllcorner '+str(xllcorner)+"\n") datafile.write('yllcorner '+str(yllcorner)+"\n") datafile.write('cellsize '+str(cellsize)+"\n") datafile.write('NODATA_value '+str(NODATA_value)+"\n") x_ex = num.linspace(xllcorner, xllcorner+(ncols-1)*cellsize, ncols) y_ex = num.linspace(yllcorner, yllcorner+(nrows-1)*cellsize, nrows) Z_ex = [[ 0., 3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33.], [ 1., 4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34.], [ 2., 5., 8., 11., 14., 17., 20., 23., 26., 29., 32., 35.], [ 3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36.], [ 4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37.], [ 5., 8., 11., 14., 17., 20., 23., 26., 29., 32., 35., 38.], [ 6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39.], [ 7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40.], [ 8., 11., 14., 17., 20., 23., 26., 29., 32., 35., 38., 41.], [ 9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39., 42.], [ 10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40., 43.]] points = axes2points(x_ex, y_ex) datavalues = linear_function(points) datavalues = datavalues.reshape(nrows,ncols) for row in datavalues: #print row datafile.write(" ".join(str(elem) for elem in row) + "\n") datafile.close() #create dem file from asc file txt_file_prj = root+'.prj' fid = open(txt_file_prj, 'w') fid.write("""Projection UTM Zone 56 Datum WGS84 Zunits NO Units METERS Spheroid WGS84 Xshift 0.0000000000 Yshift 10000000.0000000000 Parameters """) fid.close() txt_file_dem = root+'.dem' asc2dem(name_in=txt_file, name_out=root, use_cache=False, verbose=False) #convert grd to array x_grd, y_grd, Z_grd = grd2array(txt_file) #convert dem to array x_dem, y_dem, Z_dem = dem2array(txt_file_dem) #check grd2array and dem2array results are equal assert num.allclose(x_grd, x_dem) assert num.allclose(y_grd, y_dem) assert num.allclose(Z_grd, Z_dem) #check grd2array (dem2array) results are correct assert num.allclose(x_grd, x_ex) assert num.allclose(y_grd, y_ex) assert num.allclose(Z_grd, Z_ex) try: os.remove(root + '.dem') os.remove(root + '.asc') os.remove(root + '.prj') except: # Expect error on windows pass
def test_grd2array_1(self): """ Format of asc file ncols 11 nrows 12 xllcorner 240000 yllcorner 7620000 cellsize 6000 NODATA_value -9999 """ x0 = 0.0 y0 = 0.0 ncols = 11 # Nx nrows = 12 # Ny xllcorner = x0 yllcorner = y0 cellsize = 1.0 NODATA_value = -9999 #Create .asc file #txt_file = tempfile.mktemp(".asc")from anuga.config import netcdf_float root = 'test_asc_1' txt_file = root+'.asc' datafile = open(txt_file,"w") datafile.write('ncols '+str(ncols)+"\n") datafile.write('nrows '+str(nrows)+"\n") datafile.write('xllcorner '+str(xllcorner)+"\n") datafile.write('yllcorner '+str(yllcorner)+"\n") datafile.write('cellsize '+str(cellsize)+"\n") datafile.write('NODATA_value '+str(NODATA_value)+"\n") x_ex = num.linspace(xllcorner, xllcorner+(ncols-1)*cellsize, ncols) y_ex = num.linspace(yllcorner, yllcorner+(nrows-1)*cellsize, nrows) points = axes2points(x_ex, y_ex) #print points #print x.shape, x #print y.shape, y datavalues = linear_function(points) #print datavalues datavalues = datavalues.reshape(nrows,ncols) #print datavalues #print datavalues.shape for row in datavalues: #print row datafile.write(" ".join(str(elem) for elem in row) + "\n") datafile.close() #print quantity.vertex_values #print quantity.centroid_values x,y,Z = grd2array(txt_file) #print x #print y #print Z answer = [[ 0., 3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33.], [ 1., 4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34.], [ 2., 5., 8., 11., 14., 17., 20., 23., 26., 29., 32., 35.], [ 3., 6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36.], [ 4., 7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37.], [ 5., 8., 11., 14., 17., 20., 23., 26., 29., 32., 35., 38.], [ 6., 9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39.], [ 7., 10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40.], [ 8., 11., 14., 17., 20., 23., 26., 29., 32., 35., 38., 41.], [ 9., 12., 15., 18., 21., 24., 27., 30., 33., 36., 39., 42.], [ 10., 13., 16., 19., 22., 25., 28., 31., 34., 37., 40., 43.]] #print quantity.vertex_values assert num.allclose(Z, answer) assert num.allclose(x,x_ex) assert num.allclose(y,y_ex) os.remove(root + '.asc')
def test_grd2array_2(self): """ Format of asc file ncols 11 nrows 12 xllcorner 240000 yllcorner 7620000 cellsize 6000 NODATA_value -9999 """ x0 = 240000.0 y0 = 7620000.0 ncols = 11 # Nx nrows = 12 # Ny xllcorner = x0 yllcorner = y0 cellsize = 6000.0 NODATA_value = -9999 #Create .asc file #txt_file = tempfile.mktemp(".asc")from anuga.config import netcdf_float root = 'test_asc_2' txt_file = root+'.asc' datafile = open(txt_file,"w") datafile.write('ncols '+str(ncols)+"\n") datafile.write('nrows '+str(nrows)+"\n") datafile.write('xllcorner '+str(xllcorner)+"\n") datafile.write('yllcorner '+str(yllcorner)+"\n") datafile.write('cellsize '+str(cellsize)+"\n") datafile.write('NODATA_value '+str(NODATA_value)+"\n") x_ex = num.linspace(xllcorner, xllcorner+(ncols-1)*cellsize, ncols) y_ex = num.linspace(yllcorner, yllcorner+(nrows-1)*cellsize, nrows) points = axes2points(x_ex, y_ex) #print points #print x_ex.shape, x_ex #print y_ex.shape, y_ex datavalues = linear_function(points) #print datavalues datavalues = datavalues.reshape(nrows,ncols) #print datavalues #print datavalues.shape for row in datavalues: #print row datafile.write(" ".join(str(elem) for elem in row) + "\n") datafile.close() #print quantity.vertex_values #print quantity.centroid_values x,y,Z = grd2array(txt_file) #print x #print y #print Z answer = [[ 23100000., 23118000., 23136000., 23154000., 23172000., 23190000., 23208000., 23226000., 23244000., 23262000., 23280000., 23298000.], [ 23106000., 23124000., 23142000., 23160000., 23178000., 23196000., 23214000., 23232000., 23250000., 23268000., 23286000., 23304000.], [ 23112000., 23130000., 23148000., 23166000., 23184000., 23202000., 23220000., 23238000., 23256000., 23274000., 23292000., 23310000.], [ 23118000., 23136000., 23154000., 23172000., 23190000., 23208000., 23226000., 23244000., 23262000., 23280000., 23298000., 23316000.], [ 23124000., 23142000., 23160000., 23178000., 23196000., 23214000., 23232000., 23250000., 23268000., 23286000., 23304000., 23322000.], [ 23130000., 23148000., 23166000., 23184000., 23202000., 23220000., 23238000., 23256000., 23274000., 23292000., 23310000., 23328000.], [ 23136000., 23154000., 23172000., 23190000., 23208000., 23226000., 23244000., 23262000., 23280000., 23298000., 23316000., 23334000.], [ 23142000., 23160000., 23178000., 23196000., 23214000., 23232000., 23250000., 23268000., 23286000., 23304000., 23322000., 23340000.], [ 23148000., 23166000., 23184000., 23202000., 23220000., 23238000., 23256000., 23274000., 23292000., 23310000., 23328000., 23346000.], [ 23154000., 23172000., 23190000., 23208000., 23226000., 23244000., 23262000., 23280000., 23298000., 23316000., 23334000., 23352000.], [ 23160000., 23178000., 23196000., 23214000., 23232000., 23250000., 23268000., 23286000., 23304000., 23322000., 23340000., 23358000.]] #print quantity.vertex_values assert num.allclose(Z, answer) assert num.allclose(x,x_ex) assert num.allclose(y,y_ex) os.remove(root + '.asc')