def mosaic(): # [daily temp, quality for daily, not used, not used, nightly temp, quality for nightly] subset = [1, 1, 0, 0, 1, 1] output_pref = os.path.join(dest, 'MOD11A1.A2014226.mosaic') output_tif = os.path.join(dest, 'MOD11A1.A2014226.mosaic.tif') # 初始化对象 mosaic = createMosaicGDAL(files, subset, 'GTiff') mosaic.run(output_tif) mosaic.write_vrt(output_pref)
def merge_hdf(): from pymodis import convertmodis_gdal import pprint import glob fd = r'E:\GroupMenbers\Qirong\modis_1' hdf_list = glob.glob(os.path.join(fd, r'*.hdf')) modis_mosaic = convertmodis_gdal.createMosaicGDAL(hdfnames=hdf_list, subset=[1, 0, 1], outformat='GTiff') modis_mosaic.run(os.path.join(fd, r'mosaic_hdf_1.tif'))
def downloadmosaic(outfolder, temporalfolder, product, tiles, password, user, date, delta): import pymodis import glob import os delfilesinfolder(temporalfolder) year = str(date.year) month = date.month if month < 10: monthstr = '0' + str(month) else: monthstr = str(month) day = (date.day) if day < 10: daystr = '0' + str(day) else: daystr = str(day) day = year + '-' + monthstr + '-' + daystr if (product == 'MOD09GA.006') or (product == 'MOD11A1.006'): path_download = 'MOLT' if product == 'MOD09GA.006': subset = [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 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, 0] # Bandas que queremos. 1 yes 0 No if product == 'MOD11A1.006': subset = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0] if (product == 'MYD09GA.006')or (product == 'MYD11A1.006'): path_download = 'MOLA' if product == 'MYD09GA.006': subset = [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 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, 0] # Bandas que queremos. 1 yes 0 No if product == 'MYD11A1.006': subset = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0] if product == 'MOD13A2.006': path_download = 'MOLT' if product == 'MOD13A2.006': subset = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] if product == 'MCD15A2H.006': path_download = 'MOTA' if product == 'MCD15A2H.006': subset = [1, 1, 1, 1, 1, 1] if product == 'MCD43B3.005': path_download = 'MOTA' if product == 'MCD43B3.005': subset = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1] if product == 'MCD43B2.005': path_download = 'MOTA' if product == 'MCD43B2.005': subset = [1, 0, 0, 0] modisDown = pymodis.downmodis.downModis(product=product, destinationFolder=temporalfolder, password=password, path=path_download, user=user, tiles=tiles, today=day, delta=delta) modisDown.connect() modisDown.downloadsAllDay() # create the list of files to use files = glob.glob(os.path.join(temporalfolder, product[0:7] + '*.hdf')) modisParse = pymodis. parsemodis.parseModis(files[0]) # bounding box of the tiles modisParse.retBoundary() # Parse multiple data modisMultiParse = pymodis.parsemodis.parseModisMulti(files) dateout = files[0] pos_ini = dateout.index('.A') dateout = dateout[pos_ini + 2:pos_ini + 2 + 7] modisMultiParse.writexml(os.path.join( temporalfolder, 'modismultiparse.xml')) from pymodis.convertmodis_gdal import createMosaicGDAL output_tif = os.path.join(outfolder, product + dateout + '.mosaic.tif') mosaic = createMosaicGDAL(files, subset, 'GTiff') mosaic.run(output_tif) print('Mosaic for ' + dateout + ' generated!!!') return output_tif, day
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))
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/11/7 9:39 # @Author : NingAnMe <*****@*****.**> import os import glob from pymodis import convertmodis_gdal """ 拼接所需的xml文件的下载地址:https://e4ftl01.cr.usgs.gov """ # ######################### 需要修改的参数 ########################### indir = r"../sourceData" # 存放原始数据的目录 outdir = r'../resultData' # 存放结果数据的目录 # ################################################################## if not os.path.isdir(outdir): os.makedirs(outdir) # ######################### 生成HDF格式 ############################ infiles = glob.glob(os.path.join(indir, '*.hdf')) # 获取sourceData目录下所有的原始数据文件 filename_first = os.path.basename(infiles[0]) # 获取原始数据的文件名 outfilename = '.'.join(filename_first.split('.')[:-2]) + '.hdf' # 生成输出数据的文件名 outfile = os.path.join(outdir, outfilename) # 生成输出数据的完整路径 bands = [1] # 第几通道的数据 outformat = 'GTiff' # 输出文件的格式,默认为HDF4,这里设置为GEOTiff mds = convertmodis_gdal.createMosaicGDAL(infiles, bands, outformat=outformat) mds.run(outfile) print(">>>: {}".format(outfile))
# ainda não pensei em como resolver isso... #for b in bandas_simples: # saida = os.path.join(args.outdir, '{0}.A{1}{2:03}_{3}.tif'.format(args.uf, args.ano, args.doy, b)) # try: # open(saida) # print 'Mosaico {0} já existe. Próxima...'.format(saida) # continue # except IOError: # pass #out_tiff = '{0}.A{1}{2:03}.tif'.format(args.uf, args.ano, args.doy) out_base = '{0}.A{1}{2:03}'.format(args.uf, args.ano, args.doy) print 'Gerando mosaico virtual para {0} na data {1}{2:03}'.format(args.uf, args.ano, args.doy) mosaico = createMosaicGDAL(lista, subset, 'GTiff') mosaico.write_vrt(os.path.join(args.outdir, out_base)) #mosaico.run(os.path.join(args.outdir, out_tiff)) # reprojetando # preciso adicionar a barra (\) manualmente no nome do arquivo porque o write_vrt coloca espaco no nome # gdalwarp precisa da barra enquanto que o os.remove, usado mais tarde, não aceita a barra print 'Reprojetando e cortando as {0} bandas para {1}, data {2}{3:03}'.format(len(bandas_extract), args.uf, args.ano, args.doy) for b in range(len(bandas_extract)): entrada = os.path.join(args.outdir, out_base+'_'+bandas_extract[b]+'.vrt') saida_tmp = os.path.join(args.outdir, '{0}.A{1}{2:03}.{3}.temp.vrt'.format(args.uf, args.ano, args.doy, bandas_simples[b])) saida = os.path.join(args.outdir, '{0}.A{1}{2:03}.{3}.tif'.format(args.uf, args.ano, args.doy, bandas_simples[b])) comando = 'gdalwarp -of VRT -t_srs epsg:4326 -tr 0.002 0.002 {0} {1}'.format(entrada.replace(' ','\ '), saida_tmp) #print comando subprocess.call(comando, shell=True)
# -*- coding: utf-8 -*- # @Time : 2019/8/2 22:05 # @Author : PasserQi # @Email : [email protected] # @File : mosaic # @Software: PyCharm # @Version : # @Desc : import os import glob from pymodis.convertmodis_gdal import createMosaicGDAL in_dir = r"D:\mycode\GISandPython\2PyModis\2001" in_files = glob.glob( in_dir + "\*.hdf") print(in_files) output_pref = r"D:\mycode\GISandPython\2PyModis\tmp\mosic" output_tif = r"D:\mycode\GISandPython\2PyModis\tmp\mosic.tif" subset = [0 for x in range(0, 219) ] subset[11] = 1 mosaic = createMosaicGDAL(in_files, subset, 'GTiff') mosaic.run(output_tif) # mosaic.write_vrt(output_pref)