source: trunk/anuga_core/source/anuga/file_conversion/test_csv2sts.py @ 7861

Last change on this file since 7861 was 7861, checked in by hudson, 12 years ago

Fixed AABB parameter passing bug.

File size: 2.8 KB
Line 
1#external modules
2import os
3import sys
4import unittest
5import numpy as num
6from Scientific.IO.NetCDF import NetCDFFile
7
8# ANUGA modules
9from anuga.config import netcdf_float32, netcdf_float64
10from anuga.file.csv_file import load_csv_as_dict
11
12# Local modules
13from csv2sts import csv2sts
14
15# some test file we want to generate
16testfile_csv = 'small___.csv'
17sts_out = 'sts_out.sts'
18
19lat = 10
20lon = 20
21
22
23
24class Test_csv2sts(unittest.TestCase):
25    """
26        Test csv to NetCDFFile conversion functionality.
27    """
28    def setUp(self):
29        self.verbose = True
30        fid = open(testfile_csv, 'w')
31        fid.write("""time stage
320 4
331 150.66667
342 150.83334
353 151.
364 151.16667
375 -34.
386 -34.16667
397 -34.33333
408 -34.5
419 -1.
4210 -5.
4311 -9.
4412 -13.
45""")
46        fid.close()
47                 
48    def tearDown(self):
49        pass     
50 #       os.remove(testfile_csv)
51
52    def test_missing_input_file(self):
53        """
54        Test that a missing csv file raises the correct exception.
55        """
56        got_except = False
57       
58        try:
59            csv2sts('somename_not_here.csv', sts_out, 10, 20)
60        except IOError, e:
61            got_except = True
62        except:
63            assert False, 'Missing file raised wrong exception.'
64
65        assert got_except is True, 'Missing file did not raise an exception.'
66
67    def test_csv2sts_output(self):
68        """
69        Test that a csv file is correctly rendered to .sts (NetCDF) format.
70        """
71        csv2sts(testfile_csv, sts_out, latitude = lat, longitude = lon)
72        self._check_generated_sts()
73       
74    def test_run_via_commandline(self):
75        """
76        Make sure that the python file functions as a command-line tool.
77        """
78       
79        cmd = 'python ' + sys.path[0] + os.sep +'csv2sts.py --latitude ' 
80        cmd += '%s --lon %s %s %s' % (str(lat), str(lon), testfile_csv, sts_out)
81       
82        os.system(cmd)
83        self._check_generated_sts()
84
85
86    def _check_generated_sts(self):
87        """ check that we can read data out of the file """
88        sts = NetCDFFile(sts_out)
89       
90        data, names = load_csv_as_dict(testfile_csv, delimiter=' ', d_type = num.float64)
91       
92        assert sts.latitude == lat, 'latitude does not match'
93        assert sts.longitude == lon, 'longitude does not match'
94       
95        assert len(sts.variables) == len(data), 'num variables does not match'
96       
97        # make sure data is returned in exactly the expected format
98        for key, values in data.items():
99            assert list(sts.variables[key][:]) == values, \
100                                        'stored data does not match'
101
102        os.remove(sts_out)           
103
104if __name__ == "__main__":
105    suite = unittest.makeSuite(Test_csv2sts,'test')
106    runner = unittest.TextTestRunner()
107    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.