Skip to content

Commit

Permalink
✨ Add download_phoenix_grid
Browse files Browse the repository at this point in the history
  • Loading branch information
brackham committed Dec 21, 2022
1 parent fe4b67e commit ce02921
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions speclib/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import astropy.units as u
import astropy.io.fits as fits
import itertools
import numpy as np
import os
import shutil
import urllib
from astropy.io import fits
from contextlib import closing
from urllib.error import URLError

__all__ = [
"download_file",
"download_phoenix_grid",
"find_bounds",
"interpolate",
"load_flux_array",
Expand All @@ -29,6 +32,51 @@ def download_file(remote_path, local_path, verbose=True):
shutil.copyfileobj(r, f)


def download_phoenix_grid(overwrite=False):
# Define the remote and local paths
ftp_url = "ftp://phoenix.astro.physik.uni-goettingen.de"
cache_dir = os.path.join(
os.path.expanduser("~"), ".speclib/libraries/phoenix/"
)
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
fname_str = (
"lte{:05.0f}-{:0.2f}{:+0.1f}."
+ "PHOENIX-ACES-AGSS-COND-2011-HiRes.fits"
)

# Define the parameter space
param_combos = list(itertools.product(*GRID_POINTS['phoenix'].values()))

# Iterate through the parameter space
for combo in param_combos:
fname = fname_str.format(*combo)
local_path = os.path.join(cache_dir, fname)
feh_folder = "Z" + fname[13:17]
remote_path = os.path.join(
ftp_url, "HiResFITS/PHOENIX-ACES-AGSS-COND-2011", feh_folder, fname
)
# If overwriting, just go ahead and download the file
if overwrite:
download_file(remote_path, local_path)

# Otherwise, skip files that already exist locally
else:
try:
_ = fits.getdata(local_path)
continue

# If that doesn't work, download the remote file
except FileNotFoundError:
try:
download_file(remote_path, local_path)
continue

# Some low-G models are missing, e.g., lte05400-0.00+1.0...
except URLError:
continue


def find_bounds(array, value):
"""
Find and return the two nearest values in an array to a given value.
Expand Down

0 comments on commit ce02921

Please sign in to comment.