#!/usr/bin/env python
"""List downloadable ALMA product tar sizes for G034.4035 ALMAGAL MOUS,
WITHOUT tarfile expansion (fast), to gauge download feasibility."""
import numpy as np
from astropy.coordinates import SkyCoord
import astropy.units as u
from astroquery.alma import Alma
LOG=open('probe_dl.log','w')
def p(*a):
    s=' '.join(str(x) for x in a);print(s);print(s,file=LOG);LOG.flush()
a=Alma(); a.archive_url='https://almascience.eso.org'
c=SkyCoord(34.4035*u.deg,0.2282*u.deg,frame='galactic').icrs
res=a.query_region(c, radius=12*u.arcsec)
# restrict to ALMAGAL project + smaller programs; list per-MOUS
proj=np.array([str(x) for x in res['proposal_id']])
mous=np.array([str(x) for x in res['member_ous_uid']])
band=np.array([str(x) for x in res['band_list']])
p('projects here:',sorted(set(proj)))
# pick a few MOUS (prefer band 6, ALMAGAL or any)
uids=sorted(set(mous))
p('n MOUS',len(uids))
try:
    info=a.get_data_info(uids[:4], expand_tarfiles=False)
    urls=[str(x) for x in info['access_url']]
    sizes=np.array(info['content_length'],float)/1e9  # GB
    for u_,s_ in sorted(zip(urls,sizes),key=lambda t:t[1]):
        p(f'  {s_:7.2f} GB  {u_.split("/")[-1][:75]}')
    p(f'TOTAL listed: {sizes.sum():.1f} GB across {len(urls)} files')
except Exception as e:
    p('ERR',repr(e)[:200])
LOG.close()
