Exemplo n.º 1
0
 def mosaic(self, datadir, res=None, interpolation=0, crop=False,
            overwrite=False, alltouch=False):
     """ Combine tiles into a single mosaic, warp if res provided """
     if self.spatial.site is None:
         raise Exception('Site required for creating mosaics')
     start = datetime.now()
     bname = self.date.strftime('%Y%j')
     for product in self.products.products:
         sensor = self.which_sensor(product)
         if sensor is None:
             continue
         # create data directory when it is needed
         mkdir(datadir)
         # TODO - this is assuming a tif file.  Use gippy FileExtension function when it is exposed
         fout = os.path.join(datadir, '%s_%s_%s' % (bname, sensor, product)) + '.tif'
         if not os.path.exists(fout) or overwrite:
             try:
                 filenames = [self.tiles[t].filenames[(sensor, product)] for t in self.tiles]
                 images = gippy.GeoImages(filenames)
                 if self.spatial.site is not None and res is not None:
                     CookieCutter(
                         images, self.spatial.site, fout, res[0], res[1],
                         crop, interpolation, {}, alltouch,
                     )
                 else:
                     mosaic(images, fout, self.spatial.site)
             except Exception, e:
                 VerboseOut(traceback.format_exc(), 4)
                 VerboseOut("Error mosaicking %s: %s" % (fout, e))
Exemplo n.º 2
0
 def mosaic(self,
            datadir,
            res=None,
            interpolation=0,
            crop=False,
            overwrite=False):
     """ Combine tiles into a single mosaic, warp if res provided """
     if self.spatial.site is None:
         raise Exception('Site required for creating mosaics')
     start = datetime.now()
     mkdir(datadir)
     bname = self.date.strftime('%Y%j')
     for product in self.products.products:
         sensor = self.which_sensor(product)
         if sensor is None:
             continue
         # TODO - this is assuming a tif file.  Use gippy FileExtension function when it is exposed
         fout = os.path.join(datadir, '%s_%s_%s' %
                             (bname, sensor, product)) + '.tif'
         if not os.path.exists(fout) or overwrite:
             try:
                 filenames = [
                     self.tiles[t].filenames[(sensor, product)]
                     for t in self.tiles
                 ]
                 images = gippy.GeoImages(filenames)
                 if self.spatial.site is not None and res is not None:
                     CookieCutter(images, self.spatial.site, fout, res[0],
                                  res[1], crop, interpolation)
                 else:
                     mosaic(images, fout, self.spatial.site)
             except Exception, e:
                 VerboseOut(traceback.format_exc(), 4)
                 VerboseOut("Error mosaicking %s: %s" % (fout, e))
Exemplo n.º 3
0
def main():
    title = Colors.BOLD + 'GIPS Tiles (v%s)' % __version__ + Colors.OFF

    # argument parsing
    parser0 = GIPSParser(description=title)
    parser0.add_inventory_parser()
    parser0.add_process_parser()
    parser0.add_project_parser()
    parser0.add_warp_parser()
    args = parser0.parse_args()

    cls = utils.gips_script_setup(args.command, args.stop_on_error)
    print title

    with utils.error_handler():
        # create output directory if needed
        # tld is "{}_tiles_{}_{}".format(DATATYPE, RESOLUTION, SUFFIX)
        if args.notld:
            tld = args.outdir
        else:
            tld = os.path.join(args.outdir, '%s_tiles' % args.command)
            if args.res is not None:
                tld = tld + '_%sx%s' % (args.res[0], args.res[1])
            if args.suffix != '':
                tld = tld + '_' + args.suffix
        mkdir(tld)

        extents = SpatialExtent.factory(cls,
                                        site=args.site,
                                        rastermask=args.rastermask,
                                        key=args.key,
                                        where=args.where,
                                        tiles=args.tiles,
                                        pcov=args.pcov,
                                        ptile=args.ptile)
        for extent in extents:
            inv = DataInventory(cls, extent,
                                TemporalExtent(args.dates, args.days),
                                **vars(args))
            for date in inv.dates:
                for tid in inv[date].tiles:
                    # make sure back-end tiles are processed
                    inv[date].tiles[tid].process(args.products,
                                                 overwrite=False)
                    # warp the tiles & copy into place in the output dir
                    inv[date].tiles[tid].copy(tld, args.products,
                                              inv.spatial.site, args.res,
                                              args.interpolation, args.crop,
                                              args.overwrite, args.tree)

    utils.gips_exit(
    )  # produce a summary error report then quit with a proper exit status
