Changes between Version 4 and Version 5 of CSMLReadMethods


Ignore:
Timestamp:
24/07/06 15:54:19 (13 years ago)
Author:
domlowe
Comment:

First draft of how to add read methods to CSML done

Legend:

Unmodified
Added
Removed
Modified
  • CSMLReadMethods

    v4 v5  
    88 * DI.getDataForAxis(self) --- this returns the entire set of values for that axis 
    99 * DI.setVariable(self,variableName) --- this 'sets' the variable you want to read (variable: e.g Temperature, !WindSpeed etc..) 
    10  * DI.getDataForVariable(self,) --- this returns the entire set of values for that variable 
     10 * DI.getDataForVar(self,) --- this returns the entire set of values for that variable 
    1111 * DI.getSubsetOfDataForVar(self,**kwargs) --- this returns a subset of values for that variable  
    1212 * DI.closeFile(self) --- closes the open file 
     
    8484 
    8585 
    86 I think perhaps it is best to explain this by showing how the interface differs for cdms/cdunif and NAPPY data interfaces: 
    87  
    8886 
    8987 == Example Data Interfaces == 
     88 
     89I think perhaps it is best to explain the XYZInterface() by showing how the interface differs for cdms/cdunif and NAPPY data interfaces. The details of each API aren't important to understand, rather it is the structure of the !DataInterface that I am trying to illustrate. 
     90 
     91So here are the methods for two different data interfaces, cdfunifInterface() and NappyInterface(). 
    9092First, the openFile method. This is pretty straightforward, we open the file and assign the open file to self.file. 
    9193 
     
    142144 
    143145 
     146Now the 'handle' to the axis is used internally within getDataForAxis. 
     147 
    144148 === CDMS: getDataForAxis === 
    145149{{{ 
    146 #!python 
     150#!python         
     151        def getDataForAxis(self): 
     152                data = self.axisobj.getValue() 
     153        return data 
    147154}}} 
    148155 === NAPPY: getDataForAxis === 
    149156{{{ 
    150157#!python 
    151 }}} 
     158        def getDataForAxis(self): 
     159                #this is a Nappy thing - it needs to call the readData() method if it hasn't already done so   
     160                if self.file.X == None: 
     161                        self.file.readData() 
     162                #if more than one axis you need to specify which one you want (using self.axisstub) 
     163                if type(self.file.X[1])==list: 
     164                        data = self.file.X[self.axisstub] 
     165                else: 
     166                        data =self.file.X 
     167                return data 
     168}}} 
     169 
     170setVariable and getDataForVariable work in a similar way to the axis methods just shown. 
    152171 
    153172 === CDMS: setVariable === 
    154173{{{ 
    155174#!python 
     175    def setVariable(self,varname): 
     176        self.varobj=self.file.variables[varname] 
    156177}}} 
    157178 
     
    159180{{{ 
    160181#!python 
    161 }}} 
    162  
    163  === CDMS: getDataForVariable === 
    164 {{{ 
    165 #!python 
    166 }}} 
    167  
    168  === NAPPY: getDataForVariable === 
    169 {{{ 
    170 #!python 
    171 }}} 
     182        def setVariable(self,varname): 
     183                vlist=self.getListofVariables() 
     184                self.varstub=vlist.index(varname) 
     185}}} 
     186 
     187 === CDMS: getDataForVar === 
     188{{{ 
     189#!python 
     190    def getDataForVar(self): 
     191        data = self.varobj.getValue() 
     192        return data 
     193 
     194}}} 
     195 
     196 === NAPPY: getDataForVar === 
     197{{{ 
     198#!python 
     199        def getDataForVar(self): 
     200                if self.file.V == None: 
     201                        self.file.readData() 
     202                try: 
     203                    if type(self.file.V[1])==list: 
     204                        data = self.file.V[self.varstub] 
     205                    return data 
     206                except: 
     207                    data = self.file.X 
     208                    return data 
     209}}} 
     210 
     211Getting subsets of data is something that may or may not be complicated. CDMS has subsetting built-in so we just call the CDMS methods. 
    172212 
    173213 === CDMS: getSubsetOfDataForVariable === 
    174214{{{ 
    175215#!python 
    176 }}} 
     216    def getSubsetOfDataForVar(self, **kwargs): 
     217        #takes keyword args defining subset eg 
     218        #subset=getSubsetOfDataForVar(latitude=(0.,10.0), longitude=(90, 100.0)) 
     219        sel=cdms.selectors.Selector(**kwargs) 
     220        subset=self.file(self.varobj.name,sel) 
     221        #data = subset.getValue() 
     222        data = subset  #doesn't seem to matter which 
     223        return data 
     224}}} 
     225 
     226Nappy on the other hand doesn't so we need to do the subsetting within the NappyInterface class itself. 
    177227 
    178228 === NAPPY: getSubsetOfDataForVariable === 
    179229{{{ 
    180230#!python 
    181 }}} 
    182  
    183  === CDMS: closeFile === 
    184 {{{ 
    185 #!python 
    186 }}} 
    187  
    188  === NAPPY: closeFile === 
    189 {{{ 
    190 #!python 
    191 }}} 
    192  
    193  
     231    def getSubsetOfDataForVar(self, **kwargs): 
     232        #Hmmm I haven't implemented this yet. 
     233        #Either the subsetting needs to happen in NAPPY (which it doesn't) 
     234        #Or I need to write some code here to handle the subsetting 
     235}}} 
     236 
     237And finally the closeFile method. 
     238Both cdunifInterface(AbstractDI) and NappyInterface(AbstractDI) inherit the generic close file method from AbstractDI(). 
     239 === AbstractDI: closeFile === 
     240{{{ 
     241#!python 
     242        def closeFile(self): 
     243                #closes file 
     244                self.file.close() 
     245}}} 
     246 
     247 
     248There is a bit more to all this in terms of integrating it into the main trunk of the CSML code, but the first stage is to work on these methods for your file format.