Ejemplo n.º 1
0
 def _write_input_files(self, input_data_list):
     """Write input data files to list."""
     for group_name in self._namelist.get_group_names():
         for variable_name in self._namelist.get_variable_names(group_name):
             input_pathname = self._definition.get_node_element_info(variable_name, "input_pathname")
             if input_pathname is not None:
                 # This is where we end up for all variables that are paths
                 # to input data files.
                 literals = self._namelist.get_variable_value(group_name,
                                                              variable_name)
                 for literal in literals:
                     file_path = character_literal_to_string(literal)
                     if input_pathname == 'abs':
                         # No further mangling needed for absolute paths.
                         pass
                     elif input_pathname.startswith('rel:'):
                         # The part past "rel" is the name of a variable that
                         # this variable specifies its path relative to.
                         root_var = input_pathname[4:]
                         root_dir = self.get_value(root_var)
                         file_path = os.path.join(root_dir, file_path)
                     else:
                         expect(False,
                                "Bad input_pathname value: %s." %
                                input_pathname)
                     # Write to the input data list.
                     input_data_list.write("%s = %s\n" %
                                           (variable_name, file_path))
Ejemplo n.º 2
0
 def _write_input_files(self, input_data_list):
     """Write input data files to list."""
     for group_name in self._namelist.get_group_names():
         for variable_name in self._namelist.get_variable_names(group_name):
             input_pathname = self._definition.get_node_element_info(
                 variable_name, "input_pathname")
             if input_pathname is not None:
                 # This is where we end up for all variables that are paths
                 # to input data files.
                 literals = self._namelist.get_variable_value(
                     group_name, variable_name)
                 for literal in literals:
                     file_path = character_literal_to_string(literal)
                     if input_pathname == 'abs':
                         # No further mangling needed for absolute paths.
                         pass
                     elif input_pathname.startswith('rel:'):
                         # The part past "rel" is the name of a variable that
                         # this variable specifies its path relative to.
                         root_var = input_pathname[4:]
                         root_dir = self.get_value(root_var)
                         file_path = os.path.join(root_dir, file_path)
                     else:
                         expect(
                             False, "Bad input_pathname value: %s." %
                             input_pathname)
                     # Write to the input data list.
                     input_data_list.write("%s = %s\n" %
                                           (variable_name, file_path))
Ejemplo n.º 3
0
    def add_default(self, name, value=None):
        """Add a value for the specified variable to the namelist.

        If the specified variable is already defined in the object, the existing
        value is preserved. Otherwise, the `value` argument, if provided, will
        be used to set the value. If no such value is found, the defaults file
        will be consulted. If null values are present in any of the above, the
        result will be a merged array of values.

        If no value for the variable is found via any of the above, this method
        will raise an exception.
        """
        group = self._definition.get_node_element_info(name, "group")

        # Use this to see if we need to raise an error when nothing is found.
        have_value = False

        # Check for existing value.
        current_literals = self._namelist.get_variable_value(group, name)
        if current_literals != [""]:
            have_value = True

        # Check for input argument.
        if value is not None:
            have_value = True
            literals = self._to_namelist_literals(
                name, value
            )  #FIXME - this is where an array is compressed into a 3*value
            current_literals = merge_literal_lists(literals, current_literals)

        # Check for default value.
        default = self.get_default(name, allow_none=True)
        if default is not None:
            have_value = True
            default_literals = self._to_namelist_literals(name, default)
            current_literals = merge_literal_lists(default_literals,
                                                   current_literals)
        expect(have_value, "No default value found for %s." % name)

        # Go through file names and prepend input data root directory for
        # absolute pathnames.
        var_input_pathname = self._definition.get_input_pathname(name)
        if var_input_pathname == 'abs':
            current_literals = expand_literal_list(current_literals)
            for i, literal in enumerate(current_literals):
                if literal == '':
                    continue
                file_path = character_literal_to_string(literal)
                if file_path == 'UNSET' or file_path == 'idmap':
                    continue
                if file_path == 'null':
                    continue
                file_path = self.set_abs_file_path(file_path)
                expect(os.path.exists(file_path),
                       "File not found: %s = %s" % (name, literal))
                current_literals[i] = string_to_character_literal(file_path)
            current_literals = compress_literal_list(current_literals)

        # Set the new value.
        self._namelist.set_variable_value(group, name, current_literals)
