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

61 lines
3.4 KiB
Python

"""Read-only, one-frame-at-a-time diagnostic of aligned sky structure. NumPy only."""
from pathlib import Path
import json
import numpy as np
def plane(path):
with open(path,'rb') as f:
header={};done=False
while not done:
block=f.read(2880)
for i in range(0,2880,80):
s=block[i:i+80].decode('ascii');key=s[:8].strip()
if key=='END':done=True;break
if s[8:10]=='= ':header[key]=s[10:].split('/')[0].strip().strip("'").strip()
offset=f.tell()
raw=np.memmap(path,dtype='>i2',mode='r',offset=offset,shape=(int(header['NAXIS2']),int(header['NAXIS1'])))
a=raw.astype(np.float32)+float(header.get('BZERO',0))
return (a[::2,::2]+a[1::2,::2]+a[::2,1::2]+a[1::2,1::2])/4
def detrend(v):
good=np.isfinite(v);coef=np.zeros(3)
for _ in range(5):
coef=np.linalg.lstsq(design[good],v[good],rcond=None)[0]
residual=v-design@coef
sigma=max(1,1.4826*np.nanmedian(np.abs(residual[good]-np.nanmedian(residual[good]))))
good=np.isfinite(v)&(np.abs(residual)<3*sigma)
return v-design@coef
if __name__=='__main__':
p=json.loads(Path('projects/Sample Survey/project.json').read_text())
frames=[f for f in p['Frames'] if f['Target']=='NGC 1432' and f['Timestamp'].startswith('2026-09-20')]
cache=Path('docs/diagnostics/cloud-maps.npz');cache.parent.mkdir(exist_ok=True)
cols,rows=24,40
xx,yy=np.meshgrid((np.arange(cols)+.5)/cols,(np.arange(rows)+.5)/rows)
x,y=xx.ravel(),yy.ravel();design=np.column_stack([np.ones(len(x)),x-.5,y-.5])
if cache.exists():maps=np.load(cache)['maps']
else:
maps=[]
for i,f in enumerate(frames):
a=plane(f['Path']);m=np.median(a.reshape(rows,a.shape[0]//rows,cols,a.shape[1]//cols),axis=(1,3))
aa,b,tx,ty=f['Alignment'];aspect=f['Height']/f['Width']
u=(aa*x-b*y*aspect+tx)*cols-.5;v=(b*x+aa*y*aspect+ty)/aspect*rows-.5
ix=np.floor(u).astype(int);iy=np.floor(v).astype(int);valid=(ix>=0)&(iy>=0)&(ix<cols-1)&(iy<rows-1)
ix=np.clip(ix,0,cols-2);iy=np.clip(iy,0,rows-2);dx=u-ix;dy=v-iy
aligned=(m[iy,ix]*(1-dx)+m[iy,ix+1]*dx)*(1-dy)+(m[iy+1,ix]*(1-dx)+m[iy+1,ix+1]*dx)*dy
aligned[~valid]=np.nan;maps.append(detrend(aligned))
if i%50==0:print(f'maps {i}/{len(frames)}',flush=True)
maps=np.array(maps);np.savez(cache,maps=maps)
reference=np.nanmedian(maps,axis=0)
for _ in range(3):
residual=np.array([detrend(m-reference) for m in maps]);amplitude=np.nanpercentile(np.abs(residual),95,axis=1)
clean=amplitude<np.percentile(amplitude,40)
reference=np.nanmedian(maps[clean],axis=0)
residual=np.array([detrend(m-reference) for m in maps]);amp=np.nanpercentile(np.abs(residual),95,axis=1)
print('residual q:',np.percentile(amp,[0,10,25,40,50,75,90,95,99,100]).round(2).tolist())
labels=json.loads(Path('tests/real-frame-labels.json').read_text())['frames']
for label in labels:
i=next(i for i,f in enumerate(frames) if Path(f['Path']).name==label['file'])
print(label['file'],label['expected'],'amp',round(amp[i],2),'above100',round(float(np.nanmean(np.abs(residual[i])>100)),3))
np.savez('docs/diagnostics/cloud-residuals.npz',maps=maps,residual=residual,reference=reference,amplitude=amp)
Path('docs/diagnostics/cloud-map-index.json').write_text(json.dumps([f['Path'] for f in frames]))