Beispiel #1
0
    def _update_file_attribute(self):
        """
        Renamr the variable inside the file. Assume the file has its original
        path and name.
        """
        if self.temp_dir:
            orig_path = os.path.join(self.old_directory, self.old_filename)
            temp_dir = tempfile.mkdtemp(dir=self.temp_dir)
            temp_path = os.path.join(temp_dir, self.old_filename)
            shutil.copyfile(orig_path, temp_path)
            working_dir = temp_dir
        else:
            working_dir = self.old_directory

        run_ncrename(working_dir, self.old_filename,
                     self.datafile.variable_request.cmor_name,
                     self.datafile.variable_request.out_name, False)
        run_ncatted(working_dir, self.old_filename, 'variable_id', 'global',
                    'c', self.datafile.variable_request.out_name, True)

        if self.temp_dir:
            os.rename(orig_path, orig_path + '.old')
            shutil.copyfile(temp_path, orig_path)
            os.remove(orig_path + '.old')
            os.remove(temp_path)
            os.rmdir(temp_dir)
Beispiel #2
0
    def _update_file_attribute(self):
        """
        Update the source_id and make the same change in the further_info_url.
        Assume the file has its original path and name.
        """
        if self.temp_dir:
            orig_path = os.path.join(self.old_directory, self.old_filename)
            temp_dir = tempfile.mkdtemp(dir=self.temp_dir)
            temp_path = os.path.join(temp_dir, self.old_filename)
            shutil.copyfile(orig_path, temp_path)
            working_dir = temp_dir
        else:
            working_dir = self.old_directory

        # source_id
        run_ncatted(working_dir, self.old_filename, 'mip_era', 'global', 'c',
                    self.new_value, False)

        # further_info_url
        further_info_url = ('https://furtherinfo.es-doc.org/{}.{}.{}.{}.none.'
                            '{}'.format(self.new_value,
                                        self.datafile.institute.short_name,
                                        self.datafile.climate_model.short_name,
                                        self.datafile.experiment.short_name,
                                        self.datafile.rip_code))
        run_ncatted(working_dir, self.old_filename, 'further_info_url',
                    'global', 'c', further_info_url, False)

        if self.temp_dir:
            os.rename(orig_path, orig_path + '.old')
            shutil.copyfile(temp_path, orig_path)
            os.remove(orig_path + '.old')
            os.remove(temp_path)
            os.rmdir(temp_dir)
Beispiel #3
0
 def test_with_history(self):
     run_ncatted('/a',
                 'b.nc',
                 'source_id',
                 'global',
                 'c',
                 'better-model',
                 suppress_history=False)
     self.mock_run_cmd.assert_called_once_with(
         "ncatted -a source_id,global,o,c,'better-model' /a/b.nc")
Beispiel #4
0
    def _update_file_attribute(self):
        """
        Update the source_id and make the same change in the further_info_url.
        Assume the file has its original path and name.
        """
        # source_id
        run_ncatted(self.old_directory, self.old_filename, 'source_id',
                    'global', 'c', self.new_value, False)

        # further_info_url
        further_info_url = ('https://furtherinfo.es-doc.org/{}.{}.{}.{}.none.'
                            '{}'.format(self.datafile.project.short_name,
                                        self.datafile.institute.short_name,
                                        self.new_value,
                                        self.datafile.experiment.short_name,
                                        self.datafile.rip_code))
        run_ncatted(self.old_directory, self.old_filename, 'further_info_url',
                    'global', 'c', further_info_url, False)
Beispiel #5
0
 def test_int(self):
     run_ncatted('/a', 'b.nc', 'source_id', 'global', 'd', 123)
     self.mock_run_cmd.assert_called_once_with(
         "ncatted -h -a source_id,global,o,d,123 /a/b.nc")
Beispiel #6
0
 def test_var_attr(self):
     run_ncatted('/a', 'b.nc', 'cell_methods', 'tas', 'c', 'better-method')
     self.mock_run_cmd.assert_called_once_with(
         "ncatted -h -a cell_methods,tas,o,c,'better-method' /a/b.nc")
Beispiel #7
0
 def test_global_attr(self):
     run_ncatted('/a', 'b.nc', 'source_id', 'global', 'c', 'better-model')
     self.mock_run_cmd.assert_called_once_with(
         "ncatted -h -a source_id,global,o,c,'better-model' /a/b.nc")
