def get_hdu(uri, cutout=None):
    """Get a at the given uri from VOSpace, possibly doing a cutout.

    If the cutout is flips the image then we also must flip the datasec keywords.  Also, we must offset the
    datasec to reflect the cutout area being used.

    @param uri: The URI in VOSpace of the image to HDU to retrieve.
    @param cutout: A CADC data service CUTOUT paramter to be used when retrieving the observation.
    @return: fits.HDU
    """
    try:
        # the filename is based on the Simple FITS images file.
        filename = os.path.basename(uri)
        if os.access(filename, os.F_OK) and cutout is None:
            logger.debug("File already on disk: {}".format(filename))
            hdu_list = fits.open(filename, scale_back=True)
            hdu_list.verify('silentfix+ignore')

        else:
            logger.debug("Pulling: {}{} from VOSpace".format(uri, cutout))
            fpt = tempfile.NamedTemporaryFile(suffix='.fits')
            cutout = cutout is not None and cutout or ""
            copy(uri+cutout, fpt.name)
            fpt.seek(0, 2)
            fpt.seek(0)
            logger.debug("Read from vospace completed. Building fits object.")
            hdu_list = fits.open(fpt, scale_back=False)
            hdu_list.verify('silentfix+ignore')

            logger.debug("Got image from vospace")
            try:
                hdu_list[0].header['DATASEC'] = reset_datasec(cutout, hdu_list[0].header['DATASEC'],
                                                              hdu_list[0].header['NAXIS1'],
                                                              hdu_list[0].header['NAXIS2'])
            except Exception as e:
                logging.debug("error converting datasec: {}".format(str(e)))

        for hdu in hdu_list:
            logging.debug("Adding converter to {}".format(hdu))
            hdu.converter = CoordinateConverter(0, 0)
            try:
                hdu.wcs = WCS(hdu.header)
            except Exception as ex:
                logger.error("Failed trying to initialize the WCS: {}".format(ex))
    except Exception as ex:
        raise ex
    return hdu_list
def get_hdu ( uri , cutout = None ) : try : filename = os . path . basename ( uri ) if os . access ( filename , os . F_OK ) and cutout is None : logger . debug ( "File already on disk: {}" . format ( filename ) ) hdu_list = fits . open ( filename , scale_back = True ) hdu_list . verify ( 'silentfix+ignore' ) else : logger . debug ( "Pulling: {}{} from VOSpace" . format ( uri , cutout ) ) fpt = tempfile . NamedTemporaryFile ( suffix = '.fits' ) cutout = cutout is not None and cutout or "" copy ( uri + cutout , fpt . name ) fpt . seek ( 0 , 2 ) fpt . seek ( 0 ) logger . debug ( "Read from vospace completed. Building fits object." ) hdu_list = fits . open ( fpt , scale_back = False ) hdu_list . verify ( 'silentfix+ignore' ) logger . debug ( "Got image from vospace" ) try : hdu_list [ 0 ] . header [ 'DATASEC' ] = reset_datasec ( cutout , hdu_list [ 0 ] . header [ 'DATASEC' ] , hdu_list [ 0 ] . header [ 'NAXIS1' ] , hdu_list [ 0 ] . header [ 'NAXIS2' ] ) except Exception as e : logging . debug ( "error converting datasec: {}" . format ( str ( e ) ) ) for hdu in hdu_list : logging . debug ( "Adding converter to {}" . format ( hdu ) ) hdu . converter = CoordinateConverter ( 0 , 0 ) try : hdu . wcs = WCS ( hdu . header ) except Exception as ex : logger . error ( "Failed trying to initialize the WCS: {}" . format ( ex ) ) except Exception as ex : raise ex return hdu_list
Get a at the given uri from VOSpace, possibly doing a cutout.

    If the cutout is flips the image then we also must flip the datasec keywords.  Also, we must offset the
    datasec to reflect the cutout area being used.

    @param uri: The URI in VOSpace of the image to HDU to retrieve.
    @param cutout: A CADC data service CUTOUT paramter to be used when retrieving the observation.
    @return: fits.HDU