def _convert_rawacf_to_rawacf(self): """ Conversion for Borealis hdf5 rawacf to SDARN DMap rawacf files. See Also -------- __convert_rawacf_record https://superdarn.github.io/rst/superdarn/src.doc/rfc/0008.html https://borealis.readthedocs.io/en/latest/ BorealisRawacf Rawacf Raises ------ BorealisConvert2RawacfError Returns ------- dmap_recs, the records converted to DMap format """ try: recs = [] for record in self.borealis_records.items(): record_dict = \ self.__convert_rawacf_record(self.borealis_slice_id, record, self.borealis_filename) recs.append(record_dict) self._sdarn_dict = recs self._sdarn_dmap_records = dict2dmap(recs) except Exception as e: raise borealis_exceptions.BorealisConvert2RawacfError(e) from e
def _convert_bfiq_to_iqdat(self): """ Conversion for bfiq to iqdat SDARN DMap records. See Also -------- https://superdarn.github.io/rst/superdarn/src.doc/rfc/0027.html https://borealis.readthedocs.io/en/latest/ BorealisBfiq Iqdat Raises ------ BorealisConvert2IqdatError Returns ------- dmap_recs, the records converted to DMap format """ try: recs = [] for record in self.borealis_records.items(): record_dict = \ self.__convert_bfiq_record(self.borealis_slice_id, record, self.borealis_filename) recs.append(record_dict) self._sdarn_dict = recs self._sdarn_dmap_records = dict2dmap(recs) except Exception as e: raise borealis_exceptions.BorealisConvert2IqdatError(e) from e
def test_DmapRead_dmap2dict_dict2dmap(self): """ Test DmapRead from a file and convert to a dictionary. """ dmap_read = pydarn.DmapRead(rawacf_file) records = dmap_read.read_records() records = dmap_read.get_dmap_records dict_records = pydarn.dmap2dict(records) records_2 = pydarn.dict2dmap(dict_records) self.dmap_compare(records, records_2)
def test_DmapWrite_DmapRead_dict2dmap_dict2dmap(self): """ Test Convert dictionary to dmap from dict2dmap then write with DmapWrite to be read in with DmapRead and converted back to a dictionary with dmap2dict. """ dmap_dict = [{'stid': 56, 'channel': 0, 'software': 'RST', 'xcf': np.array([2.5, 3.456, 34.56, -4.5], dtype=np.float32), 'gflg': np.array([1, 0, 4, 2], np.int8)}] dmap_records = pydarn.dict2dmap(dmap_dict) dmap_write = pydarn.DmapWrite(dmap_records, 'test_dmap.dmap') dmap_write.write_dmap() dmap_read = pydarn.DmapRead("test_dmap.dmap") records = dmap_read.read_records() self.dmap_compare(dmap_dict, records) dmap_dict2 = pydarn.dict2dmap(records) self.dmap_compare(dmap_records, dmap_dict2)
def test_dict2dmap_DmapWrite_DmapRead_stream_dmap2dict(self): """ Test convert dict to dmap with dict2dmap then DmapWrite to write to a stream to be read in by DmapRead and converted back to a dictionary with dict2dmap """ dmap_dict = [{'stid': 56, 'channel': 0, 'software': 'RST', 'xcf': np.array([2.5, 3.456, 34.56, -4.5], dtype=np.float32), 'gflg': np.array([1, 0, 4, 2], np.int8)}] dmap_records = pydarn.dict2dmap(dmap_dict) dmap_write = pydarn.DmapWrite(dmap_records) dmap_stream = dmap_write.write_dmap_stream() dmap_read = pydarn.DmapRead(dmap_stream, True) records = dmap_read.read_records() self.dmap_compare(dmap_dict, records) dmap_dict2 = pydarn.dict2dmap(records) self.dmap_compare(dmap_records, dmap_dict2)
def test_SDarnWrite_incorrect_rawacf_from_dict(self): """ Test convert dictionary with incorrect type to dmap and SDarnWrite write the rawacf file Behaviour: Raise SuperDARNDataFormatTypeError """ rawacf_dict_data = copy.deepcopy(rawacf_dict_sets.rawacf_dict_data) rawacf_dict_data[0]['stid'] = np.int8(rawacf_dict_data[0]['stid']) dmap_rawacf = pydarn.dict2dmap(rawacf_dict_data) darn_write = pydarn.SDarnWrite(dmap_rawacf) with self.assertRaises(pydarn.superdarn_exceptions. SuperDARNDataFormatTypeError): darn_write.write_rawacf("test_rawacf.rawacf")
def test_dict2dmap_SDarnWrite_rawacf(self): """ Test dict2dmap to convert a dictionary to dmap then SDarnWrite write rawacf file """ rawacf_dict_data = copy.deepcopy(rawacf_dict_sets.rawacf_dict_data) dmap_rawacf = pydarn.dict2dmap(rawacf_dict_data) darn_read = pydarn.SDarnWrite(dmap_rawacf) darn_read.write_rawacf("test_rawacf.rawacf") dmap_read = pydarn.DmapRead("test_rawacf.rawacf") dmap_data = dmap_read.read_records() dmap_data = dmap_read.get_dmap_records self.dmap_compare(dmap_data, dmap_rawacf) os.remove("test_rawacf.rawacf")
def test_DmapWrite_incorrect_SDarnRead_rawacf_from_dict(self): """ Test write an incorrect data type from a dict converting from dict2dmap with DmapWrite then SDarnRead reads the file Behaviour: Raises SuperDARNDataFormatTypeError """ rawacf_dict_data = copy.deepcopy(rawacf_dict_sets.rawacf_dict_data) rawacf_dict_data[0]['stid'] = np.int8(rawacf_dict_data[0]['stid']) dmap_rawacf = pydarn.dict2dmap(rawacf_dict_data) dmap_write = pydarn.DmapWrite(dmap_rawacf) dmap_write.write_dmap("test_incorrect_rawacf.rawacf") darn_read = pydarn.SDarnRead("test_incorrect_rawacf.rawacf") with self.assertRaises(pydarn.superdarn_exceptions. SuperDARNDataFormatTypeError): darn_read.read_rawacf()
def __check_dmap_dict(self, dmap_records): """ Checks if the dmap data is a dict, if so, converts to a dmap_structure Parameter: dmap_data : List[dict] dmap_data structure to test if it is a dict of value or dmap structure Returns: dmap_structure : List[dict] returns the dmap structure of dmap_data """ try: first_value = list(dmap_records[0].values())[0] if isinstance(first_value, DmapScalar) or isinstance(first_value, DmapArray): return dmap_records else: return dict2dmap(dmap_records) except IndexError: return dmap_records
def test_dict2dmap(self): """ From utils package, testing dict2dmap function """ dmap_records_test = pydarn.dict2dmap(self.dmap_list) self.dmap_compare(dmap_records_test, self.dmap_records)