source: TI02-CSML/trunk/csml/csmllibs/netCDFWriter.py @ 2078

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/csmllibs/netCDFWriter.py@2078
Revision 2078, 1.6 KB checked in by domlowe, 12 years ago (diff)

subsetting working, but not complete

Line 
1import cdms,MV
2import sys
3class NCwriter(object):
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        print axisName
16        dataarray=MV.array(data)
17        ax=cdms.createAxis(dataarray)
18        ax.id = axisName
19        if not hasattr(self,'axes'):
20            self.axes=[]
21        if isLon is not None:
22            ax.designateLongitude()
23        elif isLat is not None:
24            ax.designateLatitude()
25        elif isTime is not None:
26            ax.designateTime()
27        for key in kwargs:
28            setattr(ax, key,kwargs[key])
29        self.axes.append(ax)
30        pass
31   
32    def addVariable(self,data, variableName,axesList,  **kwargs):
33        #creates a new variable containing data with named attributes from **kwargs
34        dataarray=MV.array(data)
35        dataarray.id=variableName
36        dataarray.name=variableName
37        for key in kwargs:
38            setattr(dataarray, key,kwargs[key])
39        axisCount=0
40        if hasattr(self, 'axes'):
41            for axis in axesList:
42                for ax in self.axes:
43                    if ax.id == axis:
44                        dataarray.setAxis(axisCount,ax)
45                        axisCount = axisCount +1
46        self.nc.write(dataarray)
47
48   
49    def closeFinishedFile(self):
50        #returns finished (hopefully) NetCDF file
51        self.nc.close()
Note: See TracBrowser for help on using the repository browser.