Ejemplo n.º 4
0
 def _write_input_files(self, input_data_list):
     """Write input data files to list."""
     for group_name in self._namelist.get_group_names():
         for variable_name in self._namelist.get_variable_names(group_name):
             input_pathname = self._definition.get_node_element_info(variable_name, "input_pathname")
             if input_pathname is not None:
                 # This is where we end up for all variables that are paths
                 # to input data files.
                 literals = self._namelist.get_variable_value(group_name, variable_name)
                 for literal in literals:
                     file_path = character_literal_to_string(literal)
                     # NOTE - these are hard-coded here and a better way is to make these extensible
                     if file_path == 'UNSET' or file_path == 'idmap':
                         continue
                     if input_pathname == 'abs':
                         # No further mangling needed for absolute paths.
                         # At this point, there are overwrites that should be ignored
                         if not os.path.isabs(file_path):
                             continue
                         else:
                             pass
                     elif input_pathname.startswith('rel:'):
                         # The part past "rel" is the name of a variable that
                         # this variable specifies its path relative to.
                         root_var = input_pathname[4:]
                         root_dir = self.get_value(root_var)
                         file_path = os.path.join(root_dir, file_path)
                     else:
                         expect(False,
                                "Bad input_pathname value: {}.".format(input_pathname))
                     # Write to the input data list.
                     input_data_list.write("{} = {}\n".format(variable_name, file_path))
Ejemplo n.º 5
0
    def add_default(self, name, value=None, ignore_abs_path=None):
        """Add a value for the specified variable to the namelist.

        If the specified variable is already defined in the object, the existing
        value is preserved. Otherwise, the `value` argument, if provided, will
        be used to set the value. If no such value is found, the defaults file
        will be consulted. If null values are present in any of the above, the
        result will be a merged array of values.

        If no value for the variable is found via any of the above, this method
        will raise an exception.
        """
        # pylint: disable=protected-access
        group = self._definition.get_group(name)

        # Use this to see if we need to raise an error when nothing is found.
        have_value = False
        # Check for existing value.
        current_literals = self._namelist.get_variable_value(group, name)

        # Check for input argument.
        if value is not None:
            have_value = True
            # if compression were to occur, this is where it does
            literals = self._to_namelist_literals(name, value)
            current_literals = merge_literal_lists(literals, current_literals)

        # Check for default value.
        default = self.get_default(name, allow_none=True)
        if default is not None:
            have_value = True
            default_literals = self._to_namelist_literals(name, default)
            current_literals = merge_literal_lists(default_literals, current_literals)
        expect(have_value, "No default value found for {}.".format(name))

        # Go through file names and prepend input data root directory for
        # absolute pathnames.
        var_type, _, var_size = self._definition.split_type_string(name)
        if var_type == "character" and ignore_abs_path is None:
            var_input_pathname = self._definition.get_input_pathname(name)
            if var_input_pathname == 'abs':
                current_literals = expand_literal_list(current_literals)
                for i, literal in enumerate(current_literals):
                    if literal == '':
                        continue
                    file_path = character_literal_to_string(literal)
                    # NOTE - these are hard-coded here and a better way is to make these extensible
                    if file_path == 'UNSET' or file_path == 'unset' or file_path == 'idmap':
                        continue
                    if file_path == 'null':
                        continue
                    file_path = self.set_abs_file_path(file_path)
                    if not os.path.exists(file_path):
                        logger.warning("File not found: {} = {}, will attempt to download in check_input_data phase".format(name, literal))
                    current_literals[i] = string_to_character_literal(file_path)
                current_literals = compress_literal_list(current_literals)

        # Set the new value.
        self._namelist.set_variable_value(group, name, current_literals, var_size)
