def confile(pm, opts, q, mosaik=False): """Create the configuration file for MRT software""" try: # try to import pymodis (modis) and some classes for i.modis.download from rmodislib import resampling, product, projection except ImportError as e: grass.fatal("Unable to load i.modis library: {}".format(e)) # return projection and datum projObj = projection() proj = projObj.returned() dat = projObj.datum() if proj == "UTM": zone = projObj.utmzone() else: zone = None cod = os.path.split(pm.hdfname)[1].split(".")[0] prod = product().fromcode(cod) if mosaik: # if mosaic it remove all the 0 from the subset string to convert all # the right layer spectr = spectral(opts, prod, q, True) else: spectr = spectral(opts, prod, q) # out prefix pref = modis_prefix(pm.hdfname) # resampling resampl = resampling(opts["method"]).returned() # projpar projpar = projObj.return_params() if projpar != "( 0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 )": dat = "NODATUM" # resolution if proj != "GEO": res = int(prod["res"]) * int(projObj.proj["meters"]) else: res = None try: conf = pm.confResample(spectr, res, pref, dat, resampl, proj, zone, projpar) return conf except IOError as e: grass.fatal(e)
def mosaic(options, remove, an, ow, fil): """Create a daily mosaic of HDF files convert to TIF and import it""" try: # try to import pymodis (modis) and some classes for i.modis.download from rmodislib import product, projection, get_proj except ImportError as e: grass.fatal("Unable to load i.modis library: {}".format(e)) try: from pymodis.convertmodis import convertModis, createMosaic from pymodis.convertmodis_gdal import createMosaicGDAL, convertModisGDAL from pymodis.parsemodis import parseModis except ImportError as e: grass.fatal("Unable to import pymodis library: {}".format(e)) dictfile, targetdir = list_files(options, True) pid = str(os.getpid()) # for each day count = len(dictfile.keys()) idx = 1 for dat, listfiles in dictfile.items(): grass.message( _("Processing <{d}> ({i}/{c})...").format(d=dat, i=idx, c=count)) grass.percent(idx, count, 5) idx += 1 pref = listfiles[0].split(os.path.sep)[-1] prod = product().fromcode(pref.split(".")[0]) 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 tempfile = open(os.path.join(targetdir, pid), "w") tempfile.writelines(listfiles) tempfile.close() # basedir of tempfile, where hdf files are write basedir = os.path.split(tempfile.name)[0] # return the spectral subset in according mrtmosaic tool format cm = createMosaic(tempfile.name, outname, options["mrtpath"], spectr) cm.run() hdfiles = glob.glob1(basedir, outname + "*.hdf") else: basedir = targetdir listfiles = [os.path.join(basedir, i) for i in listfiles] cm = createMosaicGDAL(listfiles, spectr) try: cm.write_vrt(os.path.join(basedir, outname), quiet=True) except: cm.write_vrt(os.path.join(basedir, outname)) hdfiles = glob.glob1(basedir, outname + "*.vrt") for i in hdfiles: # the full path to hdf file hdf = os.path.join(basedir, i) try: pm = parseModis(hdf) except: out = i.replace(".vrt", "") data = doy2date(dat[1:]) pm = grassParseModis(out, data) # create convertModis class and convert it in tif file if options["mrtpath"]: # create conf file fro mrt tools confname = confile(pm, options, an, True) execmodis = convertModis(hdf, confname, options["mrtpath"]) else: confname = None projwkt = get_proj("w") projObj = projection() if projObj.returned() != "GEO": res = int(prod["res"]) * int(projObj.proj["meters"]) else: res = None execmodis = convertModisGDAL(str(hdf), out, spectr, res, wkt=str(projwkt), vrt=True) # produce temporary files in input folder os.chdir(basedir) try: execmodis.run(quiet=True) except: execmodis.run() # remove hdf if remove: # import tif files import_tif( basedir=basedir, rem=remove, write=ow, pm=pm, listfile=fil, prod=prod, ) try: os.remove(hdf) os.remove(hdf + ".xml") except OSError: pass # move the hdf and hdf.xml to the dir where are the original files else: # import tif files import_tif( basedir=basedir, rem=remove, write=ow, pm=pm, target=targetdir, listfile=fil, prod=prod, ) if i not in os.listdir(targetdir): try: shutil.move(hdf, targetdir) shutil.move(hdf + ".xml", targetdir) except OSError: pass # remove the conf file try: os.remove(confname) except (OSError, TypeError) as e: pass if options["mrtpath"]: grass.try_remove(tempfile.name) grass.try_remove(os.path.join(targetdir, "mosaic", pid))
def single(options, remove, an, ow, fil): """Convert the HDF file to TIF and import it""" try: # try to import pymodis (modis) and some classes for i.modis.download from rmodislib import product, projection, get_proj except ImportError as e: grass.fatal("Unable to load i.modis library: {}".format(e)) try: from pymodis.convertmodis import convertModis from pymodis.convertmodis_gdal import convertModisGDAL from pymodis.parsemodis import parseModis except ImportError as e: grass.fatal("Unable to import pymodis library: {}".format(e)) listfile, basedir = list_files(options) if not listfile: grass.warning(_("No HDF files found")) return # for each file count = len(listfile) idx = 1 for i in listfile: if os.path.exists(i): hdf = i else: # the full path to hdf file hdf = os.path.join(basedir, i) if not os.path.exists(hdf): grass.warning(_("%s not found" % i)) continue grass.message( _("Proccessing <{f}> ({i}/{c})...").format(f=os.path.basename(hdf), i=idx, c=count)) grass.percent(idx, count, 5) idx += 1 try: pm = parseModis(hdf) except OSError: grass.fatal(_("<{}> is not a HDF file").format(hdf)) continue if options["mrtpath"]: # create conf file fro mrt tools confname = confile(pm, options, an) # create convertModis class and convert it in tif file execmodis = convertModis(hdf, confname, options["mrtpath"]) else: projwkt = get_proj("w") projObj = projection() pref = i.split(os.path.sep)[-1] prod = product().fromcode(pref.split(".")[0]) spectr = spectral(options, prod, an) if projObj.returned() != "GEO": res = int(prod["res"]) * int(projObj.proj["meters"]) else: res = None outname = "%s.%s.%s.single" % ( pref.split(".")[0], pref.split(".")[1], pref.split(".")[2], ) outname = outname.replace(" ", "_") execmodis = convertModisGDAL(str(hdf), outname, spectr, res, wkt=str(projwkt)) # produce temporary files in input folder os.chdir(basedir) try: execmodis.run(quiet=True) except: execmodis.run() import_tif(basedir=basedir, rem=remove, write=ow, pm=pm, listfile=fil, prod=prod) if options["mrtpath"]: os.remove(confname)
def mosaic(options, remove, an, ow, fil): """Create a daily mosaic of HDF files convert to TIF and import it """ try: # try to import pymodis (modis) and some classes for i.modis.download from rmodislib import product, projection, get_proj except: grass.fatal("i.modis library is not installed") try: from pymodis.convertmodis import convertModis, createMosaic from pymodis.convertmodis_gdal import createMosaicGDAL, convertModisGDAL from pymodis.parsemodis import parseModis except: grass.fatal("pymodis library is not installed") dictfile, targetdir = list_files(options, True) pid = str(os.getpid()) # for each day for dat, listfiles in dictfile.items(): pref = listfiles[0].split('/')[-1] prod = product().fromcode(pref.split('.')[0]) 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 tempfile = open(os.path.join(targetdir, pid), 'w') tempfile.writelines(listfiles) tempfile.close() # basedir of tempfile, where hdf files are write basedir = os.path.split(tempfile.name)[0] # return the spectral subset in according mrtmosaic tool format cm = createMosaic(tempfile.name, outname, options['mrtpath'], spectr) cm.run() hdfiles = glob.glob1(basedir, outname + "*.hdf") else: basedir = targetdir listfiles = [os.path.join(basedir, i) for i in listfiles] cm = createMosaicGDAL(listfiles, spectr) try: cm.write_vrt(os.path.join(basedir, outname), quiet=True) except: cm.write_vrt(os.path.join(basedir, outname)) hdfiles = glob.glob1(basedir, outname + "*.vrt") for i in hdfiles: # the full path to hdf file hdf = os.path.join(basedir, i) try: pm = parseModis(hdf) except: out = i.replace('.vrt', '') data = doy2date(dat[1:]) pm = grassParseModis(out, data) # create convertModis class and convert it in tif file if options['mrtpath']: # create conf file fro mrt tools confname = confile(pm, options, an, True) execmodis = convertModis(hdf, confname, options['mrtpath']) else: confname = None projwkt = get_proj('w') projObj = projection() if projObj.returned() != 'GEO': res = int(prod['res']) * int(projObj.proj['meters']) else: res = None execmodis = convertModisGDAL(str(hdf), out, spectr, res, wkt=str(projwkt), vrt=True) try: execmodis.run(quiet=True) except: execmodis.run() # remove hdf if remove: # import tif files import_tif(basedir=basedir, rem=remove, write=ow, pm=pm, listfile=fil, prod=prod) try: os.remove(hdf) os.remove(hdf + '.xml') except OSError: pass # move the hdf and hdf.xml to the dir where are the original files else: # import tif files import_tif(basedir=basedir, rem=remove, write=ow, pm=pm, target=targetdir, listfile=fil, prod=prod) if i not in os.listdir(targetdir): try: shutil.move(hdf, targetdir) shutil.move(hdf + '.xml', targetdir) except OSError: pass # remove the conf file try: os.remove(confname) except (OSError, TypeError) as e: pass if options['mrtpath']: grass.try_remove(tempfile.name) grass.try_remove(os.path.join(targetdir, 'mosaic', pid))
def single(options, remove, an, ow, fil): """Convert the HDF file to TIF and import it """ try: # try to import pymodis (modis) and some classes for i.modis.download from rmodislib import product, projection, get_proj except: grass.fatal("i.modis library is not installed") try: from pymodis.convertmodis import convertModis from pymodis.convertmodis_gdal import convertModisGDAL from pymodis.parsemodis import parseModis except: grass.fatal("pymodis library is not installed") listfile, basedir = list_files(options) # for each file for i in listfile: if os.path.exists(i): hdf = i else: # the full path to hdf file hdf = os.path.join(basedir, i) if not os.path.exists(hdf): grass.warning(_("%s not found" % i)) continue pm = parseModis(hdf) if options['mrtpath']: # create conf file fro mrt tools confname = confile(pm, options, an) # create convertModis class and convert it in tif file execmodis = convertModis(hdf, confname, options['mrtpath']) else: projwkt = get_proj('w') projObj = projection() pref = i.split('/')[-1] prod = product().fromcode(pref.split('.')[0]) spectr = spectral(options, prod, an) if projObj.returned() != 'GEO': res = int(prod['res']) * int(projObj.proj['meters']) else: res = None outname = "%s.%s.%s.single" % ( pref.split('.')[0], pref.split('.')[1], pref.split('.')[2]) outname = outname.replace(' ', '_') execmodis = convertModisGDAL(str(hdf), outname, spectr, res, wkt=str(projwkt)) try: execmodis.run(quiet=True) except: execmodis.run() import_tif(basedir=basedir, rem=remove, write=ow, pm=pm, listfile=fil, prod=prod) if options['mrtpath']: os.remove(confname)