/[escript]/trunk/downunder/test/python/run_datasources.py
ViewVC logotype

Diff of /trunk/downunder/test/python/run_datasources.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 4154 by jfenwick, Tue Jan 22 09:30:23 2013 UTC revision 4362 by caltinay, Tue Apr 16 04:37:13 2013 UTC
# Line 30  from esys.downunder.datasources import * Line 30  from esys.downunder.datasources import *
30  from esys.downunder.domainbuilder import DomainBuilder  from esys.downunder.domainbuilder import DomainBuilder
31    
32  # this is mainly to avoid warning messages  # this is mainly to avoid warning messages
33  logger=logging.getLogger('inv')  logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
 logger.setLevel(logging.INFO)  
 handler=logging.StreamHandler()  
 handler.setLevel(logging.INFO)  
 logger.addHandler(handler)  
34    
35  try:  try:
36      TEST_DATA_ROOT=os.environ['DOWNUNDER_TEST_DATA_ROOT']      TEST_DATA_ROOT=os.environ['DOWNUNDER_TEST_DATA_ROOT']
# Line 51  ERS_DATA = os.path.join(TEST_DATA_ROOT, Line 47  ERS_DATA = os.path.join(TEST_DATA_ROOT,
47  ERS_REF = os.path.join(TEST_DATA_ROOT, 'ermapper_test.csv')  ERS_REF = os.path.join(TEST_DATA_ROOT, 'ermapper_test.csv')
48  ERS_NULL = -99999 * 1e-6  ERS_NULL = -99999 * 1e-6
49  ERS_SIZE = [20,15]  ERS_SIZE = [20,15]
50  ERS_ORIGIN = [309097.0, 6319002.0]  ERS_ORIGIN = [309241.0, 6318655.0]
51  NC_DATA = os.path.join(TEST_DATA_ROOT, 'netcdf_test.nc')  NC_DATA = os.path.join(TEST_DATA_ROOT, 'netcdf_test.nc')
52  NC_REF = os.path.join(TEST_DATA_ROOT, 'netcdf_test.csv')  NC_REF = os.path.join(TEST_DATA_ROOT, 'netcdf_test.csv')
53  NC_NULL = 0.  NC_NULL = 0.
54  NC_SIZE = [20,15]  NC_SIZE = [20,15]
55  NC_ORIGIN = [403320.91466610413, 6414860.942530109]  NC_ORIGIN = [403320.91466610413, 6414860.942530109]
56    NUMPY_NULL = -123.4
57  VMIN=-10000.  VMIN=-10000.
58  VMAX=10000  VMAX=10000.
59  NE_V=15  NE_V=15
60  ALT=0.  ALT=0.
61  PAD_X=3  PAD_X=3
62  PAD_Y=2  PAD_Y=2
63    
64    class TestNumpyData(unittest.TestCase):
65        def test_numpy_argument_check(self):
66            # invalid data type
67            self.assertRaises(ValueError, NumpyData, '_mydatatype_', [1,2])
68            # invalid shape of data
69            self.assertRaises(ValueError, NumpyData, DataSource.GRAVITY, 42)
70            # invalid shape of data
71            self.assertRaises(ValueError, NumpyData, DataSource.GRAVITY, np.zeros((2,2,2,2)))
72            # invalid shape of error
73            self.assertRaises(ValueError, NumpyData, DataSource.GRAVITY, [1,2], [1,2,3])
74            # invalid shape of length
75            self.assertRaises(ValueError, NumpyData, DataSource.GRAVITY, [1,2], [1,2], [2,3,2])
76    
77        def test_numpy_data_1d(self):
78            DIM=1
79            testdata = np.arange(20)
80            error = 1.*np.ones(testdata.shape)
81            source = NumpyData(DataSource.GRAVITY, testdata, null_value=NUMPY_NULL)
82            X0,NP,DX=source.getDataExtents()
83            for i in range(DIM):
84                self.assertAlmostEqual(X0[i], 0., msg="Data origin wrong")
85                self.assertEqual(NP[i], testdata.shape[DIM-i-1], msg="Wrong number of data points")
86                self.assertAlmostEqual(DX[i], 1000./testdata.shape[DIM-i-1], msg="Wrong cell size")
87    
88            domainbuilder=DomainBuilder(dim=2)
89            domainbuilder.addSource(source)
90            domainbuilder.setVerticalExtents(depth=-VMIN, air_layer=VMAX, num_cells=NE_V)
91            domainbuilder.setElementPadding(PAD_X)
92            dom=domainbuilder.getDomain()
93            g,s=domainbuilder.getGravitySurveys()[0]
94    
95            outfn=os.path.join(WORKDIR, '_npdata1d.csv')
96            saveDataCSV(outfn, g=g, s=s)
97    
98            DV=(VMAX-VMIN)/NE_V
99    
100            # check data
101            nx=NP[0]+2*PAD_X
102            nz=NE_V
103            z_data=int(np.round((ALT-VMIN)/DV)-1)
104    
105            out=np.genfromtxt(outfn, delimiter=',', skip_header=1, dtype=float)
106            # recompute nz since ripley might have adjusted number of elements
107            nz=len(out)/nx
108            g_out=out[:,0].reshape(nz,nx)
109            s_out=out[:,1].reshape(nz,nx)
110            self.assertAlmostEqual(np.abs(
111                g_out[z_data, PAD_X:PAD_X+NP[0]]-testdata).max(),
112                0., msg="Difference in gravity data area")
113    
114            self.assertAlmostEqual(np.abs(
115                s_out[z_data, PAD_X:PAD_X+NP[0]]-error).max(),
116                0., msg="Difference in error data area")
117    
118            # overwrite data -> should only be padding value left
119            g_out[z_data, PAD_X:PAD_X+NP[0]]=NUMPY_NULL
120            self.assertAlmostEqual(np.abs(g_out-NUMPY_NULL).max(), 0.,
121                    msg="Wrong values in padding area")
122    
123        def test_numpy_data_2d(self):
124            DIM=2
125            testdata = np.arange(20*21).reshape(20,21)
126            error = 1.*np.ones(testdata.shape)
127            source = NumpyData(DataSource.GRAVITY, testdata, null_value=NUMPY_NULL)
128            X0,NP,DX=source.getDataExtents()
129            for i in range(DIM):
130                self.assertAlmostEqual(X0[i], 0., msg="Data origin wrong")
131                self.assertEqual(NP[i], testdata.shape[DIM-i-1], msg="Wrong number of data points")
132                self.assertAlmostEqual(DX[i], 1000./testdata.shape[DIM-i-1], msg="Wrong cell size")
133    
134            domainbuilder=DomainBuilder(dim=3)
135            domainbuilder.addSource(source)
136            domainbuilder.setVerticalExtents(depth=-VMIN, air_layer=VMAX, num_cells=NE_V)
137            domainbuilder.setElementPadding(PAD_X, PAD_Y)
138            dom=domainbuilder.getDomain()
139            g,s=domainbuilder.getGravitySurveys()[0]
140    
141            outfn=os.path.join(WORKDIR, '_npdata2d.csv')
142            saveDataCSV(outfn, g=g, s=s)
143    
144            DV=(VMAX-VMIN)/NE_V
145    
146            # check data
147            nx=NP[0]+2*PAD_X
148            ny=NP[1]+2*PAD_Y
149            nz=NE_V
150            z_data=int(np.round((ALT-VMIN)/DV)-1)
151    
152            out=np.genfromtxt(outfn, delimiter=',', skip_header=1, dtype=float)
153            # recompute nz since ripley might have adjusted number of elements
154            nz=len(out)/(nx*ny)
155            g_out=out[:,0].reshape(nz,ny,nx)
156            s_out=out[:,1].reshape(nz,ny,nx)
157            self.assertAlmostEqual(np.abs(
158                g_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]-testdata).max(),
159                0., msg="Difference in gravity data area")
160    
161            self.assertAlmostEqual(np.abs(
162                s_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]-error).max(),
163                0., msg="Difference in error data area")
164    
165            # overwrite data -> should only be padding value left
166            g_out[z_data, PAD_Y:PAD_Y+NP[1], PAD_X:PAD_X+NP[0]]=NUMPY_NULL
167            self.assertAlmostEqual(np.abs(g_out-NUMPY_NULL).max(), 0.,
168                    msg="Wrong values in padding area")
169    
170    
171  class TestErMapperData(unittest.TestCase):  class TestErMapperData(unittest.TestCase):
172      def test_ers_with_padding(self):      def test_ers_with_padding(self):
173          source = ErMapperData(DataSource.GRAVITY, headerfile=ERS_DATA,          source = ErMapperData(DataSource.GRAVITY, headerfile=ERS_DATA,
# Line 177  class TestNetCdfData(unittest.TestCase): Line 281  class TestNetCdfData(unittest.TestCase):
281  if __name__ == "__main__":  if __name__ == "__main__":
282      suite = unittest.TestSuite()      suite = unittest.TestSuite()
283      if getMPISizeWorld()==1:      if getMPISizeWorld()==1:
284          suite.addTest(unittest.makeSuite(TestErMapperData))          suite.addTest(unittest.makeSuite(TestNumpyData))
285            try:
286              import pyproj
287              haveproj=True
288            except ImportError:
289              haveproj=False
290            if haveproj:
291              suite.addTest(unittest.makeSuite(TestErMapperData))
292          if 'NetCdfData' in dir():          if 'NetCdfData' in dir():
293              suite.addTest(unittest.makeSuite(TestNetCdfData))              suite.addTest(unittest.makeSuite(TestNetCdfData))
294          else:          else:

Legend:
Removed from v.4154  
changed lines
  Added in v.4362

  ViewVC Help
Powered by ViewVC 1.1.26