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_integer_constants_wrong_dtype(self): # Check that using the wrong dtype in the integer constants # doesn't cause mayhem! 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) self.assertEqual(ffv.integer_constants.dtype, '>i8') ffv.integer_constants = ffv.integer_constants.astype('<f4') ffv.close() ffv = FieldsFileVariant(temp_path) # If the integer constants were written out with the wrong # dtype this value will go crazy - so check that it's still # OK. self.assertEqual(ffv.integer_constants[5], 96)
def test_integer_constants_wrong_dtype(self): # Check that using the wrong dtype in the integer constants # doesn't cause mayhem! 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) self.assertEqual(ffv.integer_constants.dtype, '>i8') ffv.integer_constants = ffv.integer_constants.astype('<f4') ffv.close() ffv = FieldsFileVariant(temp_path) # If the integer constants were written out with the wrong # dtype this value will go crazy - so check that it's still # OK. self.assertEqual(ffv.integer_constants[5], 96)