Exemplo n.º 1
0
    def pre(self, otb_app, parameters):
        """

        :param parameters:
        :param otb_app:
        :return:
        """
        # The following line creates an instance of the BandMath application
        self.otb_app = otbApplication.Registry_CreateApplicationWithoutLogger(
            otb_app)

        if self.otb_app is None:
            raise MajaDriverException("No app " + otb_app + " found")

        self.otb_app.Init()
        # The following lines set all the application parameters:
        LOGGER.debug("Parameters for %s : %s", otb_app, parameters)
        LOGGER.debug(
            "Parameters type %s", {
                key: (type(value), value.__class__.__name__)
                for key, value in parameters.items()
            })

        # First set input image
        if "in" in parameters:
            parameters_im = {"in": parameters["in"]}
            self.otb_app.SetParameters(parameters_im)
            if not get_test_mode():
                self.otb_app.UpdateParameters()
        # remove flag if set to false
        parameters_clean = {}
        for key, value in parameters.items():
            if key in self.otb_app.GetParametersKeys():
                if self.otb_app.GetParameterType(key) == otbApplication.ParameterType_OutputImage \
                        and len(value.split(":")) > 1:
                    # split value into value and output type
                    if value.split(":")[1] in OTB_APP_PIXELS_TYPE.keys():
                        parameters_clean[key] = (value.split(":")[0])
                        self.otb_app.SetParameterOutputImagePixelType(
                            key, OTB_APP_PIXELS_TYPE.get(value.split(":")[1]))
                    else:
                        parameters_clean[key] = value
                else:
                    parameters_clean[key] = value
            else:
                LOGGER.debug("%s removed because not in app list", key)
                continue

        self.otb_app.SetParameters(parameters_clean)
        # Update parameters for dynamics
        if not get_test_mode():
            self.otb_app.UpdateParameters()
Exemplo n.º 2
0
 def post(self, write_output=True):
     LOGGER.debug("Write output %s", write_output)
     params_keys = self.otb_app.GetParametersKeys()
     for param in params_keys:
         # role == 1 -> output
         if self.otb_app.GetParameterRole(param) == 1 or self.otb_app.GetParameterType(
                 param) == otbApplication.ParameterType_OutputImage:
             if get_test_mode():
                 if self.otb_app.GetParameterType(param) == otbApplication.ParameterType_Int:
                     self.outputs[param] = 1
                 elif self.otb_app.GetParameterType(param) == otbApplication.ParameterType_Float:
                     self.outputs[param] = 1.0
                 elif self.otb_app.GetParameterType(param) == otbApplication.ParameterType_String:
                     self.outputs[param] = "false"
                 else:
                     self.outputs[param] = copy.deepcopy(self.otb_app.GetParameterValue(param))
             else:
                 if write_output:
                     if not self.otb_app.GetParameterType(param) == otbApplication.ParameterType_Group:
                         self.outputs[param] = copy.deepcopy(self.otb_app.GetParameterValue(param))
                 else:
                     if self.otb_app.GetParameterType(param) == otbApplication.ParameterType_OutputImage:
                         self.outputs[param] = self.otb_app.GetParameterOutputImage(param)
                     else:
                         if not self.otb_app.GetParameterType(param) == otbApplication.ParameterType_Group:
                             self.outputs[param] = copy.deepcopy(self.otb_app.GetParameterValue(param))
     if write_output:
         self.otb_app.FreeRessources()
         del self.otb_app
         self.otb_app = None
Exemplo n.º 3
0
def copy_file_to_directory(sourcefilename, destinationdir, notestmode=False):
    destinationfilename = os.path.join(destinationdir, os.path.basename(sourcefilename))
    if not get_test_mode() or notestmode:
        shutil.copyfile(sourcefilename, destinationfilename)
    else:
        with open(destinationfilename, 'a'):
            os.utime(destinationfilename, None)
    return destinationfilename
Exemplo n.º 4
0
def extract_roi(input_file_path, channels, output_image, write_output=True):
    parameters = {
        "in": input_file_path,
        "cl": ["Channel" + str(idx + 1) for idx in channels],
        "out": output_image
    }
    if get_test_mode():
        parameters.pop("cl")
    app = OtbAppHandler("ExtractChannels", parameters, write_output)

    return app
