source: TI02-CSML/trunk/csml/csmllibs/csmlpreservation.py @ 4017

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

added code to assist in preservation of CF (and other) attributes. Not completely working atm

Line 
1import csml
2
3''' csmlpreservation.py  - collection of classes used to preserve the content of the original data files - e.g. for preserving CF attributes that don't have a natural place in CSML'''
4
5class PreservedVariable(object):
6    ''' a representation of a variable object from the original data file'''
7    def __init__(self, name, attributes, axes, data=None):
8        self.name=name
9        self.attributes=attributes
10        self.axes=axes #TODO: axes themselves could be PreservedVariables?
11        self.data=data
12       
13       
14    def createCDMSvar(self, sliceDefinition=None):
15        ''' create a cdms var representing this variable (or a slice of it)'''
16        return None
17     
18class PreservedData(object):
19    ''' Preserves the state of original data, eg attributes, grid mappings (CF) etc. There is scope to extend this class '''
20    def __init__(self, variablename, datainterface):
21        self.di = datainterface
22        self.varname=variablename
23        self.keyVariable=self._buildKeyVariable()
24        self.projectionVariables=self._buildProjectionVariables()
25     
26    def _buildAttributeDict(self, attribs):     
27        attribDictionary = {} 
28        for att in attribs:
29            attribDictionary[att] = self.di.getVariableAttribute(att)
30        return attribDictionary
31     
32    def _buildAxisDict(self, axes):
33         axisDictionary={}
34         for ax in axes:
35             self.di.setAxis(ax)
36             axisDictionary[ax]=self.di.getDataForAxis()
37         return axisDictionary
38         
39    def _buildKeyVariable(self):
40        self.di.setVariable(self.varname)
41        attribs = self.di.getVariableAttributes()
42        attribDictionary = self._buildAttributeDict(attribs)
43        axes=self.di.getVariableAxes()   
44        axesDict= self._buildAxisDict(axes)
45        keyVar=PreservedVariable(self.varname, attribDictionary, axesDict)
46        return keyVar
47     
48    def _buildProjectionVariables(self):
49        '''support for preservation of variables referenced via the CF grid_mapping attribute'''
50        if 'grid_mapping' in self.keyVariable.attributes:
51            #then look for coordinates attributes, and from this determine which variables should be preserved.
52            projvars=self.keyVariable.attributes['coordinates'].split(' ')  #TODO , put this section in a try except
53            projvars.append(self.keyVariable.attributes['grid_mapping'])
54            projectionVariables=[]
55            for var in projvars:
56                self.di.setVariable(var)
57                atts=self._buildAttributeDict(self.di.getVariableAttributes())
58                data=self.di.getDataForVar()
59                axes=self.di.getVariableAxes()   
60                axesDict= self._buildAxisDict(axes)             
61                pv=PreservedVariable(var, atts, axesDict, data)
62                projectionVariables.append(pv)
63        return projectionVariables
64       
65       
66       
67       
68       
69       
70       
71       
Note: See TracBrowser for help on using the repository browser.