Changes between Version 8 and Version 9 of CSMLParserHowTo


Ignore:
Timestamp:
04/02/08 12:27:17 (12 years ago)
Author:
domlowe
Comment:

updating csml parser how to.

Legend:

Unmodified
Added
Removed
Modified
  • CSMLParserHowTo

    v8 v9  
    2525 
    2626 == So...  how to actually use the parser. == 
    27  === The online parser === 
     27 === The online parser (Temporarily offline) === 
    2828 
    2929Well first the easy way. Use the online parser. This is handy for testing your CSML documents parse as expected. 
     
    3333The parser is located at: 
    3434http://proj.badc.rl.ac.uk/cgi-bin/csml2/parseTest.py 
    35 Simply browse to your CSML file and submit your query to see if your file parses.  
     35Simply browse to your CSML file and submit your query to see if your file parses.  (Currently offline, sorry) 
    3636 
    3737 === Parsing in python === 
     
    8989 
    9090 == The CSML API == 
    91 As we have just seen, the parser itself provides an API of sorts via the object hierarchy. but it is clumsy to navigate. The most common things you will want to do with features have been wrapped up in a set of simple methods. Rather than accurately document the methods here (!PyDoc does that nicely), this is how to use the methods to perform a subsetting operation on a !GridSeriesFeature: 
     91As we have just seen, the parser itself provides an API of sorts via the object hierarchy. but it is clumsy to navigate. The most common things you will want to do with features have been wrapped up in a set of simple methods. Rather than accurately document the methods here (!PyDoc does that nicely), this is how to use the methods to perform a subsetting operation on a !PointSeriesFeature: 
    9292 
    9393{{{ 
    9494#!python 
    95 import csml   #This will import csml.API amongst other things which are needed (e.g. data access libraries) 
    9695 
    97 f='coapec.xml' # your CSML file 
     96import csml 
     97 
     98f='example.xml' 
     99outputdir = 'out' 
     100 
     101#Initialise the CSML container 
     102csmlContainer=csml.API.csmlContainer.Container(outputdir,csmlname='myfile.xml',docID="mycsmldoc_1", metadata="mymetadatastring") 
    98103 
    99104#Initialise and parse the dataset 
    100 csmlds = csml.parser.Dataset()  # Create a new empty csml Dataset object 
    101 csmlds.parse(f) # parse the CSML file - this is like calling the fromXML() method of the Dataset 
     105csmldoc = csml.parser.Dataset(file=f) 
    102106 
    103 #You can now interrogate the CSML document: 
    104107 
    105108#get list of features in the dataset 
    106 flist= csmlds.getFeatureList()  
     109flist= csmldoc.getFeatureList()  
    107110print '\n Here are all the features in %s:' %f 
    108111print flist 
    109112 
    110113#select a feature by name (gml:id) 
    111 print '\n Selecting feature with gml:id = %s' %flist[4] 
    112 feature=csmlds.getFeature(flist[4]) 
     114print '\n Selecting feature with gml:id = %s' %flist[0] 
     115feature=csmldoc.getFeature(flist[0]) 
    113116 
    114 #These are some attributes, the gml:id and gml:description 
     117f#eature 'bestname' 
    115118print feature.id 
    116119print feature.description 
    117120 
    118121#get the domain of the feature 
    119 print '\n The feature has domain reference:'  
    120 print feature.getDomainReference() 
    121  
    122 #get the domain complement of the feature 
    123 print '\n The feature has domain complement :'  
    124 #print feature.getDomainComplement() 
    125  
    126 #get combined domain, this returns the domainReference and the domainComplement 
    127 print '\n The feature has domain:'  
    128 #print feature.getDomain() 
     122print '\n The feature has domain: ' 
     123print feature.getDomain() 
    129124 
    130125#get list of allowed subsettings 
     
    133128 
    134129 
    135 #Now we can subset the file based on a selection 
     130#define a selection (based on the domain) 
     131timeSelection=['2007-08-12T10:45:00', '2007-08-12T11:45:00'] 
    136132 
    137 #define a selection (you would base this on the values of the domain ref/complement but I have hardcoded it here) 
    138 timeSelection=['2794-12-1T0:0:0.0', '2844-12-1T0:0:0.0']  #max and min values (you can also provide a list of specific values) 
    139 spatialSubsetDictionary= {} 
    140 spatialSubsetDictionary['latitude']=(-30.0,30.0) 
    141 spatialSubsetDictionary['longitude']=(90, 120.0) 
    142 #If the feature is defined in any other dimension you can add that here too. 
     133#request subsetted data from features (can set paths here) and add to container 
     134#subset a feature 
     135csmlContainer.add(feature.subsetToPointSeries(times=timeSelection,  ncname='myfile.nc')) 
    143136 
    144 #request subsetted data from feature (can set output file paths here) 
    145 subsetCSML, subsetNetCDF, arraySize=feature.subsetToGridSeries(timeSelection, csmlpath='my.xml', ncpath='my.nc',**spatialSubsetDictionary) 
     137#when you have finished, get the contents of the container 
     138csmlbundle=csmlContainer.getContents() 
     139print 'CSML %s'%csmlbundle[0] # csml document 
     140paths=csmlbundle[1:] 
     141print paths #paths to netcdf files 
    146142 
    147143 
    148 #Now we have a subsetted CSML document and a NetCDF file that describe/contain your subsetted data. 
    149 print subsetCSML #csml document (string) 
    150 print subsetNetCDF # netcdf file (file) 
    151 print 'arraySize: %s' %arraySize  #this is just useful - how big is the data. 
    152144}}} 
    153145 
     146The result of this subsetting operation is a netcdf file containing your subset, and a csml file describing the contents of the netcdf file. 
    154147 
    155 But wait, perhaps this didn't work. If you couldn't perform the subset operation, then it is because you need more things installing... 
    156 All the parser operations shown up till now have just operated on a CSML document and the CSML python objects in memory. However when you perform a subsetting operation, if the data is stored in real data files, then some i/o operations take place. Typically this means installing the cdms module to read NetCDF, Nappy to read NASAAmes, and potentially other modules to read other file formats. I should probably write more on this, but installation is an area that's going to change radically so I won't for now. 
     148 
     149If this didn't work and you couldn't perform the subset operation, then it is probably because you need more things installing... 
     150 
     151All the parser operations shown up till now have just operated on a CSML document and the CSML python objects in memory. However when you perform a subsetting operation, if the data is stored in real data files, then some i/o operations take place. Typically this means installing the cdms module to read NetCDF, Nappy to read NASAAmes, and potentially other modules to read other file formats.  
    157152 
    158153 
     
    164159 * Perform operations on the CSML and underlying data using the CSML API. 
    165160 
    166 There is another thing you can do and that is to use the parser's toXML() methods to create your CSML. However that is the subject of a [wiki:UsingTheParserToCreateCSML separate how to]. 
     161There is another thing you can do and that is to use the parser's toXML() methods to create your CSML. However that is the subject of a [wiki:UsingTheParserToCreateCSMLV2 separate how to].