def convert():
    subset = [1, 1, 0, 0, 1, 1]

    output_pref = os.path.join(dest, 'MOD11A1.A2014226.h18v04')
    convertsingle = convertModisGDAL(hdfname=files[0],
                                     prefix=output_pref,
                                     subset=subset,
                                     res=1000,
                                     epsg=3035)
    convertsingle.run()

    # 转换后的VRT文件
    vrtfiles = glob.glob(os.path.join(dest, 'MOD11A1.A2014*.vrt'))
    print(vrtfiles)
    for f in vrtfiles:
        base = os.path.basename(f).replace('.vrt', '_vrt')
        output = os.path.join(dest, base)
        convertsingle = convertModisGDAL(hdfname=f,
                                         prefix=output,
                                         subset=[1, 1, 1, 1],
                                         res=1000,
                                         epsg=3035,
                                         vrt=True)
        convertsingle.run_vrt_separated()
Exemple #2
0
 def conversion(self):
     self.dlg.lineEdit.setText ( self.folderName )
     self.dlg.lineEdit_out.setText ( self.newfolderName )
     subset = []
     check = self.dlg.cmblayers.checkedItems ()
     for i in self.itemsindropdownmenu:
         if i in check:
             subset.append ( 1 )
         else:
             subset.append ( 0 )
     subset = [1 if i in check else 0 for i in self.itemsindropdownmenu]
     hdffile = self.folderName
     prefix = self.newfolderName
     outformat = 'Gtiff'
     res = 1200
     epsg = 32647
     wkt = None
     resampl = 'NEAREST_NEIGHBOR'
     vrt = False
     convert = convertmodis_gdal.convertModisGDAL ( hdffile, prefix, subset, res, outformat, epsg, wkt, resampl,
                                                    vrt )
     convert.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 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)
Exemple #5
0
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()
Exemple #6
0
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)
#!/usr/bin/env python 
# -*- coding: utf-8 -*-
# @Time    : 2019/11/7 9:39
# @Author  : NingAnMe <*****@*****.**>
import os
from pymodis import convertmodis_gdal

indir = '../sourceData'  # 输入文件的所在文件夹
filename = 'MCD19A2.A2019001.h26v03.006.2019009051509.hdf'  # 输入文件的文件名
infile = os.path.join(indir, filename)  # 输入数据的完整路径
prefix = os.path.splitext(filename)[0]  # 输出文件名的前缀
bands = [1]  # 选取的通道,这里只选了一个,选取多个[1, 1, 1, 1]
resolution = 0.01  # 单位是degree
outformat = 'GTiff'  # 输出格式,可以是gdal模块支持的所有格式
epsg = 4326  # 重投影方式,投影方式可以在https://spatialreference.org/ref/epsg/查,4326是等经纬
resampl = 'BILINEAR'

mds = convertmodis_gdal.convertModisGDAL(
    infile, prefix, bands, resolution, outformat=outformat, epsg=epsg, resampl=resampl)
mds.run()
Exemple #10
0
import argparse
from pymodis import convertmodis_gdal

parser = argparse.ArgumentParser()
parser.add_argument('--infile', help='输入文件', required=True)
# parser.add_argument('--prefix', help='输出文件的前缀', required=True)
args = parser.parse_args()

mds = convertmodis_gdal.convertModisGDAL(args.infile,
                                         args.infile, [1],
                                         0,
                                         outformat='GTiff',
                                         epsg=4326,
                                         wkt=None,
                                         resampl='BILINEAR',
                                         vrt=False)
mds.run()
    def makeProject(self):
        """Take values from wizard pages and make project file

        Post
        ----

        save file is created with information from the wizard

        """
        print "making project now"
        self.introPage = self.page(self.intro)
        print self.introPage.saveLineEdit.text()
        self.imgPage = self.page(self.img)

        saveDict = {}
        saveDict['projectsavename'] = self.introPage.saveLineEdit.text()
        saveDict['projectname'] = self.introPage.projectNameLineEdit.text()
        saveDict['author'] = self.introPage.authorLineEdit.text()
        saveDict['glacier'] = self.introPage.glacierLineEdit.text()
        saveDict['outputprefix'] = self.introPage.prefixLineEdit.text()
        saveDict['outputdirectory'] = self.introPage.outputDirLineEdit.text()
        saveDict['description'] = self.introPage.descriptionPlainTextEdit.toPlainText()
        saveDict['clipfile'] = self.imgPage.clipLineEdit.text()
        saveDict['hdfbands'] = self.imgPage.hdfBandsLineEdit.text()
        saveDict['epsg'] = self.imgPage.epsgLineEdit.text()
        saveDict['pixelsize'] = self.imgPage.pixelSizeLineEdit.text()

        saveDict = {key : str(saveDict[key]) for key in saveDict.keys()}
        hdfQueue = []
        for i in range(self.imgPage.fileListWidget.count()):
            hdfQueue.append(str(self.imgPage.fileListWidget.item(i).text()))
            #print fileQueue[-1]


        packProject(saveDict['projectsavename'],saveDict)

        pathToFullImages = os.path.join(saveDict['outputdirectory'],'TEMP_GTIFF')
        pathToFullImages = pathToFullImages + os.sep
        if not os.path.exists(saveDict['outputdirectory']) and saveDict['outputdirectory']:
            os.mkdir(saveDict['outputdirectory'])
        if not os.path.exists(pathToFullImages):
            os.mkdir(pathToFullImages)
        bandsInOrder = map(int,saveDict['hdfbands'].split(','))
        sortedBands = sorted(bandsInOrder)
        bandRank = [sortedBands.index(v) for v in bandsInOrder]

        llx, lly, urx, ury = modis.getShapefileBoundingBox(saveDict['clipfile'])
        fileQueue = []

        for f in hdfQueue:
            hdfName = '.'.join( (os.path.split(f)[-1]).split('.')[:-1])

            con = convertmodis.convertModisGDAL(f,pathToFullImages,
                               self.convertBandNumbersToSubsetList(saveDict['hdfbands']),
                               int(saveDict['pixelsize']),'GTiff',epsg=int(saveDict['epsg']))
            con.run()

            imagesInOrder = []
            images = sorted(os.listdir(pathToFullImages))
            for i in range(len(images)):
                imagesInOrder.append(os.path.join(pathToFullImages,images[bandRank.index(i)]))
            print imagesInOrder
            RGBRaster = os.path.join(pathToFullImages,'RGB.tif')
            RGBRaster8Bit = os.path.join(pathToFullImages,saveDict['outputprefix']+hdfName+'.tif')
            clippedRaster = os.path.join(saveDict['outputdirectory'],saveDict['outputprefix']+hdfName+'.tif')

            modis.compositeRasterBandsToRGB(imagesInOrder,RGBRaster,llx,lly,urx,ury)
            modis.convertRasterTo8Bit(RGBRaster,RGBRaster8Bit)
            modis.clipRasterWithShape(RGBRaster8Bit,clippedRaster,saveDict['clipfile'])
            fileQueue.append(clippedRaster)

            for rm in os.listdir(pathToFullImages):
                os.remove(os.path.join(pathToFullImages,rm))

            print hdfName

        os.rmdir(pathToFullImages)


        saveDict['filequeue'] = tuple(fileQueue)
        saveDict['filequeueindex'] = 0

        # image and tiff-based attributes
        saveDict['imageresolution'] = None
        saveDict['xdimension'] = None
        saveDict['ydimension'] = None


        packProject(saveDict['projectsavename'],saveDict)
        pass