def test_read_array_write_site_antennas_iq(self): """ Test reading, restructuring, writing, restructuring antennas_iq. Checks: - arrays that pass the read from a file can then be written as records - arrays restructured, written as records, read as records, restructured back to arrays are the same as original arrays """ dm = pydarnio.BorealisRead(self.source_antennas_iq_array_file, 'antennas_iq', 'array') arrays = dm.arrays writer = pydarnio.BorealisWrite(self.write_antennas_iq_site_file, dm.records, 'antennas_iq', 'site') del dm, writer gc.collect() self.assertTrue(os.path.isfile(self.write_antennas_iq_site_file)) dm2 = pydarnio.BorealisRead(self.write_antennas_iq_site_file, 'antennas_iq', 'site') new_arrays = dm2.arrays # restructuring happens here dictionaries_are_same = self.check_dictionaries_are_same( arrays, new_arrays) self.assertTrue(dictionaries_are_same) os.remove(self.write_antennas_iq_site_file) del dm2, arrays, new_arrays
def test_read_site_write_array_bfiq(self): """ Test reading, restructuring, writing, restructuring bfiq. Checks: - records that pass the read from a file can then be written as arrays - records restructured, written as arrays, read as arrays, restructured back to records are the same as original records """ dm = pydarnio.BorealisRead(self.source_bfiq_site_file, 'bfiq', 'site') records = dm.records arrays = dm.arrays # restructuring happens here _ = pydarnio.BorealisWrite(self.write_bfiq_array_file, arrays, 'bfiq', 'array') del dm, arrays self.assertTrue(os.path.isfile(self.write_bfiq_array_file)) dm2 = pydarnio.BorealisRead(self.write_bfiq_array_file, 'bfiq', 'array') new_records = dm2.records # restructuring happens here dictionaries_are_same = self.check_dictionaries_are_same( records, new_records) self.assertTrue(dictionaries_are_same) os.remove(self.write_bfiq_array_file) del _, dm2, records, new_records
def test_return_reader(self): """ Test the internal BorealisRead return_reader function which should be able to determine which structure of file to use if none is provided. """ site_reader = pydarnio.BorealisRead(self.bfiq_site_file_path, 'bfiq') record_data = site_reader.records random_key = random.choice(list(record_data.keys())) self.assertIsInstance(record_data[random_key]['num_slices'], np.int64) array_reader = pydarnio.BorealisRead(self.bfiq_array_file_path, 'bfiq') array_data = array_reader.arrays self.assertIsInstance(array_data['num_slices'], np.ndarray)
def test_read_write_site_bfiq(self): """ Test reading and then writing site bfiq data to a file. Checks: - records that pass the read from a file can then be written - records written and then read are the same as original """ dm = pydarnio.BorealisRead(self.source_bfiq_site_file, 'bfiq', 'site') records = dm.records _ = pydarnio.BorealisWrite(self.write_bfiq_site_file, records, 'bfiq', 'site') self.assertTrue(os.path.isfile(self.write_bfiq_site_file)) dm2 = pydarnio.BorealisRead(self.write_bfiq_site_file, 'bfiq', 'site') new_records = dm2.records dictionaries_are_same = self.check_dictionaries_are_same( records, new_records) self.assertTrue(dictionaries_are_same) os.remove(self.write_bfiq_site_file) del _, dm, dm2, records, new_records
def test_read_write_array_bfiq(self): """ Test reading and then writing array structured bfiq data to a file. Checks: - arrays that pass the read from a file can then be written - arrays written and then read are the same as original """ dm = pydarnio.BorealisRead(self.source_bfiq_array_file, 'bfiq', 'array') arrays = dm.arrays _ = pydarnio.BorealisWrite(self.write_bfiq_array_file, arrays, 'bfiq', 'array') self.assertTrue(os.path.isfile(self.write_bfiq_array_file)) dm2 = pydarnio.BorealisRead(self.write_bfiq_array_file, 'bfiq', 'array') new_arrays = dm2.arrays dictionaries_are_same = self.check_dictionaries_are_same( arrays, new_arrays) self.assertTrue(dictionaries_are_same) os.remove(self.write_bfiq_array_file) del _, dm, dm2, arrays, new_arrays
def setUp(self): self.rawacf_array_data = copy.deepcopy(borealis_array_rawacf_data) self.bfiq_array_data = copy.deepcopy(borealis_array_bfiq_data) self.bfiq_file = 'test_bfiq.bfiq.hdf5' self.rawacf_file = 'test_rawacf.rawacf.hdf5' self.iqdat_array_darn_file = './test_iqdat_array_file.dmap' self.rawacf_array_darn_file = './test_rawacf_array_file.dmap' _ = pydarnio.BorealisWrite(self.bfiq_file, self.bfiq_array_data, 'bfiq', 'array') bfiq_reader = pydarnio.BorealisRead(self.bfiq_file, 'bfiq', 'array') self.bfiq_site_data = bfiq_reader.records self.iqdat_site_darn_file = './test_iqdat_site_file.dmap' _ = pydarnio.BorealisWrite(self.rawacf_file, self.rawacf_array_data, 'rawacf', 'array') rawacf_reader = pydarnio.BorealisRead(self.rawacf_file, 'rawacf', 'array') self.rawacf_site_data = rawacf_reader.records self.rawacf_site_darn_file = './test_rawacf_site_file.dmap'
def test_writing_array_rawacf(self): """ Tests write_rawacf method - writes a rawacf file Expected behaviour ------------------ Rawacf file is produced """ test_file = "test_rawacf.rawacf.hdf5" _ = pydarnio.BorealisWrite(test_file, self.rawacf_array_data, 'rawacf', 'array') self.assertTrue(os.path.isfile(test_file)) reader = pydarnio.BorealisRead(test_file, 'rawacf', 'array') data = reader.arrays dictionaries_are_same =\ self.check_dictionaries_are_same(data, self.rawacf_array_data) self.assertTrue(dictionaries_are_same) os.remove(test_file)
def test_writing_site_bfiq(self): """ Tests write_bfiq method - writes a bfiq file Expected behaviour ------------------ bfiq file is produced """ test_file = "./test_bfiq.bfiq.hdf5" pydarnio.BorealisWrite(test_file, self.bfiq_site_data, 'bfiq', 'site') # only testing the file is created since it should only be created # at the last step after all checks have passed # Testing the integrity of the insides of the file will be part of # integration testing since we need BorealisSiteRead for that. self.assertTrue(os.path.isfile(test_file)) reader = pydarnio.BorealisRead(test_file, 'bfiq', 'site') records = reader.records dictionaries_are_same = \ self.check_dictionaries_are_same(records, self.bfiq_site_data) self.assertTrue(dictionaries_are_same) os.remove(test_file)
def test_read_array_bfiq(self): """ Test reading records from bfiq. Checks: - returns correct data structures, both records and arrays (restructuring happens internally) - returns expected values """ dm = pydarnio.BorealisRead(self.bfiq_array_file_path, 'bfiq', 'array') arrays = dm.arrays self.assertIsInstance(arrays, dict) self.assertIsInstance(arrays['num_slices'], np.ndarray) self.assertIsInstance(arrays['num_slices'][0], np.int64) records = dm.records first_record = records[dm.record_names[0]] self.assertIsInstance(records, collections.OrderedDict) self.assertIsInstance(first_record, dict) self.assertIsInstance(first_record['num_slices'], np.int64) del dm, records, arrays
def restructure(infile_name, outfile_name, infile_type, infile_structure, outfile_structure): """ This method restructures filename of structure "file_structure" into "final_structure". Parameters ---------- infile_name: str Name of the original file. outfile_name: str Name of the restructured file. infile_type: str Borealis file type of the files. infile_structure: str The current write structure of the file. One of 'array' or 'site'. outfile_structure: str The desired write structure of the file. One of 'array', 'site', 'iqdat', or 'dmap'. """ # dmap and iqdat are not borealis formats, so they are handled specially if outfile_structure == 'dmap' or outfile_structure == 'iqdat': pydarnio.BorealisConvert(infile_name, infile_type, outfile_name, borealis_file_structure=infile_structure) return # Get data from the file reader = pydarnio.BorealisRead(infile_name, infile_type, infile_structure) # Get data in correct format for writing to output file if outfile_structure == 'site': data = reader.records else: data = reader.arrays # Write to output file pydarnio.BorealisWrite(outfile_name, data, infile_type, outfile_structure)