def test_missing_file(self): dir_path = tempfile.mkdtemp() try: file_path = os.path.join(dir_path, 'missing') with six.assertRaisesRegex(self, IOError, 'No such file'): UMFile.from_file(file_path) finally: shutil.rmtree(dir_path)
def test_copy_bypath(self): ffv = UMFile.from_file(COMMON_N48_TESTDATA_PATH) with self.temp_filename() as temp_path: ffv.to_file(temp_path) assert os.path.exists(temp_path) # Read it back and repeat our basic "known content" tests ffv_rb = UMFile.from_file(temp_path) check_common_n48_testdata(self, ffv_rb)
def test_copy_byfile(self): ffv = UMFile() with self.temp_filename() as temp_path: with open(temp_path, 'wb') as temp_file: ffv.to_file(temp_file) assert os.path.exists(temp_path) # Read it back and repeat our basic "known content" tests with warnings.catch_warnings(): msg_exp = r".*Fixed length header does not define.*" warnings.filterwarnings("ignore", msg_exp) ffv_rb = UMFile.from_file(temp_path) _check_minimal_file(self, ffv_rb)
def rotate_pole(mf: mule.UMFile, lat: float = None, lon: float = None): """ Rotate the pole of a UM file Args: mf: Input UM file lat (float): Pole latitude lon (float): Pole longitude Returns: A copy of 'mf' with the pole rotated """ out = mf.copy() if lat is not None: out.real_constants.north_pole_lat = lat if lon is not None: out.real_constants.north_pole_lon = lon op = RotatePoleOp(lat, lon) for f in mf.fields: out.fields.append(op(f)) return out
def test_generic_load_all(self): # Check each datafile will load as UMFile, and still look the same. for path, name, filetype in zip(TESTFILE_PATHS, TESTFILE_NAMES, TESTFILE_TYPES): # Skip the pp files (they don't work the same at all) if filetype == "pp": continue # Check you can load it as a generic UMFile. ffv = UMFile.from_file(path) typeclass = _UM_FILE_TYPES.get(filetype) if not typeclass: msg = 'unrecognised file extension ? : {}' raise ValueError(msg.format(filetype)) # Check you can save it, and it loads back as the expected type. with self.temp_filename() as temp_filepath: ffv.to_file(temp_filepath) _ = typeclass.from_file(temp_filepath) # For good measure, check it still looks as expected, . self._file_specific_check(name, ffv)
def test_byfile(self): with open(COMMON_N48_TESTDATA_PATH) as open_file: ffv = UMFile.from_file(open_file) self.assertEqual(type(ffv), UMFile) check_common_n48_testdata(self, ffv)
def test_bypath(self): ffv = UMFile.from_file(COMMON_N48_TESTDATA_PATH) self.assertEqual(type(ffv), UMFile) check_common_n48_testdata(self, ffv)
def test_new(self): ffv = UMFile() self.assertArrayEqual(ffv.fixed_length_header.raw, [None] + [-32768] * 256) _check_minimal_file(self, ffv)
def test_unsized_component__fail(self): test_template = {"integer_constants": {}} with six.assertRaisesRegex(self, ValueError, '"num_words" has no valid default'): _ = UMFile.from_template(test_template)
def test_unknown_component__fail(self): test_template = {"junk": {}} with six.assertRaisesRegex(self, ValueError, 'unrecognised.*component.*("junk")'): _ = UMFile.from_template(test_template)
def test_component(self): test_template = {"integer_constants": {'dims': (12, )}} ffv = UMFile.from_template(test_template) self.assertEqual(ffv.integer_constants.shape, (12, ))
def test_minimal(self): ffv = UMFile.from_template({}) _check_minimal_file(self, ffv)