/adei/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/trunk

« back to all changes in this revision

Viewing changes to apps/csv2netcdf/csv2netcdf.py

  • Committer: Suren A. Chilingaryan
  • Date: 2015-02-20 18:27:24 UTC
  • Revision ID: csa@suren.me-20150220182724-ripuszrvwfjgv3d5
Include netcdf exported from ntj

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
"""csv2netcdf
 
3
 
 
4
Usage:
 
5
  csv2netcdf --file FILE [--overwrite] [--group GROUP]
 
6
  csv2netcdf (-h | --help)
 
7
  csv2netcdf --version
 
8
 
 
9
Options:
 
10
  -h --help     Show this screen
 
11
  --version     Show version
 
12
"""
 
13
import sys
 
14
import netCDF4
 
15
from docopt import docopt
 
16
from datetime import datetime, timedelta
 
17
import logging
 
18
 
 
19
DEBUG = True
 
20
VERSION = "0.0.1"
 
21
 
 
22
if DEBUG:
 
23
    logging.basicConfig(filename='/tmp/debug.log',level=logging.DEBUG)
 
24
 
 
25
def main(args):
 
26
 
 
27
    _file = args["FILE"]
 
28
    wr = args["--overwrite"]
 
29
    group_mode = args["--group"]
 
30
    group_name = args["GROUP"]
 
31
 
 
32
    #filename = ".".join([_file.split(".")[0], "nc"])
 
33
    filename = _file
 
34
 
 
35
    if wr:
 
36
        nc = netCDF4.Dataset(filename, 'w')
 
37
    else:
 
38
        nc = netCDF4.Dataset(filename, 'a')
 
39
 
 
40
    if group_mode:
 
41
        _grp = nc.createGroup(group_name)
 
42
    else:
 
43
        _grp = nc
 
44
        
 
45
    time = _grp.createDimension('time', None)
 
46
    lineno = 0
 
47
    cnt = 0
 
48
    tmp = {}
 
49
    for line in sys.stdin:
 
50
        logging.debug(line)
 
51
        a = line.split(",")
 
52
        for i, item in enumerate(a):
 
53
            var = item.strip()
 
54
            if var == "Date/Time":
 
55
                if lineno == 0:
 
56
                    _grp.createVariable("time", 'f8', ('time',))
 
57
                    tmp[i] = "time"
 
58
                else:
 
59
                    value = var
 
60
                    key = tmp[i]
 
61
                    cnt = lineno - 1
 
62
                    _grp.variables[key][cnt] = value
 
63
            else:
 
64
                if lineno == 0:
 
65
                    _grp.createVariable(var, 'f8', ('time',))
 
66
                    tmp[i] = var
 
67
                else:
 
68
                    if i == 0:
 
69
                        dt = datetime.strptime(var, "%d-%b-%y %H:%M:%S.%f")
 
70
                        value = totimestamp(dt)
 
71
                    else:
 
72
                        value = float(var)
 
73
                    key = tmp[i]
 
74
                    cnt = lineno - 1
 
75
                    _grp.variables[key][cnt] = value
 
76
        lineno+= 1
 
77
            
 
78
def totimestamp(dt, epoch=datetime(1970,1,1)):
 
79
    td = dt - epoch
 
80
    # return td.total_seconds()
 
81
    output = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 1e6
 
82
    return "%.6f" % output
 
83
 
 
84
if __name__ == '__main__':
 
85
    arguments = docopt(__doc__, version=VERSION)
 
86
    if DEBUG:
 
87
        logging.debug(arguments)
 
88
    main(arguments)