Ejemplo n.º 6
0
    def add_default(self, name, value=None):
        """Add a value for the specified variable to the namelist.

        If the specified variable is already defined in the object, the existing
        value is preserved. Otherwise, the `value` argument, if provided, will
        be used to set the value. If no such value is found, the defaults file
        will be consulted. If null values are present in any of the above, the
        result will be a merged array of values.

        If no value for the variable is found via any of the above, this method
        will raise an exception.
        """
        group = self._definition.get_node_element_info(name, "group")

        # Use this to see if we need to raise an error when nothing is found.
        have_value = False

        # Check for existing value.
        current_literals = self._namelist.get_variable_value(group, name)
        if current_literals != [""]:
            have_value = True

        # Check for input argument.
        if value is not None:
            have_value = True
            literals = self._to_namelist_literals(name, value) #FIXME - this is where an array is compressed into a 3*value
            current_literals = merge_literal_lists(literals, current_literals)

        # Check for default value.
        default = self.get_default(name, allow_none=True)
        if default is not None:
            have_value = True
            default_literals = self._to_namelist_literals(name, default)
            current_literals = merge_literal_lists(default_literals,
                                                   current_literals)
        expect(have_value, "No default value found for %s." % name)

        # Go through file names and prepend input data root directory for
        # absolute pathnames.
        var_input_pathname = self._definition.get_input_pathname(name)
        if var_input_pathname == 'abs':
            current_literals = expand_literal_list(current_literals)
            for i, literal in enumerate(current_literals):
                if literal == '':
                    continue
                file_path = character_literal_to_string(literal)
                if file_path == 'UNSET' or file_path == 'idmap':
                    continue
                if file_path == 'null':
                    continue
                file_path = self.set_abs_file_path(file_path)
                expect(os.path.exists(file_path),
                       "File not found: %s = %s" % (name, literal))
                current_literals[i] = string_to_character_literal(file_path)
            current_literals = compress_literal_list(current_literals)

        # Set the new value.
        self._namelist.set_variable_value(group, name, current_literals)
Ejemplo n.º 7
0
 def _canonicalize_value(type_, value):
     """Create 'canonical' version of a value for comparison purposes."""
     canonical_value = [fortran_namelist_base_value(scalar)
                        for scalar in value]
     canonical_value = [scalar for scalar in canonical_value if scalar != '']
     if type_ == 'character':
         canonical_value = [character_literal_to_string(scalar)
                            for scalar in canonical_value]
     elif type_ == 'integer':
         canonical_value = [int(scalar) for scalar in canonical_value]
     return canonical_value
Ejemplo n.º 8
0
 def _canonicalize_value(type_, value):
     """Create 'canonical' version of a value for comparison purposes."""
     canonical_value = [fortran_namelist_base_value(scalar)
                        for scalar in value]
     canonical_value = [scalar for scalar in canonical_value if scalar != '']
     if type_ == 'character':
         canonical_value = [character_literal_to_string(scalar)
                            for scalar in canonical_value]
     elif type_ == 'integer':
         canonical_value = [int(scalar) for scalar in canonical_value]
     return canonical_value
Ejemplo n.º 9
0
 def _to_python_value(self, name, literals):
     """Transform a literal list as needed for `get_value`."""
     var_type, _, var_size, =  self._definition.split_type_string(name, self._definition.get_type_info(name))
     if len(literals) > 0:
         value = expand_literal_list(literals)
     else:
         value = ''
         return value
     for i, scalar in enumerate(value):
         if scalar == '':
             value[i] = None
         elif var_type == 'character':
             value[i] = character_literal_to_string(scalar)
     if var_size == 1:
         return value[0]
     else:
         return value
Ejemplo n.º 10
0
 def _to_python_value(self, name, literals):
     """Transform a literal list as needed for `get_value`."""
     var_type, _, var_size, = self._definition.split_type_string(name)
     if len(literals) > 0:
         value = expand_literal_list(literals)
     else:
         value = ''
         return value
     for i, scalar in enumerate(value):
         if scalar == '':
             value[i] = None
         elif var_type == 'character':
             value[i] = character_literal_to_string(scalar)
     if var_size == 1:
         return value[0]
     else:
         return value
Ejemplo n.º 11
0
 def _write_input_files(self, data_list_path):
     """Write input data files to list."""
     # append to input_data_list file
     lines_hash = self._get_input_file_hash(data_list_path)
     with open(data_list_path, "a") as input_data_list:
         for group_name in self._namelist.get_group_names():
             for variable_name in self._namelist.get_variable_names(group_name):
                 input_pathname = self._definition.get_node_element_info(variable_name, "input_pathname")
                 if input_pathname is not None:
                     # This is where we end up for all variables that are paths
                     # to input data files.
                     literals = self._namelist.get_variable_value(group_name, variable_name)
                     for literal in literals:
                         file_path = character_literal_to_string(literal)
                         # NOTE - these are hard-coded here and a better way is to make these extensible
                         if file_path == 'UNSET' or file_path == 'idmap':
                             continue
                         if input_pathname == 'abs':
                             # No further mangling needed for absolute paths.
                             # At this point, there are overwrites that should be ignored
                             if not os.path.isabs(file_path):
                                 continue
                             else:
                                 pass
                         elif input_pathname.startswith('rel:'):
                             # The part past "rel" is the name of a variable that
                             # this variable specifies its path relative to.
                             root_var = input_pathname[4:]
                             root_dir = self.get_value(root_var)
                             file_path = os.path.join(root_dir, file_path)
                         else:
                             expect(False,
                                    "Bad input_pathname value: {}.".format(input_pathname))
                         # Write to the input data list.
                         string = "{} = {}".format(variable_name, file_path)
                         hashValue = hashlib.md5(string.rstrip().encode('utf-8')).hexdigest()
                         if hashValue not in lines_hash:
                             logger.debug("Adding line {} with hash {}".format(string,hashValue))
                             input_data_list.write(string+"\n")
                         else:
                             logger.debug("Line already in file {}".format(string))
