Changes between Version 5 and Version 6 of CSMLParserHowTo


Ignore:
Timestamp:
07/09/06 15:13:25 (13 years ago)
Author:
domlowe
Comment:

Edited CSML parser How To page to reflect new module structure

Legend:

Unmodified
Added
Removed
Modified
  • CSMLParserHowTo

    v5 v6  
    4141The parser itself doesn't have many dependencies, mainly cElementTree. However if you are building applications that use some features of the parser (eg subsetting and creating a NetCDF file) then there will be more dependencies. For now though lets just install the basic parser. 
    4242 
    43 The parser is written in Python, so   you will need Python [LINK] installed to use it.  
    44 When you have python installed, try and run the parser code. Download the parser code from {HERE}. You will need the entire 'parser' directory. 
    45 Try and run the file test.py by typing: 
     43The parser is written in Python, so   you will need Python installed to use it.  
     44When you have python installed, try and run the parser code. Check out the parser code from subversion. The code you need is here: [http://proj.badc.rl.ac.uk/ndg/browser/TI02-CSML/trunk]. You will need the entire csml directory and you will need basictest.py and example.xml from here: [http://proj.badc.rl.ac.uk/ndg/browser/TI02-CSML/trunk/Examples] 
     45 
     46Try and run the file basictest.py by typing: 
    4647{{{ 
    4748#!python    
    48 python test.py 
     49python basictest.py 
    4950}}} 
    50 If this doesn't work and you need any extra python components e.g. cElementTree you will have to install them on your system. 
     51If this doesn't work and you need any extra python components e.g. cElementTree you will have to install them on your system. You also need to make sure that the csml package you have just downloaded is in the same directory as basictest or available through your PYTHONPATH. 
    5152 
    52 Once you have everything installed, if you can run test.py you have already parsed a CSML file. 
    53 Look at the code in test.py. You can see it is calling several parser methods. 
     53Once you have everything installed, if you can run basictest.py you have already parsed a CSML file. 
     54Look at the code in basictest.py. You can see it is calling several parser methods. 
    5455{{{ 
    5556#!python 
    5657tree = ElementTree(file='example.xml') 
    57 dataset = Dataset() 
     58dataset = csml.parser.Dataset() 
    5859 
    5960#Calling the fromXML method reads the CSML into memory. 
     
    6263#This creates a new CSML document string from the CSML objects in memory. 
    6364#Hopefully the CSML output should be the same as the CSML it read in. 
    64 csml = dataset.toXML() 
     65csmldoc = dataset.toXML() 
    6566 
    6667#print it and see: 
    67 strCSML=parser_extra.PrettyPrint(csml) # this just tidies up the formatting 
     68strCSML=parser_extra.PrettyPrint(csmldoc) # this just tidies up the formatting 
    6869strCSML=parser_extra.removeInlineNS(strCSML)  #this just tidies up the namespaces 
    6970print strCSML 
     
    7374So all we are doing is creating a Dataset parser object and saying 'parse all the XML from tree.getroot()'. i.e. parse the entire CSML document. 
    7475 
    75 So now the object called 'csml' is a representation of the CSML document in memory. You can navigate this document directly by using python attributes e.g.: 
     76So now the object called 'dataset' is a representation of the CSML document in memory. You can navigate this document directly by using python attributes e.g.: 
    7677 
    7778{{{ 
     
    9192{{{ 
    9293#!python 
    93 import API   #This is all you need to import, the API module will import the parser as API.Parser 
     94import csml   #This will import csml.API amongst other things which are needed (e.g. data access libraries) 
    9495 
    9596f='coapec.xml' # your CSML file 
    9697 
    9798#Initialise and parse the dataset 
    98 csml = API.Parser.Dataset()  # Create a new empty csml Dataset object 
    99 csml.parse(f) # parse the CSML file - this is like calling the fromXML() method of the Dataset 
     99csmlds = csml.parser.Dataset()  # Create a new empty csml Dataset object 
     100csmlds.parse(f) # parse the CSML file - this is like calling the fromXML() method of the Dataset 
    100101 
    101102#You can now interrogate the CSML document: 
    102103 
    103104#get list of features in the dataset 
    104 flist= csml.getFeatureList()  
     105flist= csmlds.getFeatureList()  
    105106print '\n Here are all the features in %s:' %f 
    106107print flist 
     
    108109#select a feature by name (gml:id) 
    109110print '\n Selecting feature with gml:id = %s' %flist[4] 
    110 feature=csml.getFeature(flist[4]) 
     111feature=csmlds.getFeature(flist[4]) 
    111112 
    112113#These are some attributes, the gml:id and gml:description 
     
    152153 
    153154But wait, perhaps this didn't work. If you couldn't perform the subset operation, then it is because you need more things installing... 
    154 All the parser operations shown so far 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. 
     155All 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. 
    155156 
    156157