Changeset 4017


Ignore:
Timestamp:
10/06/08 16:23:57 (11 years ago)
Author:
domlowe
Message:

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

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

Legend:

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

    r3704 r4017  
    3131    except: 
    3232        return None 
    33      
     33 
     34def _preserveAttributes(self): 
     35    '''  attempts to preserve information from the original file if information exist, such as CF attributes, projection info etc 
     36    returns?? tbd'''     
     37    filename = self.value.rangeSet.valueArray.valueComponent.insertedExtract.components.fileList.fileNames.CONTENT.split(' ')[0] 
     38    di = csml.csmllibs.csmldataiface.DataInterface() 
     39    di = di.getUnknownInterfaceType(filename) 
     40    di.openFile(filename) 
     41    preservedData=csml.csmllibs.csmlpreservation.PreservedData(self.name.CONTENT, di) 
     42    return preservedData 
    3443     
    3544def getDomain(self): 
     
    217226    subsettedFeature=csmlWrap.createGridSeriesFeature(value=cvg,parameter=param,featureID=csml.csmllibs.csmlextra.getRandomID(),name=self.name,description=self.description) 
    218227  
     228    #Attempt to preserve any attributes from original data 
     229    preservedData=self._preserveAttributes() 
     230     
     231  
    219232    ### write netcdf using NCWriter class (wraps cdms) ### 
    220233    nc=csml.csmllibs.netCDFWriter.NCwriter(pathToSubsetNetCDF) 
     
    225238    stdname=param.getStandardName() 
    226239    nc.genWriteVar(self.name.CONTENT,ords, times, caltype, axislist, units, stdname, fulldata, fillvalue) 
     240     
     241    #now add the data we preserved earlier: 
     242    for pv in preservedData.projectionVariables: 
     243        for pvaxis in pv.axes: 
     244            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     
    227247    nc.closeFinishedFile() 
    228248    print 'NetCDF file written to %s'%pathToSubsetNetCDF 
  • TI02-CSML/trunk/csml/csmllibs/__init__.py

    r3704 r4017  
    1414import csmlxlink 
    1515import standardnames 
     16import csmlpreservation 
  • TI02-CSML/trunk/csml/csmllibs/csmldataiface.py

    r3891 r4017  
    461461        varAxesList=self.varobj.getAxisIds() 
    462462        return varAxesList 
     463     
     464    def getVariableAttributes(self): 
     465        '''gets names of all attributes belonging to variable' 
     466        returns list''' 
     467        return self.varobj.listattributes() 
    463468 
    464469    def getVariableAttribute(self,attName): 
  • TI02-CSML/trunk/csml/csmllibs/netCDFWriter.py

    r3953 r4017  
    2222             
    2323        # Now create the axis    
     24        if not hasattr(self,'axes'): 
     25            self.axes=[] 
     26        else: 
     27            for item in self.axes: 
     28                if item.id == axisName: 
     29                    return None # it's already there, don't do anything 
     30            
    2431        dataarray=MV.array(data) 
    2532        ax=cdms.createAxis(dataarray) 
    2633        ax.id = axisName 
    27         if not hasattr(self,'axes'): 
    28             self.axes=[] 
    2934        if isLon is not None: 
    3035            ax.designateLongitude() 
     
    3944            setattr(ax, key,kwargs[key]) 
    4045        self.axes.append(ax) 
    41          
    4246     
    4347    def getAxis(self, axID): 
     
    6569            setattr(dataarray, key,kwargs[key]) 
    6670        axisCount=0 
    67        
    6871        #depending on whether time is modelled in the underlying data as a dimension or not the shapes may not match 
    6972        if len(dataarray.shape) < len(axesList):   
     
    8184                        axisCount = axisCount +1       
    8285        #missing_value deprecated but in common use; 
    83         setattr(dataarray, 'missing_value' ,fillvalue) 
    84         setattr(dataarray, '_FillValue' ,fillvalue) #_FillValue does not work in CDMS!! 
     86        if fillvalue: 
     87            setattr(dataarray, 'missing_value' ,fillvalue) 
     88            setattr(dataarray, '_FillValue' ,fillvalue) #_FillValue does not work in CDMS!! 
    8589        if stdname: 
    8690            setattr(dataarray, 'standard_name' ,stdname) 
Note: See TracChangeset for help on using the changeset viewer.