wiki:UsingTheParserToCreateCSMLV2/GridSeries

Version 1 (modified by domlowe, 12 years ago) (diff)

Creating Grid Series Example for Helen

GridSeriesFeature Example

Outline example using the parser to create a GridSeriesFeature:

import csml

#set up the dataset
ds=csml.parser.Dataset()
ds.name=csml.parser.csString('Name of your dataset')

#empty feature collection:
fc=csml.parser.CSMLFeatureCollection()
#empty list
features=[]  

#for each feature do something like this:
##########################
#get the info from hdf:
NAME = 'myfeature'
DESCRIPTION='more about my feature'



f=csml.parser.GridSeriesFeature()
f.id=csml.csmllibs.csmlextra.getRandomID()
f.name=csml.parser.csString(NAME)               
f.description=csml.parser.csString(DESCRIPTION)                


#coverage@
cvg=csml.parser.GridSeriesCoverage()

#Domain
domain=csml.parser.GridSeriesDomain()
domain.srsName='ndg:crs:xyt'
domain.axisLabels='longitude latitude time'
domain.id=csml.csmllibs.csmlextra.getRandomID()
domain.dimension='3'
domain.srsDimension='3'

gct=csml.parser.GridCoordinatesTable()

#loop through each axis (in this case there are 3 axes)

AXISLABELS=['mylongitudename', 'mylatitudename', 'mytimename']
GRIDAXES=['longitude', 'latitude', 'time']
gridords=[]
for i in range(0, 3):
    gridord=csml.parser.GridOrdinateDescription()
    gridord.coordAxisLabel=csml.parser.csString(AXISLABELS[i])
    sptList=csml.parser.SpatialOrTemporalPositionList()
    sptList.id=csml.csmllibs.csmlextra.getRandomID()
    sptList.href='#linktoCorrectFileExtract'
    sptList.show='embed'
    sptList.arcrole='http://ndg.nerc.ac.uk/xlinkUsage/insert#coordinateList'
    sptList.role='http://ndg.nerc.ac.uk/fileFormat/csmlStorageDescriptor'
    gridord.coordAxisValues=sptList
    gridord.gridAxesSpanned=csml.parser.csString(GRIDAXES[i])
    seqRule=csml.parser.SequenceRule()
    seqRule.CONTENT='Linear'
    seqRule.axisOrder='+1'
    gridord.sequenceRule=seqRule
    gridords.append(gridord)
    


#attach all these to the coord  tranform table, and attach that to the domain.
gct.gridOrdinates=gridords
domain.coordTransformTable=gct

#Rangeset
rset=csml.parser.RangeSet()


#Parameter
param=csml.parser.Phenomenon()
param.href='#linktophenomenondef'



#nest together
cvg.gridSeriesDomain=domain
cvg.rangeSet=rset
f.value=cvg
f.parameter=param


descriptors=[]
#create a pretend storage descriptor
#note I've used NetCDF as the HDFFileExtract (or similar hasn't been defined in schema/parser yet)
fe=csml.parser.NetCDFExtract()
fe.variableName=csml.parser.csString('blah')
fe.id = csml.csmllibs.csmlextra.getRandomID()
fe.arraySize=csml.parser.csString('9999')
fe.fileName = csml.parser.csString('myfile')

#add it to list
descriptors.append(fe)

#and another one
fe=csml.parser.NetCDFExtract()
fe.variableName=csml.parser.csString('blah2')
fe.id = csml.csmllibs.csmlextra.getRandomID()
fe.arraySize=csml.parser.csString('999999')
fe.fileName = csml.parser.csString('myfile2')

#add it to list
descriptors.append(fe)



#create storageDescriptor collection:
sd=csml.parser.CSMLStorageDescriptor()
sd.descriptors=descriptors


#add feature to list of features
features.append(f)
###########################


#set list of features to be featureMembers property of featureCollection:
fc.featureMembers=features

#set featureCollection to be featureCollection property of dataset
ds.featureCollection=fc

#set storage descriptor to be property of dataset
ds.storageDescriptor=sd
#convert to XML
csmldoc=ds.toXML()

#pretty printing and sorting out namespaces:
strCSML=csml.parser_extra.PrettyPrint(csmldoc)
strCSML=csml.parser_extra.removeInlineNS(strCSML)

print strCSML

#save csml to file:
fout=open('myfile.xml', 'w')
fout.write(strCSML)
fout.close()