AstroFrame/tools/inspect_data.py
2026-10-03 18:29:15 -07:00

45 lines
2.2 KiB
Python

"""Read-only FITS inventory and exploratory measurements. Requires NumPy only."""
from pathlib import Path
import json, datetime, collections
import numpy as np
def header(path):
h={}
with open(path,'rb') as f:
done=False
while not done:
block=f.read(2880)
if len(block)!=2880: raise ValueError('Truncated header')
for i in range(0,2880,80):
s=block[i:i+80].decode('ascii'); k=s[:8].strip()
if k=='END': done=True; break
if s[8:10]=='= ':
v=s[10:].split('/')[0].strip().strip("'").strip(); h[k]=v
return h,f.tell()
def image(path):
h,offset=header(path)
a=np.memmap(path,dtype='>i2',mode='r',offset=offset,shape=(int(h['NAXIS2']),int(h['NAXIS1'])))
a=a.astype(np.float32)*float(h.get('BSCALE',1))+float(h.get('BZERO',0))
# Same Bayer phase per superpixel; no color interpolation or sharpening.
a=(a[::2,::2]+a[1::2,::2]+a[::2,1::2]+a[1::2,1::2])/4
return h,a
def measurements(path):
h,a=image(path); bg=float(np.median(a)); noise=float(np.median(np.abs(a[:,1:]-a[:,:-1]))/0.954)
p=a[1:-1,1:-1]; peaks=p>bg+8*max(noise,1)
for dy,dx in [(0,1),(0,-1),(1,0),(-1,0),(1,1),(1,-1),(-1,1),(-1,-1)]:
peaks &= p>a[1+dy:a.shape[0]-1+dy,1+dx:a.shape[1]-1+dx]
return {'file':str(path),'date':h.get('DATE-OBS'),'background':round(bg,2),'noise':round(noise,2),'peaks':int(peaks.sum()),'p99':float(np.percentile(a,99))}
root=Path('Sample Data'); report={}
for folder in sorted(root.iterdir()):
files=sorted(folder.glob('*.fit')); records=[]
for p in files:
h,_=header(p); records.append((h.get('DATE-OBS',''),h))
nights=collections.Counter(x[0][:10] for x in records)
samples=[measurements(files[i]) for i in np.linspace(0,len(files)-1,min(14,len(files)),dtype=int)]
report[folder.name]={'count':len(files),'dates':dict(nights),'formats':list({(h.get('NAXIS1'),h.get('NAXIS2'),h.get('BITPIX'),h.get('BAYERPAT'),h.get('EXPTIME'),h.get('FILTER'),h.get('GAIN')) for _,h in records}),'samples':samples}
Path('docs').mkdir(exist_ok=True)
Path('docs/dataset-inspection.json').write_text(json.dumps(report,indent=2))
print(json.dumps(report,indent=2))