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

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

Added new FeatureFileMap? option to Scanner. The option is called oneonly and means that the structure of just one file represents a feature spannign mulptiple directories.

  • 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       
80        tree = csmllibs.csmldirectory.DirectoryTree()
81        tree.setTopDirectory(self.topdir)
82        tree.readDirectory()
83        subdirs = tree.getSubDirectories()
84        print subdirs
85        allfiles= tree.getAllCSMLSupportedFiles()
86        print allfiles
87        ffmap = csmllibs.csmlfeaturefilemap.FeatureFileMap()
88       
89        #create representative file from first file
90        repfile=csmllibs.csmlfeaturefilemap.representativeFile(allfiles[0])
91        repfile.setFeatureType(self.ft)
92       
93        #create related files from all other files
94        for f in allfiles[1:]:
95            relfile = csmllibs.csmlfeaturefilemap.relatedFile(f)
96            relfile.setFeatureType(self.ft)
97            repfile.addRelatedFile(relfile)
98        ffmap.addRepresentativeFile(repfile)
99        return ffmap
100
101
102           
103                       
104    def mixedft(self):
105        ''' allows the use of a dictonary object to store feature file map info not well tested '''
106        tree = csmllibs.csmldirectory.DirectoryTree()
107        tree.setTopDirectory(self.topdir)
108        tree.readDirectory()
109        subdirs = tree.getSubDirectories()
110        ffmap = csmllibs.csmlfeaturefilemap.FeatureFileMap()
111        for dir in subdirs:     
112                files=tree.getCSMLSupportedFilesInSubDir(dir)
113                for f in files:
114                        repfile=csmllibs.csmlfeaturefilemap.representativeFile(f)
115                        #look up feature type in dictionary
116                        ft=self.ftdictionary[f]
117                        repfile.setFeatureType(ft)
118                        ffmap.addRepresentativeFile(repfile)   
119        return ffmap
Note: See TracBrowser for help on using the repository browser.