Changeset 148


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

Added support for ScientificPython?

Location:
CCCC/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • CCCC/trunk/README.txt

    r147 r148  
    2323  --aMap                # Read in some attribute mappings and run tests with virtual substitutions, see also map2nco.py 
    2424 
     25After running: 
     26 
     27The log file directory may contain hundreds of files with reports of errors. To get a summary, run: 
     28 
     29python summary.py <log file directory> 
     30 
     31This will produce a listing of errors, the number of times they occur and up to two of the files which contain the error. It is hoped that inspection of one or 2 files will provide enough information to trace the problems which lead to the error reports. 
    2532 
    2633Called from python: 
     
    4350------------ 
    4451 
    45 The library can uses the cdms2 module or the netCDF4 module to read NetCDF files. 
    46 By default, it will use the cdms2 module if available. Support for the netCDF4 module has been added recently. 
     52The library can uses the cdms2, python-netCDF4 or Scientific module to read NetCDF files. 
     53By default, it will use the cdms2 module if available. Support for the netCDF4 and Scientific modules has been added recently. 
     54To change the default, change the order in which modules are listed in the "supportedNetcdf" list in file_utils.py 
     55 
     56Is available as part of the cdat-lite package (http://proj.badc.rl.ac.uk/cedaservices/wiki/CdatLite ). 
     57For python-netCDF4, see http://code.google.com/p/netcdf4-python/. 
     58For Scientific see http://dirac.cnrs-orleans.fr/plone/software/scientificpython/  . Note that search engines confuse "ScientificPython" with "SciPy". The SciPy package also contains a netcdf API, but when tested in April 2014 this could not read data from NetCDF 4 files, and so is not supported here. 
    4759 
    4860OUTPUT 
  • 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.