source: TI02-CSML/trunk/Scanner/csmllibs/csmlfiles.py @ 1352

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

Lots of changes to enable ArrayGenerators? to work. Not working fully but checking in at convenient point

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