예제 #1
0
class Level2PreProcArgParser(DefaultLoggingClass):

    def __init__(self):
        super(Level2PreProcArgParser, self).__init__(self.__class__.__name__)
        self.error = ErrorStatus()
        self.pysiral_config = ConfigInfo()
        self._args = None

    def parse_command_line_arguments(self):
        # use python module argparse to parse the command line arguments
        # (first validation of required options and data types)
        self._args = self.parser.parse_args()

    def critical_prompt_confirmation(self):

        # Any confirmation prompts can be overriden by --no-critical-prompt
        no_prompt = self._args.no_critical_prompt

        # if --remove_old is set, all previous l1bdata files will be
        # erased for all month
        if self._args.remove_old and not no_prompt:
            message = "You have selected to remove all previous " + \
                "l2p files for the requested period\n" + \
                "(Note: use --no-critical-prompt to skip confirmation)\n" + \
                "Enter \"YES\" to confirm and continue: "
            result = raw_input(message)

            if result != "YES":
                sys.exit(1)

    @property
    def parser(self):
        # XXX: Move back to caller

        # Take the command line options from default settings
        # -> see config module for data types, destination variables, etc.
        clargs = DefaultCommandLineArguments()

        # List of command line option required for pre-processor
        # (argname, argtype (see config module), destination, required flag)
        options = [
            ("-start", "date", "start_date", False),
            ("-stop", "date", "stop_date", False),
            ("-l2i-product-dir", "l2i-product-dir", "l2i_product_dir", True),
            ("-l2p-output", "l2p-output", "l2p_output", False),
            ("-exclude-month", "exclude-month", "exclude_month", False),
            ("-doi", "doi", "doi", False),
            ("--remove-old", "remove-old", "remove_old", False),
            ("--no-critical-prompt", "no-critical-prompt",
             "no_critical_prompt", False),
            ("--no-overwrite-protection", "no-overwrite-protection",
             "overwrite_protection", False),
            ("--overwrite-protection", "overwrite-protection",
             "overwrite_protection", False)]

        # create the parser
        parser = argparse.ArgumentParser()
        for option in options:
            argname, argtype, destination, required = option
            argparse_dict = clargs.get_argparse_dict(
                argtype, destination, required)
            parser.add_argument(argname, **argparse_dict)

        parser.set_defaults(overwrite_protection=True)

        return parser

    @property
    def arg_dict(self):
        """ Return the arguments as dictionary """
        return self._args.__dict__

    @property
    def start(self):
        return self._args.start_date

    @property
    def stop(self):
        return self._args.stop_date

    @property
    def exclude_month(self):
        return self._args.exclude_month

    @property
    def doi(self):
        return self._args.doi

    @property
    def overwrite_protection(self):
        return self._args.overwrite_protection

    @property
    def l2i_product_dir(self):
        l2i_product_dir = self._args.l2i_product_dir
        if os.path.isdir(l2i_product_dir):
            return os.path.normpath(l2i_product_dir)
        else:
            msg = "Invalid l2i product dir: %s" % str(l2i_product_dir)
            self.error.add_error("invalid-l2i-product-dir", msg)
            self.error.raise_on_error()

    @property
    def l2p_output(self):
        l2p_output = self._args.l2p_output
        filename = self.pysiral_config.get_settings_file("output", "l2p", l2p_output)
        if filename is None:
            msg = "Invalid l2p outputdef filename or id: %s\n" % l2p_output
            msg = msg + " \nRecognized Level-2 output definitions ids:\n"
            l2p_output_ids = self.pysiral_config.get_setting_ids("output", "l2p")
            for l2p_output_id in l2p_output_ids:
                msg = msg + "    - " + l2p_output_id+"\n"
            self.error.add_error("invalid-l2p-outputdef", msg)
            self.error.raise_on_error()
        else:
            return filename

    @property
    def remove_old(self):
        return self._args.remove_old and not self._args.overwrite_protection
