"""
Inventory what FITS data we actually have on disk for the Bullet Cluster.
Read headers to determine filter / instrument / target coverage.
"""
import os
import glob
from collections import defaultdict

# Suppress astropy WARN noise
import warnings
warnings.filterwarnings('ignore')

from astropy.io import fits

base = r'C:\Users\QCCI\Downloads\UM_FUM_Package_For_Alfred_McBride_2026-05-14\Bullet_Cluster_Data'

print("=" * 70)
print("INVENTORY OF DOWNLOADED BULLET CLUSTER DATA")
print("=" * 70)

# NIRCam
print("\n--- JWST NIRCam ---")
nircam_files = glob.glob(os.path.join(base, 'JWST_GO4598_NIRCAM', '*.fits'))
nircam_by_filter = defaultdict(int)
nircam_by_filter_bytes = defaultdict(int)
for f in nircam_files:
    sz = os.path.getsize(f)
    try:
        with fits.open(f, memmap=True) as hdul:
            h = hdul[0].header
            filt = h.get('FILTER', '?')
            nircam_by_filter[filt] += 1
            nircam_by_filter_bytes[filt] += sz
    except Exception as e:
        nircam_by_filter['ERROR'] += 1

print(f"Total NIRCam files: {len(nircam_files)}")
print(f"Total NIRCam bytes: {sum(nircam_by_filter_bytes.values())/1e9:.2f} GB")
print(f"\nBy filter:")
for filt in sorted(nircam_by_filter.keys()):
    print(f"  {filt:10s}  {nircam_by_filter[filt]:4d} files  {nircam_by_filter_bytes[filt]/1e9:.2f} GB")

# F277W standalone
print("\n--- JWST F277W standalone ---")
f277w_files = glob.glob(os.path.join(base, 'JWST_GO4598_F277W', '*.fits'))
print(f"  {len(f277w_files)} files")

# HST
print("\n--- HST ACS/WFC/NICMOS ---")
hst_files = glob.glob(os.path.join(base, 'HST_ACS_BulletCluster', '*.fits'))
hst_by_filter = defaultdict(int)
hst_by_filter_bytes = defaultdict(int)
hst_by_instr = defaultdict(int)
hst_by_program = defaultdict(int)
for f in hst_files:
    sz = os.path.getsize(f)
    try:
        with fits.open(f, memmap=True) as hdul:
            h = hdul[0].header
            filt = (h.get('FILTER1', '') or h.get('FILTER', '')).strip()
            filt2 = (h.get('FILTER2', '') or '').strip()
            if filt == 'CLEAR1L' or filt == 'CLEAR1S':
                filt = filt2
            if filt == 'CLEAR2L' or filt == 'CLEAR2S':
                filt = filt
            instr = h.get('INSTRUME', '?')
            prog = h.get('PROPOSID', '?')
            hst_by_filter[filt] += 1
            hst_by_filter_bytes[filt] += sz
            hst_by_instr[instr] += 1
            hst_by_program[prog] += 1
    except Exception as e:
        hst_by_filter['ERROR'] += 1

print(f"Total HST files: {len(hst_files)}")
print(f"Total HST bytes: {sum(hst_by_filter_bytes.values())/1e9:.2f} GB")
print(f"\nBy filter:")
for filt in sorted(hst_by_filter.keys()):
    print(f"  {filt:10s}  {hst_by_filter[filt]:4d} files  {hst_by_filter_bytes[filt]/1e9:.2f} GB")
print(f"\nBy instrument:")
for instr, n in sorted(hst_by_instr.items()):
    print(f"  {instr}: {n}")
print(f"\nBy program:")
for prog, n in sorted(hst_by_program.items()):
    print(f"  Program {prog}: {n}")

# Chandra
print("\n--- Chandra ACIS-I ---")
chandra_files = glob.glob(os.path.join(base, 'Chandra_BulletCluster_cdc373', '**', '*.fits*'), recursive=True)
print(f"  {len(chandra_files)} files")

# Total
total_files = len(nircam_files) + len(f277w_files) + len(hst_files) + len(chandra_files)
total_bytes = (sum(nircam_by_filter_bytes.values()) +
               sum(os.path.getsize(f) for f in f277w_files) +
               sum(hst_by_filter_bytes.values()) +
               sum(os.path.getsize(f) for f in chandra_files))
print(f"\n=== GRAND TOTAL ===")
print(f"  {total_files} files, {total_bytes/1e9:.2f} GB")
