source: TI02-CSML/trunk/csml/API/csmlContainer.py @ 2774

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/csml/API/csmlContainer.py@2774
Revision 2774, 4.6 KB checked in by domlowe, 13 years ago (diff)

better handling of arrays to speed up subsetting

Line 
1import csml
2OUTPUTDIR=None
3
4class globalContainer(object):
5    '''container for (optional) shared variables (eg outputdir)'''
6    OUTPUTDIR=None
7    CACHE={} #cache used for storing fileextract domain data, eg lat lon values, that are often reused
8
9class Container(object):
10    '''CSMLContainer object is used to hold features before generating a CSML document. This is useful because it allows you to subset several features sequentially then build a single CSML document containing all the subsetted features '''
11   
12    def __init__(self,outputdir, csmlname='myfile.xml',docID='none',metadata='unspecified', datasetName='unspecified'):
13        self.outputdir =outputdir
14        globalContainer.OUTPUTDIR=outputdir
15        self.csmlname=csmlname
16        self.datasetName=datasetName
17        ''' Initialise CSML Dataset and FeatureCollection and populate with basic Metadata'''
18        '''
19        docID = gml:id of Dataset element
20        metadata = gml:metaDataProperty of Dataset element  #hmm may need to allow a list here.
21        csmlname = name of resulting csml file
22        datasetName = name used in <name> element of csml doc.
23        '''
24        #initialise a new document
25        #Create an Empty Dataset & FeatureCollection
26        self.ds = csml.parser.Dataset()
27        #Set attributes of dataset
28        #if 'metadata' is a URI, set it as href attribute if MetaDataProperty instance,
29        #else set it as a text attribute of a MetaDataProperty instance.
30        #commented out temporarily for now (V2 rewrite)
31        self.mdp=csml.parser.csString()
32        if csml.parser_extra.isURI(metadata):
33            self.mdp.href=metadata
34        else: 
35            self.mdp.CONTENT= metadata
36       
37        #set the id
38        self.ds.id=docID
39        self.dm=[] #empty list to hold definition members (e.g. UnitDefinitions)
40        self.fm=[] #empty list to hold feature members (e.g. GridFeatures, PointFeatures)
41        self.sd=[] #empty list to hold storage descriptors (e.g. NetCDFExtract)
42        #the dataset is now initialised. now you can add features/defintions/arraydescriptors
43        #using the methods supplied.
44        #when finished building CSML objects call the getContents method to return the document.       
45        '''containerContents contains the CSML Document (first item) and then any NetCDF files that are to be delivered with the document '''
46        self.containerContents=[]
47        self.ncfiles=[]
48   
49    def appendFileExtract(self, fileextract):
50        ''' Append File Extract to Dataset '''
51        #append to Dataset instance
52        pass
53   
54    def appendFeature(self, feature):
55        ''' Append feature to FeatureCollection'''
56        #append to FeatureCollection instance
57        self.fm.append(feature)
58   
59    def attachNetCDFFile(self, ncfile):
60        ''' Attach a NetCDF file to the container object. Multiple NetCDF files can be referenced from multiple features and returned with the CSML document'''
61        #append NetCDF file path to containerContents
62        self.ncfiles.append(ncfile)
63   
64    def attachDescriptor(self, descriptor):
65        self.sd.append(descriptor)
66
67    def add(self, featureplusfileplusdescriptor):
68        #featureplusfileplusdescriptor should be a feature, and a filepath to the netcdf file, and a storage descriptor
69        self.appendFeature(featureplusfileplusdescriptor[0])
70        self.attachNetCDFFile(featureplusfileplusdescriptor[1])       
71        #handle multiple descriptors:
72        if type(featureplusfileplusdescriptor[2]) is list:
73            for item in featureplusfileplusdescriptor[2]:
74                self.attachDescriptor(item)
75        else:
76            self.attachDescriptor(featureplusfileplusdescriptor[2])
77       
78   
79    def getContents(self):
80        self.ds.storageDescriptor=csml.parser.CSMLStorageDescriptor(descriptors=self.sd)
81        self.ds.featureCollection=csml.parser.CSMLFeatureCollection(featureMembers=self.fm)
82        self.ds.featureCollection.metaDataProperty=self.mdp
83        self.ds.featureCollection.id=csml.csmllibs.csmlextra.getRandomID()
84        self.ds.name=csml.parser.csString(self.datasetName)
85        csmlout=self.ds.toXML()
86        #parse and pretty print the result to file
87        strCSML=csml.parser_extra.PrettyPrint(csmlout)
88        strCSML=csml.parser_extra.removeInlineNS(strCSML)
89        csml.csmllibs.csmlextra.checkDirExists(self.outputdir)
90        filepath=self.outputdir + '/' + self.csmlname
91        f=open(filepath,'w')
92        f.write(strCSML)
93        f.close()
94        self.containerContents.append(self.csmlname)
95        for ncfile in self.ncfiles:
96            self.containerContents.append(ncfile)
97        return self.containerContents
Note: See TracBrowser for help on using the repository browser.