예제 #2
0
class Level3ProcArgParser(DefaultLoggingClass):
    def __init__(self):
        super(Level3ProcArgParser, self).__init__(self.__class__.__name__)
        self.error = ErrorStatus()
        self.pysiral_config = ConfigInfo()
        self._args = None

    def parse_command_line_arguments(self):
        # use python module argparse to parse the command line arguments
        # (first validation of required options and data types)
        self._args = self.parser.parse_args()

        # Add addtional check to make sure either `l1b-files` or
        # `start ` and `stop` are set


#        l1b_file_preset_is_set = self._args.l1b_files_preset is not None
#        start_and_stop_is_set = self._args.start_date is not None and \
#            self._args.stop_date is not None
#
#        if l1b_file_preset_is_set and start_and_stop_is_set:
#            self.parser.error("-start & -stop and -l1b-files are exclusive")
#
#        if not l1b_file_preset_is_set and not start_and_stop_is_set:
#            self.parser.error("either -start & -stop or -l1b-files required")

    def critical_prompt_confirmation(self):

        # Any confirmation prompts can be overriden by --no-critical-prompt
        no_prompt = self._args.no_critical_prompt

        # if --remove_old is set, all previous l1bdata files will be
        # erased for all month
        if self._args.remove_old and not no_prompt:
            message = "You have selected to remove all previous " + \
                "l3 files for the requested period\n" + \
                "(Note: use --no-critical-prompt to skip confirmation)\n" + \
                "Enter \"YES\" to confirm and continue: "
            result = raw_input(message)

            if result != "YES":
                sys.exit(1)

    @property
    def parser(self):
        # XXX: Move back to caller

        # Take the command line options from default settings
        # -> see config module for data types, destination variables, etc.
        clargs = DefaultCommandLineArguments()

        # List of command line option required for pre-processor
        # (argname, argtype (see config module), destination, required flag)
        options = [("-l2i-product-dir", "l2i-product-dir",
                    "l2i_basedir", True),
                   ("-l3-settings", "l3-settings", "l3_settings", False),
                   ("-l3-griddef", "l3-griddef", "l3_griddef", True),
                   ("-l3-output", "l3-output", "l3_output", True),
                   ("-start", "date", "start_date", True),
                   ("-stop", "date", "stop_date", True),
                   ("-period", "period", "period", False),
                   ("-doi", "doi", "doi", False),
                   ("-data-record-type", "data_record_type",
                    "data_record_type", False),
                   ("--remove-old", "remove-old", "remove_old", False),
                   ("--no-critical-prompt", "no-critical-prompt",
                    "no_critical_prompt", False)]

        # create the parser
        parser = argparse.ArgumentParser()
        for option in options:
            argname, argtype, destination, required = option
            argparse_dict = clargs.get_argparse_dict(argtype, destination,
                                                     required)
            parser.add_argument(argname, **argparse_dict)

        return parser

    @property
    def arg_dict(self):
        """ Return the arguments as dictionary """
        return self._args.__dict__

    @property
    def start(self):
        return self._args.start_date

    @property
    def stop(self):
        return self._args.stop_date

    @property
    def period(self):
        return self._args.period

    @property
    def doi(self):
        return self._args.doi

    @property
    def data_record_type(self):
        return self._args.data_record_type

    @property
    def l2i_product_directory(self):
        return os.path.join(self.l3_product_basedir, "l2i")

    @property
    def l3_settings_file(self):
        l3_settings = self._args.l3_settings
        filename = self.pysiral_config.get_settings_file(
            "proc", "l3", l3_settings)
        if filename is None:
            msg = "Invalid l3 settings filename or id: %s\n" % l3_settings
            msg = msg + " \nRecognized Level-3 processor setting ids:\n"
            for l3_settings_id in self.pysiral_config.get_setting_ids(
                    "proc", "l3"):
                msg = msg + "  " + l3_settings_id + "\n"
            self.error.add_error("invalid-l3-settings", msg)
            self.error.raise_on_error()
        else:
            return filename

    @property
    def l3_griddef(self):
        l3_griddef = self._args.l3_griddef
        filename = self.pysiral_config.get_settings_file(
            "grid", None, l3_griddef)
        if filename is None:
            msg = "Invalid griddef filename or id: %s\n" % l3_griddef
            msg = msg + "    Recognized grid definition ids:\n"
            for griddef_id in self.pysiral_config.get_setting_ids("griddef"):
                msg = msg + "    - " + griddef_id + "\n"
            self.error.add_error("invalid-griddef", msg)
            self.error.raise_on_error()
        else:
            return filename

    @property
    def l3_output_file(self):
        l3_output = self._args.l3_output
        filename = self.pysiral_config.get_settings_file(
            "output", "l3", l3_output)
        if filename is None:
            msg = "Invalid output definition filename or id: %s\n" % l3_output
            msg = msg + "    Recognized output definition ids:\n"
            for output_id in self.pysiral_config.get_setting_ids(
                    "output", "l3"):
                msg = msg + "    - " + output_id + "\n"
            self.error.add_error("invalid-outputdef", msg)
            self.error.raise_on_error()
        else:
            return filename

    @property
    def l3_product_basedir(self):
        """ Returns the base directory (one level below l2i) """
        # 1. Clean up the path
        product_basedir = os.path.abspath(self._args.l2i_basedir)
        dirs = os.path.split(product_basedir)
        if dirs[1] == "l2i":
            return dirs[0]
        else:
            return product_basedir

    @property
    def remove_old(self):
        return self._args.remove_old and not self._args.overwrite_protection
