Exemple #1
0
    def apply_fix(self):
        """
        Fix the affected file

        This should replicate the Unix commands:

        ncap2 -h -s 'tos=tos-273.15f' filename.nc filename.nc.temp
        rm filename.nc
        mv filename.nc.temp filename.nc
        ncatted -h -a units,tos,m,c,'degC' filename.nc
        """
        if not self._is_kelvin():
            raise ExistingAttributeError(self.filename, 'units',
                                         'Units are not K.')

        self.command = (f"ncap2 -h -s '{self.variable_name}="
                        f"{self.variable_name}-273.15f'")
        self._run_nco_command(Ncap2Error)
        units_command = (
            f"ncatted -h -a units,{self.variable_name},m,c,'degC' "
            f"{os.path.join(self.directory, self.filename)}")
        try:
            run_command(units_command)
        except Exception:
            raise NcattedError(
                type(self).__name__, self.filename, units_command,
                traceback.format_exc())
Exemple #2
0
 def _calculate_new_value(self):
     """
     Check the existing attribute is `AOGCM` and change it to `AGCM`.
     """
     if self.existing_value != 'AOGCM':
         raise ExistingAttributeError(self.filename, self.attribute_name,
                                      'Existing value is not AOGCM.')
     else:
         self.new_value = 'AGCM'
Exemple #3
0
    def _calculate_new_value(self):
        """
        Convert the tracking_id to the correct form.
        """
        if self.existing_value.startswith('hdl:'):
            raise ExistingAttributeError(self.filename, self.attribute_name,
                                         'Existing tracking_id attribute '
                                         'starts with hdl:')

        self.new_value = 'hdl:21.14100/{}'.format(self.existing_value)
Exemple #4
0
    def _calculate_new_value(self):
        """
        The new value is the existing string converted to a double.
        """
        if not self.existing_value.startswith('http:'):
            raise ExistingAttributeError(self.filename, self.attribute_name,
                                         'Existing further_info_url attribute '
                                         'does not start with http:')

        self.new_value = self.existing_value.replace('http:', 'https:', 1)
Exemple #5
0
    def apply_fix(self):
        """
        Run ncpdq and then swap the columns in lat_bnds.
        """
        # check that the latitude is decreasing and that the fix is actually
        # needed
        if not self._is_lat_decreasing():
            raise ExistingAttributeError(self.filename, 'latitude',
                                         'Latitude is not decreasing.')
        self.command = 'ncpdq -a -lat'
        self._run_nco_command(NcpdqError)

        original_nc = os.path.join(self.directory, self.filename)
        bnds_file = original_nc + '.bnds'
        corrected_bnds_file = bnds_file + '_corr'

        # Remove any temporary files left over from a previous failed
        # run as they can prevent ncks and ncpdq from running.
        for filename in (bnds_file, corrected_bnds_file):
            if os.path.exists(filename):
                os.remove(filename)

        commands = [
            # Copy lat_bnds to a new file
            f'ncks -v lat_bnds {original_nc} {bnds_file}',
            # Swap the colums
            f'ncpdq -a -bnds {bnds_file} {corrected_bnds_file}',
            # Remove history from lat_bnds as this is pasted back into the file
            f'ncatted -h -a history,global,d,, {corrected_bnds_file}',
            # Paste the fixed bnds back into the original file
            f'ncks -A -v lat_bnds {corrected_bnds_file} {original_nc}'
        ]
        for cmd in commands:
            try:
                run_command(cmd)
            except Exception:
                exceptions = {
                    'ncks': NcksError,
                    'ncpdq': NcpdqError,
                    'ncatted': NcattedError
                }
                cmd_name = cmd.split()[0]
                raise exceptions[cmd_name](type(self).__name__, self.filename,
                                           cmd, traceback.format_exc())
        os.remove(bnds_file)
        os.remove(corrected_bnds_file)
Exemple #6
0
    def _calculate_new_value(self):
        """
        The new value is the existing string converted to a double.
        """
        if not self.existing_value.startswith(
                'https://furtherinfo.es-doc.org/CMIP6'):
            raise ExistingAttributeError(
                self.filename,
                self.attribute_name,
                'Existing further_info_url attribute does not start with '
                'https://furtherinfo.es-doc.org/CMIP6'
            )

        self.new_value = self.existing_value.replace(
            'https://furtherinfo.es-doc.org/CMIP6',
            'https://furtherinfo.es-doc.org/PRIMAVERA',
            1
        )