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

Last change on this file since 7863 was 7863, checked in by hudson, 13 years ago

Fixed Windows test fail.

File size: 2.9 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        """ Setup for all tests. """
30        self.verbose = True
31        fid = open(testfile_csv, 'w')
32        fid.write("""time stage
330 4
341 150.66667
352 150.83334
363 151.
374 151.16667
385 -34.
396 -34.16667
407 -34.33333
418 -34.5
429 -1.
4310 -5.
4411 -9.
4512 -13.
46""")
47        fid.close()
48                 
49    def tearDown(self):
50        """ Cleanup for all tests. """     
51        os.remove(testfile_csv)
52
53    def test_missing_input_file(self):
54        """
55        Test that a missing csv file raises the correct exception.
56        """
57        got_except = False
58       
59        try:
60            csv2sts('somename_not_here.csv', sts_out, 10, 20)
61        except IOError, e:
62            got_except = True
63        except:
64            assert False, 'Missing file raised wrong exception.'
65
66        assert got_except is True, 'Missing file did not raise an exception.'
67
68    def test_csv2sts_output(self):
69        """
70        Test that a csv file is correctly rendered to .sts (NetCDF) format.
71        """
72        csv2sts(testfile_csv, sts_out, latitude = lat, longitude = lon)
73        self._check_generated_sts()
74       
75    def test_run_via_commandline(self):
76        """
77        Make sure that the python file functions as a command-line tool.
78        """
79       
80        cmd = 'python ' + sys.path[0] + os.sep +'csv2sts.py --latitude ' 
81        cmd += '%s --lon %s %s %s' % (str(lat), str(lon), testfile_csv, sts_out)
82       
83        os.system(cmd)
84        self._check_generated_sts()
85
86
87    def _check_generated_sts(self):
88        """ check that we can read data out of the file """
89        sts = NetCDFFile(sts_out)
90       
91        data, names = load_csv_as_dict(testfile_csv, delimiter=' ', d_type = num.float64)
92       
93        assert sts.latitude == lat, 'latitude does not match'
94        assert sts.longitude == lon, 'longitude does not match'
95       
96        assert len(sts.variables) == len(data), 'num variables does not match'
97       
98        # make sure data is returned in exactly the expected format
99        for key, values in data.items():
100            assert list(sts.variables[key][:]) == values, \
101                                        'stored data does not match'
102
103        if not sys.platform == 'win32':
104            # Windows cannot delete the file for some reason.
105            os.remove(sts_out)           
106
107if __name__ == "__main__":
108    suite = unittest.makeSuite(Test_csv2sts,'test')
109    runner = unittest.TextTestRunner()
110    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.