source: trunk/anuga_core/source/anuga/file_conversion/csv2sts.py @ 7853

Last change on this file since 7853 was 7853, checked in by hudson, 14 years ago

Added csv2sts tool.

File size: 4.3 KB
Line 
1#!/usr/bin/env python
2
3"""
4    Module to convert a .csv file to an .sts file.
5   
6    Use this module to convert an arbitrary csv file to an sts file.
7    The csv file must have a header containing the column names. This will
8    be converted to a NetCDF .sts file containing the same data, with the
9    addition of optional latitude and longitude values.
10
11    For example, the following .csv file:
12   
13        time stage
14        0 4
15        1 150.66667
16        2 150.83334
17        3 151.
18        4 151.16667
19        5 -34.
20        6 -34.16667
21        7 -34.33333
22        8 -34.5
23        9 -1.
24        10 -5.
25        11 -9.
26        12 -13.
27   
28    Using this command:
29        python csv2sts --lat 14 --lon 56 infile.csv foo.sts
30    Will be converted to the following .sts file:
31
32        netcdf foo {
33        dimensions:
34            number_of_timesteps = 13 ;
35        variables:
36            double stage(number_of_timesteps) ;
37            double time(number_of_timesteps) ;
38
39        // global attributes:
40                :latitude = 14. ;
41                :longitude = 56. ;
42        data:
43
44         stage = 4, 150.66667, 150.83334, 151, 151.16667, -34, -34.16667, -34.33333,
45            -34.5, -1, -5, -9, -13 ;
46
47         time = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ;
48        }
49   
50"""
51
52import csv
53import os
54import sys
55import getopt
56from anuga.utilities import log
57import numpy as num
58from Scientific.IO.NetCDF import NetCDFFile
59from anuga.file.csv_file import load_csv_as_dict
60from anuga.config import netcdf_mode_w, netcdf_float
61
62
63def csv2sts(infile, outfile, latitude = None, longitude = None,
64                    verbose = False):
65    """
66        Take a csv file and convert it to an sts file.
67       
68        May be used for timeseries, or any other data.
69    """
70       
71    timeseries_data, col_names = load_csv_as_dict(infile, delimiter=' ')
72   
73    if not col_names:
74        raise IOError('csv2sts: file %s is empty or unreadable.' % infile)
75   
76    if verbose:
77        log.critical('csv2sts input data:')
78        for col in col_names:
79            log.critical('column ' + col + ':')
80            log.critical(timeseries_data[col])       
81
82    data_len = len(timeseries_data.values()[0])
83    if verbose:
84        log.critical('   data length = %d.' % data_len)
85   
86    fid = NetCDFFile(outfile, netcdf_mode_w)
87
88    fid.createDimension('number_of_timesteps', data_len)
89
90    if latitude:
91        fid.latitude = latitude
92       
93    if longitude:
94        fid.longitude = longitude
95   
96    for col in col_names:
97        fid.createVariable(col, netcdf_float, ('number_of_timesteps',))
98       
99        fid.variables[col].assignValue(timeseries_data[col])
100
101    fid.close()
102
103                 
104
105######
106# Script is being run from command line.
107#
108
109def usage():
110    print 'csv2sts - convert a csv file to an sts file.'
111    print 'Usage: csv2sts [-hv] [--help] [--verbose]',
112    print '[-x --lat --latitude <degrees>]',
113    print '[-y --lon --longitude <degrees>] <in.csv> <out.sts>'
114    print 'eg:'
115    print 'python csv2sts.py -v --lat 10 --lon 20 infile.csv sts_out.sts'
116    print
117
118def main(argv):                         
119    """ Script is being run from the command line. """
120    lat = None
121    lon = None
122    verbose = False
123   
124    try:                               
125        long_parms = ["help", "verbose", \
126                        "lat=", "lon=", "latitude=", "longitude="]
127        opts, args = getopt.getopt(argv, "hvx:y:", long_parms)
128    except getopt.GetoptError:   
129        usage()     
130        sys.exit(2)
131    for opt, arg in opts:   
132        if opt in ("-h", "--help"):
133            usage()                     
134            sys.exit()         
135        elif opt in ("-x", "--lat", "--latitude"):
136            lat = float(arg) 
137        elif opt in ("-y", "--lon", "--longitude"):
138            lon = float(arg)
139        if opt in ("-v", "--verbose"):
140            verbose = True
141                           
142    if len(args) != 2:
143        usage()     
144        sys.exit(2)       
145   
146    infile = args[0]   
147    outfile = args[1]
148   
149    if verbose:
150        msg = 'csv2sts: converting %s to %s' % (infile, outfile)
151        if lat and lon:
152            msg += ' with lat = %d, lon = %d...' % (lat, lon)
153        print msg
154    csv2sts(infile, outfile, lat, lon)
155   
156    if verbose:
157        print 'done!'
158
159
160
161   
162if __name__ == "__main__":
163    """ Entry point if run from command line """
164    main(sys.argv[1:])       
Note: See TracBrowser for help on using the repository browser.