source: TI02-CSML/branches/CSML2/csmllibs/csmlfiles.py @ 1887

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/branches/CSML2/csmllibs/csmlfiles.py@1887
Revision 1887, 4.8 KB checked in by domlowe, 13 years ago (diff)

Initial changes to scanner to use csml 2 featuretypes. Very basic outline of GridSeriesFeature? working.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#version 0.1
2#Dominic Lowe, BADC 8th December 2005
3#import csmllibs
4import sys
5import csml.csmllibs.csmldirectory
6
7
8class FileMapMaker(object):
9    ''' class which handles the lower level featurefilemap and directorytree classes
10    to build a featurefilemap object of featuretypes/files.
11    current relationships are onetomany, onetoone and mixedft,
12    but others could be created '''
13    def __init__(self,topdirectory, ftorftdictionary):
14            self.topdir=topdirectory
15            if type(ftorftdictionary) is str:
16                    self.ft = ftorftdictionary
17            elif type(ftorftdictionary) is dict:
18                    self.ftdictionary=ftorftdictionary
19            else:
20                    #wrong type
21                    print 'FileMapMaker takes a  featuretype string or dictionary'
22                    sys.exit()
23   
24    def onetomany(self):
25        ''' onetomany means one file per directory/subdirectory is the representative file '''
26        tree = csml.csmllibs.csmldirectory.DirectoryTree()
27        tree.setTopDirectory(self.topdir)
28        tree.readDirectory()
29        subdirs = tree.getSubDirectories()
30        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
31        for dir in subdirs:
32                file=tree.getFirstInSubDir(dir)
33                if file == None:
34                        continue
35                #create representative file
36                repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(file)
37                repfile.setFeatureType(self.ft)
38                try:
39                        otherfiles=tree.getAllCSMLFilesExceptFirst(dir)
40                        errtest=otherfiles[0]
41                except:
42                        print 'no other csml files in this directory'
43                        #add representative file with no related files.
44                        ffmap.addRepresentativeFile(repfile)
45                        continue
46               
47                #add otherfiles as related files with feature type, ft
48                #note they could have individual featuretypes.
49
50                for f in otherfiles:
51                        relfile = csml.csmllibs.csmlfeaturefilemap.relatedFile(f)
52                        relfile.setFeatureType(self.ft)
53                        repfile.addRelatedFile(relfile)
54                ffmap.addRepresentativeFile(repfile)
55        return ffmap
56               
57    def onetoone(self):
58        """ onetoone means each feature is self contained within any individual file
59        """
60        tree = csml.csmllibs.csmldirectory.DirectoryTree()
61        tree.setTopDirectory(self.topdir)
62        tree.readDirectory()
63        subdirs = tree.getSubDirectories()
64        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
65        for dir in subdirs:     
66                files=tree.getCSMLSupportedFilesInSubDir(dir)
67                for f in files:
68                        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(f)
69                        repfile.setFeatureType(self.ft)
70                        ffmap.addRepresentativeFile(repfile)
71        return ffmap   
72       
73    def oneonly(self):
74        """ oneonly means one file represents feature spanning multiple directories
75        assumes no file in toplevel directory, and then lots of subdirectories at next level containing files
76        """
77        tree = csml.csmllibs.csmldirectory.DirectoryTree()
78        tree.setTopDirectory(self.topdir)
79        tree.readDirectory()
80        subdirs = tree.getSubDirectories()
81        allfiles= tree.getAllCSMLSupportedFiles()
82        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
83       
84        #create representative file from first file
85        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(allfiles[0])
86        repfile.setFeatureType(self.ft)
87        #create related files from all other files
88        for f in allfiles[1:]:
89            relfile = csml.csmllibs.csmlfeaturefilemap.relatedFile(f)
90            relfile.setFeatureType(self.ft)
91            repfile.addRelatedFile(relfile)
92        ffmap.addRepresentativeFile(repfile)
93        return ffmap
94
95
96           
97                       
98    def mixedft(self):
99        ''' allows the use of a dictonary object to store feature file map info not well tested '''
100        tree = csml.csmllibs.csmldirectory.DirectoryTree()
101        tree.setTopDirectory(self.topdir)
102        tree.readDirectory()
103        subdirs = tree.getSubDirectories()
104        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
105        for dir in subdirs:     
106                files=tree.getCSMLSupportedFilesInSubDir(dir)
107                for f in files:
108                        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(f)
109                        #look up feature type in dictionary
110                        ft=self.ftdictionary[f]
111                        repfile.setFeatureType(ft)
112                        ffmap.addRepresentativeFile(repfile)   
113        return ffmap
Note: See TracBrowser for help on using the repository browser.