modis_multiparse.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env python
  2. # script to obtain the bounding box of several tiles and write xml file of the mosaic
  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. # python 2 and 3 compatibility
  22. from __future__ import print_function
  23. #import system library
  24. import sys
  25. import os
  26. #import modis library
  27. try:
  28. from pymodis import optparse_gui
  29. wxpython = True
  30. except:
  31. wxpython = False
  32. from pymodis import parsemodis
  33. from pymodis import optparse_required
  34. def readDict(dic):
  35. """Function to decode dictionary"""
  36. out = ""
  37. for k, v in dic.items():
  38. out += "%s = %s\n" % (k, v)
  39. return out
  40. def main():
  41. """Main function"""
  42. #usage
  43. usage = "usage: %prog [options] hdf_files_list"
  44. if 1 == len(sys.argv) and wxpython:
  45. option_parser_class = optparse_gui.OptionParser
  46. else:
  47. option_parser_class = optparse_required.OptionParser
  48. parser = option_parser_class(usage=usage, description='modis_multiparse')
  49. #spatial extent
  50. parser.add_option("-b", action="store_true", dest="bound", default=False,
  51. help="print the values related to the spatial max extent")
  52. #write into file
  53. parser.add_option("-w", "--write", dest="output", metavar="OUTPUT_FILE",
  54. help="write the MODIS XML metadata file for MODIS mosaic")
  55. (options, args) = parser.parse_args()
  56. #create modis object
  57. if len(args) == 0 and not wxpython:
  58. parser.print_help()
  59. sys.exit(1)
  60. if len(args) < 2:
  61. parser.error("You have to define the name of multiple HDF files")
  62. for arg in args:
  63. if not os.path.isfile(arg):
  64. parser.error(arg + " does not exist or is not a file")
  65. modisOgg = parsemodis.parseModisMulti(args)
  66. if options.bound:
  67. modisOgg.valBound()
  68. print(readDict(modisOgg.boundary))
  69. elif options.output:
  70. modisOgg.writexml(options.output)
  71. print("%s write correctly" % options.output)
  72. else:
  73. parser.error("You have to choose at least one option")
  74. #add options
  75. if __name__ == "__main__":
  76. main()