Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    def test_dem2array(self):
        """Test conversion from dem to array
        """

        import time, os


        #Write test asc file
        root = 'dem2arraytest_1'

        filename = root+'.asc'
        fid = open(filename, 'w')
        fid.write("""ncols         11
nrows         12
xllcorner     2000
yllcorner     3000
cellsize      3
NODATA_value  -9999
""")
        #Create linear function
        ref_points = []
        ref_elevation = []
        x0 = 2000
        y = 3000
        cellsize = 3.0
        xvec = x0 + cellsize*num.arange(11)
        yvec = y + cellsize*(num.arange(12))
        z = -1
        for i in range(12):
            y = y - cellsize
            for j in range(11):
                x = x0 + xvec[j]
                z += 1
                ref_points.append ([x,y])
                ref_elevation.append(z)
                fid.write('%f ' %z)
            fid.write('\n')

        fid.close()

        #print 'sending pts', ref_points
        #print 'sending elev', ref_elevation
        
        Z_ex = num.array(ref_elevation,num.float).reshape(12,11)

        #Write prj file with metadata
        metafilename = root+'.prj'
        fid = open(metafilename, 'w')


        fid.write("""Projection UTM
Zone 56
Datum WGS84
Zunits NO
Units METERS
Spheroid WGS84
Xshift 0.0000000000
Yshift 10000000.0000000000
Parameters
""")
        fid.close()

        #Convert to NetCDF dem
        
        asc2dem(filename)


        x,y, Z = dem2array(root+'.dem')
        
#         print Z
#         print Z_ex
#         print x
#         print xvec
#         print y
#         print yvec        
        
        assert num.allclose(Z,Z_ex)
        assert num.allclose(x,xvec)
        assert num.allclose(y,yvec)

               
        #assert num.allclose(x,)

        try:
            os.remove(root + '.dem')
            os.remove(root + '.asc')
            os.remove(root + '.prj')
        except:
            # Expect error on windows
            pass
    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
Exemplo n.º 4
0
    def test_dem2array(self):
        """Test conversion from dem to array
        """

        import time, os


        #Write test asc file
        root = 'dem2arraytest_1'

        filename = root+'.asc'
        fid = open(filename, 'w')
        fid.write("""ncols         11
nrows         12
xllcorner     2000
yllcorner     3000
cellsize      3
NODATA_value  -9999
""")
        #Create linear function
        ref_points = []
        ref_elevation = []
        x0 = 2000
        y = 3000
        cellsize = 3.0
        xvec = x0 + cellsize*num.arange(11)
        yvec = y + cellsize*(num.arange(12))
        z = -1
        for i in range(12):
            y = y - cellsize
            for j in range(11):
                x = x0 + xvec[j]
                z += 1
                ref_points.append ([x,y])
                ref_elevation.append(z)
                fid.write('%f ' %z)
            fid.write('\n')

        fid.close()

        #print 'sending pts', ref_points
        #print 'sending elev', ref_elevation
        
        Z_ex = num.array(ref_elevation,num.float).reshape(12,11)
	Z_ex = num.fliplr(Z_ex.T)
        
	#Write prj file with metadata
        metafilename = root+'.prj'
        fid = open(metafilename, 'w')


        fid.write("""Projection UTM
Zone 56
Datum WGS84
Zunits NO
Units METERS
Spheroid WGS84
Xshift 0.0000000000
Yshift 10000000.0000000000
Parameters
""")
        fid.close()

        #Convert to NetCDF dem
        
        asc2dem(filename)


        x,y, Z = dem2array(root+'.dem')
        
#         print x
#         print xvec
#         print y
#         print yvec        
        assert num.allclose(Z,Z_ex)
        assert num.allclose(x,xvec)
        assert num.allclose(y,yvec)

               
        #assert num.allclose(x,)

        try:
            os.remove(root + '.dem')
            os.remove(root + '.asc')
            os.remove(root + '.prj')
        except:
            # Expect error on windows
            pass