modis_parse.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env python
  2. # script to parse variable from xml file
  3. #
  4. # (c) Copyright Luca Delucchi 2012
  5. # Authors: Luca Delucchi
  6. # Email: luca dot delucchi at iasma dot it
  7. #
  8. ##################################################################
  9. #
  10. # This MODIS Python script is licensed under the terms of GNU GPL 2.
  11. # This program is free software; you can redistribute it and/or
  12. # modify it under the terms of the GNU General Public License as
  13. # published by the Free Software Foundation; either version 2 of
  14. # the License, or (at your option) any later version.
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. # See the GNU General Public License for more details.
  19. #
  20. ##################################################################
  21. """Script to read metadata from a MODIS HDF file"""
  22. # python 2 and 3 compatibility
  23. from __future__ import print_function
  24. # import system library
  25. import sys
  26. import os
  27. # import modis library
  28. try:
  29. from pymodis import optparse_gui
  30. WXPYTHON = True
  31. except:
  32. WXPYTHON = False
  33. from pymodis import parsemodis
  34. from pymodis import optparse_required
  35. ERROR = "You have to define the name of HDF file"
  36. def readDict(dic):
  37. """Function to decode dictionary"""
  38. out = ""
  39. for k, v in dic.items():
  40. out += "%s = %s\n" % (k, v)
  41. return out
  42. def main():
  43. """Main function"""
  44. # usage
  45. usage = "usage: %prog [options] hdf_file"
  46. if 1 == len(sys.argv) and WXPYTHON:
  47. option_parser_class = optparse_gui.OptionParser
  48. else:
  49. option_parser_class = optparse_required.OptionParser
  50. parser = option_parser_class(usage=usage, description='modis_parse')
  51. # write into file
  52. parser.add_option("-w", "--write", dest="output", metavar="OUTPUT_FILE",
  53. help="write the chosen information into a file",
  54. type='output')
  55. # all data
  56. parser.add_option("-a", action="store_true", dest="all", default=False,
  57. help="print all possible values of metadata")
  58. # spatial extent
  59. parser.add_option("-b", action="store_true", dest="boundary",
  60. default=False, help="print the values related to the "
  61. "spatial max extent")
  62. # data files
  63. parser.add_option("-d", action="store_true", dest="data", default=False,
  64. help="print the values related to the date files")
  65. # data granule
  66. parser.add_option("-e", action="store_true", dest="data_ecs",
  67. default=False, help="print the values related to the "
  68. "ECSDataGranule")
  69. # input files
  70. parser.add_option("-i", action="store_true", dest="input", default=False,
  71. help="print the input layers")
  72. # other values
  73. parser.add_option("-o", action="store_true", dest="other", default=False,
  74. help="print the other values")
  75. # platform information
  76. parser.add_option("-p", action="store_true", dest="platform", default=False,
  77. help="print the values related to platform")
  78. # data quality
  79. parser.add_option("-q", action="store_true", dest="qa", default=False,
  80. help="print the values related to quality")
  81. # psas
  82. parser.add_option("-s", action="store_true", dest="psas", default=False,
  83. help="print the values related to psas")
  84. # time
  85. parser.add_option("-t", action="store_true", dest="time", default=False,
  86. help="print the values related to times")
  87. # layers
  88. parser.add_option("-l", action="store_true", dest="layers", default=False,
  89. help="print the names of layer in HDF file")
  90. # return options and argument
  91. (options, args) = parser.parse_args()
  92. if len(args) == 0 and not WXPYTHON:
  93. parser.print_help()
  94. sys.exit(1)
  95. if not args:
  96. parser.error(ERROR)
  97. else:
  98. if not isinstance(args, list):
  99. parser.error(ERROR)
  100. if not os.path.isfile(args[0]):
  101. parser.error(ERROR + '. ' + args[0] + ' does not exists')
  102. # create modis object
  103. modisOgg = parsemodis.parseModis(args[0])
  104. # the output string
  105. outString = ""
  106. if options.all or options.boundary:
  107. outString += readDict(modisOgg.retBoundary())
  108. if options.all or options.time:
  109. outString += "InsertTime = %s\n" % modisOgg.retInsertTime()
  110. outString += "LastUpdate = %s\n" % modisOgg.retLastUpdate()
  111. outString += readDict(modisOgg.retRangeTime())
  112. if options.all or options.data_ecs:
  113. outString += readDict(modisOgg.retDataGranule())
  114. if options.all or options.data:
  115. outString += readDict(modisOgg.retDataFiles())
  116. if options.all or options.input:
  117. outString += 'InputFiles = '
  118. outString += ', '.join(modisOgg.retInputGranule())
  119. outString += '\n'
  120. if options.all or options.platform:
  121. outString += readDict(modisOgg.retPlatform())
  122. if options.all or options.psas:
  123. outString += readDict(modisOgg.retPSA())
  124. if options.all or options.qa:
  125. out = modisOgg.retMeasure()
  126. for mes in out.values():
  127. outString += mes['ParameterName'] + '\n'
  128. outString += readDict(mes['QAStats'])
  129. outString += readDict(mes['QAFlags'])
  130. if options.all or options.other:
  131. outString += readDict(modisOgg.retCollectionMetaData())
  132. outString += "PGEVersion = %s\n" % modisOgg.retPGEVersion()
  133. outString += "BrowseProduct = %s\n" % modisOgg.retBrowseProduct()
  134. if options.all or options.layers:
  135. outString += modisOgg.getLayersName()
  136. if not outString:
  137. print("Please select at least one flag")
  138. # write option it is set write the string into file
  139. elif options.output:
  140. outFile = open(options.output, 'w')
  141. outFile.write(outString)
  142. outFile.close()
  143. print("{name} write correctly".format(name=outFile.name))
  144. # else print the string
  145. else:
  146. print(outString)
  147. if __name__ == "__main__":
  148. main()