Ignore:
Timestamp:
17/04/14 09:49:52 (6 years ago)
Author:
mjuckes
Message:

Added support for ScientificPython?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • CCCC/trunk/file_utils.py

    r147 r148  
    55# Third party imports 
    66 
    7 #### netcdf --- currently only support for cmds2 -- re-arranged to facilitate support for alternative modules 
     7#### netcdf --- currently support cdms2, python-netCDF4 and Scientific 
    88 
    99l = pkgutil.iter_modules() 
    1010ll = map( lambda x: x[1], l ) 
    1111 
    12 supportedNetcdf = ['cdms2'] 
    13 supportedNetcdf = ['cdms2','netCDF4'] 
     12supportedNetcdf = ['cdms2','netCDF4','Scientific'] 
    1413 
    1514installedSupportedNetcdf = [] 
     
    3332  ncLib = None 
    3433 
     34if ncLib == 'Scientific': 
     35  from Scientific.IO import NetCDF as ncdf 
     36 
    3537## end of netcdf import. 
    36  
    3738 
    3839class fileMetadata: 
     
    5960      return 
    6061    if ncLib == 'cdms2': 
    61       self.loadNcCdms(fpath) 
     62      self.loadNc__Cdms(fpath) 
    6263    elif ncLib == 'netCDF4': 
    63       self.loadNcNetcdf4(fpath) 
     64      self.loadNc__Netcdf4(fpath) 
     65    elif ncLib == 'Scientific': 
     66      self.loadNc__Scientific(fpath) 
    6467    else: 
    6568      raise 'No supported netcdf module assigned' 
    6669 
    67   def loadNcCdms(self,fpath): 
     70  def loadNc__Cdms(self,fpath): 
    6871    self.nc = cdms2.open( fpath ) 
    6972    for k in self.nc.attributes.keys(): 
     
    9699    self.nc.close() 
    97100 
    98   def loadNcNetcdf4(self,fpath): 
     101### 
     102### attributes in .__dict__ dictionary 
     103### variables in .variables dicttionary 
     104### dimension lengths in .dimensions 
     105### <variable>.getValue() returns an numpy.ndarray 
     106### data type in <variable>.getValue().dtype 
     107### for scalar variables, <variable>.getValue().tolist() returns a scalar. 
     108### 
     109  def loadNc__Scientific(self,fpath): 
     110    self.nc = ncdf.NetCDFFile( fpath, 'r' ) 
     111    for k in self.nc.__dict__.keys(): 
     112      self.ga[k] = self.nc.__dict__[k] 
     113      ##if len( self.ga[k] ) == 1: 
     114        ##self.ga[k] = self.ga[k][0] 
     115    for v in self.nc.variables.keys(): 
     116      if v not in self.nc.dimensions.keys(): 
     117        self.va[v] = {} 
     118        for k in self.nc.variables[v].__dict__.keys(): 
     119          self.va[v][k] = self.nc.variables[v].__dict__[k] 
     120        self.va[v]['_type'] = str( self.nc.variables[v].getValue().dtype ) 
     121        if v in ['plev','plev_bnds','height']: 
     122        ### Note: returns a scalar if data has a scalar value. 
     123          x = self.nc.variables[v].getValue().tolist() 
     124          if type(x) != type([]): 
     125            x = [x] 
     126          self.va[v]['_data'] = x 
     127 
     128    for v in self.nc.dimensions.keys(): 
     129      self.da[v] = {} 
     130      if v in self.nc.variables.keys(): 
     131        for k in self.nc.variables[v].__dict__.keys(): 
     132          self.da[v][k] = self.nc.variables[v].__dict__[k] 
     133        self.da[v]['_type'] = str( self.nc.variables[v].getValue().dtype ) 
     134        self.da[v]['_data'] = self.nc.variables[v].getValue().tolist() 
     135      else: 
     136        self.da[v]['_type'] = 'index (no data variable)' 
     137       
     138    self.nc.close() 
     139 
     140  def loadNc__Netcdf4(self,fpath): 
    99141    self.nc = netCDF4.Dataset(fpath, 'r') 
    100142    for k in self.nc.ncattrs(): 
Note: See TracChangeset for help on using the changeset viewer.