from astroquery.mast import Observations
import os
Observations.login(token=os.getenv('MAST_API_KEY'))
program_id="5365"
obs_table = Observations.query_criteria(proposal_id="5365", obs_collection="JWST", instrument_name="MIRI")
products = Observations.get_product_list(obs_table)
download_directory = "/orange/adamginsburg/jwst/sgrb2/NB"

Observations.download_products(products, download_dir=download_directory, cache=True)

# for product in products:
#     file_uri = product['productFilename']
#     local_file_path = os.path.join(download_directory, file_uri)

#     if os.path.exists(local_file_path):
#         print(f"File already exists, skipping: {file_uri}")
#     else:
#         print(f"Downloading: {file_uri}")
#         Observations.download_products([product], download_dir=download_directory)

# print("Download process complete.")
