|
import os |
|
import random |
|
from glob import glob |
|
import json |
|
|
|
import numpy as np |
|
from astropy.io import fits |
|
from astropy.coordinates import Angle |
|
from astropy import units as u |
|
from fsspec.core import url_to_fs |
|
|
|
from huggingface_hub import hf_hub_download |
|
import datasets |
|
from datasets import DownloadManager |
|
|
|
from tqdm import tqdm |
|
|
|
def lris_read_amp(inp, ext, redchip=False, applygain=True): |
|
""" |
|
Modified from pypeit.spectrographs.keck_lris.lris_read_amp -- Jon Brown, Josh Bloom |
|
cf. https://github.com/KerryPaterson/Imaging_pipelines |
|
Read one amplifier of an LRIS multi-extension FITS image |
|
|
|
Parameters |
|
---------- |
|
inp: tuple |
|
(str,int) filename, extension |
|
(hdu,int) FITS hdu, extension |
|
|
|
Returns |
|
------- |
|
data |
|
predata |
|
postdata |
|
x1 |
|
y1 |
|
|
|
;------------------------------------------------------------------------ |
|
function lris_read_amp, filename, ext, $ |
|
linebias=linebias, nobias=nobias, $ |
|
predata=predata, postdata=postdata, header=header, $ |
|
x1=x1, x2=x2, y1=y1, y2=y2, GAINDATA=gaindata |
|
;------------------------------------------------------------------------ |
|
; Read one amp from LRIS mHDU image |
|
;------------------------------------------------------------------------ |
|
""" |
|
|
|
if isinstance(inp, str): |
|
hdu = fits.open(inp) |
|
else: |
|
hdu = inp |
|
|
|
|
|
|
|
head0 = hdu[0].header |
|
precol = head0["precol"] |
|
postpix = head0["postpix"] |
|
|
|
|
|
binning = head0["BINNING"] |
|
xbin, ybin = [int(ibin) for ibin in binning.split(",")] |
|
precol = precol // xbin |
|
postpix = postpix // xbin |
|
|
|
|
|
temp = hdu[ext].data.transpose() |
|
tsize = temp.shape |
|
nxt = tsize[0] |
|
|
|
|
|
header = hdu[ext].header |
|
detsec = header["DETSEC"] |
|
x1, x2, y1, y2 = np.array(load_sections(detsec, fmt_iraf=False)).flatten() |
|
|
|
|
|
datasec = header["DATASEC"] |
|
xdata1, xdata2, ydata1, ydata2 = np.array( |
|
load_sections(datasec, fmt_iraf=False) |
|
).flatten() |
|
|
|
|
|
predata = temp[0:precol, :] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not redchip: |
|
xshape = 1024 // xbin |
|
else: |
|
xshape = xdata2 - xdata1 + 1 // xbin |
|
|
|
|
|
if (xdata1 - 1) != precol: |
|
|
|
errStr = "Something wrong in LRIS datasec or precol" |
|
print(errStr) |
|
|
|
if (xshape + precol + postpix) != temp.shape[0]: |
|
|
|
errStr = "Wrong size for in LRIS detector somewhere. Funny binning?" |
|
print(errStr) |
|
|
|
data = temp[precol : precol + xshape, :] |
|
postdata = temp[nxt - postpix : nxt, :] |
|
|
|
|
|
if x1 > x2: |
|
xt = x2 |
|
x2 = x1 |
|
x1 = xt |
|
data = np.flipud(data) |
|
|
|
|
|
if y1 > y2: |
|
yt = y2 |
|
y2 = y1 |
|
y1 = yt |
|
data = np.fliplr(data) |
|
predata = np.fliplr(predata) |
|
postdata = np.fliplr(postdata) |
|
|
|
|
|
gaindata = 0.0 * data + 1.0 |
|
|
|
return data, gaindata, predata, postdata, x1, y1 |
|
|
|
|
|
def load_sections(string, fmt_iraf=True): |
|
""" |
|
Modified from pypit.core.parse.load_sections -- Jon Brown, Josh Bloom |
|
cf. https://github.com/KerryPaterson/Imaging_pipelines |
|
From the input string, return the coordinate sections |
|
|
|
Parameters |
|
---------- |
|
string : str |
|
character string of the form [x1:x2,y1:y2] |
|
x1 = left pixel |
|
x2 = right pixel |
|
y1 = bottom pixel |
|
y2 = top pixel |
|
fmt_iraf : bool |
|
Is the variable string in IRAF format (True) or |
|
python format (False) |
|
|
|
Returns |
|
------- |
|
sections : list (or None) |
|
the detector sections |
|
""" |
|
xyrng = string.strip("[]()").split(",") |
|
if xyrng[0] == ":": |
|
xyarrx = [0, 0] |
|
else: |
|
xyarrx = xyrng[0].split(":") |
|
|
|
|
|
if len(xyarrx[0]) == 0: |
|
xyarrx[0] = 0 |
|
if len(xyarrx[1]) == 0: |
|
xyarrx[1] = -1 |
|
if xyrng[1] == ":": |
|
xyarry = [0, 0] |
|
else: |
|
xyarry = xyrng[1].split(":") |
|
|
|
|
|
if len(xyarry[0]) == 0: |
|
xyarry[0] = 0 |
|
if len(xyarry[1]) == 0: |
|
xyarry[1] = -1 |
|
if fmt_iraf: |
|
xmin = max(0, int(xyarry[0]) - 1) |
|
xmax = int(xyarry[1]) |
|
ymin = max(0, int(xyarrx[0]) - 1) |
|
ymax = int(xyarrx[1]) |
|
else: |
|
xmin = max(0, int(xyarrx[0])) |
|
xmax = int(xyarrx[1]) |
|
ymin = max(0, int(xyarry[0])) |
|
ymax = int(xyarry[1]) |
|
return [[xmin, xmax], [ymin, ymax]] |
|
|
|
|
|
def sec2slice( |
|
subarray, one_indexed=False, include_end=False, require_dim=None, transpose=False |
|
): |
|
""" |
|
Modified from pypit.core.parse.sec2slice -- Jon Brown |
|
|
|
Convert a string representation of an array subsection (slice) into |
|
a list of slice objects. |
|
|
|
Args: |
|
subarray (str): |
|
The string to convert. Should have the form of normal slice |
|
operation, 'start:stop:step'. The parser ignores whether or |
|
not the string has the brackets '[]', but the string must |
|
contain the appropriate ':' and ',' characters. |
|
one_indexed (:obj:`bool`, optional): |
|
The string should be interpreted as 1-indexed. Default |
|
is to assume python indexing. |
|
include_end (:obj:`bool`, optional): |
|
**If** the end is defined, adjust the slice such that |
|
the last element is included. Default is to exclude the |
|
last element as with normal python slicing. |
|
require_dim (:obj:`int`, optional): |
|
Test if the string indicates the slice along the proper |
|
number of dimensions. |
|
transpose (:obj:`bool`, optional): |
|
Transpose the order of the returned slices. The |
|
following are equivalent:: |
|
|
|
tslices = parse_sec2slice('[:10,10:]')[::-1] |
|
tslices = parse_sec2slice('[:10,10:]', transpose=True) |
|
|
|
Returns: |
|
tuple: A tuple of slice objects, one per dimension of the |
|
prospective array. |
|
|
|
Raises: |
|
TypeError: |
|
Raised if the input `subarray` is not a string. |
|
ValueError: |
|
Raised if the string does not match the required |
|
dimensionality or if the string does not look like a |
|
slice. |
|
""" |
|
|
|
if not isinstance(subarray, (str, bytes)): |
|
raise TypeError("Can only parse string-based subarray sections.") |
|
|
|
sections = subarray.strip("[]").split(",") |
|
|
|
ndim = len(sections) |
|
if require_dim is not None and ndim != require_dim: |
|
raise ValueError( |
|
"Number of slices ({0}) in {1} does not match ".format(ndim, subarray) |
|
+ "required dimensions ({0}).".format(require_dim) |
|
) |
|
|
|
|
|
slices = [] |
|
for s in sections: |
|
|
|
if ":" not in s: |
|
raise ValueError("Unrecognized slice string: {0}".format(s)) |
|
|
|
_s = [None if x == "" else int(x) for x in s.split(":")] |
|
if len(_s) > 3: |
|
raise ValueError( |
|
"String as too many sections. Must have format 'start:stop:step'." |
|
) |
|
if len(_s) < 3: |
|
|
|
_s += [None] |
|
if one_indexed: |
|
|
|
_s = [None if x is None else x - 1 for x in _s] |
|
if include_end and _s[1] is not None: |
|
|
|
_s[1] += 1 |
|
|
|
slices += [slice(*_s)] |
|
return tuple(slices[::-1] if transpose else slices) |
|
|
|
def read_lris(raw_file, det=None, TRIM=False): |
|
""" |
|
Modified from pypeit.spectrographs.keck_lris.read_lris -- Jon Brown, Josh Bloom |
|
cf. https://github.com/KerryPaterson/Imaging_pipelines |
|
|
|
Read a raw LRIS data frame (one or more detectors) |
|
Packed in a multi-extension HDU |
|
Based on readmhdufits.pro |
|
|
|
Parameters |
|
---------- |
|
raw_file : str |
|
Filename |
|
det : int, optional |
|
Detector number; Default = both |
|
TRIM : bool, optional |
|
Trim the image? |
|
|
|
Returns |
|
------- |
|
array : ndarray |
|
Combined image |
|
header : FITS header |
|
sections : list |
|
List of datasec, oscansec, ampsec sections |
|
""" |
|
|
|
hdu = fits.open(raw_file) |
|
head0 = hdu[0].header |
|
|
|
|
|
precol = head0["PRECOL"] |
|
postpix = head0["POSTPIX"] |
|
preline = head0["PRELINE"] |
|
postline = head0["POSTLINE"] |
|
|
|
|
|
|
|
|
|
if head0["INSTRUME"] == "LRISBLUE": |
|
redchip = False |
|
else: |
|
redchip = True |
|
|
|
|
|
dsec = [] |
|
osec = [] |
|
nxdata_sum = 0 |
|
|
|
|
|
binning = head0["BINNING"] |
|
xbin, ybin = [int(ibin) for ibin in binning.split(",")] |
|
|
|
|
|
n_ext = len(hdu) - 1 |
|
xcol = [] |
|
xmax = 0 |
|
ymax = 0 |
|
xmin = 10000 |
|
ymin = 10000 |
|
for i in np.arange(1, n_ext + 1): |
|
theader = hdu[i].header |
|
detsec = theader["DETSEC"] |
|
if detsec != "0": |
|
|
|
x1, x2, y1, y2 = np.array(load_sections(detsec, fmt_iraf=False)).flatten() |
|
|
|
|
|
|
|
xt = max(x2, x1) |
|
xmax = max(xt, xmax) |
|
yt = max(y2, y1) |
|
ymax = max(yt, ymax) |
|
|
|
|
|
xt = min(x1, x2) |
|
xmin = min(xmin, xt) |
|
yt = min(y1, y2) |
|
ymin = min(ymin, yt) |
|
|
|
xcol.append(xt) |
|
|
|
|
|
nx = xmax - xmin + 1 |
|
ny = ymax - ymin + 1 |
|
|
|
|
|
nx = nx // xbin |
|
ny = ny // ybin |
|
|
|
|
|
precol = precol // xbin |
|
postpix = postpix // xbin |
|
|
|
|
|
if det in [1, 2]: |
|
nx = nx // 2 |
|
n_ext = n_ext // 2 |
|
det_idx = np.arange(n_ext, dtype=np.int) + (det - 1) * n_ext |
|
elif det is None: |
|
det_idx = np.arange(n_ext).astype(int) |
|
else: |
|
raise ValueError("Bad value for det") |
|
|
|
|
|
if not TRIM: |
|
nx += n_ext * (precol + postpix) |
|
ny += preline + postline |
|
|
|
|
|
array = np.zeros((nx, ny), dtype="uint16") |
|
gain_array = np.zeros((nx, ny), dtype="uint16") |
|
order = np.argsort(np.array(xcol)) |
|
|
|
|
|
for kk, i in enumerate(order[det_idx]): |
|
|
|
|
|
data, gaindata, predata, postdata, x1, y1 = lris_read_amp( |
|
hdu, i + 1, redchip=redchip |
|
) |
|
|
|
|
|
if not TRIM: |
|
|
|
buf = predata.shape |
|
nxpre = buf[0] |
|
xs = kk * precol |
|
xe = xs + nxpre |
|
|
|
array[xs:xe, :] = predata |
|
gain_array[xs:xe, :] = predata |
|
|
|
|
|
buf = data.shape |
|
nxdata = buf[0] |
|
nydata = buf[1] |
|
|
|
|
|
xs = n_ext * precol + nxdata_sum |
|
xe = xs + nxdata |
|
|
|
|
|
nxdata_sum += nxdata |
|
|
|
|
|
|
|
section = "[{:d}:{:d},{:d}:{:d}]".format( |
|
preline, nydata, xs, xe |
|
) |
|
|
|
dsec.append(section) |
|
array[xs:xe, :] = data |
|
gain_array[xs:xe, :] = gaindata |
|
|
|
|
|
buf = postdata.shape |
|
nxpost = buf[0] |
|
xs = nx - n_ext * postpix + kk * postpix |
|
xe = xs + nxpost |
|
section = "[:,{:d}:{:d}]".format(xs, xe) |
|
osec.append(section) |
|
|
|
array[xs:xe, :] = postdata |
|
gain_array[xs:xe, :] = postdata |
|
|
|
else: |
|
buf = data.shape |
|
nxdata = buf[0] |
|
nydata = buf[1] |
|
|
|
xs = (x1 - xmin) // xbin |
|
xe = xs + nxdata |
|
ys = (y1 - ymin) // ybin |
|
ye = ys + nydata - postline |
|
|
|
yin1 = preline |
|
yin2 = nydata - postline |
|
|
|
array[xs:xe, ys:ye] = data[:, yin1:yin2] |
|
gain_array[xs:xe, ys:ye] = gaindata[:, yin1:yin2] |
|
|
|
|
|
obzero = head0["BZERO"] |
|
head0["O_BZERO"] = obzero |
|
head0["BZERO"] = 32768 - obzero |
|
|
|
|
|
return array.T, head0 |
|
|
|
|
|
def make_split_jsonl_files( |
|
config_type="tiny", data_dir="./data", outdir="./splits", seed=42 |
|
): |
|
""" |
|
Create jsonl files for the GBI-16-2D dataset. |
|
|
|
config_type: str, default="tiny" |
|
The type of split to create. Options are "tiny" and "full". |
|
data_dir: str, default="./data" |
|
The directory where the FITS files are located. |
|
outdir: str, default="./splits" |
|
The directory where the jsonl files will be created. |
|
seed: int, default=42 |
|
The seed for the random split. |
|
""" |
|
random.seed(seed) |
|
os.makedirs(outdir, exist_ok=True) |
|
|
|
fits_files = glob(os.path.join(data_dir, "*.fits")) |
|
random.shuffle(fits_files) |
|
if config_type == "tiny": |
|
train_files = fits_files[:2] |
|
test_files = fits_files[2:3] |
|
elif config_type == "full": |
|
split_idx = int(0.8 * len(fits_files)) |
|
train_files = fits_files[:split_idx] |
|
test_files = fits_files[split_idx:] |
|
else: |
|
raise ValueError("Unsupported config_type. Use 'tiny' or 'full'.") |
|
|
|
def create_jsonl(files, split_name): |
|
output_file = os.path.join(outdir, f"{config_type}_{split_name}.jsonl") |
|
with open(output_file, "w") as out_f: |
|
for file in tqdm(files): |
|
|
|
image_id = os.path.basename(file).split(".fits")[0] |
|
with fits.open(file, memmap=False) as hdul: |
|
if len(hdul) > 1: |
|
|
|
data, header = read_lris(file) |
|
dim_1 = data.shape[0] |
|
dim_2 = data.shape[1] |
|
header = fits.header.Header(header) |
|
else: |
|
dim_1 = hdul[0].header.get("NAXIS1", 0) |
|
dim_2 = hdul[0].header.get("NAXIS2", 0) |
|
header = hdul[0].header |
|
|
|
ras = header.get("RA", "0") |
|
ra = float( |
|
Angle(f"{ras} hours").to_string(unit=u.degree, decimal=True) |
|
) |
|
decs = header.get("DEC", "0") |
|
dec = float( |
|
Angle(f"{decs} degrees").to_string(unit=u.degree, decimal=True) |
|
) |
|
pixscale = header.get("CD1_2", 0.135) |
|
rotation = header.get("ROTPOSN", 0.0) |
|
exposure_time = header.get("TTIME", 0.0) |
|
item = { |
|
"image_id": image_id, |
|
"image": file, |
|
"ra": ra, |
|
"dec": dec, |
|
"pixscale": pixscale, |
|
"rotation_angle": rotation, |
|
"dim_1": dim_1, |
|
"dim_2": dim_2, |
|
"exposure_time": exposure_time, |
|
} |
|
out_f.write(json.dumps(item) + "\n") |
|
|
|
create_jsonl(train_files, "train") |
|
create_jsonl(test_files, "test") |
|
|
|
|
|
if __name__ == "__main__": |
|
make_split_jsonl_files("tiny") |
|
make_split_jsonl_files("full") |