Exemplo n.º 4
0
def main():
    title = Colors.BOLD + 'GIPS Tiles (v%s)' % __version__ + Colors.OFF

    # argument parsing
    parser0 = GIPSParser(description=title)
    parser0.add_inventory_parser()
    parser0.add_process_parser()
    parser0.add_project_parser()
    parser0.add_warp_parser()
    args = parser0.parse_args()

    try:
        print title
        cls = import_data_class(args.command)

        # create tld: DATATYPE_tiles_RESOLUTION_SUFFIX
        if args.notld:
            tld = args.outdir
        else:
            tld = os.path.join(args.outdir, '%s_tiles' % args.command)
            if args.res is not None:
                tld = tld + '_%sx%s' % (args.res[0], args.res[1])
            if args.suffix != '':
                tld = tld + '_' + args.suffix
        mkdir(tld)

        extents = SpatialExtent.factory(cls, args.site, args.key, args.where,
                                        args.tiles, args.pcov, args.ptile)
        for extent in extents:
            inv = DataInventory(cls, extent,
                                TemporalExtent(args.dates, args.days),
                                **vars(args))
            for date in inv.dates:
                for tid in inv[date].tiles:
                    # make sure back-end tiles are processed
                    inv[date].tiles[tid].process(args.products,
                                                 overwrite=False)
                    # warp the tiles
                    inv[date].tiles[tid].copy(tld, args.products,
                                              inv.spatial.site, args.res,
                                              args.interpolation, args.crop,
                                              args.overwrite, args.tree)

    except Exception, e:
        import traceback
        VerboseOut(traceback.format_exc(), 4)
        print 'Warp Tiles error: %s' % e
Exemplo n.º 5
0
    def mosaic(self, datadir, res=None, interpolation=0, crop=False,
               overwrite=False, alltouch=False):
        """For each product, combine its tiles into a single mosaic.

        Warp if res provided."""
        if self.spatial.site is None:
            raise Exception('Site required for creating mosaics')
        start = datetime.now()
        bname = self.date.strftime('%Y%j')

        # look in each Data() and dig out its (sensor, product_type) pairs
        sp_pile = [(s, p) for d in self.tiles.values() for (s, p) in d.filenames
                        if p in self.products.products]
        # work on each product in turn, gathering up filenames as needed
        for (sensor, product) in sp_pile:
            # create data directory when it is needed
            mkdir(datadir)
            # TODO - this is assuming a tif file.  Use gippy FileExtension function when it is exposed
            fn = '{}_{}_{}.tif'.format(bname, sensor, product)
            final_fp = os.path.join(datadir, fn)
            if not os.path.exists(final_fp) or overwrite:
                err_msg = ("Error mosaicking " + final_fp + ". Did you forget"
                           " to specify a resolution (`--res x x`)?")
                with utils.error_handler(err_msg, continuable=True), \
                        utils.make_temp_dir(dir=datadir,
                                            prefix='mosaic') as tmp_dir:
                    tmp_fp = os.path.join(tmp_dir, fn) # for safety
                    filenames = [self.tiles[t].filenames[(sensor, product)]
                                 for t in self.tiles
                                 if (sensor, product) in self.tiles[t].filenames
                    ]
                    images = gippy.GeoImages(filenames)
                    if self.spatial.rastermask is not None:
                        gridded_mosaic(images, tmp_fp,
                                       self.spatial.rastermask, interpolation)
                    elif self.spatial.site is not None and res is not None:
                        CookieCutter(
                            images, self.spatial.site, tmp_fp, res[0], res[1],
                            crop, interpolation, {}, alltouch,
                        )
                    else:
                        mosaic(images, tmp_fp, self.spatial.site)
                    os.rename(tmp_fp, final_fp)
        t = datetime.now() - start
        VerboseOut('%s: created project files for %s tiles in %s' % (self.date, len(self.tiles), t), 2)
