Changeset 4019


Ignore:
Timestamp:
11/06/08 16:48:42 (11 years ago)
Author:
domlowe
Message:

more changes to allow preservation of netcdf attributes. Not complete, but not broken

Location:
TI02-CSML/trunk/csml
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/csml/API/ops_GridSeriesFeature.py

    r4017 r4019  
    237237        axislist.append(self.gridnames[a]) 
    238238    stdname=param.getStandardName() 
    239     nc.genWriteVar(self.name.CONTENT,ords, times, caltype, axislist, units, stdname, fulldata, fillvalue) 
    240      
    241     #now add the data we preserved earlier: 
     239     
     240    #get preserved attributes for the variable and assoicated axes 
     241    preservedAttributes=preservedData.keyVariable.attributes 
     242    axisattdict=preservedData.keyVariable.axesattribs 
     243    #remove attributes that will clash with other values already defined 
     244    for kw in ['units', 'fillvalue']: 
     245        if kw in preservedAttributes: 
     246            del preservedAttributes[kw] 
     247        for item in axisattdict: 
     248            if kw in axisattdict[item]: 
     249                del axisattdict[item][kw] 
     250     
     251    nc.genWriteVar(self.name.CONTENT,ords, times, caltype, axislist, units, stdname, fulldata, fillvalue,axisattribs=axisattdict, **preservedAttributes) 
     252     
     253    #now add any other data we preserved earlier, e.g. projection variables: 
    242254    for pv in preservedData.projectionVariables: 
    243255        for pvaxis in pv.axes: 
    244256            nc.addAxis(pvaxis,pv.axes[pvaxis], pv.attributes) #of form: nc.addAxis(axisname, data)  
    245         nc.addVariable(pv.data, pv.name, pv.axes.keys(), -9999) #TODO get fill value, get missing attributes 
    246      
     257        print pv.data 
     258        #TODO slice the extra variables 
     259        #nc.addVariable(pv.data, pv.name, pv.axes.keys(),**pv.attributes)    
    247260    nc.closeFinishedFile() 
    248261    print 'NetCDF file written to %s'%pathToSubsetNetCDF 
  • TI02-CSML/trunk/csml/csmllibs/csmldataiface.py

    r4017 r4019  
    443443        return attValue 
    444444     
     445    def getAxisAttributes(self): 
     446        ''' get all attribute names as list ''' 
     447        return self.axisobj.attributes.keys() 
     448         
    445449    def getTimeUnits(self): 
    446450        ''' get time units - only works if set axis is time...''' 
  • TI02-CSML/trunk/csml/csmllibs/csmlpreservation.py

    r4018 r4019  
    55class PreservedVariable(object): 
    66    ''' a representation of a variable object from the original data file''' 
    7     def __init__(self, name, attributes, axes, data=None): 
     7    def __init__(self, name, attributes, axes, axisattribs, data=None): 
    88        self.name=name 
    99        self.attributes=attributes 
    10         self.axes=axes #TODO: axes themselves could be PreservedVariables? 
     10        self.axes=axes 
     11        self.axesattribs=axisattribs  
    1112        self.data=data 
    1213         
    13          
    14     def createCDMSvar(self, sliceDefinition=None): 
    15         ''' create a cdms var representing this variable (or a slice of it)''' 
    16         return None 
     14    def slicevar(self, sliceDefinition=None): 
     15        ''' slice the data of the preserved variable''' 
     16        self.data=self.data[sliceDefinition] #?? 
     17 
    1718      
    1819class PreservedData(object): 
     
    3738         return axisDictionary 
    3839          
     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          
    3952    def _buildKeyVariable(self): 
    4053        self.di.setVariable(self.varname) 
     
    4356        axes=self.di.getVariableAxes()     
    4457        axesDict= self._buildAxisDict(axes) 
    45         keyVar=PreservedVariable(self.varname, attribDictionary, axesDict) 
     58        axisattribs=self._buildAxisAttributes(axes) 
     59        keyVar=PreservedVariable(self.varname, attribDictionary, axesDict, axisattribs) 
    4660        return keyVar 
    4761       
    4862    def _buildProjectionVariables(self): 
    49         '''support for preservation of variables referenced via the CF grid_mapping attribute''' 
     63        '''support for preservation of variables referenced via the CF grid_mapping attribute'(if they are there, otherwise just returns an empty list)''' 
    5064        projectionVariables=[] 
    5165        if 'grid_mapping' in self.keyVariable.attributes: 
     
    5973                axes=self.di.getVariableAxes()     
    6074                axesDict= self._buildAxisDict(axes)               
    61                 pv=PreservedVariable(var, atts, axesDict, data) 
     75                pv=PreservedVariable(var, atts, axesDict, {}, data) 
    6276                projectionVariables.append(pv) 
    6377        return projectionVariables 
  • TI02-CSML/trunk/csml/csmllibs/netCDFWriter.py

    r4017 r4019  
    6161         
    6262         
    63     def addVariable(self,data, variableName,axesList, fillvalue, stdname=None, **kwargs):         
     63    def addVariable(self,data, variableName,axesList, fillvalue=None, stdname=None, **kwargs):         
    6464        #creates a new variable containing data with named attributes from **kwargs        
    6565        dataarray=MV.array(data)       
     
    9191        self.nc.write(dataarray) 
    9292 
    93     def genWriteVar(self,varid, ordinates, times, caltype, axisorder, unitlist, stdname, fulldata, fillvalue, **kwargs): 
    94  
    95         #**kwargs may contain additional axes not contained in a GridCoordinatesTable - e.g latitude, longitude may be stored in other attribute not in the rectified grid.      
     93    def genWriteVar(self,varid, ordinates, times, caltype, axisorder, unitlist, stdname, fulldata, fillvalue, axisattribs={}, **kwargs): 
     94         #**kwargs may contain additional axes not contained in a GridCoordinatesTable - e.g latitude, longitude may be stored in other attribute not in the rectified grid.      
    9695        axesdone=[] 
    9796        floatTimes=[] 
     
    127126                        position=ax[0] 
    128127                axesdone.append(name) 
    129                 self.addAxis(name,vals,isLon=lon,isLat=lat,units=unitlist[position])#to do, units attribute for CF compliance 
     128                attribs={} 
     129                if name in axisattribs: 
     130                    #get the original attributes for this axis. 
     131                    attribs=axisattribs[name] 
     132                self.addAxis(name,vals,isLon=lon,isLat=lat,units=unitlist[position], **attribs)#to do, units attribute for CF compliance 
    130133         
    131134        if kwargs is not None:    
     
    143146                self.addAxis(name,vals,isLon=lon,isLat=lat,units=unitlist[position]) 
    144147                axesdone.append(name)         
    145         self.addVariable(fulldata,varid, axisorder, fillvalue, units=unitlist[-1], stdname=stdname )  
     148        self.addVariable(fulldata,varid, axisorder, fillvalue=fillvalue, units=unitlist[-1], stdname=stdname, **kwargs)  
    146149 
    147150        #for ax in self.axes: #don't think this is needed! 
Note: See TracChangeset for help on using the changeset viewer.