Exemplo n.º 5
0
def otb_copy_file_to_directory(sourceFilename, destinationDir):
    destinationFilename = os.path.join(destinationDir, os.path.basename(sourceFilename))
    if not get_test_mode():
        if type(sourceFilename).__name__ == "SwigPyObject":
            write_images([sourceFilename], [destinationFilename])
        else:
            shutil.copyfile(sourceFilename, destinationFilename)
    else:
        with open(destinationFilename, 'a'):
            os.utime(destinationFilename, None)
    return destinationFilename
Exemplo n.º 6
0
 def __init__(self, otb_app, parameters, write_output=True):
     self._c1 = MajaOtbCots()
     self._app_name = otb_app
     LOGGER.debug("Initializing : " + self._app_name)
     if "ram" not in list(parameters.keys()):
         curr_ram = int(memory_used_by_process_current(os.getpid()))
         avail_ram = OtbAppHandler.ram_to_use - curr_ram
         if avail_ram < OtbAppHandler.ram_to_use/OtbAppHandler.ram_limit_factor:
             parameters["ram"] = str(OtbAppHandler.ram_to_use / OtbAppHandler.ram_limit_factor)
         else:
             parameters["ram"] = str(avail_ram)
     LOGGER.debug(parameters)
     self._write_output = not is_croco_off() and (write_output or is_croco_on())
     self._c1.pre(otb_app, parameters)
     if not get_test_mode():
         self._run()
     self._post()
Exemplo n.º 7
0
def otb_copy_image_to_file(source, dest, raise_exceptions=True):
    if not get_test_mode():
        if type(source).__name__ == "SwigPyObject":
            write_images([source], [dest])
        else:
            LOGGER.debug("source : " + source + " , dest : " + dest)
            try:
                if not os.path.exists(dest) or not os.path.samefile(source, dest):
                    shutil.copyfile(source, dest)
            except IOError as err:
                if raise_exceptions:
                    raise MajaIOError(err)
                else:
                    LOGGER.warn("Copy failed !!!")

    else:
        with open(dest, 'a'):
            os.utime(dest, None)
    def run_rasterize(self, inputfilename, xmin, ymin, xmax, ymax, size_x, size_y, projection,
                      outputfilename, gdaladditionalcommandlineparameters):
        """

        :param inputfilename: string
        :param xmin: int
        :param ymin: int
        :param xmax: int
        :param ymax: int
        :param size_x: int
        :param size_y: int
        :param projection: string
        :param outputfilename: string
        :param gdaladditionalcommandlineparameters: string
        :return:
        """
        # Call the gdal_rasterize command system with the specific parameters
        self.command_line = (
            """gdal_rasterize """ +
            gdaladditionalcommandlineparameters +
            """ -te """ +
            str(xmin) +
            """ """ +
            str(ymin) +
            """ """ +
            str(xmax) +
            """ """ +
            str(ymax) +
            """ -ts """ +
            str(size_x) +
            """ """ +
            str(size_y) +
            """ -a_srs '""" +
            projection +
            """ ' """ +
            inputfilename +
            """ """ +
            outputfilename)
        LOGGER.debug(self.command_line)
        if not get_test_mode():
            self.run()
        return self.status
Exemplo n.º 9
0
def copy_file(source, dest, notestmode=False):
    if not get_test_mode() or notestmode:
        shutil.copyfile(source, dest)
    else:
        with open(dest, 'a'):
            os.utime(dest, None)
