source: qesdi/data_processing/convertPre.py @ 5419

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/qesdi/data_processing/convertPre.py@5419
Revision 5419, 4.2 KB checked in by dgrant, 10 years ago (diff)

Code following grid - except that I don't think it is a grid

Line 
1import netCDF4
2import sys
3import numpy
4import time
5
6from TMCReader import TMCReader
7from netCDF4 import Dataset
8
9class PrecipetationReader(TMCReader):
10    """A class to read the ascii text from the precipetation data file"""
11
12    def storeLine(self, line):
13        precipDataPt = PrecipetationDataPt(line.split())
14        self.coordTable.insert(precipDataPt, precipDataPt.lat, precipDataPt.lon)
15
16
17
18class PrecipetationDataPt:
19    """Class to store data for a specific point"""
20
21    def __init__(self,preVals):
22        self.lat=float(preVals[0])
23        self.lon=float(preVals[1])
24        self.jan=float(preVals[2])
25        self.feb=float(preVals[3])
26        self.mar=float(preVals[4])
27        self.apr=float(preVals[5])
28        self.may=float(preVals[6])
29        self.jun=float(preVals[7])
30        self.jul=float(preVals[8])
31        self.aug=float(preVals[9])
32        self.sep=float(preVals[10])
33        self.oct=float(preVals[11])
34        self.nov=float(preVals[12])
35        self.dec=float(preVals[13])
36
37class PrecipetationWriter:
38    """Class to read precipetation data"""
39
40    def __init__(self,lookupTable):
41        self.dataTable = lookupTable
42
43    def writeData(self, path):
44        self.rootgrp = Dataset(path, 'w',format='NETCDF4')
45        self.createDimensions()
46        self.createVariables()
47        preVar = self.rootgrp.createVariable('Precipetation','f',('time','lat','lon'))
48        self.setGlobalAttributes()
49        self.setVariableAttributes()
50        self.writeVariables()
51        self.rootgrp.close()
52
53    def createDimensions(self):
54        self.rootgrp.createDimension('time', 12)
55        self.rootgrp.createDimension('nv', 2)
56        self.rootgrp.createDimension('n',self.lookupTable.getLength)
57
58    def createVariables(self):
59        self.timeVar = self.rootgrp.createVariable('time','d', ('time',))
60        self.latVar = self.rootgrp.createVariable('lat', 'f4', ('lat',))
61        self.lonVar = self.rootgrp.createVariable('lon', 'f4', ('lon',))
62        self.boundsVar = self.rootgrp.createVariable('climatology_bounds','d',('time','nv',))
63        self.preVar = self.rootgrp.createVariable('precipetation','f4',('time','lat','lon',))
64
65    def setGlobalAttributes(self):
66        self.rootgrp.history = 'Created ' + time.ctime(time.time())
67
68    def setVariableAttributes(self):
69        self.latVar.units = 'degrees north'
70        self.lonVar.units ='degrees east'
71        self.timeVar.units = 'days since 1961-1-1'
72        self.timeVar.climatology = 'climatology bounds'
73
74    def writeVariables(self):
75        self.latVar[:] = self.dataTable.getLats()
76        self.lonVar[:] = self.dataTable.getLons()
77        self.timeVar[:] = [15,45,75,105,135,165,195,225,255,285,315,345]
78        self.preVar[:] = self.getPrecipetationData()
79
80    def getPrecipetationData(self):
81        for lt in self.latVar:
82            for ln in self.lonVar:
83                print lt + ln
84                precipetationDataPt = self.dataTable.lookup(lt,ln)
85                self.preVar[0][lt][ln] = precipetationDataPt.jan
86                self.preVar[1][lt][ln] = precipetationDataPt.feb
87                self.preVar[2][lt][ln] = precipetationDataPt.mar
88                self.preVar[3][lt][ln] = precipetationDataPt.apr
89                self.preVar[4][lt][ln] = precipetationDataPt.may
90                self.preVar[5][lt][ln] = precipetationDataPt.jun
91                self.preVar[6][lt][ln] = precipetationDataPt.jul
92                self.preVar[7][lt][ln] = precipetationDataPt.aug
93                self.preVar[8][lt][ln] = precipetationDataPt.sep
94                self.preVar[9][lt][ln] = precipetationDataPt.oct
95                self.preVar[10][lt][ln] = precipetationDataPt.nov
96                self.preVar[11][lt][ln] = precipetationDataPt.dec
97
98
99def main():
100    print time.ctime(time.time())
101    precipetationReader = PrecipetationReader()
102    precipetationReader.readData(sys.argv[1])
103    print precipetationReader.getLookupTable().lookup(-59.083,  -26.583).jan
104    print 'lat len ' + str(len(precipetationReader.getLookupTable().getLats()))
105    print 'lon len ' + str(len(precipetationReader.getLookupTable().getLons()))
106    precipetationWriter = PrecipetationWriter(precipetationReader.getLookupTable())
107    precipetationWriter.writeData(sys.argv[2])
108
109if __name__ == "__main__":
110    main()
Note: See TracBrowser for help on using the repository browser.