Skip to content

Commit

Permalink
ICRS test FITS
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukeaso-astron committed Jan 10, 2025
1 parent db257eb commit f9d3a1f
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 19 deletions.
15 changes: 12 additions & 3 deletions plotastrodata/analysis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def __post_init__(self):
self.fitsimage_org = None
self.sigma_org = None
self.fitsheader = None
self.pv = False

def binning(self, width: list[int, int, int] = [1, 1, 1]):
"""Binning up neighboring pixels in the v, y, and x domain.
Expand Down Expand Up @@ -495,11 +496,11 @@ def writetofits(self, fitsimage: str = 'out.fits', header: dict = {}):
fitsimage (str, optional): Output FITS file name. Defaults to 'out.fits'.
header (dict, optional): Header dictionary. Defaults to {}.
"""
if self.y is None:
if self.pv:
print('writetofits does not support PV diagram yet.')
return False

h = dict(**header)
h = {}
cx, cy = (0, 0) if self.center is None else coord2xy(self.center)
h['NAXIS1'] = len(self.x)
h['CRPIX1'] = np.argmin(np.abs(self.x)) + 1
Expand All @@ -518,7 +519,11 @@ def writetofits(self, fitsimage: str = 'out.fits', header: dict = {}):
h['BMAJ'] = self.beam[0] / 3600
h['BMIN'] = self.beam[1] / 3600
h['BPA'] = self.beam[2]
data2fits(d=self.data, h=h, templatefits=self.fitsimage_org,
h0 = header
for k in h:
if k not in h0:
h0[k] = h[k]
data2fits(d=self.data, h=h0, templatefits=self.fitsimage_org,
fitsimage=fitsimage)


Expand Down Expand Up @@ -654,6 +659,8 @@ def read(self, d: AstroData, xskip: int = 1, yskip: int = 1):
d.sigma_org = [d.sigma_org] * d.n
if type(d.fitsheader) is not list:
d.fitsheader = [d.fitsheader] * d.n
if type(d.pv) is not list:
d.pv = [d.pv] * d.n
grid0 = [d.x, d.y, d.v]
for i in range(d.n):
if d.center[i] == 'common':
Expand All @@ -680,6 +687,7 @@ def read(self, d: AstroData, xskip: int = 1, yskip: int = 1):
d.beam[i] = fd.get_beam(dist=self.dist)
d.bunit[i] = d.fitsheader[i]['BUNIT']
if d.data[i] is not None:
d.pv[i] = self.pv
d.sigma_org[i] = d.sigma[i]
d.sigma[i] = estimate_rms(d.data[i], d.sigma[i])
d.data[i], grid = trim(data=d.data[i],
Expand Down Expand Up @@ -736,3 +744,4 @@ def read(self, d: AstroData, xskip: int = 1, yskip: int = 1):
d.fitsimage_org = d.fitsimage_org[0]
d.sigma_org = d.sigma_org[0]
d.fitsheader = d.fitsheader[0]
d.pv = d.pv[0]
21 changes: 14 additions & 7 deletions plotastrodata/fits_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ def get_center(self) -> str:
"""
ra_deg = self.get_header('CRVAL1')
dec_deg = self.get_header('CRVAL2')
return xy2coord([ra_deg, dec_deg])
radesys = self.get_header('RADESYS')
a = xy2coord([ra_deg, dec_deg])
if radesys is not None:
a = f'{radesys} {a}'
return a

def gen_data(self, Tb: bool = False, log: bool = False,
drop: bool = True, restfreq: float = None) -> None:
Expand Down Expand Up @@ -180,6 +184,9 @@ def gen_grid(self, center: str | None = None, dist: float = 1.,
# spatial center
if center is not None:
c0 = xy2coord([h['CRVAL1'], h['CRVAL2']])
if 'RADESYS' in h:
radesys = h['RADESYS']
c0 = f'{radesys} {c0}'
cx, cy = coord2xy(center, c0)
else:
cx, cy = 0, 0
Expand Down Expand Up @@ -233,19 +240,19 @@ def gen_v(s: np.ndarray) -> None:
else:
s = (1 - s / freq) * cu.c_kms - vsys
case 'm/s':
print(f'{key}=\'m/s\' was found.')
print(f'{key}=\'m/s\' found.')
s = s * 1e-3 - vsys
case 'M/S':
print(f'{key}=\'M/S\' was found.')
print(f'{key}=\'M/S\' found.')
s = s * 1e-3 - vsys
case 'km/s':
print(f'{key}=\'km/s\' was found.')
print(f'{key}=\'km/s\' found.')
s = s - vsys
case 'KM/S':
print(f'{key}=\'KM/S\' was found.')
print(f'{key}=\'KM/S\' found.')
s = s - vsys
case _:
print(f'Unknown CUNIT3 {cunitv} was found.'
print(f'Unknown CUNIT3 {cunitv} found.'
+ ' v is read as is.')
s = s - vsys

Expand Down Expand Up @@ -348,7 +355,7 @@ def data2fits(d: np.ndarray | None = None, h: dict = {},
header = w.to_header()
hdu = fits.PrimaryHDU(d, header=header)
for k in _h:
if not ('COMMENT' in k or 'HISTORY' in k):
if not ('COMMENT' in k or 'HISTORY' in k) and _h[k] is not None:
hdu.header[k] = _h[k]
hdu = fits.HDUList([hdu])
hdu.writeto(fitsimage.replace('.fits', '') + '.fits', overwrite=True)
32 changes: 23 additions & 9 deletions plotastrodata/other_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,27 @@ def _getframe(coord: str, s: str = '') -> tuple:
Returns:
tuple: updated coord and frame. frame is FK5(equinox='J2000), FK4(equinox='B1950'), or 'icrs'.
"""
if len(s := coord.split()) == 3:
coord = f'{s[1]} {s[2]}'
if 'J2000' in s[0]:
if len(c := coord.split()) == 3:
coord = f'{c[1]} {c[2]}'
if 'J2000' in c[0]:
print(f'J2000 found in coord{s}. FK5(J2000) is used.')
frame = FK5(equinox='J2000')
elif 'B1950' in s[0]:
elif 'FK5' in c[0]:
print(f'FK5 found in coord{s}. FK5(J2000) is used.')
frame = FK5(equinox='J2000')
elif 'B1950' in c[0]:
print(f'B1950 found in coord{s}. FK4(B1950) is used.')
frame = FK4(equinox='B1950')
elif 'ICRS' in s[0]:
elif 'FK4' in c[0]:
print(f'FK4 found in coord{s}. FK4(B1950) is used.')
frame = FK4(equinox='B1950')
elif 'ICRS' in c[0]:
frame = 'icrs'
else:
print(f'Unknown equinox found in coord{s}. ICRS is used')
frame = 'icrs'
else:
frame = 'icrs'
frame = None
return coord, frame


Expand Down Expand Up @@ -104,14 +110,20 @@ def coord2xy(coords: str | list, coordorg: str = '00h00m00s 00d00m00s',
Returns:
np.ndarray: [(array of) alphas, (array of) deltas] in degree. The shape of alphas and deltas is the input shape. With a single input, the output is [alpha0, delta0].
"""
coordorg, frameorg_c = _getframe(coordorg, 'org')
frameorg = frameorg_c if frameorg is None else _updateframe(frameorg)
if type(coords) is list:
for i in range(len(coords)):
coords[i], frame_c = _getframe(coords[i])
else:
coords, frame_c = _getframe(coords)
frame = frame_c if frame is None else _updateframe(frame)
coordorg, frameorg_c = _getframe(coordorg, 'org')
frameorg = frameorg_c if frameorg is None else _updateframe(frameorg)
if frame is None and frameorg is not None:
frame = frameorg
if frame is not None and frameorg is None:
frameorg = frame
if frame is None and frameorg is None:
frame = frameorg = 'icrs'
clist = SkyCoord(coords, frame=frame)
c0 = SkyCoord(coordorg, frame=frameorg)
xy = c0.spherical_offsets_to(clist)
Expand All @@ -134,7 +146,9 @@ def xy2coord(xy: list, coordorg: str = '00h00m00s 00d00m00s',
"""
coordorg, frameorg_c = _getframe(coordorg, 'org')
frameorg = frameorg_c if frameorg is None else _updateframe(frameorg)
frame = 'icrs' if frame is None else _updateframe(frame)
if frameorg is None:
frameorg = 'icrs'
frame = frameorg if frame is None else _updateframe(frame)
c0 = SkyCoord(coordorg, frame=frameorg)
coords = c0.spherical_offsets_by(*xy * units.degree)
coords = coords.transform_to(frame=frame)
Expand Down
Binary file modified testFITS/test2D.fits
Binary file not shown.
Binary file modified testFITS/test2D_2.fits
Binary file not shown.
Binary file modified testFITS/test2Damp.fits
Binary file not shown.
Binary file modified testFITS/test2Dang.fits
Binary file not shown.
Binary file modified testFITS/test3D.fits
Binary file not shown.
Binary file modified testFITS/testB.fits
Binary file not shown.
Binary file modified testFITS/testG.fits
Binary file not shown.
Binary file modified testFITS/testR.fits
Binary file not shown.

0 comments on commit f9d3a1f

Please sign in to comment.