source: TI02-CSML/trunk/csml/csmllibs/csmlfiles.py @ 1594

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

several bug fixes to subsetting

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
RevLine 
[52]1#version 0.1
2#Dominic Lowe, BADC 8th December 2005
[1483]3#import csmllibs
[52]4import sys
[1493]5import csml.csmllibs.csmldirectory
[52]6
7
[1462]8class FileMapMaker(object):
[1156]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 '''
[1552]26        tree = csml.csmllibs.csmldirectory.DirectoryTree()
[1156]27        tree.setTopDirectory(self.topdir)
28        tree.readDirectory()
29        subdirs = tree.getSubDirectories()
[1552]30        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
[1156]31       
32        for dir in subdirs:
33                file=tree.getFirstInSubDir(dir)
34                if file == None:
35                        continue
36                #create representative file
[1552]37                repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(file)
[1156]38                repfile.setFeatureType(self.ft)
39                try:
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:
[1552]52                        relfile = csml.csmllibs.csmlfeaturefilemap.relatedFile(f)
[1156]53                        relfile.setFeatureType(self.ft)
54                        repfile.addRelatedFile(relfile)
55                ffmap.addRepresentativeFile(repfile)
56               
57        return ffmap
[52]58               
[1156]59    def onetoone(self):
60        """ onetoone means each feature is self contained within any individual file
61        """
[1552]62        tree = csml.csmllibs.csmldirectory.DirectoryTree()
[1156]63        tree.setTopDirectory(self.topdir)
64        tree.readDirectory()
65        subdirs = tree.getSubDirectories()
[1552]66        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
[1156]67        for dir in subdirs:     
68                files=tree.getCSMLSupportedFilesInSubDir(dir)
69                for f in files:
[1552]70                        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(f)
[1156]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        """
[1552]79        tree = csml.csmllibs.csmldirectory.DirectoryTree()
[1156]80        tree.setTopDirectory(self.topdir)
81        tree.readDirectory()
82        subdirs = tree.getSubDirectories()
83        print subdirs
84        allfiles= tree.getAllCSMLSupportedFiles()
85        print allfiles
[1552]86        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
[1156]87       
88        #create representative file from first file
[1552]89        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(allfiles[0])
[1156]90        repfile.setFeatureType(self.ft)
91        #create related files from all other files
[1466]92        for f in allfiles[1:]:
[1552]93            relfile = csml.csmllibs.csmlfeaturefilemap.relatedFile(f)
[1156]94            relfile.setFeatureType(self.ft)
95            repfile.addRelatedFile(relfile)
96        ffmap.addRepresentativeFile(repfile)
97        return ffmap
[52]98
[1156]99
100           
101                       
102    def mixedft(self):
103        ''' allows the use of a dictonary object to store feature file map info not well tested '''
[1552]104        tree = csml.csmllibs.csmldirectory.DirectoryTree()
[1156]105        tree.setTopDirectory(self.topdir)
106        tree.readDirectory()
107        subdirs = tree.getSubDirectories()
[1552]108        ffmap = csml.csmllibs.csmlfeaturefilemap.FeatureFileMap()
[1156]109        for dir in subdirs:     
110                files=tree.getCSMLSupportedFilesInSubDir(dir)
111                for f in files:
[1552]112                        repfile=csml.csmllibs.csmlfeaturefilemap.representativeFile(f)
[1156]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.