[GRASS-SVN] r66575 - in grass-addons/grass7/raster/r.modis: libmodis r.modis.import

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Oct 22 05:20:30 PDT 2015


Author: lucadelu
Date: 2015-10-22 05:20:30 -0700 (Thu, 22 Oct 2015)
New Revision: 66575

Modified:
   grass-addons/grass7/raster/r.modis/libmodis/rmodislib.py
   grass-addons/grass7/raster/r.modis/r.modis.import/r.modis.import.py
Log:
r.modis: PEP8 cleanup; update r.modis.import for the next t.rast.modis

Modified: grass-addons/grass7/raster/r.modis/libmodis/rmodislib.py
===================================================================
--- grass-addons/grass7/raster/r.modis/libmodis/rmodislib.py	2015-10-22 10:37:47 UTC (rev 66574)
+++ grass-addons/grass7/raster/r.modis/libmodis/rmodislib.py	2015-10-22 12:20:30 UTC (rev 66575)
@@ -55,33 +55,36 @@
         # url to download products
         urlbase = 'http://e4ftl01.cr.usgs.gov'
         usrsnow = 'ftp://n4ftl01u.ecs.nasa.gov'
-        ### values of lst product:
+        # values of lst product:
         lst_spec = '( 1 0 0 0 1 0 0 0 0 0 0 0 )'
         lst_specqa = '( 1 1 0 0 1 1 0 0 0 0 0 0 )'
         # suffix for the lst product (key is the lst map, value the QA)
-        lst1km_suff = {'.LST_Day_1km': '.QC_Day', '.LST_Night_1km': '.QC_Night'}
-        lst6km_suff = {'.LST_Day_6km': '.QC_Day', '.LST_Night_6km': '.QC_Night'}
+        lst1km_suff = {'.LST_Day_1km': '.QC_Day',
+                       '.LST_Night_1km': '.QC_Night'}
+        lst6km_suff = {'.LST_Day_6km': '.QC_Day',
+                       '.LST_Night_6km': '.QC_Night'}
         # color for lst product
         lst_color = ['celsius']
-        ### values of vi product:
+        # values of vi product:
         vi_spec = '( 1 1 0 0 0 0 0 0 0 0 0 0 )'
         vi_specqa = '( 1 1 1 0 0 0 0 0 0 0 0 1 )'
         vi_color = ['ndvi', 'evi']
         vi250m_suff = {'.250m_16_days_NDVI': '.250m_16_days_VI_Quality',
-        '.250m_16_days_EVI': '.250m_16_days_VI_Quality'}
+                       '.250m_16_days_EVI': '.250m_16_days_VI_Quality'}
         vi500m_suff = {'.500m_16_days_NDVI': '.500m_16_days_VI_Quality',
-        '.500m_16_days_EVI': '.500m_16_days_VI_Quality'}
+                       '.500m_16_days_EVI': '.500m_16_days_VI_Quality'}
         vi1km_suff = {'.1_km_16_days_NDVI': '.1_km_16_days_VI_Quality',
-        '.1_km_16_days_EVI': '.1_km_16_days_VI_Quality'}
-        ### values of snow product:
+                      '.1_km_16_days_EVI': '.1_km_16_days_VI_Quality'}
+        # values of snow product:
         snow1_spec = ('( 1 )')
         snow1_specqa = ('( 1 1 )')
         snow1_suff = {'.Snow_Cover_Daily_Tile': '.Snow_Spatial_QA'}
         snow8_spec = ('( 1 1 )')
         snow_color = ['gyr']  # TODO CREATE THE COLOR TABLE FOR MODIS_SNOW
-        snow8_suff = {'.Maximum_Snow_Extent': None, '.Eight_Day_Snow_Cover': None}
+        snow8_suff = {'.Maximum_Snow_Extent': None,
+                      '.Eight_Day_Snow_Cover': None}
         lstL2_spec = 'LST; QC; Error_LST; Emis_31; Emis_32; View_angle; View_time'
