def process(self, _): """ Perform the main work of the task and return the results as a Python data structure. """ if (self._DEBUG): print("({}.process): ARGS={}".format(self.TOOL_NAME, self.args), file=sys.stderr) # process the given, already validated FITS file fits_file = self.args.get('fits_file') ignore_list = self.args.get( 'ignore_list') or fits_utils.FITS_IGNORE_KEYS which_hdu = self.args.get('which_hdu', 0) try: with fits.open(fits_file) as hdus_list: if (not fits_utils.has_image_data(hdus_list)): errMsg = f"Skipping FITS file '{fits_file}': no image data in primary HDU" raise errors.UnsupportedType(errMsg) hdrs = fits_utils.get_header_fields(hdus_list, which_hdu, ignore_list) except OSError as oserr: errMsg = "Unable to read image metadata from FITS file '{}': {}.".format( fits_file, oserr) raise errors.ProcessingError(errMsg) metadata = dict() # create overall metadata structure finfo = gather_file_info(fits_file) if (finfo is not None): # add common file information metadata['file_info'] = finfo if (hdrs is not None): # add the headers to the metadata metadata['headers'] = hdrs return metadata # return the results of processing
def test_get_header_fields_ignore_empty(self): hdrs = None with fits.open(self.m13_tstfyl) as hdus: hdrs = utils.get_header_fields(hdus, ignore=[]) print(hdrs) assert hdrs is not None assert len(hdrs) > 0 assert len(hdrs) == 19 # really 25 but duplicates elided assert 'CTYPE1' in hdrs assert 'SIMPLE' in hdrs assert 'COMMENT' in hdrs # should not be removed
def test_get_header_fields_default(self): hdrs = None with fits.open(self.m13_tstfyl) as hdus: hdrs = utils.get_header_fields(hdus) print(hdrs) assert hdrs is not None assert len(hdrs) > 0 assert len( hdrs) == 18 # really 25 but duplicates elided, comments removed assert 'CTYPE1' in hdrs assert 'SIMPLE' in hdrs assert 'COMMENT' not in hdrs # removed by default
def process(self, _): """ Perform the main work of the task and return the results as a Python data structure. """ if (self._DEBUG): print("({}.process): ARGS={}".format(self.TOOL_NAME, self.args), file=sys.stderr) # process the given, already validated FITS file fits_file = self.args.get('fits_file') ignore_list = self.args.get( 'ignore_list') or fits_utils.FITS_IGNORE_KEYS catalog_hdu = self.args.get('catalog_hdu', 1) try: with fits.open(fits_file) as hdus_list: if (not fits_utils.has_catalog_data(hdus_list)): errMsg = f"Skipping FITS file '{fits_file}': no catalog in HDU 1" raise errors.UnsupportedType(errMsg) hdrs = fits_utils.get_header_fields(hdus_list, catalog_hdu, ignore_list) cinfo = fits_utils.get_column_info(hdus_list, catalog_hdu) fits_rec = hdus_list[catalog_hdu].data data = fits_utils.rows_from_data(fits_rec) table = Table.read(hdus_list, hdu=catalog_hdu) meta = fits_utils.get_table_meta_attribute(table) except OSError as oserr: errMsg = "Unable to read catalog data from FITS file '{}': {}.".format( fits_file, oserr) raise errors.ProcessingError(errMsg) outdata = dict() # create overall ouput structure finfo = gather_file_info(fits_file) if (finfo is not None): # add common file information outdata['file_info'] = finfo if (hdrs is not None): # add the headers to the output outdata['headers'] = hdrs if (cinfo is not None): # add column metadata to the output outdata['column_info'] = cinfo outdata['meta'] = meta # add extra table metadata to the output outdata['data'] = data # add the data table to the output return outdata # return the results of processing
def test_get_header_fields_badindex(self): hdrs = None with fits.open(self.m13_tstfyl) as hdus: hdrs = utils.get_header_fields(hdus, 1) # no HDU at index 1 assert hdrs is None