File size: 17,807 Bytes
f8c5348 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 |
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
;------------------------------------------------------------------------
"""
# Parse input
if isinstance(inp, str):
hdu = fits.open(inp)
else:
hdu = inp
# Get the pre and post pix values
# for LRIS red POSTLINE = 20, POSTPIX = 80, PRELINE = 0, PRECOL = 12
head0 = hdu[0].header
precol = head0["precol"]
postpix = head0["postpix"]
# Deal with binning
binning = head0["BINNING"]
xbin, ybin = [int(ibin) for ibin in binning.split(",")]
precol = precol // xbin
postpix = postpix // xbin
# get entire extension...
temp = hdu[ext].data.transpose() # Silly Python nrow,ncol formatting
tsize = temp.shape
nxt = tsize[0]
# parse the DETSEC keyword to determine the size of the array.
header = hdu[ext].header
detsec = header["DETSEC"]
x1, x2, y1, y2 = np.array(load_sections(detsec, fmt_iraf=False)).flatten()
# parse the DATASEC keyword to determine the size of the science region (unbinned)
datasec = header["DATASEC"]
xdata1, xdata2, ydata1, ydata2 = np.array(
load_sections(datasec, fmt_iraf=False)
).flatten()
# grab the components...
predata = temp[0:precol, :]
# datasec appears to have the x value for the keywords that are zero
# based. This is only true in the image header extensions
# not true in the main header. They also appear inconsistent between
# LRISr and LRISb!
# data = temp[xdata1-1:xdata2-1,*]
# data = temp[xdata1:xdata2+1, :]
# JB: LRIS-R is windowed differently, so the default pypeit checks fail
# xshape is calculated from datasec.
# For blue, its 1024,
# For red, the chip dimensions are different AND the observations are windowed
# In windowed mode each amplifier has differently sized data sections
if not redchip:
xshape = 1024 // xbin # blue
else:
xshape = xdata2 - xdata1 + 1 // xbin # red
# do some sanity checks
if (xdata1 - 1) != precol:
# msgs.error("Something wrong in LRIS datasec or precol")
errStr = "Something wrong in LRIS datasec or precol"
print(errStr)
if (xshape + precol + postpix) != temp.shape[0]:
# msgs.error("Wrong size for in LRIS detector somewhere. Funny binning?")
errStr = "Wrong size for in LRIS detector somewhere. Funny binning?"
print(errStr)
data = temp[precol : precol + xshape, :]
postdata = temp[nxt - postpix : nxt, :]
# flip in X as needed...
if x1 > x2:
xt = x2
x2 = x1
x1 = xt
data = np.flipud(data) # reverse(temporary(data),1)
# flip in Y as needed...
if y1 > y2:
yt = y2
y2 = y1
y1 = yt
data = np.fliplr(data)
predata = np.fliplr(predata)
postdata = np.fliplr(postdata)
# dummy gain data since we're keeping as uint16
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 a lower/upper limit on the array slicing is not given (e.g. [:100] has no lower index specified),
# set the lower/upper limit to be the first/last index.
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 a lower/upper limit on the array slicing is not given (e.g. [5:] has no upper index specified),
# set the lower/upper limit to be the first/last index.
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.
"""
# Check it's a string
if not isinstance(subarray, (str, bytes)):
raise TypeError("Can only parse string-based subarray sections.")
# Remove brackets if they're included
sections = subarray.strip("[]").split(",")
# Check the dimensionality
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)
)
# Convert the slice of each dimension from a string to a slice
# object
slices = []
for s in sections:
# Must be able to find the colon
if ":" not in s:
raise ValueError("Unrecognized slice string: {0}".format(s))
# Initial conversion
_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:
# Include step
_s += [None]
if one_indexed:
# Decrement to convert from 1- to 0-indexing
_s = [None if x is None else x - 1 for x in _s]
if include_end and _s[1] is not None:
# Increment to include last
_s[1] += 1
# Append the new slice
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
# Get post, pre-pix values
precol = head0["PRECOL"]
postpix = head0["POSTPIX"]
preline = head0["PRELINE"]
postline = head0["POSTLINE"]
# get the detector
# this just checks if its the blue one and assumes red if not
# note the red fits headers don't even have this keyword???
if head0["INSTRUME"] == "LRISBLUE":
redchip = False
else:
redchip = True
# Setup for datasec, oscansec
dsec = []
osec = []
nxdata_sum = 0
# get the x and y binning factors...
binning = head0["BINNING"]
xbin, ybin = [int(ibin) for ibin in binning.split(",")]
# First read over the header info to determine the size of the output array...
n_ext = len(hdu) - 1 # Number of extensions (usually 4)
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":
# parse the DETSEC keyword to determine the size of the array.
x1, x2, y1, y2 = np.array(load_sections(detsec, fmt_iraf=False)).flatten()
# find the range of detector space occupied by the data
# [xmin:xmax,ymin:ymax]
xt = max(x2, x1)
xmax = max(xt, xmax)
yt = max(y2, y1)
ymax = max(yt, ymax)
# find the min size of the array
xt = min(x1, x2)
xmin = min(xmin, xt)
yt = min(y1, y2)
ymin = min(ymin, yt)
# Save
xcol.append(xt)
# determine the output array size...
nx = xmax - xmin + 1
ny = ymax - ymin + 1
# change size for binning...
nx = nx // xbin
ny = ny // ybin
# Update PRECOL and POSTPIX
precol = precol // xbin
postpix = postpix // xbin
# Deal with detectors
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")
# change size for pre/postscan...
if not TRIM:
nx += n_ext * (precol + postpix)
ny += preline + postline
# allocate output array...
array = np.zeros((nx, ny), dtype="uint16")
gain_array = np.zeros((nx, ny), dtype="uint16")
order = np.argsort(np.array(xcol))
# insert extensions into master image...
for kk, i in enumerate(order[det_idx]):
# grab complete extension...
data, gaindata, predata, postdata, x1, y1 = lris_read_amp(
hdu, i + 1, redchip=redchip
)
# insert components into output array...
if not TRIM:
# insert predata...
buf = predata.shape
nxpre = buf[0]
xs = kk * precol
xe = xs + nxpre
array[xs:xe, :] = predata
gain_array[xs:xe, :] = predata
# insert data...
buf = data.shape
nxdata = buf[0]
nydata = buf[1]
# JB: have to track the number of xpixels
xs = n_ext * precol + nxdata_sum
xe = xs + nxdata
# now log how many pixels that was
nxdata_sum += nxdata
# Data section
# section = '[{:d}:{:d},{:d}:{:d}]'.format(preline,nydata-postline, xs, xe) # Eliminate lines
section = "[{:d}:{:d},{:d}:{:d}]".format(
preline, nydata, xs, xe
) # DONT eliminate lines
dsec.append(section)
array[xs:xe, :] = data # Include postlines
gain_array[xs:xe, :] = gaindata # Include postlines
# ; insert postdata...
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]
# make sure BZERO is a valid integer for IRAF
obzero = head0["BZERO"]
head0["O_BZERO"] = obzero
head0["BZERO"] = 32768 - obzero
# Return, transposing array back to goofy Python indexing
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):
#print(file, flush=True, end="...")
image_id = os.path.basename(file).split(".fits")[0]
with fits.open(file, memmap=False) as hdul:
if len(hdul) > 1:
# multiextension ... paste together
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") |