Beispiel #8
0
    def _update_file_attribute(self):
        """
        Update the variant_label and make the same change in its constituent
        parts and the further_info_url. Assume the file has its original path
        and name.
        """
        # variant_label
        run_ncatted(self.old_directory, self.old_filename, 'variant_label',
                    'global', 'c', self.new_value, False)

        # indexes
        ripf = re.match(r'^r(\d+)i(\d+)p(\d+)f(\d+)$', self.new_value)
        run_ncatted(self.old_directory, self.old_filename, 'realization_index',
                    'global', 's', int(ripf.group(1)), False)
        run_ncatted(self.old_directory,
                    self.old_filename, 'initialization_index', 'global', 's',
                    int(ripf.group(2)), False)
        run_ncatted(self.old_directory, self.old_filename, 'physics_index',
                    'global', 's', int(ripf.group(3)), False)
        run_ncatted(self.old_directory, self.old_filename, 'forcing_index',
                    'global', 's', int(ripf.group(4)), False)

        # further_info_url
        further_info_url = ('https://furtherinfo.es-doc.org/{}.{}.{}.{}.none.'
                            '{}'.format(self.datafile.project.short_name,
                                        self.datafile.institute.short_name,
                                        self.datafile.climate_model.short_name,
                                        self.datafile.experiment.short_name,
                                        self.new_value))
        run_ncatted(self.old_directory, self.old_filename, 'further_info_url',
                    'global', 'c', further_info_url, False)
Beispiel #9
0
    def _update_file_attribute(self):
        """
        Update the institution_id and make the same change in further_info_url,
        institution and license.
        Assume the file has its original path and name.
        """
        if self.temp_dir:
            orig_path = os.path.join(self.old_directory, self.old_filename)
            temp_dir = tempfile.mkdtemp(dir=self.temp_dir)
            temp_path = os.path.join(temp_dir, self.old_filename)
            shutil.copyfile(orig_path, temp_path)
            working_dir = temp_dir
        else:
            working_dir = self.old_directory

        # institution_id
        run_ncatted(working_dir, self.old_filename, 'institution_id', 'global',
                    'c', self.new_value, False)

        # institution
        new_insts = {
            'MOHC':
            'Met Office Hadley Centre, Fitzroy Road, Exeter, Devon, '
            'EX1 3PB, UK',
            'NERC':
            'Natural Environment Research Council, STFC-RAL, Harwell, '
            'Oxford, OX11 0QX, UK'
        }
        inst = new_insts[self.new_value]
        run_ncatted(working_dir, self.old_filename, 'institution', 'global',
                    'c', inst)

        # further_info_url
        further_info_url = (
            'https://furtherinfo.es-doc.org/{}.{}.{}.{}.none.{}'.format(
                self.datafile.project.short_name, self.new_value,
                self.datafile.climate_model.short_name,
                self.datafile.experiment.short_name, self.datafile.rip_code))
        run_ncatted(working_dir, self.old_filename, 'further_info_url',
                    'global', 'c', further_info_url)

        # license
        license_txt = (
            f'CMIP6 model data produced by {self.new_value} is licensed under '
            f'a Creative Commons Attribution-ShareAlike 4.0 International '
            f'License (https://creativecommons.org/licenses). Consult '
            f'https://pcmdi.llnl.gov/CMIP6/TermsOfUse for terms of use '
            f'governing CMIP6 output, including citation requirements and '
            f'proper acknowledgment. Further information about this data, '
            f'including some limitations, can be found via the '
            f'further_info_url (recorded as a global attribute in this file). '
            f'The data producers and data providers make no warranty, either '
            f'express or implied, including, but not limited to, warranties '
            f'of merchantability and fitness for a particular purpose. All '
            f'liabilities arising from the supply of the information '
            f'(including any liability arising in negligence) are excluded to '
            f'the fullest extent permitted by law.')
        run_ncatted(working_dir, self.old_filename, 'license', 'global', 'c',
                    license_txt)

        if self.temp_dir:
            os.rename(orig_path, orig_path + '.old')
            shutil.copyfile(temp_path, orig_path)
            os.remove(orig_path + '.old')
            os.remove(temp_path)
            os.rmdir(temp_dir)