Esempio n. 1
0
    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 process(self, metadata):
        """
        Perform the main work of the task and return the results as a Python data structure.
        This method overrides JWST_ObsCoreCalcTask method to use iRods file access.
        """
        if (self._DEBUG):
            print("({}.process): ARGS={}".format(self.TOOL_NAME, self.args),
                  file=sys.stderr)

        # use the specified HDU of the FITS file to compute the WCS information
        which_hdu = self.args.get('which_hdu', 0)

        # get the iRods file path argument of the file to be opened
        irff_path = self.args.get('irods_fits_file')

        try:
            # get the FITS file at the specified path
            irff = self.irods.getf(irff_path, absolute=True)

            # sanity check on the given FITS file
            if (irff.size < FITS_BLOCK_SIZE):
                errMsg = "Skipping file too small to be a valid FITS file: '{}'".format(
                    irff_path)
                raise errors.UnsupportedType(errMsg)

            if (self._DEBUG):
                print("({}): Reading iRods FITS file '{}'.".format(
                    self.TOOL_NAME, irff_path),
                      file=sys.stderr)

            # try to get the specified header and read WCS info from it
            header = self.irods.get_header(irff, which_hdu)
            if (header):
                wcs_info = self.irods.get_WCS(header)
            else:  # unable to read the specified header
                errMsg = "Unable to find or read HDU {} of FITS file '{}'.".format(
                    which_hdu, irff_path)
                raise errors.ProcessingError(errMsg)

        except DataObjectDoesNotExist as dodne:
            errMsg = "Unable to find the specified iRods FITS file '{}'.".format(
                irff_path)
            raise errors.ProcessingError(errMsg)

        except OSError as oserr:
            errMsg = "Unable to read WCS info from iRods FITS file '{}': {}.".format(
                irff_path, oserr)
            raise errors.ProcessingError(errMsg)

        # check that we got the WCS information from the file
        if (wcs_info is None):
            errMsg = "No WCS info found in iRods FITS file '{}'.".format(
                irff_path)
            raise errors.ProcessingError(errMsg)

        # try to produce values for each of the desired result fields
        calculated = self.calculate_results(wcs_info, metadata)
        metadata['calculated'] = calculated  # add calculations to metadata

        return metadata  # return the results of processing
Esempio n. 3
0
 def test_ute_code(self):
     ute = xcpt.UnsupportedType(self.EMSG, self.ECODE)
     print(ute)
     print(type(ute))
     assert ute.error_code == self.ECODE
     utedict = ute.to_dict()
     assert 'message' in utedict
     assert 'error_code' in utedict
     assert utedict.get('message') == self.EMSG
     assert utedict.get('error_code') == self.ECODE
     utetup = ute.to_tuple()
     assert utetup[0] == self.EMSG
     assert utetup[1] == self.ECODE
Esempio n. 4
0
 def test_ute(self):
     ute = xcpt.UnsupportedType(self.EMSG)
     print(ute)
     print(type(ute))
     assert ute.error_code == xcpt.UnsupportedType.ERROR_CODE
     utedict = ute.to_dict()
     assert 'message' in utedict
     assert 'error_code' in utedict
     assert utedict.get('message') == self.EMSG
     assert utedict.get('error_code') == xcpt.UnsupportedType.ERROR_CODE
     utetup = ute.to_tuple()
     assert utetup[0] == self.EMSG
     assert utetup[1] == xcpt.UnsupportedType.ERROR_CODE
     utestr = str(ute)
     print(utestr)
     assert str(xcpt.UnsupportedType.ERROR_CODE) in utestr
     assert 'wrong' in utestr
Esempio n. 5
0
    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
Esempio n. 6
0
    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)

        # get the selection and filtering arguments
        which_hdu = self.args.get('which_hdu', 0)
        ignore_list = self.args.get(
            'ignore_list') or fits_utils.FITS_IGNORE_KEYS

        # get the iRods file path argument of the file to be opened
        irff_path = self.args.get('irods_fits_file')

        try:
            # get the FITS file at the specified path
            irff = self.irods.getf(irff_path, absolute=True)

            # sanity check on the given FITS file
            if (irff.size < FITS_BLOCK_SIZE):
                errMsg = "Skipping file too small to be a valid FITS file: '{}'".format(
                    irff_path)
                raise errors.UnsupportedType(errMsg)

            # actually read the file to get the specified header
            header = self.irods.get_header(irff, which_hdu)
            if (header):
                if (not self.irods.is_image_header(header)):
                    errMsg = "HDU {} is not an image header. Skipping FITS file '{}'.".format(
                        which_hdu, irff_path)
                    raise errors.ProcessingError(errMsg)

                # get and save some common file information
                file_info = self.irods.get_irods_file_info(irff)

                # get additional metadata ABOUT the iRods file itself
                irods_metadata = self.irods.get_irods_metadata(irff)

                # get any content metadata attached to the file
                content_metadata = self.irods.get_content_metadata(irff)

                # now try to read the FITS header from the FITS file
                hdrs = fits_utils.get_fields_from_header(header, ignore_list)

            else:  # unable to read the specified header
                errMsg = "Unable to read image metadata from HDU {} of FITS file '{}'.".format(
                    which_hdu, irff_path)
                raise errors.ProcessingError(errMsg)

        except DataObjectDoesNotExist as dodne:
            errMsg = "Unable to find the specified iRods FITS file '{}'.".format(
                irff_path)
            raise errors.ProcessingError(errMsg)

        except OSError as oserr:
            errMsg = "Unable to read image metadata from iRods FITS file '{}': {}.".format(
                irff_path, oserr)
            raise errors.ProcessingError(errMsg)

        metadata = dict()  # create overall metadata structure
        metadata[
            'file_info'] = file_info  # add previously gathered remote file information
        metadata['irods_metadata'] = irods_metadata
        metadata['content_metadata'] = content_metadata

        if (hdrs is not None):
            metadata['headers'] = hdrs  # add the headers to the metadata
        return metadata  # return the results of processing