-        ### values of surface reflectance product:
+        # values of surface reflectance product:
         surf_spec = '( 1 1 1 1 1 1 1 0 0 0 0 0 0 )'
         surf_specqa = '( 1 1 1 1 1 1 1 1 0 0 0 0 0 )'
         surf_suff = {'.sur_refl_b01': '.sur_refl_qc_500m', '.sur_refl_b02':
@@ -89,147 +92,118 @@
                      '.sur_refl_b04': '.sur_refl_qc_500m', '.sur_refl_b05':
                      '.sur_refl_qc_500m', '.sur_refl_b06': '.sur_refl_qc_500m',
                      '.sur_refl_b07': '.sur_refl_qc_500m'}
-
+        # granularity
+        daily = 1
+        eight = 8
+        sixteen = 16
         self.prod = value
-        lst = {'lst_aqua_daily_1000': {'url': urlbase,
-                                       'folder': 'MOLA/',
-                                       'prod': 'MYD11A1.005',
+        lst = {'lst_aqua_daily_1000': {'url': urlbase, 'folder': 'MOLA/',
+                                       'prod': 'MYD11A1.005', 'days': daily,
                                        'spec': lst_spec, 'spec_qa': lst_specqa,
                                        'suff': lst1km_suff, 'res': 1000,
-                                       'color': lst_color
-              },
-              'lst_terra_daily_1000': {'url': urlbase,
-                                       'folder': 'MOLT/',
-                                       'prod': 'MOD11A1.005',
+                                       'color': lst_color},
+               'lst_terra_daily_1000': {'url': urlbase, 'folder': 'MOLT/',
+                                        'prod': 'MOD11A1.005', 'days': daily,
+                                        'spec': lst_spec, 'spec_qa': lst_specqa,
+                                        'suff': lst1km_suff, 'res': 1000,
+                                        'color': lst_color},
+               'lst_terra_eight_1000': {'url': urlbase, 'folder': 'MOLT/',
+                                        'prod': 'MOD11A2.005', 'days': eight,
+                                        'spec': lst_spec, 'spec_qa': lst_specqa,
+                                        'suff': lst1km_suff, 'res': 1000,
+                                        'color': lst_color},
+               'lst_aqua_eight_1000': {'url': urlbase, 'folder': 'MOLA/',
+                                       'prod': 'MYD11A2.005', 'days': eight,
                                        'spec': lst_spec, 'spec_qa': lst_specqa,
                                        'suff': lst1km_suff, 'res': 1000,
-                                       'color': lst_color
-              },
-              'lst_terra_eight_1000': {'url': urlbase,
-                                       'folder': 'MOLT/',
-                                       'prod': 'MOD11A2.005',
+                                       'color': lst_color},
+               'lst_terra_daily_6000': {'url': urlbase, 'folder': 'MOLT/',
+                                        'prod': 'MOD11B1.005', 'days': daily,
+                                        'spec': lst_spec, 'spec_qa': lst_specqa,
+                                        'suff': lst6km_suff, 'res': 6000,
+                                        'color': lst_color},
+               'lst_aqua_daily_6000': {'url': urlbase, 'folder': 'MOLA/',
+                                       'prod': 'MYD11B1.005', 'days': daily,
                                        'spec': lst_spec, 'spec_qa': lst_specqa,
-                                       'suff': lst1km_suff, 'res': 1000,
-                                       'color': lst_color
-              },
-              'lst_aqua_eight_1000': {'url': urlbase,
-                                      'folder': 'MOLA/',
-                                      'prod': 'MYD11A2.005',
-                                      'spec': lst_spec, 'spec_qa': lst_specqa,
-                                      'suff': lst1km_suff, 'res': 1000,
-                                      'color': lst_color
-              },
-              'lst_terra_daily_6000': {'url': urlbase,
-                                       'folder': 'MOLT/',
-                                       'prod': 'MOD11B1.005',
-                                       'spec': lst_spec, 'spec_qa': lst_specqa,
                                        'suff': lst6km_suff, 'res': 6000,
-                                       'color': lst_color
-              },
-              'lst_aqua_daily_6000': {'url': urlbase,
-                                      'folder': 'MOLA/',
-                                      'prod': 'MYD11B1.005',
-                                      'spec': lst_spec, 'spec_qa': lst_specqa,
-                                      'suff': lst6km_suff, 'res': 6000,
-                                      'color': lst_color
-              },
-
-        }
-        vi = {'ndvi_terra_sixteen_250': {'url': urlbase,
-                                         'folder': 'MOLT/',
+                                       'color': lst_color}
+               }
+        vi = {'ndvi_terra_sixteen_250': {'url': urlbase, 'folder': 'MOLT/',
                                          'prod': 'MOD13Q1.005',
                                          'spec': vi_spec, 'spec_qa': vi_specqa,
                                          'suff': vi250m_suff, 'res': 250,
-                                         'color': vi_color
-              },
-              'ndvi_aqua_sixteen_250': {'url': urlbase,
-                                        'folder': 'MOLA/',
+                                         'color': vi_color, 'days': sixteen},
+              'ndvi_aqua_sixteen_250': {'url': urlbase, 'folder': 'MOLA/',
                                         'prod': 'MYD13Q1.005',
                                         'spec': vi_spec, 'spec_qa': vi_specqa,
                                         'suff': vi250m_suff, 'res': 250,
-                                        'color': vi_color
-              },
-              'ndvi_terra_sixteen_500': {'url': urlbase,
-                                         'folder': 'MOLT/',
+                                        'color': vi_color, 'days': sixteen},
+              'ndvi_terra_sixteen_500': {'url': urlbase, 'folder': 'MOLT/',
                                          'prod': 'MOD13A1.005',
                                          'spec': vi_spec, 'spec_qa': vi_specqa,
                                          'suff': vi1km_suff, 'res': 500,
-                                         'color': vi_color
-              },
-              'ndvi_aqua_sixteen_500': {'url': urlbase,
-                                        'folder': 'MOLA/',
+                                         'color': vi_color, 'days': sixteen},
+              'ndvi_aqua_sixteen_500': {'url': urlbase, 'folder': 'MOLA/',
                                         'prod': 'MYD13A1.005',
                                         'spec': vi_spec, 'spec_qa': vi_specqa,
                                         'suff': vi500m_suff, 'res': 500,
-                                        'color': vi_color
-              },
-              'ndvi_terra_sixteen_1000': {'url': urlbase,
-                                          'folder': 'MOLT/',
+                                        'color': vi_color, 'days': sixteen},
+              'ndvi_terra_sixteen_1000': {'url': urlbase, 'folder': 'MOLT/',
                                           'prod': 'MOD13A2.005',
                                           'spec': vi_spec, 'spec_qa': vi_specqa,
                                           'suff': vi500m_suff, 'res': 1000,
-                                          'color': vi_color
-              },
-              'ndvi_aqua_sixteen_1000': {'url': urlbase,
-                                         'folder': 'MOLA/',
+                                          'color': vi_color, 'days': sixteen},
+              'ndvi_aqua_sixteen_1000': {'url': urlbase, 'folder': 'MOLA/',
                                          'prod': 'MYD13A2.005',
                                          'spec': vi_spec, 'spec_qa': vi_specqa,
                                          'suff': vi1km_suff, 'res': 1000,
-                                         'color': vi_color
+                                         'color': vi_color, 'days': sixteen}
               }
-        }
         surf_refl = {'surfreflec_terra_eight_500': {'url': urlbase,
                                                     'folder': 'MOLT/',
                                                     'prod': 'MOD09A1.005',
                                                     'spec': surf_spec,
                                                     'spec_qa': surf_specqa,
-                                                    'res': 500,
+                                                    'res': 500, 'days': eight,
                                                     'color': snow_color,
-                                                    'suff': surf_suff
-                                                   },
+                                                    'suff': surf_suff},
                      'surfreflec_aqua_eight_500': {'url': urlbase,
                                                    'folder': 'MOLA/',
                                                    'prod': 'MYD09A1.005',
                                                    'spec': surf_spec,
                                                    'spec_qa': surf_specqa,
-                                                   'res': 500,
+                                                   'res': 500, 'days': eight,
                                                    'color': snow_color,
-                                                   'suff': surf_suff
-                                                  }
-        }
-        snow = {'snow_terra_daily_500': {'url': usrsnow,
-                                         'folder': 'SAN/MOST/',
+                                                   'suff': surf_suff}
+                     }
+        snow = {'snow_terra_daily_500': {'url': usrsnow, 'folder': 'SAN/MOST/',
                                          'prod': 'MOD10A1.005',
-                                         'spec': snow1_spec,
+                                         'spec': snow1_spec, 'days': daily,
                                          'spec_qa': snow1_specqa,
                                          'color': snow_color,
-                                         'suff': snow1_suff, 'res': 500
-               },
-               'snow_aqua_daily_500': {'url': usrsnow,
-                                       'folder': 'SAN/MOSA/',
-                                       'prod': 'MYD10A1.005',
-                                       'spec': snow1_spec,
-                                       'spec_qa': snow1_specqa,
-                                       'color': snow_color,
-                                       'suff' : snow1_suff, 'res' : 500
-               },
-               'snow_terra_eight_500': {'url': usrsnow,
-                                        'folder': 'SAN/MOST/',
-                                        'prod': 'MOD10A2.005',
-                                        'spec': snow8_spec,
-                                        'spec_qa': None,
+                                         'suff': snow1_suff, 'res': 500},
+                'snow_aqua_daily_500': {'url': usrsnow,
+                                        'folder': 'SAN/MOSA/',
+                                        'prod': 'MYD10A1.005',
+                                        'spec': snow1_spec, 'days': daily,
+                                        'spec_qa': snow1_specqa,
                                         'color': snow_color,
-                                        'suff': snow8_suff, 'res' : 500
-                },
-                'snow_aqua_eight_500' : {'url': usrsnow,
-                                         'folder': 'SAN/MOSA/',
-                                         'prod': 'MYD10A2.005',
+                                        'suff': snow1_suff, 'res': 500},
+                'snow_terra_eight_500': {'url': usrsnow,
+                                         'folder': 'SAN/MOST/',
+                                         'prod': 'MOD10A2.005',
                                          'spec': snow8_spec,
-                                         'spec_qa': None,
+                                         'spec_qa': None, 'days': eight,
                                          'color': snow_color,
-                                         'suff' : snow8_suff, 'res' : 500
+                                         'suff': snow8_suff, 'res': 500},
+                'snow_aqua_eight_500': {'url': usrsnow,
+                                        'folder': 'SAN/MOSA/',
+                                        'prod': 'MYD10A2.005',
+                                        'spec': snow8_spec,
+                                        'spec_qa': None, 'days': eight,
+                                        'color': snow_color,
+                                        'suff': snow8_suff, 'res': 500}
                 }
