Module hdf4MapReader
[hide private]
[frames] | no frames]

Source Code for Module hdf4MapReader

  1  """\
 
  2  Created on 03/01/2011
 
  3  
 
  4  @author: Luis Lopez
 
  5  @contact:  Luis dot Lopez at nsidc dot org
 
  6  
 
  7  HDF map reader
 
  8  V. 0.1.14
 
  9  
 
 10  This program is licensed under the GPL v3 GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
 
 11  
 
 12  About the project:
 
 13  
 
 14  The main goal of the HDF mapping project is data preservation to ensure that scientific data stored as HDF4 files
 
 15  will be accessible in the future without relaying on a specific API or platform.  This is being addressed 
 
 16  using XML to represent all the necessary information of an HDF4 file and its content in a structured way.
 
 17  
 
 18  The HDF map reader is being developed using this approach.
 
 19      
 
 20  
 
 21  hdf4MapReader  is the main module, it parses command line argument as follows:
 
 22  
 
 23  ./hdf4MapReader  -f [filename] -l|-e [HDF_object]
 
 24  
 
 25  [filename]: a Valid XML map file
 
 26  
 
 27  -e: extract the object(s)
 
 28  
 
 29  -l: list the object(s)
 
 30  
 
 31  [HDF_object]: VData, SDS, RIS, MDATA, ALL     
 
 32     
 
 33    
 
 34  """ 
 35  from Utils import utils 
 36  from XMLparser import XMLparser 
 37  from optparse import OptionParser 
 38  import os 
 39  import sys 
 40  #from optparse import OptionParser
 
 41  #from time import gmtime, strftime
 
 42  
 
 43  
 
 44  
 
45 -def usage(args):
46 ''' 47 Print the usage of the command line, the paths are relative to the script location. 48 49 ./hdf4MapReader -f [filename] -l|-e [HDF_object] [-b] 50 51 [filename]: a Valid XML map file 52 -e: extract the object(s) 53 -l: list the object(s) 54 [HDF_object]: VData, SDS, RIS, MDATA, ALL 55 -b: dumps the object(s) in binary .dat files. 56 ''' 57 print "Usage: ", args[len(args)-1] ," -f [filename] -l|-e [HDF_object]" 58 print " " 59 print " [filename]: a Valid XML map file" 60 print " -e: extract the object(s)" 61 print " -l: list the object(s)" 62 print " [HDF_object]: VData, SDS, RIS, MDATA, ALL"
63
64 -def main():
65 ''' 66 This function validate the command line arguments and creates a sub directory in the same path as the XML file and 67 with the same name. This directory will be used to store the output files. 68 69 If the parameters are correct and the reader has the necessary permissions to create the output directory, an instance of 70 'XMLparser' will be created. 71 72 ''' 73 util=utils() 74 parser = OptionParser() 75 76 parser.add_option("-f", "--file", dest="filename",type="string", 77 help="Loads the XML FILE", metavar="FILE") 78 79 parser.add_option("-l", "--list", dest="list", 80 help="List all the mapped objects in the XML FILE", metavar="LIST") 81 82 parser.add_option("-e", "--extract", dest="extract", 83 help="Extracts all the mapped objects in the XML FILE", metavar="OBJECTS") 84 85 parser.add_option("-b", "--binary", dest="binary",default=False,nargs=0, 86 help="Extracts each selected object in a binary .dat file", metavar="DUMP") 87 88 89 90 (options, args) = parser.parse_args() 91 92 93 #Getting the program name 94 args=sys.argv[0].split("/") 95 if len(args)==1: 96 args=sys.argv[0].split("\\") 97 98 #Relative paths for Windows: 99 if options.filename!=None: 100 if not os.path.isabs(options.filename): 101 if sys.platform.find("WIN") and (sys.argv[0].find(".exe")!=-1): 102 options.filename= os.path.normpath(os.path.join(sys.path[0],"..\\" +options.filename)) 103 else: 104 usage(args) 105 return 106 107 108 if options.filename==None: 109 print "Required argument [-f|--file] missing" 110 print "" 111 usage(args) 112 exit(-1) 113 elif not os.path.exists( options.filename): 114 print "The file does not exist or it is an incorrect filename: " + options.filename 115 print "" 116 usage(args) 117 exit(-1) 118 else: 119 if options.list==None and options.extract==None: 120 print " -l or -e required" 121 print "" 122 usage() 123 exit(-1) 124 elif options.list and options.extract: 125 print "Options -l and -e are mutually exclusive" 126 print "" 127 print options 128 print "" 129 usage(args) 130 exit(-1) 131 elif options.list not in ["VData","SDS","RIS","MDATA","ALL"] and options.extract not in ["VData","SDS","RIS","MDATA","ALL"]: 132 print " A valid HDF object name is required" 133 print "" 134 print options 135 print "" 136 usage(args) 137 exit(-1) 138 else: 139 #print options 140 #We have correct arguments 141 relative_path=util.getRelativePath(options.filename) 142 print relative_path, len(relative_path) 143 if not os.path.exists(options.filename + "_dump"):# Be careful with relative paths in Linux 144 try: 145 os.makedirs(options.filename+ "_dump") 146 print "Directory created :" + options.filename 147 except: 148 print "Failed to create a sub directory to store the output files: " + options.filename 149 exit(-1) 150 else: 151 print "The output directory already exist: " + options.filename 152 153 if options.list:# We call the parser just to list the objects mapped in the XML file 154 parser= XMLparser(relative_path,options.filename,"l",options.list,options.binary) 155 parser.parseXML() 156 else:# We call the parser to extract a given object(s) described in the XML map. 157 # At the moment just Gruops and VData are supported 158 #print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) # benchmarking 159 parser= XMLparser(relative_path,options.filename,"e",options.extract,options.binary) 160 parser.parseXML() 161 print "Dumping complete"
162 #print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) 163 164 165 if __name__ == '__main__': 166 main()# Call the main function to parse the command line arguments and create an instance of XMLparser. 167