예제 #3
0
class Level2ProcArgParser(DefaultLoggingClass):
    def __init__(self):
        super(Level2ProcArgParser, self).__init__(self.__class__.__name__)
        self.error = ErrorStatus()
        self.pysiral_config = ConfigInfo()
        self._args = None

    def parse_command_line_arguments(self):
        # use python module argparse to parse the command line arguments
        # (first validation of required options and data types)
        self._args = self.parser.parse_args()

        # Add additional check to make sure either `l1b-files` or
        # `start ` and `stop` are set
        l1b_file_preset_is_set = self._args.l1b_files_preset is not None
        start_and_stop_is_set = self._args.start_date is not None and \
            self._args.stop_date is not None

        if l1b_file_preset_is_set and start_and_stop_is_set:
            self.parser.error("-start & -stop and -l1b-files are exclusive")

        if not l1b_file_preset_is_set and not start_and_stop_is_set:
            self.parser.error("either -start & -stop or -l1b-files required")

    def critical_prompt_confirmation(self):

        # Any confirmation prompts can be overridden by --no-critical-prompt
        no_prompt = self._args.no_critical_prompt

        # if --remove_old is set, all previous l1bdata files will be
        # erased for all month
        if self._args.remove_old and not no_prompt:
            message = "You have selected to remove all previous " + \
                "l2 files for the requested period\n" + \
                "(Note: use --no-critical-prompt to skip confirmation)\n" + \
                "Enter \"YES\" to confirm and continue: "
            result = raw_input(message)

            if result != "YES":
                sys.exit(1)

    @property
    def parser(self):
        # XXX: Move back to caller

        # Take the command line options from default settings
        # -> see config module for data types, destination variables, etc.
        clargs = DefaultCommandLineArguments()

        # List of command line option required for pre-processor
        # (argname, argtype (see config module), destination, required flag)
        options = [("-l2-settings", "l2-settings", "l2_settings", True),
                   ("-run-tag", "run-tag", "run_tag", False),
                   ("-start", "date", "start_date", False),
                   ("-stop", "date", "stop_date", False),
                   ("-l1b-files", "l1b_files", "l1b_files_preset", False),
                   ("-exclude-month", "exclude-month", "exclude_month", False),
                   ("-input-version", "input-version", "input_version", False),
                   ("-l2-output", "l2-output", "l2_output", False),
                   ("--remove-old", "remove-old", "remove_old", False),
                   ("--no-critical-prompt", "no-critical-prompt",
                    "no_critical_prompt", False),
                   ("--no-overwrite-protection", "no-overwrite-protection",
                    "overwrite_protection", False),
                   ("--overwrite-protection", "overwrite-protection",
                    "overwrite_protection", False)]

        # create the parser
        parser = argparse.ArgumentParser()
        for option in options:
            argname, argtype, destination, required = option
            argparse_dict = clargs.get_argparse_dict(argtype, destination,
                                                     required)
            parser.add_argument(argname, **argparse_dict)

        parser.set_defaults(overwrite_protection=True)

        return parser

    @property
    def arg_dict(self):
        """ Return the arguments as dictionary """
        return self._args.__dict__

    @property
    def start(self):
        return self._args.start_date

    @property
    def stop(self):
        return self._args.stop_date

    @property
    def run_tag(self):
        """ run_tag is a str or relative path that determines the output directory for
        the Level-2 processor. If the -run-tag option is not specified, the output
        directory will be the `product_repository` specification in `local_machine_def`
        with the l2 settings file basename as subfolder.

        One can however specify a custom string, or a relative path, with subfolders
        defined by `\` or `/`, e.g.

        Examples:
            -run-tag cs2awi_v2p0_nrt
            -run-tag c3s/cdr/cryosat2/v1p0/nh
        """

        # Get from command line arguments (default: None)
        run_tag = self._args.run_tag

        # If argument is empty use the basename of the l2 settings file
        if run_tag is None:
            run_tag = self._args.l2_settings
            # Settings file may be specified as full path and not just the id
            if os.path.isfile(run_tag):
                run_tag = file_basename(run_tag)

        # split the run-tag on potential path separators
        run_tag = re.split(r'[\\|/]', run_tag)
        return run_tag

    @property
    def exclude_month(self):
        return self._args.exclude_month

    @property
    def overwrite_protection(self):
        return self._args.overwrite_protection

    @property
    def l2_settings_file(self):
        l2_settings = self._args.l2_settings
        filename = self.pysiral_config.get_settings_file(
            "proc", "l2", l2_settings)
        if filename is None:
            msg = "Invalid l2 settings filename or id: %s\n" % l2_settings
            msg = msg + " \nRecognized Level-2 processor setting ids:\n"
            for l2_settings_id in self.pysiral_config.get_setting_ids(
                    "proc", "l2"):
                msg = msg + "  " + l2_settings_id + "\n"
            self.error.add_error("invalid-l2-settings", msg)
            self.error.raise_on_error()
        else:
            return filename

    @property
    def l1b_version(self):
        return self._args.input_version

    @property
    def l1b_predef_files(self):
        l1b_files = glob.glob(self._args.l1b_files_preset)
        return l1b_files

    @property
    def l2_output(self):
        l2_output = self._args.l2_output
        filename = self.pysiral_config.get_settings_file(
            "output", "l2i", l2_output)
        if filename is None:
            msg = "Invalid l2 outputdef filename or id: %s\n" % l2_output
            msg = msg + " \nRecognized Level-2 output definitions ids:\n"
            l2_output_ids = self.pysiral_config.get_setting_ids(
                "output", "l2i")
            for l2_output_id in l2_output_ids:
                msg = msg + "    - " + l2_output_id + "\n"
            self.error.add_error("invalid-l2-outputdef", msg)
            self.error.raise_on_error()
        else:
            return filename

    @property
    def is_time_range_request(self):
        return self._args.l1b_files_preset is None

    @property
    def remove_old(self):
        return self._args.remove_old and not self._args.overwrite_protection