-        }
         self.products = {}
         self.products.update(lst)
         self.products.update(vi)
@@ -243,7 +217,7 @@
                                                   'folder': 'MOLA/',
                                                   'prod': 'MYD11_L2.005',
                                                   'spec': lstL2_spec}
-        }
+                               }
 
     def returned(self):
         if self.products.keys().count(self.prod) == 1:
@@ -251,8 +225,9 @@
         elif self.products_swath.keys().count(self.prod) == 1:
             return self.products_swath[self.prod]
         else:
-            grass.fatal(_("The code insert is not supported yet. Consider to " \
-                      "ask on the grass-dev mailing list for future support"))
+            grass.fatal(_("The code insert is not supported yet. Consider "
+                          "to ask on the grass-dev mailing list for future "
+                          "support"))
 
     def fromcode(self, code):
         import string
@@ -262,7 +237,7 @@
         for k, v in self.products_swath.iteritems():
             if string.find(v['prod'], code) != -1:
                 return self.products_swath[k]
-        grass.fatal(_("The code insert is not supported yet. Consider to " \
+        grass.fatal(_("The code insert is not supported yet. Consider to "
                       "ask on the grass-dev mailing list for future support"))
 
     def color(self, code=None):
@@ -398,9 +373,9 @@
             FE = self._par('+x_0')
             FN = self._par('+y_0')
             return self._outpar(SMajor, SMinor, 0.0, Factor, CentMer,
-                                 TrueScale, FE, FN, swath)
+                                TrueScale, FE, FN, swath)
         else:
-            grass.fatal(_('Projection not supported, please contact the ' \
+            grass.fatal(_('Projection not supported, please contact the '
                           'GRASS-dev mailing list'))
 
     def datum(self):
@@ -418,4 +393,4 @@
 
     def utmzone(self):
         """Return the utm zone number"""
-        return self.proj['zone']
\ No newline at end of file
+        return self.proj['zone']

Modified: grass-addons/grass7/raster/r.modis/r.modis.import/r.modis.import.py
===================================================================
--- grass-addons/grass7/raster/r.modis/r.modis.import/r.modis.import.py	2015-10-22 10:37:47 UTC (rev 66574)
+++ grass-addons/grass7/raster/r.modis/r.modis.import/r.modis.import.py	2015-10-22 12:20:30 UTC (rev 66575)
@@ -22,7 +22,7 @@
 #############################################################################
 
 #%module
-#% description: Import single or multiple tiles of MODIS products using pyModis/MRT
+#% description: Import single or multiple tiles of MODIS products using pyModis
 #% keyword: raster
 #% keyword: MODIS
 #%end
@@ -38,6 +38,11 @@
 #% key: q
 #% description: Ignore the QA map layer
 #%end
+#%flag
+#% key: w
+#% description: Create a text file to use into t.rast.modis
+#%end
+
 #%option
 #% key: input
 #% type: string
@@ -90,8 +95,9 @@
 import shutil
 import grass.script as grass
 from datetime import date
+from datetime import timedelta
 from grass.pygrass.utils import get_lib_path
-
+import tempfile
 path = get_lib_path(modname='r.modis', libname='libmodis')
 if path is None:
     grass.fatal("Not able to find the modis library directory.")
@@ -216,7 +222,7 @@
     dataobj = date(int(data[0]), int(data[1]), int(data[2]))
     grass.run_command('r.timestamp', map=mapp, quiet=True,
                       date=dataobj.strftime("%d %b %Y"))
-    return 0
+    return dataobj
     # color
 #    if string.find(mapp, 'QC') != -1 or string.find(mapp, 'Quality') != -1 or \
 #    string.find(mapp, 'QA') != -1:
@@ -231,12 +237,22 @@
 #        grass.run_command('r.colors', quiet=True, map=mapp, color=coll[0])
 
 
-def import_tif(out, basedir, rem, write, pm, target=None):
+def modis_prefix(inp, mosaic=False):
+    """return the modis prefix"""
+    modlist = os.path.split(inp)[1].split('.')
+    if mosaic:
+        return '.'.join(modlist[:2])
+    else:
+        return '.'.join(modlist[:3])
+
+
+def import_tif(out, basedir, rem, write, pm, prod, target=None, listfile=None):
     """Import TIF files"""
     # list of tif files
-    tifiles = glob.glob1(basedir, "*.tif")
+    pref = modis_prefix(pm.hdfname)
+    tifiles = glob.glob1(basedir, "{pr}*.tif".format(pr=pref))
     if not tifiles:
-        tifiles = glob.glob1(os.getcwd(), "*.tif")
+        tifiles = glob.glob1(os.getcwd(), "{pr}*.tif".format(pr=pref))
     if not tifiles:
         grass.fatal(_('Error during the conversion'))
     # check if user is in latlong location to set flag l
@@ -248,11 +264,12 @@
     # for each file import it
     for t in tifiles:
         basename = os.path.splitext(t)[0]
+        basename = basename.replace(' ', '_')
         name = os.path.join(basedir, t)
         if not os.path.exists(name):
             name = os.path.join(os.getcwd(), t)
         if not os.path.exists(name):
-            grass.warning(_("File %s doesn't find" % basename))
+            grass.warning(_("File %s doesn't find" % name))
             continue
         filesize = int(os.path.getsize(name))
         if filesize < 1000:
@@ -266,12 +283,17 @@
         except:
             grass.warning(_('Error during import of %s' % basename))
             continue
-        metadata(pm, basename)
+        data = metadata(pm, basename)
         if rem:
             os.remove(name)
         if target:
             if target != basedir:
                 shutil.move(name, target)
+        if listfile:
+            fdata = data + timedelta(prod['days'])
+            listfile.write("{name}|{sd}|{fd}\n".format(name=basename,
+                                                       sd=data.strftime("%Y-%m-%d"),
+                                                       fd=fdata.strftime("%Y-%m-%d")))
     return outfile
 
 
@@ -283,7 +305,7 @@
         grass.warning(_("Raster map <%s> not found") % (pref + suff))
 
 
-def single(options, remove, an, ow):
+def single(options, remove, an, ow, fil):
     """Convert the HDF file to TIF and import it
     """
     listfile, basedir = list_files(options)
@@ -313,10 +335,10 @@
                 res = int(prod['res']) * int(projObj.proj['meters'])
             else:
                 res = None
-            prod = product().fromcode(pref.split('.')[0])
             outname = "%s.%s.%s.single" % (pref.split('.')[0],
                                            pref.split('.')[1],
                                            pref.split('.')[2])
+            outname = outname.replace(' ', '_')
             execmodis = convertModisGDAL(hdf, outname, spectr, res,
                                          wkt=projwkt)
         execmodis.run()
@@ -324,12 +346,14 @@
         if not output:
             output = os.path.split(hdf)[1].rstrip('.hdf')
         # import tif files
-        import_tif(output, basedir, remove, ow, pm)
+        output = output.replace(' ', '_')
+        import_tif(out=output, basedir=basedir, rem=remove, write=ow, pm=pm,
+                   listfile=fil, prod=prod)
         if options['mrtpath']:
             os.remove(confname)
 
 
-def mosaic(options, remove, an, ow):
+def mosaic(options, remove, an, ow, fil):
     """Create a daily mosaic of HDF files convert to TIF and import it
     """
     dictfile, targetdir = list_files(options, True)
@@ -341,6 +365,7 @@
         spectr = spectral(options, prod, an)
         spectr = spectr.lstrip('( ').rstrip(' )')
         outname = "%s.%s.mosaic" % (pref.split('.')[0], pref.split('.')[1])
+        outname = outname.replace(' ', '_')
         # create mosaic
         if options['mrtpath']:
             # create the file with the list of name
@@ -382,13 +407,15 @@
             # remove hdf
             if remove:
                 # import tif files
-                import_tif(outname, basedir, remove, ow, pm)
+                import_tif(out=outname, basedir=basedir, rem=remove, write=ow,
+                           pm=pm, listfile=fil, prod=prod)
                 os.remove(hdf)
                 os.remove(hdf + '.xml')
             # move the hdf and hdf.xml to the dir where are the original files
             else:
                 # import tif files
-                import_tif(outname, basedir, remove, ow, pm, targetdir)
+                import_tif(out=outname, basedir=basedir, rem=remove, write=ow,
+                           pm=pm, target=targetdir, listfile=fil, prod=prod)
                 try:
                     shutil.move(hdf, targetdir)
                     shutil.move(hdf + '.xml', targetdir)
@@ -439,15 +466,30 @@
         analyze = False
     else:
         analyze = True
+    if options['spectral']:
+        count = options['spectral'].strip('(').strip(')').split().count('1')
+    else:
+        count = 0
+    outfile = None
+    if flags['w'] and count == 1:
+        outfile = tempfile.NamedTemporaryFile(delete=False)
+    elif flags['w'] and count != 1:
+        grass.warning(_("To use correctly the file in t.rast.import you have "
+                        "to select only a subset in the 'spectral' option. "
+                        "Out file will be not created"))
     # check if import simple file or mosaic
     if flags['m'] and options['input'] != '':
         grass.fatal(_('It is not possible to create a mosaic with a single'
                       ' HDF file'))
         return 0
     elif flags['m']:
-        mosaic(options, remove, analyze, over)
+        mosaic(options, remove, analyze, over, outfile)
     else:
-        single(options, remove, analyze, over)
+        single(options, remove, analyze, over, outfile)
+    if outfile:
+        outfile.close()
+        grass.message(_("You can continue with t.rast.modis "
+                        "'input={name}'".format(name=outfile.name)))
 
 if __name__ == "__main__":
     options, flags = grass.parser()



More information about the grass-commit mailing list