def test_copy(self): # Checks that copying all the attributes to a new file # re-creates the original with minimal differences. src_path = tests.get_data_path(('FF', 'ancillary', 'qrparm.mask')) ffv_src = FieldsFileVariant(src_path, FieldsFileVariant.READ_MODE) with self.temp_filename() as temp_path: ffv_dest = FieldsFileVariant(temp_path, FieldsFileVariant.CREATE_MODE) ffv_dest.fixed_length_header = ffv_src.fixed_length_header for name, kind in FieldsFileVariant._COMPONENTS: setattr(ffv_dest, name, getattr(ffv_src, name)) ffv_dest.fields = ffv_src.fields ffv_dest.close() # Compare the files at a binary level. src = np.fromfile(src_path, dtype='>i8', count=-1) dest = np.fromfile(temp_path, dtype='>i8', count=-1) changed_indices = np.where(src != dest)[0] # Allow for acceptable differences. self.assertArrayEqual(changed_indices, [110, 111, 125, 126, 130, 135, 140, 142, 144, 160]) # All but the last difference is from the use of IMDI # instead of 1 to mark an unused dimension length. self.assertArrayEqual(dest[changed_indices[:-1]], [IMDI] * 9) # The last difference is to the length of the DATA component # because we've padded the last field. self.assertEqual(dest[160], 956416)
def test_copy(self): # Checks that copying all the attributes to a new file # re-creates the original with minimal differences. src_path = tests.get_data_path(('FF', 'ancillary', 'qrparm.mask')) ffv_src = FieldsFileVariant(src_path, FieldsFileVariant.READ_MODE) with self.temp_filename() as temp_path: ffv_dest = FieldsFileVariant(temp_path, FieldsFileVariant.CREATE_MODE) ffv_dest.fixed_length_header = ffv_src.fixed_length_header for name, kind in FieldsFileVariant._COMPONENTS: setattr(ffv_dest, name, getattr(ffv_src, name)) ffv_dest.fields = ffv_src.fields ffv_dest.close() # Compare the files at a binary level. src = np.fromfile(src_path, dtype='>i8', count=-1) dest = np.fromfile(temp_path, dtype='>i8', count=-1) changed_indices = np.where(src != dest)[0] # Allow for acceptable differences. self.assertArrayEqual( changed_indices, [110, 111, 125, 126, 130, 135, 140, 142, 144, 160]) # All but the last difference is from the use of IMDI # instead of 1 to mark an unused dimension length. self.assertArrayEqual(dest[changed_indices[:-1]], [IMDI] * 9) # The last difference is to the length of the DATA component # because we've padded the last field. self.assertEqual(dest[160], 956416)
def test_create(self): # Check we can create a new file from scratch, with the correct # cross-referencing automatically applied to the headers to # enable it to load again. with self.temp_filename() as temp_path: ffv = FieldsFileVariant(temp_path, FieldsFileVariant.CREATE_MODE) ffv.fixed_length_header = FixedLengthHeader([-1] * 256) ffv.fixed_length_header.data_set_format_version = 20 ffv.fixed_length_header.sub_model = 1 ffv.fixed_length_header.dataset_type = 3 constants = IMDI * np.ones(46, dtype=int) constants[5] = 4 constants[6] = 5 ffv.integer_constants = constants ints = IMDI * np.ones(45, dtype=int) ints[17] = 4 # LBROW ints[18] = 5 # LBNPT ints[20] = 0 # LBPACK ints[21] = 2 # LBREL ints[38] = 1 # LBUSER(1) reals = range(19) src_data = np.arange(20, dtype='f4').reshape((4, 5)) ffv.fields = [Field2(ints, reals, src_data)] ffv.close() ffv = FieldsFileVariant(temp_path) # Fill with -1 instead of IMDI so we can detect where IMDI # values are being automatically set. expected = -np.ones(256, dtype=int) expected[0] = 20 expected[1] = 1 expected[4] = 3 expected[99:101] = (257, 46) # Integer constants expected[104:106] = IMDI expected[109:112] = IMDI expected[114:117] = IMDI expected[119:122] = IMDI expected[124:127] = IMDI expected[129:131] = IMDI expected[134:136] = IMDI expected[139:145] = IMDI expected[149:152] = (303, 64, 1) # 303 = 256 + 46 + 1 expected[159:161] = (2049, 2048) # Compare using lists because we get more helpful error messages! self.assertEqual(list(ffv.fixed_length_header.raw), list(expected)) self.assertArrayEqual(ffv.integer_constants, constants) self.assertIsNone(ffv.real_constants) self.assertEqual(len(ffv.fields), 1) for field in ffv.fields: data = field.get_data() self.assertArrayEqual(data, src_data)
def test_create(self): # Check we can create a new file from scratch, with the correct # cross-referencing automatically applied to the headers to # enable it to load again. with self.temp_filename() as temp_path: ffv = FieldsFileVariant(temp_path, FieldsFileVariant.CREATE_MODE) ffv.fixed_length_header = FixedLengthHeader([-1] * 256) ffv.fixed_length_header.data_set_format_version = 20 ffv.fixed_length_header.sub_model = 1 ffv.fixed_length_header.dataset_type = 3 constants = IMDI * np.ones(46, dtype=int) constants[5] = 4 constants[6] = 5 ffv.integer_constants = constants ints = IMDI * np.ones(45, dtype=int) ints[17] = 4 # LBROW ints[18] = 5 # LBNPT ints[20] = 0 # LBPACK ints[21] = 2 # LBREL ints[38] = 1 # LBUSER(1) reals = list(range(19)) src_data = np.arange(20, dtype='f4').reshape((4, 5)) ffv.fields = [Field2(ints, reals, src_data)] ffv.close() ffv = FieldsFileVariant(temp_path) # Fill with -1 instead of IMDI so we can detect where IMDI # values are being automatically set. expected = -np.ones(256, dtype=int) expected[0] = 20 expected[1] = 1 expected[4] = 3 expected[99:101] = (257, 46) # Integer constants expected[104:106] = IMDI expected[109:112] = IMDI expected[114:117] = IMDI expected[119:122] = IMDI expected[124:127] = IMDI expected[129:131] = IMDI expected[134:136] = IMDI expected[139:145] = IMDI expected[149:152] = (303, 64, 1) # 303 = 256 + 46 + 1 expected[159:161] = (2049, 2048) # Compare using lists because we get more helpful error messages! self.assertEqual(list(ffv.fixed_length_header.raw), list(expected)) self.assertArrayEqual(ffv.integer_constants, constants) self.assertIsNone(ffv.real_constants) self.assertEqual(len(ffv.fields), 1) for field in ffv.fields: data = field.get_data() self.assertArrayEqual(data, src_data)
def test_fixed_length_header_wrong_dtype(self): # Check that using the wrong dtype in the fixed length header # doesn't confuse things. src_path = tests.get_data_path(('FF', 'n48_multi_field')) with self.temp_filename() as temp_path: shutil.copyfile(src_path, temp_path) ffv = FieldsFileVariant(temp_path, FieldsFileVariant.UPDATE_MODE) header_values = ffv.fixed_length_header.raw self.assertEqual(header_values.dtype, '>i8') header = FixedLengthHeader(header_values.astype('<i4')) ffv.fixed_length_header = header ffv.close() ffv = FieldsFileVariant(temp_path) # If the header was written out with the wrong dtype this # value will go crazy - so check that it's still OK. self.assertEqual(ffv.fixed_length_header.sub_model, 1)
def test_fixed_length_header_wrong_dtype(self): # Check that using the wrong dtype in the fixed length header # doesn't confuse things. src_path = tests.get_data_path(('FF', 'n48_multi_field')) with self.temp_filename() as temp_path: shutil.copyfile(src_path, temp_path) ffv = FieldsFileVariant(temp_path, FieldsFileVariant.UPDATE_MODE) header_values = ffv.fixed_length_header.raw self.assertEqual(header_values.dtype, '>i8') header = FixedLengthHeader(header_values.astype('<i4')) ffv.fixed_length_header = header ffv.close() ffv = FieldsFileVariant(temp_path) # If the header was written out with the wrong dtype this # value will go crazy - so check that it's still OK. self.assertEqual(ffv.fixed_length_header.sub_model, 1)