productmodis.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # class to download modis data
  4. #
  5. # (c) Copyright Luca Delucchi 2010-2016
  6. # Authors: Luca Delucchi
  7. # Email: luca dot delucchi at fmach dot it
  8. #
  9. ##################################################################
  10. #
  11. # The Modis class is licensed under the terms of GNU GPL 2
  12. # This program is free software; you can redistribute it and/or
  13. # modify it under the terms of the GNU General Public License as
  14. # published by the Free Software Foundation; either version 2 of
  15. # the License, or (at your option) any later version.
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  19. # See the GNU General Public License for more details.
  20. #
  21. ##################################################################
  22. class product:
  23. """Definition of MODIS product with url and path in the ftp server
  24. """
  25. def __init__(self, value=None):
  26. # url to download products
  27. urlbase = 'http://e4ftl01.cr.usgs.gov'
  28. usrsnow = 'ftp://n5eil01u.ecs.nsidc.org'
  29. # values of lst product:
  30. lst_spec = '( 1 0 0 0 1 0 0 0 0 0 0 0 )'
  31. lst_specqa = '( 1 1 0 0 1 1 0 0 0 0 0 0 )'
  32. # suffix for the lst product (key is the lst map, value the QA)
  33. lst1km_suff = {'.LST_Day_1km': '.QC_Day',
  34. '.LST_Night_1km': '.QC_Night'}
  35. lst6km_suff = {'.LST_Day_6km': '.QC_Day',
  36. '.LST_Night_6km': '.QC_Night'}
  37. # color for lst product
  38. lst_color = ['celsius']
  39. # values of vi product:
  40. vi_spec = '( 1 1 0 0 0 0 0 0 0 0 0 0 )'
  41. vi_specqa = '( 1 1 1 0 0 0 0 0 0 0 0 1 )'
  42. vi_color = ['ndvi', 'evi']
  43. vi250m_suff = {'.250m_16_days_NDVI': '.250m_16_days_VI_Quality',
  44. '.250m_16_days_EVI': '.250m_16_days_VI_Quality'}
  45. vi500m_suff = {'.500m_16_days_NDVI': '.500m_16_days_VI_Quality',
  46. '.500m_16_days_EVI': '.500m_16_days_VI_Quality'}
  47. vi1km_suff = {'.1_km_16_days_NDVI': '.1_km_16_days_VI_Quality',
  48. '.1_km_16_days_EVI': '.1_km_16_days_VI_Quality'}
  49. # values of snow product:
  50. snow1_spec = ('( 1 )')
  51. snow1_specqa = ('( 1 1 )')
  52. snow1_suff = {'.Snow_Cover_Daily_Tile': '.Snow_Spatial_QA'}
  53. snow8_spec = ('( 1 1 )')
  54. snow_color = ['gyr'] # TODO CREATE THE COLOR TABLE FOR MODIS_SNOW
  55. snow8_suff = {'.Maximum_Snow_Extent': None,
  56. '.Eight_Day_Snow_Cover': None}
  57. lstL2_spec = 'LST; QC; Error_LST; Emis_31; Emis_32; View_angle; View_time'
  58. # values of surface reflectance product:
  59. surf_spec = '( 1 1 1 1 1 1 1 0 0 0 0 0 0 )'
  60. surf_specqa = '( 1 1 1 1 1 1 1 1 0 0 0 0 0 )'
  61. surf_suff = {'.sur_refl_b01': '.sur_refl_qc_500m', '.sur_refl_b02':
  62. '.sur_refl_qc_500m', '.sur_refl_b03': '.sur_refl_qc_500m',
  63. '.sur_refl_b04': '.sur_refl_qc_500m', '.sur_refl_b05':
  64. '.sur_refl_qc_500m', '.sur_refl_b06': '.sur_refl_qc_500m',
  65. '.sur_refl_b07': '.sur_refl_qc_500m'}
  66. # value for water product
  67. water_spec = ('( 1 )')
  68. water_specqa = ('( 1 1 )')
  69. water_suff = {'.water_mask': '.water_mask_QA'}
  70. # granularity
  71. daily = 1
  72. eight = 8
  73. sixteen = 16
  74. self.prod = value
  75. lst = {'lst_aqua_daily_1000': {'url': urlbase, 'folder': 'MOLA/',
  76. 'prod': 'MYD11A1.005', 'days': daily,
  77. 'spec': lst_spec, 'spec_qa': lst_specqa,
  78. 'suff': lst1km_suff, 'res': 1000,
  79. 'color': lst_color},
  80. 'lst_terra_daily_1000': {'url': urlbase, 'folder': 'MOLT/',
  81. 'prod': 'MOD11A1.005', 'days': daily,
  82. 'spec': lst_spec, 'spec_qa': lst_specqa,
  83. 'suff': lst1km_suff, 'res': 1000,
  84. 'color': lst_color},
  85. 'lst_terra_eight_1000': {'url': urlbase, 'folder': 'MOLT/',
  86. 'prod': 'MOD11A2.005', 'days': eight,
  87. 'spec': lst_spec, 'spec_qa': lst_specqa,
  88. 'suff': lst1km_suff, 'res': 1000,
  89. 'color': lst_color},
  90. 'lst_aqua_eight_1000': {'url': urlbase, 'folder': 'MOLA/',
  91. 'prod': 'MYD11A2.005', 'days': eight,
  92. 'spec': lst_spec, 'spec_qa': lst_specqa,
  93. 'suff': lst1km_suff, 'res': 1000,
  94. 'color': lst_color},
  95. 'lst_terra_daily_6000': {'url': urlbase, 'folder': 'MOLT/',
  96. 'prod': 'MOD11B1.005', 'days': daily,
  97. 'spec': lst_spec, 'spec_qa': lst_specqa,
  98. 'suff': lst6km_suff, 'res': 6000,
  99. 'color': lst_color},
  100. 'lst_aqua_daily_6000': {'url': urlbase, 'folder': 'MOLA/',
  101. 'prod': 'MYD11B1.005', 'days': daily,
  102. 'spec': lst_spec, 'spec_qa': lst_specqa,
  103. 'suff': lst6km_suff, 'res': 6000,
  104. 'color': lst_color}
  105. }
  106. vi = {'ndvi_terra_sixteen_250': {'url': urlbase, 'folder': 'MOLT/',
  107. 'prod': 'MOD13Q1.005',
  108. 'spec': vi_spec, 'spec_qa': vi_specqa,
  109. 'suff': vi250m_suff, 'res': 250,
  110. 'color': vi_color, 'days': sixteen},
  111. 'ndvi_aqua_sixteen_250': {'url': urlbase, 'folder': 'MOLA/',
  112. 'prod': 'MYD13Q1.005',
  113. 'spec': vi_spec, 'spec_qa': vi_specqa,
  114. 'suff': vi250m_suff, 'res': 250,
  115. 'color': vi_color, 'days': sixteen},
  116. 'ndvi_terra_sixteen_500': {'url': urlbase, 'folder': 'MOLT/',
  117. 'prod': 'MOD13A1.005',
  118. 'spec': vi_spec, 'spec_qa': vi_specqa,
  119. 'suff': vi1km_suff, 'res': 500,
  120. 'color': vi_color, 'days': sixteen},
  121. 'ndvi_aqua_sixteen_500': {'url': urlbase, 'folder': 'MOLA/',
  122. 'prod': 'MYD13A1.005',
  123. 'spec': vi_spec, 'spec_qa': vi_specqa,
  124. 'suff': vi500m_suff, 'res': 500,
  125. 'color': vi_color, 'days': sixteen},
  126. 'ndvi_terra_sixteen_1000': {'url': urlbase, 'folder': 'MOLT/',
  127. 'prod': 'MOD13A2.005',
  128. 'spec': vi_spec, 'spec_qa': vi_specqa,
  129. 'suff': vi500m_suff, 'res': 1000,
  130. 'color': vi_color, 'days': sixteen},
  131. 'ndvi_aqua_sixteen_1000': {'url': urlbase, 'folder': 'MOLA/',
  132. 'prod': 'MYD13A2.005',
  133. 'spec': vi_spec, 'spec_qa': vi_specqa,
  134. 'suff': vi1km_suff, 'res': 1000,
  135. 'color': vi_color, 'days': sixteen}
  136. }
  137. surf_refl = {'surfreflec_terra_eight_500': {'url': urlbase,
  138. 'folder': 'MOLT/',
  139. 'prod': 'MOD09A1.005',
  140. 'spec': surf_spec,
  141. 'spec_qa': surf_specqa,
  142. 'res': 500, 'days': eight,
  143. 'color': snow_color,
  144. 'suff': surf_suff},
  145. 'surfreflec_aqua_eight_500': {'url': urlbase,
  146. 'folder': 'MOLA/',
  147. 'prod': 'MYD09A1.005',
  148. 'spec': surf_spec,
  149. 'spec_qa': surf_specqa,
  150. 'res': 500, 'days': eight,
  151. 'color': snow_color,
  152. 'suff': surf_suff}
  153. }
  154. snow = {'snow_terra_daily_500': {'url': usrsnow, 'folder': 'SAN/MOST/',
  155. 'prod': 'MOD10A1.005',
  156. 'spec': snow1_spec, 'days': daily,
  157. 'spec_qa': snow1_specqa,
  158. 'color': snow_color,
  159. 'suff': snow1_suff, 'res': 500},
  160. 'snow_aqua_daily_500': {'url': usrsnow,
  161. 'folder': 'SAN/MOSA/',
  162. 'prod': 'MYD10A1.005',
  163. 'spec': snow1_spec, 'days': daily,
  164. 'spec_qa': snow1_specqa,
  165. 'color': snow_color,
  166. 'suff': snow1_suff, 'res': 500},
  167. 'snow_terra_eight_500': {'url': usrsnow,
  168. 'folder': 'SAN/MOST/',
  169. 'prod': 'MOD10A2.005',
  170. 'spec': snow8_spec,
  171. 'spec_qa': None, 'days': eight,
  172. 'color': snow_color,
  173. 'suff': snow8_suff, 'res': 500},
  174. 'snow_aqua_eight_500': {'url': usrsnow,
  175. 'folder': 'SAN/MOSA/',
  176. 'prod': 'MYD10A2.005',
  177. 'spec': snow8_spec,
  178. 'spec_qa': None, 'days': eight,
  179. 'color': snow_color,
  180. 'suff': snow8_suff, 'res': 500}
  181. }
  182. water = {'water_terra_250': {'url': urlbase, 'folder': 'MOLT/',
  183. 'prod': 'MOD44W.005', 'spec': water_spec,
  184. 'spec_qa': water_specqa, 'res': 250,
  185. 'suff': water_suff, 'days': daily,
  186. 'color': snow_color}
  187. }
  188. self.products = {}
  189. self.products.update(lst)
  190. self.products.update(vi)
  191. self.products.update(snow)
  192. self.products.update(surf_refl)
  193. self.products.update(water)
  194. self.products_swath = {'lst_terra_daily': {'url': urlbase,
  195. 'folder': 'MOLT/',
  196. 'prod': 'MOD11_L2.005',
  197. 'spec': lstL2_spec},
  198. 'lst_aqua_daily': {'url': urlbase,
  199. 'folder': 'MOLA/',
  200. 'prod': 'MYD11_L2.005',
  201. 'spec': lstL2_spec}
  202. }
  203. def returned(self):
  204. if self.products.keys().count(self.prod) == 1:
  205. return self.products[self.prod]
  206. elif self.products_swath.keys().count(self.prod) == 1:
  207. return self.products_swath[self.prod]
  208. else:
  209. raise Exception ("The code insert is not supported yet. Consider "
  210. "to ask on the grass-dev mailing list for future "
  211. "support")
  212. def fromcode(self, code):
  213. import string
  214. for k, v in self.products.iteritems():
  215. if string.find(v['prod'], code) != -1:
  216. return self.products[k]
  217. for k, v in self.products_swath.iteritems():
  218. if string.find(v['prod'], code) != -1:
  219. return self.products_swath[k]
  220. raise Exception("The code insert is not supported yet. Consider to "
  221. "open a ticket in https://github.com/lucadelu/pyModis/issues")
  222. def color(self, code=None):
  223. if code:
  224. return self.fromcode(code)['color']
  225. else:
  226. return self.returned()['color']
  227. def suffix(self, code=None):
  228. if code:
  229. return self.fromcode(code)['suff']
  230. else:
  231. return self.returned()['suff']
  232. def __str__(self):
  233. prod = self.returned()
  234. string = "url: " + prod['url'] + ", folder: " + prod['folder']
  235. if prod.keys().count('spec') == 1:
  236. string += ", spectral subset: " + prod['spec']
  237. if prod.keys().count('spec_qa') == 1:
  238. string += ", spectral subset qa:" + prod['spec_qa']
  239. return string