Changeset 936


Ignore:
Timestamp:
16/05/06 13:10:10 (13 years ago)
Author:
domlowe
Message:

ioDemo now returns a csml file & netcdf file (works for GridSeries? data only...)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TI02-CSML/trunk/parser/csmlio.py

    r929 r936  
    33 
    44from  Parser import * 
    5 import nappy 
    6 import cdms 
    75import parser_extra 
    8 from xml.dom.minidom import parseString 
    96import elementtree.ElementTree as etree 
     7from Scientific.IO.NetCDF import *  #use this instead of cdms for now for it's simple interface.. 
     8from Numeric import * 
    109 
    1110#import the DataInterface module from the Scanner, assumes it is in a sibling directory to the one containing csmlio.py 
     
    3332        self.domainComplement=None 
    3433        #this is a fix to the  ElementTree namespace problem that namespaces are usually represented as ns0, ns1, ns2 etc. 
    35         etree._namespace_map.update({ 
    36         'http://www.opengis.net/om': 'om',  'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'}) 
     34        etree._namespace_map.update({'http://www.opengis.net/om': 'om',  'http://www.opengis.net/gml': 'gml','http://ndg.nerc.ac.uk/csml' : 'csml', 'http://www.w3.org/1999/xlink':'xlink'}) 
    3735     
    3836    def __setFeature(self,featureID): 
     
    118116        return fileExtractList 
    119117     
    120     def subsetList(self, fileExtractID, valueList, minValue, maxValue): 
    121         #This is a simple list subsetter. Given a List of values (which may have been extracted from a NetCDF/NASAAmes file 
    122         #or may have been taken from inline CSML, it doesn't matter) this function returns a subset based on max and min values. 
    123         #There is inefficiency here as you have to read in the whole  'axis' before subsetting         
    124         # Also It assumes monotonically varying axis (although a non-monotonically varying axis 'might' work depending on it's shape) 
    125         #find position of minValue in list: 
    126 #         self.__setArrayDescriptor(fileExtractID) 
    127 #          
    128         #find position of maxValue in list: 
    129         pass 
    130         #slice list: 
    131          
    132         return subset 
    133          
    134      
     118    def writeCSML(self,var, timeSubset, fulldata,**kwargs): 
     119        #this takes the existing CSML file and reduces it so that it just contains  
     120        #a description of the subsetted data. 
     121        output_file='temp.xml' 
     122        dset = self.dataset 
     123        feat=self.currentFeature # this is the feature we need to 'subset' 
     124        dset.featureCollection.members=[]# clear out all the existing features. 
     125        dset.arrayDescriptors=[] 
     126        if isinstance(feat, GridSeriesFeature): 
     127            strTimes ='' 
     128            for time in self.times: 
     129                strTimes= strTimes + ' ' + time  
     130            feat.domain.domainReference=TimePositionList(timePositions=strTimes) 
     131            strFiles='' 
     132            for file in self.files: 
     133                strFiles= strFiles + ' ' + file 
     134            print dir(feat.rangeSet.aggregatedArray.component) 
     135            for comp in feat.rangeSet.aggregatedArray.component: 
     136                comp.fileName=strFiles 
     137#            sys.exit() 
     138             
     139        dset.featureCollection.members.append(feat) 
     140        csml = dset.toXML()     
     141        #Tidy up and print the CSML document: 
     142        #strCSML= parseString(tostring(csml)).toprettyxml() 
     143        strCSML=parser_extra.PrettyPrint(csml) 
     144        strCSML=parser_extra.removeInlineNS(strCSML) 
     145        print strCSML 
     146         
     147         
     148         
     149         
    135150    def writeNetCDF(self,var, timeSubset, fulldata,**kwargs): 
    136151        #writes a new netcdf file containing a subsetted dataset: 
     
    141156                
    142157        output_file='temp.nc' 
    143         from Scientific.IO.NetCDF import *  #use this for it's simple interface.. 
    144         from Numeric import * 
    145158        ncfile = NetCDFFile ( output_file, 'w' ) 
    146         # create the dimensions 
    147          
    148          
     159        # create the dimensions         
    149160        ncfile.createDimension ( 'time', len(timeSubset)) 
    150161        time_var = ncfile.createVariable ( 'time', Float, ('time',) ) 
     
    175186        #TODO: need to order items in domainComplement to match order in subsetted data 
    176187        #for now this will work 
    177         dc = self.currentFeature.domain.domainComplement 
    178 #         print dc.ordinates[0].definesAxis 
    179 #         print dc.ordinates[1].definesAxis 
    180 #         print dc.ordinates[2].definesAxis 
    181          
    182              
     188        dc = self.currentFeature.domain.domainComplement        
    183189        if len(dc.ordinates)==3: 
    184190            feature_var = ncfile.createVariable (self.currentFeature.id, Float, ('time',dc.ordinates[2].definesAxis,dc.ordinates[1].definesAxis,dc.ordinates[0].definesAxis)) 
    185         
     191        elif len(dc.ordinates)==2: 
     192            feature_var = ncfile.createVariable (self.currentFeature.id, Float, ('time',dc.ordinates[1].definesAxis,dc.ordinates[0].definesAxis)) 
    186193        feature_var[:]=fulldata[:] 
    187              
    188194        ncfile.close() 
    189          
    190195     
    191196    def getDataForExtract(self, fileExtractID): 
     
    259264                domainref = self.getDomainReference(featureID) 
    260265                filelog=[] 
    261                 for time in timeSubset: 
     266                self.times=timeSubset 
     267                self.files=[] 
     268                for time in self.times: 
    262269                    listPosition=domainref['t'].split().index(time) 
    263270                    for comp in self.currentFeature.rangeSet.aggregatedArray.component: 
     
    265272                        print comp.fileName.split() 
    266273                        data = self.getDataForVariable(comp.fileName.split()[listPosition], var, **kwargs) 
     274                        self.files.append(comp.fileName.split()[listPosition]) 
    267275                        filelog.append(comp.fileName.split()[listPosition]) 
    268276                        #print comp.fileName.split()[listPosition] + '  ' + var 
     
    277285                print 'FILES:' +str(filelog)                 
    278286                self.writeNetCDF(var, timeSubset,fulldata,**kwargs) 
     287                self.writeCSML(var, timeSubset,fulldata,**kwargs) 
    279288        return fulldata 
    280289     
     
    294303        #OrientedTrajectory 
    295304        #TimePositionList 
    296         if isinstance(self.currentFeature.domain.domainReference,TimePositionList): 
    297 #             time = [] 
    298 #             time.append('t') 
    299 #             time.append(self.currentFeature.domain.domainReference.timePositions) 
    300 #             domainref.append(time) 
    301              
     305        if isinstance(self.currentFeature.domain.domainReference,TimePositionList):           
    302306            time = {} 
    303307            time['t'] = self.currentFeature.domain.domainReference.timePositions 
    304308            domainref  = time 
    305              
    306309        #Position 
    307310        #OrientedPosition 
     
    316319        self.__setFeature(featureID) 
    317320        dc = self.currentFeature.domain.domainComplement 
    318          
    319321        #domainComplement could be one of: 
    320322        #Null 
    321323        #DirectPositionList 
    322324        #Grid 
    323          
    324325        if isinstance(dc, Grid): 
    325326            for ordinate in dc.ordinates: 
    326327                domaincomp[ordinate.definesAxis]=self.getDataForExtract(ordinate.axisValues.id) 
    327 #                 newaxis = [] 
    328 #                 newaxis.append(ordinate.definesAxis) 
    329 #                 data = self.getData(ordinate.axisValues.id) # axisValues has already been resolved to the fileExtract so the gml:id can be passed straight to getData() 
    330 #                 newaxis.append(data) 
    331 #                 domaincomp.append(newaxis) 
    332328        self.domainComplement=domaincomp 
    333329        return domaincomp 
     
    343339        for key in dr.keys(): 
    344340            domain[key]=dr[key] 
    345              
    346         #domain.append(self.getDomainComplement(featureID)) 
    347         #domain.append(self.getDomainReference(featureID)) 
    348341        return domain 
Note: See TracChangeset for help on using the changeset viewer.