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

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

changing code to use new style python classes

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        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        for key in kwargs:
27            setattr(ax, key,kwargs[key])
28        self.axes.append(ax)
29        pass
30   
31    def addVariable(self,data, variableName,axesList,  **kwargs):
32        #creates a new variable containing data with named attributes from **kwargs
33        dataarray=MV.array(data)
34        dataarray.id=variableName
35        dataarray.name=variableName
36        for key in kwargs:
37            setattr(dataarray, key,kwargs[key])
38        axisCount=0
39        if hasattr(self, 'axes'):
40            for axis in axesList:
41                for ax in self.axes:
42                    if ax.id == axis:
43                        dataarray.setAxis(axisCount,ax)
44                        axisCount = axisCount +1
45        self.nc.write(dataarray)
46
47   
48    def closeFinishedFile(self):
49        #returns finished (hopefully) NetCDF file
50        self.nc.close()
Note: See TracBrowser for help on using the repository browser.