source: TI02-CSML/trunk/parser/API/NetCDFWriter.py @ 1420

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/parser/API/NetCDFWriter.py@1420
Revision 1420, 1.5 KB checked in by domlowe, 13 years ago (diff)

Adding NCWriter class to simplify netCDF output (not complete)

Line 
1import cdms,MV
2import sys
3class NCwriter:
4    #This provides a simplified wrapper to CDMS to write a CF compliant NetCDF
5    def __init__(self, fileName):
6        #initiates a new NetCDF file
7        self.nc=cdms.open(fileName,'w')
8   
9    def setGlobalAttributes(self, **kwargs):
10        #sets global attributes
11        pass
12   
13    def addAxis(self,axisName, data,isLon=None,isLat=None,isTime=None,**kwargs):
14        # Now create the axis
15        dataarray=MV.array(data)
16        ax=cdms.createAxis(dataarray)
17        ax.id = axisName
18        if not hasattr(self,'axes'):
19            self.axes=[]
20        if isLon is not None:
21            ax.designateLongitude()
22        elif isLat is not None:
23            ax.designateLatitude()
24        elif isTime is not None:
25            ax.designateTime()
26        self.axes.append(ax)
27        pass
28   
29    def addVariable(self,data, variableName,axesList,  **kwargs):
30        #creates a new variable containing data with named attributes from **kwargs
31        dataarray=MV.array(data)
32        print dataarray.shape
33        dataarray.id=variableName
34        for key in kwargs:
35            setattr(dataarray, key,kwargs[key])
36        axisCount=0
37        if hasattr(self, 'axes'):
38            for axis in axesList:
39                for ax in self.axes:
40                    if ax.id == axis:
41                        dataarray.setAxis(axisCount,ax)
42                        axisCount = axisCount +1
43        self.nc.write(dataarray)
44
45   
46    def closeFinishedFile(self):
47        #returns finished (hopefully) NetCDF file
48        self.nc.close()
Note: See TracBrowser for help on using the repository browser.