modis_download.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/env python
  2. # script to download massive MODIS data from ftp
  3. #
  4. # (c) Copyright Luca Delucchi 2010
  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 download massive MODIS data"""
  22. import sys
  23. import os
  24. import getpass
  25. try:
  26. from pymodis import optparse_gui
  27. WXPYTHON = True
  28. except:
  29. WXPYTHON = False
  30. from pymodis import optparse_required
  31. from pymodis import downmodis
  32. def main():
  33. """Main function"""
  34. # usage
  35. usage = "usage: %prog [options] destination_folder"
  36. if 1 == len(sys.argv) and WXPYTHON:
  37. option_parser_class = optparse_gui.OptionParser
  38. else:
  39. option_parser_class = optparse_required.OptionParser
  40. parser = option_parser_class(usage=usage, description='modis_download')
  41. # url
  42. parser.add_option("-u", "--url", default="https://e4ftl01.cr.usgs.gov",
  43. help="http/ftp server url [default=%default]",
  44. dest="url")
  45. # username and password from stdin
  46. parser.add_option("-I", "--input", dest="input", action="store_true",
  47. help="insert user and password from standard input")
  48. # password
  49. parser.add_option("-P", "--password", dest="password",
  50. help="password to connect to the server")
  51. # username
  52. parser.add_option("-U", "--username", dest="user",
  53. help="username to connect to the server")
  54. # tiles
  55. parser.add_option("-t", "--tiles", dest="tiles", default=None,
  56. help="string of tiles separated with comma "
  57. "[default=%default for all tiles]")
  58. # path to add the path in the server
  59. parser.add_option("-s", "--source", dest="path", default="MOLT",
  60. help="directory on the http/ftp server "
  61. "[default=%default]")
  62. # path to add the url
  63. parser.add_option("-p", "--product", dest="prod", default="MOD11A1.005",
  64. help="product name as on the http/ftp server "
  65. "[default=%default]")
  66. # delta
  67. parser.add_option("-D", "--delta", dest="delta", default=10,
  68. help="delta of day from the first day "
  69. "[default=%default]")
  70. # first day
  71. parser.add_option("-f", "--firstday", dest="today", default=None,
  72. help="the day to start download [default=%default is for"
  73. " today]; if you want change data you must use "
  74. "this format YYYY-MM-DD", metavar="FIRST_DAY")
  75. # last day
  76. parser.add_option("-e", "--endday", dest="enday", default=None,
  77. metavar="LAST_DAY", help="the day to stop download "
  78. "[default=%default]; if you want change"
  79. " data you must use this format YYYY-MM-DD")
  80. # debug
  81. parser.add_option("-x", action="store_true", dest="debug", default=False,
  82. help="this is useful for debugging the "
  83. "download [default=%default]")
  84. # jpg
  85. parser.add_option("-j", action="store_true", dest="jpg", default=False,
  86. help="download also the jpeg files [default=%default]")
  87. # only one day
  88. parser.add_option("-O", dest="oneday", action="store_true", default=False,
  89. help="download only one day, it set "
  90. "delta=1 [default=%default]")
  91. # all days
  92. parser.add_option("-A", dest="alldays", action="store_true", default=False,
  93. help="download all days, it useful for initial download"
  94. " of a product. It overwrite the 'firstday' and "
  95. "'endday' options [default=%default]")
  96. # remove file with size = 0
  97. parser.add_option("-r", dest="empty", action="store_true", default=False,
  98. help="remove empty files (size equal to zero) from "
  99. "'destination_folder' [default=%default]")
  100. #parser.add_option("-A", dest="alldays", action="store_true", default=True,
  101. #help="download all days from the first")
  102. # set false several options
  103. parser.set_defaults(oneday=False)
  104. parser.set_defaults(debug=False)
  105. parser.set_defaults(jpg=False)
  106. # return options and argument
  107. (options, args) = parser.parse_args()
  108. # test if args[0] it is set
  109. if len(args) == 0 and not WXPYTHON:
  110. parser.print_help()
  111. sys.exit(1)
  112. if len(args) == 0:
  113. parser.error("You have to define the destination folder for HDF file")
  114. if not os.path.isdir(args[0]):
  115. parser.error("The destination folder is not a dir or not exists")
  116. # check if oneday option it is set
  117. if options.oneday:
  118. options.delta = 1
  119. if options.input:
  120. if sys.version_info.major == 3:
  121. user = input("Username: ")
  122. else:
  123. user = raw_input("Username: ")
  124. password = getpass.getpass()
  125. else:
  126. user = options.user
  127. password = options.password
  128. # set modis object
  129. modisOgg = downmodis.downModis(url=options.url, user=user,
  130. password=password,
  131. destinationFolder=args[0],
  132. tiles=options.tiles, path=options.path,
  133. product=options.prod, today=options.today,
  134. enddate=options.enday, jpg=options.jpg,
  135. delta=int(options.delta),
  136. debug=options.debug)
  137. # connect to ftp
  138. modisOgg.connect()
  139. if modisOgg.nconnection <= 20:
  140. # download data
  141. modisOgg.downloadsAllDay(clean=options.empty, allDays=options.alldays)
  142. else:
  143. parser.error("A problem with the connection occured")
  144. #add options
  145. if __name__ == "__main__":
  146. main()