Beispiel #1
0
def run_merge(*src, argv=None):
    """
    Run gdal_merge using an external process.

    :param src: The input filenames or GDalDatasetWrapper-objects
    :param argv: The arguments as list, e.g. ["-o", "/path/to/dst"]
    :return: A gdal.Dataset object
    :rtype: ``osgeo.gdal.Dataset``
    """
    tmpdir = tempfile.mkdtemp()
    inputs = []
    for i, drv in enumerate(src):
        if type(drv) != str:
            tmppath = os.path.join(tmpdir, "input_%s.tif" % i)
            drv.write(tmppath)
            inputs.append(tmppath)
        else:
            inputs.append(src)

    if "-o" in argv:
        outpath = argv[argv.index("-o") + 1]
    else:
        outpath = os.path.join(
            tempfile.gettempdir(),
            "%s.tif" % next(tempfile._get_candidate_names()))
        logger.debug("Writing to file %s" % outpath)
        argv += ["-o", outpath]
    argv = gdal.GeneralCmdLineProcessor(argv)
    options = argv + inputs
    assert run_external_app("gdal_merge.py",
                            options) == 0, "Error running gdal_merge"
    remove_directory(tmpdir)
    return gdal.Open(outpath)
Beispiel #2
0
    def create_working_dir(self, dtm, gipps):
        """
        Create a temporary working directory for a single start maja execution
        Then, link all files of the input directory, which are:
            Product(s) (1C/2A)
            GIPPs
            DTM
            (CAMS) if existing
        :param dtm: The DTM object
        :param gipps: The GIPP object
        :return: The full path to the created input directory
        """
        from Common.FileSystem import create_directory, remove_directory
        # Try to remove the directory before proceeding:
        remove_directory(self.input_dir)
        create_directory(self.input_dir)
        create_directory(self.wdir)
        if not os.path.isdir(self.input_dir) or not os.path.isdir(self.wdir):
            raise OSError("Cannot create temp directory %s, %s" %
                          (self.input_dir, self.wdir))
        self.l1.link(self.input_dir)
        for f in self.aux_files:
            f.link(self.input_dir)
        dtm.link(self.input_dir)
        gipps.link(self.input_dir)

        return self.input_dir
Beispiel #3
0
 def execute(self, maja, dtm, gipp, conf):
     """
     Run the workplan with its given parameters
     :param maja: The path to the maja executable
     :param dtm: The DTM object
     :param gipp: The GIPP object
     :param conf: The full path to the userconf folder
     :return: The return code of the Maja app
     """
     from Common.FileSystem import remove_directory
     self.create_working_dir(dtm, gipp)
     l2_prods = self.__get_closest_l2_products()
     if not l2_prods:
         logger.error("Cannot find previous L2 product for date %s in %s" %
                      (self.date, self.outdir))
         if len(self.remaining_l1) >= self.nbackward:
             logging.info("Setting up a BACKWARD execution instead.")
             l1_list = self.remaining_l1[:self.nbackward]
             cams_dates = [prod.date for prod in l1_list + [self.l1]]
             cams_files = self.filter_cams_by_products(
                 self.remaining_cams, cams_dates)
             backup_wp = Backward(self.root,
                                  self.outdir,
                                  self.l1,
                                  l1_list=l1_list,
                                  log_level=self.log_level,
                                  skip_errors=self.skip_errors,
                                  cams=self.aux_files + cams_files)
         else:
             logging.info("Setting up an INIT execution instead.")
             backup_wp = Init(self.root,
                              self.outdir,
                              self.l1,
                              self.log_level,
                              cams=self.aux_files)
         return backup_wp.execute(maja, dtm, gipp, conf)
     if len(l2_prods) > 1:
         logger.info("%s products found for date %s" %
                     (len(l2_prods), self.date))
     self.l2 = self._get_l2_product(l2_prods)
     # Link additional L2 products:
     self.l2.link(self.input_dir)
     return_code = self.launch_maja(maja,
                                    wdir=self.wdir,
                                    inputdir=self.input_dir,
                                    outdir=self.outdir,
                                    conf=conf)
     remove_directory(self.input_dir)
     return return_code
Beispiel #4
0
 def execute(self, maja, dtm, gipp, conf):
     """
     Run the workplan with its given parameters
     :param maja: The path to the maja executable
     :param dtm: The DTM object
     :param gipp: The GIPP object
     :param conf: The full path to the userconf folder
     :return: The return code of the Maja app
     """
     from Common.FileSystem import remove_directory
     self.create_working_dir(dtm, gipp)
     return_code = self.launch_maja(maja,
                                    wdir=self.wdir,
                                    inputdir=self.input_dir,
                                    outdir=self.outdir,
                                    conf=conf)
     remove_directory(self.input_dir)
     return return_code