Exemplo n.º 10
0
    def extract_cams_datas(self, corner_lat, corner_lon, p_imageproductaquisitiontimejd):
        self.valid = False
        if not self.searchvalidcamsfilenames(p_imageproductaquisitiontimejd):
            LOGGER.info("No CAM found for JD Date " + str(p_imageproductaquisitiontimejd))
            return
        
        if p_imageproductaquisitiontimejd < self._cams_46r1_switch:
            try:
                self._models.remove("AMMONIUM")
                self._models.remove("NITRATE")
                LOGGER.debug("Removed 46r1 models.")
            except ValueError:
                LOGGER.warning("Cannot remove 46r1 models. CAMS models present: %s" % self._models)

        cams_app_param = {"models": self._models,
                          "rhsampling": self._rhsampling,
                          "limaod": self._limaod,
                          "lat": corner_lat,
                          "lon": corner_lon,
                          "datejulian": p_imageproductaquisitiontimejd,
                          "sumofaot": os.path.join(self._working_dir, "tmpaot.tiff")
                          }

        # Create the xml instance of cams app configuration file
        xml_model_list = ModelListType()
        for model, mapex in list(self._extinction_map.items()):
            xml_model = ModelType()
            xml_extinction_coeff_list = Extinction_Coefs_ListType()
            count = 1
            for ext in mapex["extinction_coeffs"]:
                xml_extinction_coeff = Extinction_CoefType()
                xml_extinction_coeff.set_Name(ext["Name"])
                xml_extinction_coeff.set_Values(ext["Values"])
                xml_extinction_coeff.set_Description(ext["Description"])
                xml_extinction_coeff.set_n(count)
                count = count + 1
                xml_extinction_coeff_list.add_Extinction_Coef(xml_extinction_coeff)
            xml_extinction_coeff_list.set_count(len(model))
            xml_model.set_RhDep(bool(mapex["rh_dep"]))
            xml_model.set_Name(model)
            xml_model.set_Extinction_Coefs_List(xml_extinction_coeff_list)
            xml_model_list.add_Model(xml_model)
        xml_cams = CAMS()
        xml_cams.set_ModelList(xml_model_list)
        xml_cams.set_RhTab(self._rhtab)

        # build file
        output = io.StringIO()
        output.write('<?xml version="1.0" ?>\n')
        xml_cams.export(output, 0, name_='CAMS', namespacedef_='', pretty_print=True)
        xml_filename = os.path.join(self._working_dir, "cams_description.xml")
        with open(xml_filename, "w") as fh:
            fh.write(output.getvalue().replace("    ", "  "))
            LOGGER.info("Writed cams description to " + xml_filename)

        cams_app_param["camsdescription"] = xml_filename

        if self._beforefilefound:
            cams_app_param["before.dateutc"] = self._beforeFile["date_utc"]
            cams_app_param["before.aotfile"] = self._beforeFile["aot_file"]
            cams_app_param["before.mrfile"] = self._beforeFile["mr_file"]
            cams_app_param["before.rhfile"] = self._beforeFile["rh_file"]
            cams_app_param["before.modellevels"] = self._beforeFile["model_levels"]
            if "NbNonInterpolate" in  self._beforeFile.keys():
                cams_app_param["before.nbnointerpol"] = self._beforeFile["NbNonInterpolate"]

        if self._afterfilefound:
            cams_app_param["after.dateutc"] = self._afterFile["date_utc"]
            cams_app_param["after.aotfile"] = self._afterFile["aot_file"]
            cams_app_param["after.mrfile"] = self._afterFile["mr_file"]
            cams_app_param["after.rhfile"] = self._afterFile["rh_file"]
            cams_app_param["after.modellevels"] = self._afterFile["model_levels"]
            if "NbNonInterpolate" in  self._afterFile.keys():
                cams_app_param["after.nbnointerpol"] = self._afterFile["NbNonInterpolate"]

        self.__camsapp = OtbAppHandler("CAMSComputation", cams_app_param, write_output=True)

        l_app_proportions = self.__camsapp.getoutput().get("proportions")
        if get_test_mode():
            for m in xml_model_list.get_Model():
                self.proportions[m.get_Name()] = 1.0 / len(xml_model_list.get_Model())
        else:
            for p in l_app_proportions.split(";"):
                if len(p) != 0:
                    l_model = p.split("=")[0]
                    l_value = p.split("=")[1]
                    self.proportions[l_model] = l_value
        self.out_rh_sampling = self.__camsapp.getoutput().get("rh")
        self.valid = True
        self.out_sum_of_aot = self.__camsapp.getoutput().get("sumofaot")