def test_lcurve(self): """Test light curve production.""" from astropy.io.fits import Header command = ('{0} -e {1} {2} --safe-interval ' '{3} {4} --nproc 2 -b 0.5 -o {5}').format( os.path.join( self.datadir, 'monol_testA_nustar_fpma_ev_calib' + HEN_FILE_EXTENSION), 3, 50, 100, 300, os.path.join( self.datadir, 'monol_testA_E3-50_lc' + HEN_FILE_EXTENSION)) hen.lcurve.main(command.split()) new_filename = \ os.path.join(os.path.join(self.datadir, 'monol_testA_E3-50_lc' + HEN_FILE_EXTENSION)) assert os.path.exists(new_filename) lc = hen.io.load_lcurve(new_filename) assert hasattr(lc, 'header') # Test that the header is correctly conserved Header.fromstring(lc.header) assert hasattr(lc, 'gti') gti_to_test = hen.io.load_events(self.first_event_file).gti assert np.allclose(gti_to_test, lc.gti)
def load(filepath, pos_filepath): """ :param filepath: fits image path :return: an Image """ # load image data information data = pyfits.getdata(filepath, hdu=0) primary = Header(pyfits.getheader(filepath, 0)) headers = [primary] extcount = int(primary.get("NEXTEND", 0)) for idx in range(1, extcount): ext = Header(pyfits.getheader(filepath, idx)) headers.append(ext) # load position information pos_primary = Header(pyfits.getheader(pos_filepath, 0)) pos_headers = [pos_primary] pos_extcount = int(pos_primary.get("NEXTEND", 0)) for idx in range(1, pos_extcount): ext = Header(pyfits.getheader(pos_filepath, idx)) pos_headers.append(ext) return Image(array(data), headers, pos_headers)
def get_lightcurve_dataset_from_stingray_Lightcurve(lcurve, header=None, header_comments=None, hduname='RATE', column=CONFIG.TIME_COLUMN): from astropy.io.fits import Header dataset = get_hdu_type_dataset("LIGHTCURVE", [column, hduname], hduname) hdu_table = dataset.tables[hduname] if header is None: if not hasattr(lcurve, 'header'): logging.warn("Light curve has no header") lcurve.header = Header() header = Header.fromstring(lcurve.header) header = dict() for header_column in header: header[header_column] = str(header[header_column]) header_comments[header_column] = \ str(header.comments[header_column]) hdu_table.set_header_info(header, header_comments) hdu_table.columns[column].add_values(lcurve.time) hdu_table.columns[hduname].add_values(lcurve.counts, lcurve.counts_err) dataset.tables["GTI"] = \ DsHelper.get_gti_table_from_stingray_gti(lcurve.gti) return dataset
def test_wl_to_index(ws, result): "wavelength to index" # header h_dict = {"CRVAL1": 3500, 'CDELT1': 2} h = Header() for k, v in h_dict.items(): h.set(k, v) out = ft.wavelength_to_index(h, ws) assert out == result
def get_eventlist_dataset_from_stingray_Eventlist(evlist, header=None, header_comments=None, hduname='EVENTS', column=CONFIG.TIME_COLUMN): from astropy.io.fits import Header evt_columns = [column, "PI"] if hasattr(evlist, 'energy'): evt_columns = [column, "PI", "E"] dataset = get_hdu_type_dataset("EVENTS", evt_columns, hduname) hdu_table = dataset.tables[hduname] if header is None: if not hasattr(evlist, 'header'): logging.warn("Event list has no header") evlist.header = Header() header = Header.fromstring(evlist.header) header = dict() for header_column in header: header[header_column] = str(header[header_column]) header_comments[header_column] = \ str(header.comments[header_column]) hdu_table.set_header_info(header, header_comments) hdu_table.columns[column].add_values(evlist.time) if hasattr(evlist, 'energy'): if evlist.energy is not None and len(evlist.energy) == len( evlist.time): hdu_table.columns['E'].add_values(evlist.energy) else: logging.warn( "Event list energies differs from event counts, setted all energies as 0" ) hdu_table.columns['E'].add_values(np.zeros_like(evlist.time)) if hasattr(evlist, 'pi') and evlist.pi is not None and len( evlist.pi) == len(evlist.time): hdu_table.columns['PI'].add_values(evlist.pi) else: logging.warn("Event list has no PI values, using np.zeros_like") hdu_table.columns['PI'].add_values(np.zeros_like(evlist.time)) dataset.tables["GTI"] = \ DsHelper.get_gti_table_from_stingray_gti(evlist.gti) return dataset
def test_treat_event_file_nustar(self): from astropy.io.fits import Header treat_event_file(self.fits_fileA) lcurve_from_events(self.new_filename) newfile = \ os.path.join(self.datadir, 'monol_testA_nustar_fpma_lc' + HEN_FILE_EXTENSION) assert os.path.exists(newfile) type, data = get_file_type(newfile) assert type == 'lc' assert isinstance(data, Lightcurve) Header.fromstring(data.header) assert hasattr(data, 'mjdref') assert data.mjdref > 0
def load(filepath): """ :param filepath: fits image path :return: an Image """ data = pyfits.getdata(filepath, hdu=0) primary = Header(pyfits.getheader(filepath, 0)) headers = [primary] extcount = int(primary.get("NEXTEND", 0)) for idx in range(1, extcount): ext = Header(pyfits.getheader(filepath, idx)) headers.append(ext) return Image(array(data), headers)
def from_properties(cls, size, center_pixel, center_sky, pixelscale): """ This fucntion ... :param size: :param center_pixel: :param center_sky: :param pixelscale: :return: """ # TODO: doesn't work # see try below # Construct header header = Header() header["CRPIX1"] = center_pixel.x header["CRVAL1"] = center_sky.ra.to("deg").value header["CDELT1"] = pixelscale.x.to("deg").value header["CRPIX2"] = center_pixel.y header["CRVAL2"] = center_sky.dec.to("deg").value header["CDELT2"] = pixelscale.y.to("deg").value header["NAXIS1"] = size.x header["NAXIS2"] = size.y header["RADESYS"] = "ICRS" header["CTYPE1"] = "RA--TAN" header["CTYPE2"] = "DEC--TAN" # Create and return return cls(header=header)
def read_bliss_file(path): fits = fitsio.FITS(path) # The header for the image that this catalog was derived from is # stored in the first extension of the FITS file. However, it's format # is weird, so we parse it. # First we build a string hdrstr = '\n'.join(fits['LDAC_IMHEAD'].read()[0][0]) # Then we use astropy to parse that string into a dict hdr = Header.fromstring(hdrstr, sep='\n') # Now we read the catalog observations = fits['LDAC_OBJECTS'].read() # The image header gives us access to image-level quantities, like # EXPNUM, CCDNUM, MJD-OBS, etc. Careful, these quantities may have a different byte order than the catalog data. EXPNUM = np.tile(hdr['EXPNUM'], len(observations)) CCDNUM = np.tile(hdr['CCDNUM'], len(observations)) # We can then append those quantities to the object array observations = rec_append_fields(observations, names=['EXPNUM', 'CCDNUM'], data=[EXPNUM, CCDNUM]) return observations
def make_header(self, ra, dec, width, pix_size): """ This function ... :return: """ # ra and dec are taken from the ra2000 and de2000 of the attached DustPedia_LEDAWISE_Herschel.csv table # width is 0.5 degrees for galaxies with D25<6 arcmin, and 1 degree for galaxies with D25>=6 arcmin (as listed in DustPedia_LEDAWISE_Herschel.csv) # pix_size is 3.2 for GALEX, and 0.45 for SDSS. # ra = ra.to("deg").value dec = dec.to("deg").value width = width.to("deg").value pix_size = pix_size.to("arcsec").value # Determine the path to the temporary header file header_path = fs.join(self.temp_path, "header.hdr") # Create the header montage.commands.mHdr(str(ra) + ' ' + str(dec), width, header_path, pix_size=pix_size) # Load the header return Header.fromtextfile(header_path)
def _get_flag_defs(header: fits.Header): """Get flag definition dictionary from the the header Parameters ---------- header : dict-like object Returns ------- flag_defs : dict-like object """ flag_defs = {} for key, val in header.items(): if key.startswith('MP_'): try: intval = int(val) if intval > 31: raise ValueError(f'bit value {intval} for flag {key}' ' is too large') flagkey = key[3:] flag_defs[flagkey] = intval except ValueError: pass if len(flag_defs) > 0: return flag_defs
def __validate_bintable_fits_format(header: fits.Header) -> None: # https://github.com/astropy/astropy/blob/master/astropy/io/fits/hdu/table.py#L548 # Implemented the validators that are aligned with the FITS Spec # http://articles.adsabs.harvard.edu/pdf/1995A%26AS..113..159C assert header['NAXIS'] == 2 assert header['BITPIX'] == 8 assert header['TFIELDS'] > 0 and header['TFIELDS'] < 1000 for idx in range(1, header['TFIELDS'] + 1): t_form: str = header.get(f'TFORM{idx}', None) assert not t_form is None t_type: str = header.get(f'TTYPE{idx}', None) assert not t_type is None else: if idx > 999: raise NotImplementedError(f'Invalid FITS Format')
def __new__(cls, value, unit=None, dtype=None, copy=True, wcs=None, meta=None, mask=None, header=None, spectral_unit=None, fill_value=np.nan, wcs_tolerance=0.0): #log.debug("Creating a OneDSpectrum with __new__") if np.asarray(value).ndim != 1: raise ValueError("value should be a 1-d array") if wcs is not None and wcs.wcs.naxis != 1: raise ValueError("wcs should have two dimension") self = u.Quantity.__new__(cls, value, unit=unit, dtype=dtype, copy=copy).view(cls) self._wcs = wcs self._meta = {} if meta is None else meta self._wcs_tolerance = wcs_tolerance self._initial_set_mask(mask) self._fill_value = fill_value if header is not None: self._header = header else: self._header = Header() self._spectral_unit = spectral_unit if spectral_unit is None: if 'CUNIT1' in self._header: self._spectral_unit = u.Unit(self._header['CUNIT1']) elif self._wcs is not None: self._spectral_unit = u.Unit(self._wcs.wcs.cunit[0]) return self
def get_readnoise(self, fn_or_header): """ get the read out noise and sensitivity from the table given the fits header """ if isinstance(fn_or_header, Header): header = fn_or_header else: with open(fn_or_header, 'rb') as fp: header = Header.fromfile(fp) # CCD acquisition mode mode = header['OUTPTAMP'] lmode = self['Mode'] == mode # Readout clock frequency freq = 1. / header['READTIME'] freq_MHz = round(freq / 1.e6) # The integer frequency in MHz as a string lfreq = self['FreqMHz'] == freq_MHz # Preamp gain setting preamp = header['PREAMP'] lpreamp = self['PreAmp'] == preamp # serial number (SHOC 1 or 2) serno = header['SERNO'] lserno = self['SerNo'] == serno return self._from_bools(lmode, lfreq, lpreamp, lserno)
def images_header(self): """ This function ... :return: """ return Header.fromtextfile(self.environment.images_header_path)
def test_region_wcs(ref_name, reg_name, header_name): header = Header.fromtextfile(join(rootdir, header_name)) wcs = astropy.wcs.WCS(header=header) # Header-only ref_region = pyregion_open(join(rootdir, ref_name)).as_imagecoord(header) # With WCS r = pyregion_open(join(rootdir, reg_name)).as_imagecoord(header, wcs=wcs) assert len(r) == len(ref_region) for ref_reg, reg in zip(ref_region, r): if reg.name == "rotbox": reg.name = "box" assert ref_reg.name == reg.name # Normalize everything like angles ref_list = np.asarray(ref_reg.coord_list) reg_list = np.asarray(reg.coord_list) assert_allclose((ref_list + 180) % 360 - 180, (reg_list + 180) % 360 - 180, atol=0.03) assert ref_reg.exclude == reg.exclude
def __new__(cls, value, unit=None, dtype=None, copy=True, wcs=None, meta=None, mask=None, header=None): if np.asarray(value).ndim != 2: raise ValueError("value should be a 2-d array") if wcs is not None and wcs.wcs.naxis != 2: raise ValueError("wcs should have two dimension") self = u.Quantity.__new__(cls, value, unit=unit, dtype=dtype, copy=copy).view(cls) self._wcs = wcs self._meta = {} if meta is None else meta self._mask = mask if header is not None: self._header = header else: self._header = Header() return self
def validate_header(cls, header: fits.Header, onerror='raise'): required = {k for k, c in cls.__cards__.items() if c.required} missing = required - set(header.keys()) # first let's test for any missing required keys if missing: log_or_raise( f"Header is missing the following required keywords: {missing}", RequiredMissing, log=log, onerror=onerror, ) # no go through each of the header items and validate them with the schema for pos, card in enumerate(header.cards): kw = card.keyword if kw not in cls.__cards__: if kw.rstrip('0123456789') not in IGNORE: log_or_raise( f'Unexpected header card "{str(card).strip()}"', AdditionalHeaderCard, log=log, onerror=onerror ) continue cls.__cards__[card.keyword].validate(card, pos, onerror)
def create_header(fits): from astropy.io.fits import Header if isstring(fits): fits = fitsio.FITS(fits) hdrstr = '\n'.join(fits['LDAC_IMHEAD'].read()[0][0]) return Header.fromstring(hdrstr,sep='\n')
def header(self): """ This function ... :return: """ # Determine the path to the header file return Header.fromtextfile(header_path)
def get_images_header(modeling_path): """ This function ... :param modeling_path: :return: """ return Header.fromtextfile(modeling_path)
def __new__(cls, value, unit=None, dtype=None, copy=True, wcs=None, meta=None, mask=None, header=None, spectral_unit=None, fill_value=np.nan, beams=None, wcs_tolerance=0.0): #log.debug("Creating a OneDSpectrum with __new__") if np.asarray(value).ndim != 1: raise ValueError("value should be a 1-d array") if wcs is not None and wcs.wcs.naxis != 1: raise ValueError("wcs should have two dimension") self = u.Quantity.__new__(cls, value, unit=unit, dtype=dtype, copy=copy).view(cls) self._wcs = wcs self._meta = {} if meta is None else meta self._wcs_tolerance = wcs_tolerance if mask is None: mask = BooleanArrayMask(np.ones_like(self.value, dtype=bool), self._wcs, shape=self.value.shape) elif isinstance(mask, np.ndarray): if mask.shape != self.value.shape: raise ValueError("Mask shape must match the spectrum shape.") mask = BooleanArrayMask(mask, self._wcs, shape=self.value.shape) elif isinstance(mask, MaskBase): pass else: raise TypeError("mask of type {} is not a supported mask " "type.".format(type(mask))) # Validate the mask before setting mask._validate_wcs(new_data=self.value, new_wcs=self._wcs, wcs_tolerance=self._wcs_tolerance) self._mask = mask self._fill_value = fill_value if header is not None: self._header = header else: self._header = Header() self._spectral_unit = spectral_unit if spectral_unit is None: if 'CUNIT1' in self._header: self._spectral_unit = u.Unit(self._header['CUNIT1']) elif self._wcs is not None: self._spectral_unit = u.Unit(self._wcs.wcs.cunit[0]) if beams is not None: self.beams = beams # HACK: OneDSpectrum should eventually become not-a-quantity # Maybe it should be a u.Quantity(np.ma)? self._data = self.value return self
def test_get_lightcurve_dataset_from_stingray_Lightcurve(capsys): from stingray.lightcurve import Lightcurve from astropy.io.fits import Header lc = Lightcurve([0, 1], [2, 2]) ds = get_lightcurve_dataset_from_stingray_Lightcurve(lc) out, err = capsys.readouterr() assert err.strip().endswith("Light curve has no header") header = Header() header["Bu"] = "Bu" lc.header = header.tostring() ds = get_lightcurve_dataset_from_stingray_Lightcurve(lc) assert np.allclose(ds.tables["RATE"].columns["TIME"].values, lc.time) assert np.allclose(ds.tables["RATE"].columns["RATE"].values, lc.counts)
def __init__(self, name=None, value=None, comment=None, synonyms=None): self._hdr = Header() self.name = name self.value = value self.comment = comment if synonyms is None: self.synonyms = [] else: self.synonyms = synonyms
def from_header_string(cls, string): """ This function ... :param string: :return: """ header = Header.fromstring(string) return cls.from_header(header)
def create_header(cls, catalog): if isinstance(catalog,np.ndarray): data = catalog else: fits = cls.parse_catalog(catalog) data = fits[cls._hdrhdu].read()[0][0] data = data[~np.char.startswith(data,' =')] s = '\n'.join(data) return Header.fromstring(s,sep='\n')
def build_position(hdf5_wcs, window, telescope): h = Header() # logging.error(hdf5_wcs) # logging.error(window) h['NAXIS1'] = window['X1'].data[telescope] - window['X0'].data[ telescope] + 1 h['NAXIS2'] = window['Y1'].data[telescope] - window['Y0'].data[ telescope] + 1 h['CRVAL1'] = hdf5_wcs['CRVAL1'].data[telescope] h['CRVAL2'] = hdf5_wcs['CRVAL2'].data[telescope] h['CRPIX1'] = hdf5_wcs['CRPIX1'].data[telescope] h['CRPIX2'] = hdf5_wcs['CRPIX2'].data[telescope] # h['CDELT1'] = 4.0 / 3600.0 # h['CDELT2'] = 4.0 / 3600.0 # # JJK - use only one of CD* or CDELT* - they are either conflicting or # redundant # h['CD1_1'] = hdf5_wcs['CD1_1'].data[telescope] h['CD1_2'] = hdf5_wcs['CD1_2'].data[telescope] h['CD2_1'] = hdf5_wcs['CD2_1'].data[telescope] h['CD2_2'] = hdf5_wcs['CD2_2'].data[telescope] w = wcs.WCS() bounds = CoordPolygon2D() result = w.calc_footprint(header=h) for ii in result: vertex = ValueCoord2D(ii[0], ii[1]) bounds.vertices.append(vertex) # ref_coord_x = RefCoord(mc.to_float(pix1), ra) # ref_coord_y = RefCoord(mc.to_float(pix2), dec) # coord = Coord2D(ref_coord_x, ref_coord_y) # dimension = Dimension2D(2, 2) # pixscale = 4.0 / 3600.0 => 4", per JJK # VLASS takes the cd?? approach shown here # function = CoordFunction2D(dimension=dimension, # ref_coord=coord, # cd11=4.0/3600.0, # cd12=0.0, # cd21=0.0, # cd22=4.0/3600.0) axis = CoordAxis2D(axis1=Axis(ctype='RA---TAN', cunit='deg'), axis2=Axis(ctype='DEC--TAN', cunit='deg'), error1=None, error2=None, range=None, bounds=bounds, function=None) return SpatialWCS(axis=axis, coordsys='FK5', equinox=2000.0, resolution=None)
def test_get_lightcurve_dataset_from_stingray_Lightcurve(capsys): from stingray.lightcurve import Lightcurve from astropy.io.fits import Header lc = Lightcurve([0, 1], [2, 2]) ds = get_lightcurve_dataset_from_stingray_Lightcurve(lc) out, err = capsys.readouterr() if err: assert err.strip().endswith("Light curve has no header") header = Header() header["Bu"] = "Bu" lc.header = header.tostring() ds = get_lightcurve_dataset_from_stingray_Lightcurve(lc) assert np.allclose(ds.tables["RATE"].columns["TIME"].values, lc.time) assert np.allclose(ds.tables["RATE"].columns["RATE"].values, lc.counts)
def fake_header(extver, version, timesys, telescop): return Header({ "SIMPLE": "T", "BITPIX": 8, "NAXIS": 0, "EXTVER": extver, "VERSION": version, 'TIMESYS': "{}".format(timesys), "TELESCOP": "{}".format(telescop) })
def test_load_data(): expected_datacube = np.arange(120).reshape(4,5,6) expected_header = Header.fromstring("SIMPLE = T / conforms to FITS standard BITPIX = 64 / array data type NAXIS = 3 / number of array dimensions NAXIS1 = 6 NAXIS2 = 5 NAXIS3 = 4 CTYPE1 = 'VELO-LSR' CTYPE2 = 'GLON-CAR' CTYPE3 = 'GLAT-CAR' CRVAL1 = '' CRVAL2 = '' CRVAL3 = '' CDELT1 = '' CDELT2 = '' CDELT3 = '' CRPIX1 = '' CRPIX2 = '' CRPIX3 = '' END ") datacube, header = load_data("test_data.fits", data_path="production/test/") assert_allclose(datacube, expected_datacube) assert_equal(header, expected_header)
def test_calibrate(self): """Test event file calibration.""" from astropy.io.fits import Header command = '{0} -r {1}'.format( os.path.join(self.datadir, 'monol_testA_nustar_fpma_ev' + HEN_FILE_EXTENSION), os.path.join(self.datadir, 'test.rmf')) hen.calibrate.main(command.split()) new_filename = os.path.join( self.datadir, 'monol_testA_nustar_fpma_ev_calib' + HEN_FILE_EXTENSION) assert os.path.exists(new_filename) ev = hen.io.load_events(new_filename) assert hasattr(ev, 'header') Header.fromstring(ev.header) assert hasattr(ev, 'gti') gti_to_test = hen.io.load_events(self.first_event_file).gti assert np.allclose(gti_to_test, ev.gti)
def fake_header(extver, version, timesys, telescop): return Header({ "SIMPLE": "T", "BITPIX": 8, "NAXIS": 0, "EXTVER": extver, "VERSION": version, 'TIMESYS': f"{timesys}", "TELESCOP": f"{telescop}" })
def __new__(cls, value, unit=None, dtype=None, copy=True, wcs=None, meta=None, mask=None, header=None, beam=None, fill_value=np.nan, read_beam=False, wcs_tolerance=0.0): if np.asarray(value).ndim != 2: raise ValueError("value should be a 2-d array") if wcs is not None and wcs.wcs.naxis != 2: raise ValueError("wcs should have two dimension") self = u.Quantity.__new__(cls, value, unit=unit, dtype=dtype, copy=copy).view(cls) self._wcs = wcs self._meta = {} if meta is None else meta self._wcs_tolerance = wcs_tolerance self._initial_set_mask(mask) self._fill_value = fill_value if header is not None: self._header = header else: self._header = Header() if beam is None: if "beam" in self.meta: beam = self.meta['beam'] elif read_beam: beam = cube_utils.try_load_beam(header) if beam is None: warnings.warn("Cannot load beam from header.", BeamWarning) if beam is not None: self.beam = beam self.meta['beam'] = beam # TODO: Enable header updating when non-celestial slices are # properly handled in the WCS object. # self._header.update(beam.to_header_keywords()) self._cache = {} return self
def make_header(ra, dec, width, pix_size, returns="header"): """ This function ... :param ra: :param dec: :param width: :param pix_size: :param returns: :return: """ import montage_wrapper as montage # ra and dec are taken from the ra2000 and de2000 of the attached DustPedia_LEDAWISE_Herschel.csv table # width is 0.5 degrees for galaxies with D25<6 arcmin, and 1 degree for galaxies with D25>=6 arcmin (as listed in DustPedia_LEDAWISE_Herschel.csv) # pix_size is 3.2 for GALEX, and 0.45 for SDSS. # Convert to degrees and the pixelsize in arcseconds ra_deg = ra.to("deg").value dec_deg = dec.to("deg").value width_deg = width.to("deg").value pix_size_arcsec = pix_size.to("arcsec").value # Determine the path to the temporary header file header_path = fs.join(temp_montage_path, "header.hdr") # Create the header montage.commands.mHdr(str(ra_deg) + ' ' + str(dec_deg), width_deg, header_path, pix_size=pix_size_arcsec) # Load the header if returns == "header": return Header.fromtextfile(header_path) elif returns == "path": return header_path elif returns == ["header", "path"]: return Header.fromtextfile(header_path), header_path elif returns == ["path", "header"]: return header_path, Header.fromtextfile(header_path) else: raise ValueError("Invalid option for 'returns'")
def test_with_wcs(): data = np.arange(100).reshape(10, 10) header = Header() wcs = WCS(fix=False) wcs.wcs.cd = [[0.9, 0.8], [0.7, 0.6]] header.set('REMAIN1', 'VALUE1') header.set('DQ1', 'dqvalue1') header.set('NAXIS', 2) test_subject = CutoutND(data, wcs=wcs) cutout_result = test_subject.extract([(1, 6, 2), (4, 10, 2)]) result_wcs = cutout_result.wcs np.testing.assert_array_equal([[1.8, 1.6], [1.4, 1.2]], result_wcs.wcs.cd, 'Wrong CD output.')
def test_permute_data_to_standard_order(): expected_datacube = np.arange(120).reshape(4,5,6).transpose(2,0,1) expected_header = Header.fromstring("SIMPLE = T / conforms to FITS standard BITPIX = 64 / array data type NAXIS = 3 / number of array dimensions NAXIS1 = 5 NAXIS2 = 4 NAXIS3 = 6 CTYPE1 = 'GLON-CAR' CTYPE2 = 'GLAT-CAR' CTYPE3 = 'VELO-LSR' CRVAL1 = '' CRVAL2 = '' CRVAL3 = '' CDELT1 = '' CDELT2 = '' CDELT3 = '' CRPIX1 = '' CRPIX2 = '' CRPIX3 = '' END ") # if something's wrong with load_data then it should fail the previous test datacube, header = permute_data_to_standard_order(*load_data("test_data.fits", data_path="production/test/")) assert_allclose(datacube, expected_datacube) assert_equal(header, expected_header) assert_equal(expected_header['ctype1'], 'GLON-CAR') assert_equal(header['ctype1'], 'GLON-CAR')
def test_get_eventlist_dataset_from_stingray_Eventlist(capsys): from stingray.events import EventList from astropy.io.fits import Header ev = EventList(time=[0, 1], pi=[2, 2], energy=[3., 4.], gti=np.array([[-0.5, 1.5]])) ds = get_eventlist_dataset_from_stingray_Eventlist(ev) out, err = capsys.readouterr() print("Out:", out) print("Err:", err) if err: assert "Event list has no header" in err header = Header() header["Bu"] = "Bu" ev.header = header.tostring() ds = get_eventlist_dataset_from_stingray_Eventlist(ev) assert np.allclose(ds.tables["EVENTS"].columns["TIME"].values, ev.time) if "ENERGY" in ds.tables["EVENTS"].columns: assert np.allclose(ds.tables["EVENTS"].columns["ENERGY"].values, ev.energy) assert np.allclose(ds.tables["EVENTS"].columns["PI"].values, ev.pi)
def create_test_data(): filename = "production/test/test_data.fits" datacube = np.arange(120).reshape(4,5,6) key_bases = ['naxis', 'ctype', 'crval', 'cdelt', 'crpix'] keys = list(flatten([[x+'1', x+'2', x+'3'] for x in key_bases])) header = Header.fromkeys(keys) header['ctype1'] = 'VELO-LSR' header['ctype2'] = 'GLON-CAR' header['ctype3'] = 'GLAT-CAR' writeto(open(filename, 'wb'), datacube, header=header)
def get_eventlist_dataset_from_stingray_Eventlist(evlist, header=None, header_comments=None, hduname='EVENTS', column=CONFIG.TIME_COLUMN): from astropy.io.fits import Header evt_columns = [column, "PI"] if hasattr(evlist, 'energy'): evt_columns = [column, "PI", "E"] dataset = get_hdu_type_dataset("EVENTS", evt_columns, hduname) hdu_table = dataset.tables[hduname] if header is None: if not hasattr(evlist, 'header'): logging.warn("Event list has no header") evlist.header = Header() header = Header.fromstring(evlist.header) header = dict() for header_column in header: header[header_column] = str(header[header_column]) header_comments[header_column] = \ str(header.comments[header_column]) hdu_table.set_header_info(header, header_comments) hdu_table.columns[column].add_values(evlist.time) if hasattr(evlist, 'energy'): if evlist.energy is not None and len(evlist.energy) == len(evlist.time): hdu_table.columns['E'].add_values(evlist.energy) else: logging.warn("Event list energies differs from event counts, setted all energies as 0") hdu_table.columns['E'].add_values(np.zeros_like(evlist.time)) if hasattr(evlist, 'pi') and evlist.pi is not None and len(evlist.pi) == len(evlist.time): hdu_table.columns['PI'].add_values(evlist.pi) else: logging.warn("Event list has no PI values, using np.zeros_like") hdu_table.columns['PI'].add_values(np.zeros_like(evlist.time)) dataset.tables["GTI"] = \ DsHelper.get_gti_table_from_stingray_gti(evlist.gti) return dataset
def test_region(ref_name, reg_name, header_name): header = Header.fromtextfile(join(rootdir, header_name)) ref_region = pyregion_open(join(rootdir, ref_name)).as_imagecoord(header) r = pyregion_open(join(rootdir, reg_name)).as_imagecoord(header) assert len(r) == len(ref_region) for ref_reg, reg in zip(ref_region, r): if reg.name == "rotbox": reg.name = "box" assert ref_reg.name == reg.name # Normalize everything like angles ref_list = np.asarray(ref_reg.coord_list) reg_list = np.asarray(reg.coord_list) assert_allclose((ref_list + 180) % 360 - 180, (reg_list + 180) % 360 - 180, atol=0.03) assert ref_reg.exclude == reg.exclude
def load_maps(self): """ This function ... :return: """ # Inform the user log.info("Loading the input maps ...") # Determine path to maps directory maps_path = fs.join(m81_data_path, "maps") # Determine the path to the header file header_path = fs.join(maps_path, "header.txt") header = Header.fromtextfile(header_path) wcs = CoordinateSystem(header=header) # Old stars old_map_path = fs.join(maps_path, old_filename) old_map = Frame.from_file(old_map_path) old_map.wcs = wcs self.maps["old"] = old_map # young stars young_map_path = fs.join(maps_path, young_filename) young_map = Frame.from_file(young_map_path) young_map.wcs = wcs self.maps["young"] = young_map # Ionizing stars ionizing_map_path = fs.join(maps_path, ionizing_filename) ionizing_map = Frame.from_file(ionizing_map_path) ionizing_map.wcs = wcs self.maps["ionizing"] = ionizing_map # Dust dust_map_path = fs.join(maps_path, dust_filename) dust_map = Frame.from_file(dust_map_path) dust_map.wcs = wcs self.maps["dust"] = dust_map
HEADER_SIMPLE_CELESTIAL = """ WCSAXES = 2 CTYPE1 = RA---TAN CTYPE2 = DEC--TAN CRVAL1 = 10 CRVAL2 = 20 CRPIX1 = 30 CRPIX2 = 40 CDELT1 = -0.1 CDELT2 = 0.1 CROTA2 = 0. CUNIT1 = deg CUNIT2 = deg """ WCS_SIMPLE_CELESTIAL = WCS(Header.fromstring(HEADER_SIMPLE_CELESTIAL, sep='\n')) def test_simple_celestial(): wcs = WCS_SIMPLE_CELESTIAL # Low-level API assert wcs.pixel_n_dim == 2 assert wcs.world_n_dim == 2 assert wcs.array_shape is None assert wcs.pixel_shape is None assert wcs.world_axis_physical_types == ['pos.eq.ra', 'pos.eq.dec'] assert wcs.world_axis_units == ['deg', 'deg']
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText from astropy.io.fits import Header from astropy.wcs import WCS from wcsaxes import WCSAxes import pyregion region_list = ["test_text.reg", "test_context.reg"] # Create figure fig = plt.figure(figsize=(8, 4)) # Parse WCS information header = Header.fromtextfile("sample_fits01.header") wcs = WCS(header) # Create axes ax1 = WCSAxes(fig, [0.1, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax1) ax2 = WCSAxes(fig, [0.5, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax2) # Hide labels on y axis ax2.coords[1].set_ticklabel_position("") for ax, reg_name in zip([ax1, ax2], region_list): ax.set_xlim(300, 1300) ax.set_ylim(300, 1300)
def print_shape_list(shape_list): for idx, shape in enumerate(shape_list, start=1): print("[region %d]" % idx) print() print("%s; %s(%s)" % (shape.coord_format, shape.name, ", ".join([str(s) for s in shape.coord_list]))) print(shape.attr[0]) print(", ".join(["%s=%s" % (k, v.strip()) for k, v in list(shape.attr[1].items())])) print() if __name__ == "__main__": print("** coordinate in FK5 **") print() filename = "test01_print.reg" # filename = "test_text.reg" # filename = "test01.reg" shape_list = pyregion.open(filename) print_shape_list(shape_list) print() print() print("** coordinate in image **") print() header = Header.fromtextfile("test.header") shape_list2 = shape_list.as_imagecoord(header=header) print_shape_list(shape_list2)
def demo_header(): return Header.fromtextfile(join(rootdir, "sample_fits01.header"))
def demo_header(): return Header.fromtextfile("sample_fits02.header")
def demo_header(): from astropy.io.fits import Header return Header.fromtextfile("sample_fits01.header")
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText from astropy.io.fits import Header from astropy.wcs import WCS from astropy.visualization.wcsaxes import WCSAxes import pyregion region_list = [ "test_text.reg", "test_context.reg", ] # Create figure fig = plt.figure(figsize=(8, 4)) # Parse WCS information header = Header.fromtextfile('sample_fits01.header') wcs = WCS(header) # Create axes ax1 = WCSAxes(fig, [0.1, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax1) ax2 = WCSAxes(fig, [0.5, 0.1, 0.4, 0.8], wcs=wcs) fig.add_axes(ax2) # Hide labels on y axis ax2.coords[1].set_ticklabel_position('') for ax, reg_name in zip([ax1, ax2], region_list): ax.set_xlim(300, 1300) ax.set_ylim(300, 1300)
CTYPE3 = GLON-CAR CRVAL1 = 10 CRVAL2 = 20 CRVAL3 = 25 CRPIX1 = 30 CRPIX2 = 40 CRPIX3 = 45 CDELT1 = -0.1 CDELT2 = 0.5 CDELT3 = 0.1 CUNIT1 = deg CUNIT2 = Hz CUNIT3 = deg """ WCS_SPECTRAL_CUBE = WCS(Header.fromstring(HEADER_SPECTRAL_CUBE, sep='\n')) WCS_SPECTRAL_CUBE.pixel_bounds = [(-1, 11), (-2, 18), (5, 15)] @pytest.mark.parametrize("item, ndim, expected", ( ([Ellipsis, 10], 4, [slice(None)] * 3 + [10]), ([10, slice(20, 30)], 5, [10, slice(20, 30)] + [slice(None)] * 3), ([10, Ellipsis, 8], 10, [10] + [slice(None)] * 8 + [8]) )) def test_sanitize_slice(item, ndim, expected): new_item = sanitize_slices(item, ndim) # FIXME: do we still need the first two since the third assert # should cover it all? assert len(new_item) == ndim assert all(isinstance(i, (slice, int)) for i in new_item) assert new_item == expected
def test_calculate_rotation_angle(region_frame, header_name, rot_angle): header = Header.fromtextfile(os.path.join(rootdir, header_name)) assert_allclose( _calculate_rotation_angle(region_frame, header), rot_angle, atol=0.001 )
def test_header(): return Header.fromtextfile("test.header")