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

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

added code to preserve CF projection variables and subset them accordingly

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, axisattribs, data=None):
8        self.name=name
9        self.attributes=attributes
10        self.axes=axes
11        self.axesattribs=axisattribs
12        self.data=data
13       
14    def slicevar(self, sliceDefinition=None):
15        ''' slice the data of the preserved variable'''
16        self.data=self.data[sliceDefinition] #??
17
18     
19class PreservedData(object):
20    ''' Preserves the state of original data, eg attributes, grid mappings (CF) etc. There is scope to extend this class '''
21    def __init__(self, variablename, datainterface):
22        self.di = datainterface
23        self.varname=variablename
24        self.keyVariable=self._buildKeyVariable()
25        self.projectionVariables=self._buildProjectionVariables()
26     
27    def _buildAttributeDict(self, attribs):     
28        attribDictionary = {} 
29        for att in attribs:
30            attribDictionary[att] = self.di.getVariableAttribute(att)
31        return attribDictionary
32     
33    def _buildAxisDict(self, axes):
34         axisDictionary={}
35         for ax in axes:
36             self.di.setAxis(ax)
37             axisDictionary[ax]=self.di.getDataForAxis()
38         return axisDictionary
39         
40    def _buildAxisAttributes(self, axes):
41         '''  Builds a dictionary containing dictionaries of key values pairs for the attibutes of each axis..'''
42         axisattribs={}
43         for ax in axes:
44             self.di.setAxis(ax)
45             attvalues=self.di.getAxisAttributes()
46             attdict={}
47             for a in attvalues:
48                 attdict[a]=self.di.getAxisAttribute(a)
49             axisattribs[ax]=attdict             
50         return axisattribs
51         
52    def _buildKeyVariable(self):
53        self.di.setVariable(self.varname)
54        attribs = self.di.getVariableAttributes()
55        attribDictionary = self._buildAttributeDict(attribs)
56        axes=self.di.getVariableAxes()   
57        axesDict= self._buildAxisDict(axes)
58        axisattribs=self._buildAxisAttributes(axes)
59        keyVar=PreservedVariable(self.varname, attribDictionary, axesDict, axisattribs)
60        return keyVar
61     
62    def _buildProjectionVariables(self):
63        '''support for preservation of variables referenced via the CF grid_mapping attribute'(if they are there, otherwise just returns an empty list)'''
64        projectionVariables=[]
65        if 'grid_mapping' in self.keyVariable.attributes:
66            #then look for coordinates attributes, and from this determine which variables should be preserved.
67            projvars=self.keyVariable.attributes['coordinates'].split(' ')  #TODO , put this section in a try except
68            projvars.append(self.keyVariable.attributes['grid_mapping'])
69            for var in projvars:
70                self.di.setVariable(var)
71                atts=self._buildAttributeDict(self.di.getVariableAttributes())
72                data=self.di.getDataForVar()
73                axes=self.di.getVariableAxes()   
74                axesDict= self._buildAxisDict(axes)             
75                pv=PreservedVariable(var, atts, axesDict, {}, data)
76                projectionVariables.append(pv)
77        return projectionVariables
78       
79       
80       
81       
82def getDomainSlice(subsettedDomain, originalDomain):
83    ''' Utility function to convert the subsettedDomain to a set of slice tuples based on its position within the original domain. This information is needed to retrospectively subset the additional projection variables'''
84    sliceinfo={}   
85    try:
86        for key in subsettedDomain:
87            firstvalue=eval(subsettedDomain[key].split(',')[0])
88            lastvalue=eval(subsettedDomain[key].split(',')[-1:][0])
89            vals= originalDomain[key].tolist()
90            sliceinfo[key]=(vals.index(firstvalue), vals.index(lastvalue))
91    except:
92            pass # skip key
93    return sliceinfo
94       
95       
96       
Note: See TracBrowser for help on using the repository browser.