Ejemplo n.º 12
0
 def _write_input_files(self, input_data_list):
     """Write input data files to list."""
     for group_name in self._namelist.get_group_names():
         for variable_name in self._namelist.get_variable_names(group_name):
             input_pathname = self._definition.get_node_element_info(
                 variable_name, "input_pathname")
             if input_pathname is not None:
                 # This is where we end up for all variables that are paths
                 # to input data files.
                 literals = self._namelist.get_variable_value(
                     group_name, variable_name)
                 for literal in literals:
                     file_path = character_literal_to_string(literal)
                     # NOTE - these are hard-coded here and a better way is to make these extensible
                     if file_path == 'UNSET' or file_path == 'idmap':
                         continue
                     if input_pathname == 'abs':
                         # No further mangling needed for absolute paths.
                         # At this point, there are overwrites that should be ignored
                         if not os.path.isabs(file_path):
                             continue
                         else:
                             pass
                     elif input_pathname.startswith('rel:'):
                         # The part past "rel" is the name of a variable that
                         # this variable specifies its path relative to.
                         root_var = input_pathname[4:]
                         root_dir = self.get_value(root_var)
                         file_path = os.path.join(root_dir, file_path)
                     else:
                         expect(
                             False, "Bad input_pathname value: %s." %
                             input_pathname)
                     # Write to the input data list.
                     input_data_list.write("%s = %s\n" %
                                           (variable_name, file_path))
Ejemplo n.º 13
0
    def add_default(self, name, value=None, ignore_abs_path=None):
        """Add a value for the specified variable to the namelist.

        If the specified variable is already defined in the object, the existing
        value is preserved. Otherwise, the `value` argument, if provided, will
        be used to set the value. If no such value is found, the defaults file
        will be consulted. If null values are present in any of the above, the
        result will be a merged array of values.

        If no value for the variable is found via any of the above, this method
        will raise an exception.
        """
        # pylint: disable=protected-access
        group = self._definition.get_group(name)

        # Use this to see if we need to raise an error when nothing is found.
        have_value = False

        # Check for existing value.
        current_literals = self._namelist.get_variable_value(group, name)
        if current_literals != [""]:
            have_value = True
            # Do not proceed further since this has been obtained the -infile contents
            return

        # Check for input argument.
        if value is not None:
            have_value = True
            # if compression were to occur, this is where it does
            literals = self._to_namelist_literals(name, value)
            current_literals = merge_literal_lists(literals, current_literals)

        # Check for default value.
        default = self.get_default(name, allow_none=True)
        if default is not None:
            have_value = True
            default_literals = self._to_namelist_literals(name, default)
            current_literals = merge_literal_lists(default_literals,
                                                   current_literals)
        expect(have_value, "No default value found for %s." % name)

        # Go through file names and prepend input data root directory for
        # absolute pathnames.
        if ignore_abs_path is None:
            var_input_pathname = self._definition.get_input_pathname(name)
            if var_input_pathname == 'abs':
                current_literals = expand_literal_list(current_literals)
                for i, literal in enumerate(current_literals):
                    if literal == '':
                        continue
                    file_path = character_literal_to_string(literal)
                    # NOTE - these are hard-coded here and a better way is to make these extensible
                    if file_path == 'UNSET' or file_path == 'idmap':
                        continue
                    if file_path == 'null':
                        continue
                    file_path = self.set_abs_file_path(file_path)
                    if not os.path.exists(file_path):
                        logger.warn(
                            "File not found: %s = %s, will attempt to download in check_input_data phase"
                            % (name, literal))
                    current_literals[i] = string_to_character_literal(
                        file_path)
                current_literals = compress_literal_list(current_literals)

        # Set the new value.
        self._namelist.set_variable_value(group, name, current_literals)