source: qesdi/data_processing/TMCWriter.py @ 5434

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/data_processing/TMCWriter.py@5434
Revision 5434, 2.8 KB checked in by dgrant, 12 years ago (diff)

Can now read in precip data and write functionaly correct netCDF. Still
need to make this conform to CF

Line 
1
2import time
3from netCDF4 import date2num
4from datetime import datetime
5
6class TMCWriter(object):
7    """ Generic Writer class for the TMC data - writes to netcdf"""
8
9    def __init__(self,lookupTable):   
10        self.dataTable = lookupTable
11   
12    def createDimensions(self):
13        self.rootgrp.createDimension('time', 12)
14        self.rootgrp.createDimension('nv', 2)
15        self.rootgrp.createDimension('lat',self.dataTable.getLatSize())
16        self.rootgrp.createDimension('lon',self.dataTable.getLonSize())
17
18    def setGlobalAttributes(self):
19        self.rootgrp.history = 'Created ' + time.ctime(time.time())
20
21    def createVariables(self):
22        self.timeVar = self.rootgrp.createVariable('time','d', ('time',))
23        self.latVar = self.rootgrp.createVariable('lat', 'f4', ('lat',))
24        self.lonVar = self.rootgrp.createVariable('lon', 'f4', ('lon',))
25        self.boundsVar = self.rootgrp.createVariable('climatology_bounds','d',('time','nv',))
26
27    def setVariableAttributes(self):
28        self.latVar.units = 'degrees north'
29        self.lonVar.units ='degrees east'
30        self.timeVar.units = 'days since 1961-1-1'
31        self.timeVar.climatology = 'climatology_bounds'
32        self.timeVar.calendar = 'gregorian'
33
34    def writeVariables(self):
35        self.latVar[:] = self.dataTable.getLats()
36        self.lonVar[:] = self.dataTable.getLons()
37        dates = [datetime(1961,1,16,12),
38                 datetime(1961,2,15),
39                 datetime(1961,3,16,12),
40                 datetime(1961,4,16),
41                 datetime(1961,5,16,12),
42                 datetime(1961,6,16),
43                 datetime(1961,7,16,12),
44                 datetime(1961,8,16,12),
45                 datetime(1961,9,16,),
46                 datetime(1961,10,16,12),
47                 datetime(1961,11,16),
48                 datetime(1961,12,16,12)]                         
49        self.timeVar[:] = date2num(dates,self.timeVar.units,self.timeVar.calendar)
50        dateBounds = [[datetime(1961,1,1),datetime(1990,2,1)],
51                     [datetime(1961,2,1),datetime(1990,3,1)],
52                     [datetime(1961,3,1),datetime(1990,4,1)],
53                     [datetime(1961,4,1),datetime(1990,5,1)],
54                     [datetime(1961,5,1),datetime(1990,6,1)],
55                     [datetime(1961,6,1),datetime(1990,7,1)],
56                     [datetime(1961,7,1),datetime(1990,8,1)],
57                     [datetime(1961,8,1),datetime(1990,9,1)],
58                     [datetime(1961,9,1),datetime(1990,10,1)],
59                     [datetime(1961,10,1),datetime(1990,11,1)],
60                     [datetime(1961,11,1),datetime(1990,12,1)],
61                     [datetime(1961,12,1),datetime(1991,1,1)]]
62        self.boundsVar[:] = [date2num(datepair,self.timeVar.units,self.timeVar.calendar) for datepair in dateBounds]
63           
64
Note: See TracBrowser for help on using the repository browser.