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

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

fixing ID/naming issues and paths

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