def main(): """Main function""" #usage usage = "usage: %prog [options] hdflist_file" if 1 == len(sys.argv): option_parser_class = optparse_gui.OptionParser else: option_parser_class = optparse_required.OptionParser parser = option_parser_class(usage=usage, description='modis_mosaic') #spatial extent #mrt path parser.add_option("-m", "--mrt", dest="mrt_path", required=True, help="the path to MRT software", metavar="MRT_PATH", type='directory') parser.add_option("-o", "--output", dest="output", required=True, help="the name of output mosaic", metavar="OUTPUT_FILE") #write into file parser.add_option("-s", "--subset", dest="subset", help="a subset of product's layers. The string should" \ " be similar to: 1 0 [default: all layers]") (options, args) = parser.parse_args() #check the number of tiles if not args: print ERROR sys.exit() else: if type(args) != ListType: print ERROR sys.exit() elif len(args) > 1: print ERROR sys.exit() if not os.path.isfile(args[0]): parser.error("You have to pass the name of a file containing HDF " \ "files. (One HDF file for line)") #check is a subset it is set if not options.subset: options.subset = False else: if string.find(options.subset, '(') != -1 or string.find(options.subset, ')') != -1: parser.error('ERROR: The spectral string should be similar to: "1 0"') modisOgg = convertmodis.createMosaic(args[0], options.output, options.mrt_path, options.subset) modisOgg.run()
def main(): """Main function""" # usage usage = "usage: %prog [options] hdflist_file" parser = OptionParser(usage=usage) # spatial extent # 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", required=True, help="the name of output mosaic", metavar="OUTPUT_FILE" ) # write into file parser.add_option( "-s", "--subset", dest="subset", help="a subset of product's layers. The string should" " be similar to: 1 0 [default: all layers]", ) (options, args) = parser.parse_args() # check the number of tiles if len(args) > 1: parser.error("You have to pass the name of a file containing HDF " "files. (One HDF file for line)") if not os.path.isfile(args[0]): parser.error("You have to pass the name of a file containing HDF " "files. (One HDF file for line)") # check is a subset it is set if not options.subset: options.subset = False else: if string.find(options.subset, "(") != -1 or string.find(options.subset, ")") != -1: parser.error('ERROR: The spectral string should be similar to: "1 0"') modisOgg = convertmodis.createMosaic(args[0], options.output, options.mrt, options.subset) modisOgg.run()
def main(): """Main function""" # usage usage = "usage: %prog [options] hdflist_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_mosaic') groupR = OptionGroup(parser, 'General options') groupG = OptionGroup(parser, 'Options for GDAL') groupM = OptionGroup(parser, 'Options for MRT') # output groupR.add_option("-o", "--output", dest="output", required=True, help="the name or prefix (for VRT) of output mosaic", metavar="OUTPUT_FILE") # subset groupR.add_option("-s", "--subset", dest="subset", help="a subset of product layers. The string should" " be similar to: 1 0 [default: all layers]") # options only for GDAL groupG.add_option("-f", "--output-format", dest="output_format", metavar="OUTPUT_FORMAT", default="GTiff", help="output format supported: GTiff, HDF4Image" " [default=%default]") # groupG.add_option("-g", "--grain", dest="grain", # type="int", help="force the spatial resolution of output" # " file") # options for set VRT groupG.add_option("-v", "--vrt", dest="vrt", action="store_true", default=False, help="Create a GDAL VRT file. No other " "GDAL options have to been set") # mrt path groupM.add_option("-m", "--mrt", dest="mrt_path", type='directory', help="the path to MRT software", metavar="MRT_PATH") parser.add_option_group(groupR) parser.add_option_group(groupG) parser.add_option_group(groupM) (options, args) = parser.parse_args() # check the number of tiles if len(args) == 0 and not WXPYTHON: parser.print_help() sys.exit(1) if not args: parser.error(ERROR) sys.exit() else: if type(args) != ListType: parser.error(ERROR) sys.exit() elif len(args) > 1: parser.error(ERROR) sys.exit() if not os.path.isfile(args[0]): parser.error("You have to define the name of a text file containing " "HDF files. (One HDF file for line)") # check is a subset it is set if not options.subset: options.subset = False else: if string.find(options.subset, '(') != -1 or string.find( options.subset, ')') != -1: parser.error('ERROR: The spectral string should be similar to: ' '"1 0" without "(" and ")"') # if not options.grain and options.vrt: # options.grain = False # elif not options.grain and options.vrt: # parser.error("You have to define the resolution of output file. Please" # " set -g/--grain option") if options.mrt_path: modisOgg = convertmodis.createMosaic(args[0], options.output, options.mrt_path, options.subset) modisOgg.run() else: tiles = [] with open(args[0]) as f: for l in f: name = os.path.splitext(l.strip())[0] if '.hdf' not in name: tiles.append(l.strip()) modisOgg = convertmodis_gdal.createMosaicGDAL(tiles, options.subset, options.output_format) if options.vrt: modisOgg.write_vrt(options.output) else: modisOgg.run(options.output)
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] hdflist_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_mosaic') groupR = OptionGroup(parser, 'General options') groupG = OptionGroup(parser, 'Options for GDAL') groupM = OptionGroup(parser, 'Options for MRT') # output groupR.add_option("-o", "--output", dest="output", required=True, help="the name or prefix (for VRT) of output mosaic", metavar="OUTPUT_FILE") # subset groupR.add_option("-s", "--subset", dest="subset", help="a subset of product layers. The string should" " be similar to: 1 0 [default: all layers]") # options for set VRT groupR.add_option("-v", "--vrt", dest="vrt", action="store_true", default=False, help="Create a GDAL VRT file. No other " "GDAL options have to been set") # options only for GDAL groupG.add_option("-f", "--output-format", dest="output_format", metavar="OUTPUT_FORMAT", default="GTiff", help="output format supported: GTiff, HDF4Image" " [default=%default]") groupG.add_option("-g", "--grain", dest="grain", type="int", help="the spatial resolution of output file") # mrt path groupM.add_option("-m", "--mrt", dest="mrt_path", required=True, help="the path to MRT software", metavar="MRT_PATH", type='directory') parser.add_option_group(groupR) parser.add_option_group(groupG) parser.add_option_group(groupM) (options, args) = parser.parse_args() # check the number of tiles if len(args) == 0 and not WXPYTHON: parser.print_help() sys.exit(1) if not args: parser.error(ERROR) sys.exit() else: if type(args) != ListType: parser.error(ERROR) sys.exit() elif len(args) > 1: parser.error(ERROR) sys.exit() if not os.path.isfile(args[0]): parser.error("You have to define the name of a text file containing " "HDF files. (One HDF file for line)") # check is a subset it is set if not options.subset: options.subset = False else: if string.find(options.subset, '(') != -1 or string.find(options.subset, ')') != -1: parser.error('ERROR: The spectral string should be similar to: ' '"1 0" without "(" and ")"') if not options.grain and options.vrt: options.grain = False elif not options.grain and options.vrt: parser.error("You have to define the resolution of output file. Please" " set -g/--grain option") if options.mrt_path: modisOgg = convertmodis.createMosaic(args[0], options.output, options.mrt_path, options.subset) modisOgg.run() else: tiles = [] with open(args[0]) as f: for l in f: name = os.path.splitext(l.strip())[0] if '.hdf' not in name: tiles.append(l.strip()) modisOgg = convertmodis_gdal.createMosaicGDAL(tiles, options.subset, options.output_format) if options.vrt: modisOgg.write_vrt(options.output) else: modisOgg.run(options.output)
def main(): """Main function""" # usage usage = "usage: %prog [options] hdflist_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_mosaic") groupR = OptionGroup(parser, "General options") groupG = OptionGroup(parser, "Options for GDAL") groupM = OptionGroup(parser, "Options for MRT") # output groupR.add_option( "-o", "--output", dest="output", required=True, help="the name or prefix (for VRT) of output mosaic", metavar="OUTPUT_FILE", ) # subset groupR.add_option( "-s", "--subset", dest="subset", help="a subset of product layers. The string should" " be similar to: 1 0 [default: all layers]", ) # options only for GDAL groupG.add_option( "-f", "--output-format", dest="output_format", metavar="OUTPUT_FORMAT", default="GTiff", help="output format supported: GTiff, HDF4Image" " [default=%default]", ) # groupG.add_option("-g", "--grain", dest="grain", # type="int", help="force the spatial resolution of output" # " file") # options for set VRT groupG.add_option( "-v", "--vrt", dest="vrt", action="store_true", default=False, help="Create a GDAL VRT file. No other " "GDAL options have to been set", ) # mrt path groupM.add_option( "-m", "--mrt", dest="mrt_path", type="directory", help="the path to MRT software", metavar="MRT_PATH" ) parser.add_option_group(groupR) parser.add_option_group(groupG) parser.add_option_group(groupM) (options, args) = parser.parse_args() # check the number of tiles if len(args) == 0 and not WXPYTHON: parser.print_help() sys.exit(1) if not args: parser.error(ERROR) sys.exit() else: if not isinstance(args, list): parser.error(ERROR) sys.exit() elif len(args) > 1: parser.error(ERROR) sys.exit() if not os.path.isfile(args[0]): parser.error("You have to define the name of a text file containing " "HDF files. (One HDF file for line)") # check is a subset it is set if not options.subset: options.subset = False else: if options.subset.strip().startswith("(") or options.subset.strip().endswith(")"): parser.error("ERROR: The spectral string should be similar to: " '"1 0" without "(" and ")"') # if not options.grain and options.vrt: # options.grain = False # elif not options.grain and options.vrt: # parser.error("You have to define the resolution of output file. Please" # " set -g/--grain option") if options.mrt_path: modisOgg = convertmodis.createMosaic(args[0], options.output, options.mrt_path, options.subset) modisOgg.run() else: tiles = dict() dire = os.path.dirname(args[0]) with open(args[0]) as f: for l in f: name = os.path.splitext(l.strip())[0] day = name.split(".")[1] if day not in tiles.keys(): tiles[day] = list() if ".hdf" not in name: if dire not in l: fname = os.path.join(dire, l.strip()) else: fname = l.strip() tiles[day].append(fname) for day in tiles.keys(): modisOgg = convertmodis_gdal.createMosaicGDAL(tiles[day], options.subset, options.output_format) output = "{da}_{fi}".format(da=day, fi=options.output) if options.vrt: modisOgg.write_vrt(output) else: modisOgg.run(output)
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 main(): """Main function""" # usage usage = "usage: %prog [options] hdflist_file" if 1 == len(sys.argv): option_parser_class = optparse_gui.OptionParser else: option_parser_class = optparse_required.OptionParser parser = option_parser_class(usage=usage, description="modis_mosaic") groupR = OptionGroup(parser, "Required options") groupG = OptionGroup(parser, "Options for GDAL") groupM = OptionGroup(parser, "Options for MRT") # spatial extent groupR.add_option( "-o", "--output", dest="output", required=True, help="the name of output mosaic", metavar="OUTPUT_FILE" ) # write into file groupR.add_option( "-s", "--subset", dest="subset", help="a subset of product layers. The string should" " be similar to: 1 0 [default: all layers]", ) # 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("-g", "--grain", dest="resolution", type="int", help="the spatial resolution of output file") groupG.add_option("--formats", dest="formats", action="store_true", help="print supported GDAL formats") # mrt path groupM.add_option( "-m", "--mrt", dest="mrt_path", required=True, help="the path to MRT software", metavar="MRT_PATH", type="directory", ) parser.add_option_group(groupR) parser.add_option_group(groupG) parser.add_option_group(groupM) (options, args) = parser.parse_args() # check the number of tiles if not args: print ERROR sys.exit() else: if type(args) != ListType: print ERROR sys.exit() elif len(args) > 1: print ERROR sys.exit() if not os.path.isfile(args[0]): parser.error("You have to define the name of a text file containing HDF " "files. (One HDF file for line)") # check is a subset it is set if not options.subset: options.subset = False else: 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: modisOgg = convertmodis.createMosaic(args[0], options.output, options.mrt_path, options.subset) # else: # modisOgg = gdal_merge. modisOgg.run()
def main(): """Main function""" #usage usage = "usage: %prog [options] hdflist_file" if 1 == len(sys.argv): option_parser_class = optparse_gui.OptionParser else: option_parser_class = optparse_required.OptionParser parser = option_parser_class(usage=usage, description='modis_mosaic') groupR = OptionGroup(parser, 'Required options') groupG = OptionGroup(parser, 'Options for GDAL') groupM = OptionGroup(parser, 'Options for MRT') #spatial extent groupR.add_option("-o", "--output", dest="output", required=True, help="the name of output mosaic", metavar="OUTPUT_FILE") #write into file groupR.add_option("-s", "--subset", dest="subset", help="a subset of product layers. The string should" \ " be similar to: 1 0 [default: all layers]") #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("-g", "--grain", dest="resolution", type="int", help="the spatial resolution of output file") groupG.add_option("--formats", dest="formats", action="store_true", help="print supported GDAL formats") #mrt path groupM.add_option("-m", "--mrt", dest="mrt_path", required=True, help="the path to MRT software", metavar="MRT_PATH", type='directory') parser.add_option_group(groupR) parser.add_option_group(groupG) parser.add_option_group(groupM) (options, args) = parser.parse_args() #check the number of tiles if not args: print ERROR sys.exit() else: if type(args) != ListType: print ERROR sys.exit() elif len(args) > 1: print ERROR sys.exit() if not os.path.isfile(args[0]): parser.error("You have to define the name of a text file containing HDF " \ "files. (One HDF file for line)") #check is a subset it is set if not options.subset: options.subset = False else: 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: modisOgg = convertmodis.createMosaic(args[0], options.output, options.mrt_path, options.subset) # else: # modisOgg = gdal_merge. modisOgg.run()