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 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 main(): """Main function""" # usage usage = "usage: %prog [options] hdf_file" if 1 == len(sys.argv) and WXPYTHON: option_parser_class = optparse_gui.OptionParser else: option_parser_class = optparse_required.OptionParser parser = option_parser_class(usage=usage, description='modis_convert') groupR = OptionGroup(parser, 'General options') groupG = OptionGroup(parser, 'Options for GDAL') groupM = OptionGroup(parser, 'Options for MRT') # options used by both methos groupR.add_option("-s", "--subset", dest="subset", required=True, help="a subset of product's layers. The string should " "be similar to: ( 1 0 )") groupR.add_option("-o", "--output", dest="output", required=True, help="the prefix of output file", metavar="OUTPUT_FILE") groupR.add_option("-g", "--grain", dest="resolution", required=True, type="float", help="the spatial resolution of output " "file") help_resampl = "the method of resampling." help_resampl += " -- mrt methods: {res}".format(res=parsemodis.RESAM_LIST) help_resampl += " -- gdal methods: {res}".format(res=convertmodis_gdal.RESAM_GDAL) help_resampl = removeBracs(help_resampl) res_choice = parsemodis.RESAM_LIST + convertmodis_gdal.RESAM_GDAL groupR.add_option("-r", "--resampl", dest="resampling", help=help_resampl + " [default=%default]", metavar="RESAMPLING_TYPE", default='NEAREST_NEIGHBOR', type='choice', choices=res_choice) # options only for GDAL groupG.add_option("-f", "--output-format", dest="output_format", metavar="OUTPUT_FORMAT", default="GTiff", help="output format supported by GDAL [default=%default]") groupG.add_option("-e", "--epsg", dest="epsg", metavar="EPSG", help="EPSG code for the output") groupG.add_option("-w", "--wkt_file", dest="wkt", metavar="WKT", help="file or string containing projection definition" " in WKT format") groupG.add_option("-v", "--vrt", dest="vrt", action="store_true", default=False, help="Read from a GDAL VRT file.") groupG.add_option("--formats", dest="formats", action="store_true", help="print supported GDAL formats") # options only for MRT groupM.add_option("-m", "--mrt", dest="mrt_path", type='directory', help="the path to MRT software", metavar="MRT_PATH") help_datum = "the code of datum. Available: {dat}".format(dat=parsemodis.DATUM_LIST) help_datum = removeBracs(help_datum) groupM.add_option("-d", "--datum", dest="datum", default="WGS84", type='choice', choices=parsemodis.DATUM_LIST, help=help_datum + " [default=%default]") help_pt = "the output projection system. Available: {proj}".format(proj=parsemodis.PROJ_LIST) help_pt = removeBracs(help_pt) groupM.add_option("-t", "--proj_type", dest="projection_type", type='choice', metavar="PROJECTION_SYSTEM", choices=parsemodis.PROJ_LIST, action='store', help=help_pt + " [default=%default]", default='GEO') groupM.add_option("-p", "--proj_parameters", dest="projection_parameter", metavar="PROJECTION_PARAMETERS", default='( 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0' ' 0.0 0.0 0.0 0.0 )', help="a list of projection parameters, for more info " "check the 'Appendix C' of MODIS reprojection tool user" "'s manual https://lpdaac.usgs.gov/content/download" "/4831/22895/file/mrt41_usermanual_032811.pdf " "[default=%default]") groupM.add_option("-u", "--utm", dest="utm_zone", metavar="UTM_ZONE", help="the UTM zone if projection system is UTM") parser.add_option_group(groupR) parser.add_option_group(groupG) parser.add_option_group(groupM) # return options and argument (options, args) = parser.parse_args() # check the argument if len(args) == 0 and not WXPYTHON: parser.print_help() sys.exit(1) if len(args) > 1: parser.error("You have to define the name of HDF file.") if not os.path.isfile(args[0]): parser.error("You have to define the name of HDF file.") if string.find(options.subset, '(') == -1 or string.find(options.subset, ')') == -1: parser.error('ERROR: The spectral string should be similar to: "( 1 0 )"') if options.mrt_path: if not options.output.endswith('.tif') and \ not options.output.endswith('.hdf') and \ not options.output.endswith('.hdr'): parser.error("Valid extensions for output are .hdf, .hdr, or .tif") from pymodis import convertmodis modisParse = parsemodis.parseModis(args[0]) confname = modisParse.confResample(options.subset, options.resolution, options.output, options.datum, options.resampling, options.projection_type, options.utm_zone, options.projection_parameter) modisConver = convertmodis.convertModis(args[0], confname, options.mrt_path) else: modisConver = convertmodis_gdal.convertModisGDAL(args[0], options.output, options.subset, options.resolution, options.output_format, options.epsg, options.wkt, options.resampling, options.vrt) modisConver.run()
def main(): """Main function""" # usage usage = "usage: %prog [options] hdf_file" if 1 == len(sys.argv) and WXPYTHON: option_parser_class = optparse_gui.OptionParser else: option_parser_class = optparse_required.OptionParser parser = option_parser_class(usage=usage, description='modis_convert') groupR = OptionGroup(parser, 'General options') groupG = OptionGroup(parser, 'Options for GDAL') groupM = OptionGroup(parser, 'Options for MRT') # options used by both methos groupR.add_option("-s", "--subset", dest="subset", required=True, help="a subset of product's layers. The string should " "be similar to: ( 1 0 )") groupR.add_option("-o", "--output", dest="output", required=True, help="the prefix of output file", metavar="OUTPUT_FILE") groupR.add_option("-g", "--grain", dest="resolution", type="float", help="the spatial resolution of output file") help_resampl = "the method of resampling." help_resampl += " -- mrt methods: {res}".format(res=parsemodis.RESAM_LIST) help_resampl += " -- gdal methods: {res}".format( res=convertmodis_gdal.RESAM_GDAL) help_resampl = removeBracs(help_resampl) res_choice = parsemodis.RESAM_LIST + convertmodis_gdal.RESAM_GDAL groupR.add_option("-r", "--resampl", dest="resampling", help=help_resampl + " [default=%default]", metavar="RESAMPLING_TYPE", default='NEAREST_NEIGHBOR', type='choice', choices=res_choice) # options only for GDAL groupG.add_option( "-f", "--output-format", dest="output_format", metavar="OUTPUT_FORMAT", default="GTiff", help="output format supported by GDAL [default=%default]") groupG.add_option("-e", "--epsg", dest="epsg", metavar="EPSG", help="EPSG code for the output") groupG.add_option("-w", "--wkt_file", dest="wkt", metavar="WKT", help="file or string containing projection definition" " in WKT format") groupG.add_option("-v", "--vrt", dest="vrt", action="store_true", default=False, help="Read from a GDAL VRT file.") groupG.add_option("--formats", dest="formats", action="store_true", help="print supported GDAL formats") # options only for MRT groupM.add_option("-m", "--mrt", dest="mrt_path", type='directory', help="the path to MRT software", metavar="MRT_PATH") help_datum = "the code of datum. Available: {dat}".format( dat=parsemodis.DATUM_LIST) help_datum = removeBracs(help_datum) groupM.add_option("-d", "--datum", dest="datum", default="WGS84", type='choice', choices=parsemodis.DATUM_LIST, help=help_datum + " [default=%default]") help_pt = "the output projection system. Available: {proj}".format( proj=parsemodis.PROJ_LIST) help_pt = removeBracs(help_pt) groupM.add_option("-t", "--proj_type", dest="projection_type", type='choice', metavar="PROJECTION_SYSTEM", choices=parsemodis.PROJ_LIST, action='store', help=help_pt + " [default=%default]", default='GEO') groupM.add_option("-p", "--proj_parameters", dest="projection_parameter", metavar="PROJECTION_PARAMETERS", default='( 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0' ' 0.0 0.0 0.0 0.0 )', help="a list of projection parameters, for more info " "check the 'Appendix C' of MODIS reprojection tool user" "'s manual https://lpdaac.usgs.gov/content/download" "/4831/22895/file/mrt41_usermanual_032811.pdf " "[default=%default]") groupM.add_option("-u", "--utm", dest="utm_zone", metavar="UTM_ZONE", help="the UTM zone if projection system is UTM") parser.add_option_group(groupR) parser.add_option_group(groupG) parser.add_option_group(groupM) # return options and argument (options, args) = parser.parse_args() # check the argument if len(args) == 0 and not WXPYTHON: parser.print_help() sys.exit(1) if len(args) > 1: parser.error("You have to define the name of HDF file.") if not os.path.isfile(args[0]): parser.error("You have to define the name of HDF file.") if string.find(options.subset, '(') == -1 or string.find( options.subset, ')') == -1: parser.error( 'ERROR: The spectral string should be similar to: "( 1 0 )"') if options.mrt_path: if not options.output.endswith('.tif') and \ not options.output.endswith('.hdf') and \ not options.output.endswith('.hdr'): parser.error("Valid extensions for output are .hdf, .hdr, or .tif") from pymodis import convertmodis modisParse = parsemodis.parseModis(args[0]) confname = modisParse.confResample(options.subset, options.resolution, options.output, options.datum, options.resampling, options.projection_type, options.utm_zone, options.projection_parameter) modisConver = convertmodis.convertModis(args[0], confname, options.mrt_path) else: modisConver = convertmodis_gdal.convertModisGDAL( args[0], options.output, options.subset, options.resolution, options.output_format, options.epsg, options.wkt, options.resampling, options.vrt) modisConver.run()
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)
def main(): """Main function""" #usage usage = "usage: %prog [options] hdf_file" parser = OptionParser(usage=usage) #layer subset parser.add_option("-s", "--subset", dest="subset", required=True, help="a subset of product's layers. The string should "\ "be similar to: 1 0") #mrt path parser.add_option("-m", "--mrt", dest="mrt", required=True, help="the path to MRT software", metavar="MRT_PATH") parser.add_option("-o", "--output", dest="output", help="the name of output file", metavar="OUTPUT_FILE") parser.add_option("-g", "--grain", dest="res", type="int", help="the spatial resolution of output file") help_datum = "the code of datum. Available: %s" % parsemodis.DATUM_LIST help_datum = removeBracs(help_datum) parser.add_option("-d", "--datum", dest="datum", default="WGS84", type='choice', choices=parsemodis.DATUM_LIST, help=help_datum + " [default: %default]") help_resampl = "the type of resampling. Available: %s" % parsemodis.RESAM_LIST help_resampl = removeBracs(help_resampl) parser.add_option("-r", "--resampl", dest="resampl", help=help_resampl + " [default: %default]", metavar="RESAMPLING_TYPE", default='NEAREST_NEIGHBOR', type='choice', choices=parsemodis.RESAM_LIST) parser.add_option("-p", "--proj_parameters", dest="pp", metavar="PROJECTION_PARAMETERS", default='( 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"\ " 0.0 0.0 0.0 0.0 )', help="a list of projection parameters, for more info "\ "check the 'Appendix C' of MODIS reprojection tool user"\ "'s manual https://lpdaac.usgs.gov/content/download" \ "/4831/22895/file/mrt41_usermanual_032811.pdf "\ "[default: %default]") help_pt = "the output projection system. Available: %s" % parsemodis.PROJ_LIST help_pt = removeBracs(help_pt) parser.add_option("-t", "--proj_type", dest="pt", default='GEO', type='choice', metavar="PROJECTION_SYSTEM", choices=parsemodis.PROJ_LIST, action='store', help=help_pt + " [default: %default]") parser.add_option("-u", "--utm", dest="utm", metavar="UTM_ZONE", help="the UTM zone if projection system is UTM") #return options and argument (options, args) = parser.parse_args() #check the argument if len(args) > 1: parser.error("You have to pass the name of HDF file.") if not os.path.isfile(args[0]): parser.error("You have to pass the name of HDF file.") if string.find(options.subset, '(') == -1 or string.find(options.subset, ')') == -1: parser.error('ERROR: The spectral string should be similar to: "( 1 0 )"') modisParse = parsemodis.parseModis(args[0]) confname = modisParse.confResample(options.subset, options.res, options.output, options.datum, options.resampl, options.pt, options.utm, options.pp) modisConver = convertmodis.convertModis(args[0], confname, options.mrt) modisConver.run()