Exemplo n.º 6
0
 def copy(self,
          dout,
          products,
          site=None,
          res=None,
          interpolation=0,
          crop=False,
          overwrite=False,
          tree=False):
     """ Copy products to new directory, warp to projection if given site """
     # TODO - allow hard and soft linking options
     if res is None:
         res = self.Asset._defaultresolution
         #VerboseOut('Using default resolution of %s x %s' % (res[0], res[1]))
     dout = os.path.join(dout, self.id)
     if tree:
         dout = os.path.join(dout, self.date.strftime('%Y%j'))
     mkdir(dout)
     products = self.RequestedProducts(products)
     bname = '%s_%s' % (self.id, self.date.strftime('%Y%j'))
     for p in products.requested:
         if p not in self.sensors:
             # this product is not available for this day
             continue
         sensor = self.sensors[p]
         fin = self.filenames[(sensor, p)]
         fout = os.path.join(dout, "%s_%s_%s.tif" % (bname, sensor, p))
         if not os.path.exists(fout) or overwrite:
             try:
                 if site is not None:
                     # warp just this tile
                     resampler = ['near', 'bilinear', 'cubic']
                     cmd = 'gdalwarp %s %s -t_srs "%s" -tr %s %s -r %s' % \
                            (fin, fout, site.Projection(), res[0], res[1], resampler[interpolation])
                     print cmd
                     #result = commands.getstatusoutput(cmd)
                 else:
                     gippy.GeoImage(fin).Process(fout)
                     #shutil.copyfile(fin, fout)
             except Exception:
                 VerboseOut(traceback.format_exc(), 4)
                 VerboseOut("Problem creating %s" % fout)
     procstr = 'copied' if site is None else 'warped'
     VerboseOut('%s tile %s: %s files %s' %
                (self.date, self.id, len(products.requested), procstr))
Exemplo n.º 7
0
def main():
    title = Colors.BOLD + 'GIPS Tiles (v%s)' % __version__ + Colors.OFF

    # argument parsing
    parser0 = GIPSParser(description=title)
    parser0.add_inventory_parser()
    parser0.add_process_parser()
    parser0.add_project_parser()
    parser0.add_warp_parser()
    args = parser0.parse_args()

    try:
        print title
        cls = data_class(args.command)

        # create tld: DATATYPE_tiles_RESOLUTION_SUFFIX
        if args.notld:
            tld = args.outdir
        else:
            tld = os.path.join(args.outdir, '%s_tiles' % args.command)
            if args.res is not None:
                tld = tld + '_%sx%s' % (args.res[0], args.res[1])
            if args.suffix != '':
                tld = tld + '_' + args.suffix
        mkdir(tld)

        extents = SpatialExtent.factory(cls, args.site, args.key, args.where, args.tiles, args.pcov, args.ptile)
        for extent in extents:
            inv = DataInventory(cls, extent, TemporalExtent(args.dates, args.days), **vars(args))
            for date in inv.dates:
                for tid in inv[date].tiles:
                    # make sure back-end tiles are processed
                    inv[date].tiles[tid].process(args.products, overwrite=False)
                    # warp the tiles
                    inv[date].tiles[tid].copy(tld, args.products, inv.spatial.site,
                                              args.res, args.interpolation, args.crop, args.overwrite, args.tree)

    except Exception, e:
        import traceback
        VerboseOut(traceback.format_exc(), 4)
        print 'Warp Tiles error: %s' % e
Exemplo n.º 8
0
 def copy(self, dout, products, site=None, res=None, interpolation=0, crop=False, overwrite=False, tree=False):
     """ Copy products to new directory, warp to projection if given site """
     # TODO - allow hard and soft linking options
     if res is None:
         res = self.Asset._defaultresolution
         #VerboseOut('Using default resolution of %s x %s' % (res[0], res[1]))
     dout = os.path.join(dout, self.id)
     if tree:
         dout = os.path.join(dout, self.date.strftime('%Y%j'))
     mkdir(dout)
     products = self.RequestedProducts(products)
     bname = '%s_%s' % (self.id, self.date.strftime('%Y%j'))
     for p in products.requested:
         if p not in self.sensors:
             # this product is not available for this day
             continue
         sensor = self.sensors[p]
         fin = self.filenames[(sensor, p)]
         fout = os.path.join(dout, "%s_%s_%s.tif" % (bname, sensor, p))
         if not os.path.exists(fout) or overwrite:
             try:
                 if site is not None:
                     # warp just this tile
                     resampler = ['near', 'bilinear', 'cubic']
                     cmd = 'gdalwarp %s %s -t_srs "%s" -tr %s %s -r %s' % \
                            (fin, fout, site.Projection(), res[0], res[1], resampler[interpolation])
                     print cmd
                     #result = commands.getstatusoutput(cmd)
                 else:
                     gippy.GeoImage(fin).Process(fout)
                     #shutil.copyfile(fin, fout)
             except Exception:
                 VerboseOut(traceback.format_exc(), 4)
                 VerboseOut("Problem creating %s" % fout)
     procstr = 'copied' if site is None else 'warped'
     VerboseOut('%s tile %s: %s files %s' % (self.date, self.id, len(products.requested), procstr))