Skip to content

Commit

Permalink
This commit introduces a function which download spectral files data
Browse files Browse the repository at this point in the history
from the OSF. Tests and examples are updated.
  • Loading branch information
lsoucasse committed Jul 4, 2024
1 parent c142303 commit 05b0744
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 10 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ jobs:
- name: Test with pytest
run: |
pip install pytest
export FWL_DATA="./fwl_data"
source SOCRATES/set_rad_env
pytest
11 changes: 9 additions & 2 deletions examples/SocRadConv.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from janus.utils.atmosphere_column import atmos
import janus.utils.StellarSpectrum as StellarSpectrum
from janus.utils.ReadSpectralFile import ReadBandEdges
from janus.utils.data import DownloadSpectralFiles

####################################
##### Stand-alone initial conditions
Expand All @@ -42,6 +43,8 @@
# Set up dirs
if os.environ.get('RAD_DIR') == None:
raise Exception("Socrates environment variables not set! Have you installed Socrates and sourced set_rad_env?")
if os.environ.get('FWL_DATA') == None:
raise Exception("The FWL_DATA environment variable where spectral data will be downloaded needs to be set up!")
dirs = {
"janus": str(files("janus"))+"/",
"output": os.path.abspath(os.getcwd())+"/output/"
Expand Down Expand Up @@ -74,11 +77,15 @@
shutil.rmtree(dirs["output"])
os.mkdir(dirs["output"])

#Download required spectral files
DownloadSpectralFiles("/Dayspring")
DownloadSpectralFiles("/stellar_spectra")

# Move/prepare spectral file
print("Inserting stellar spectrum")
StellarSpectrum.InsertStellarSpectrum(
dirs["janus"]+"data/spectral_files/Dayspring/256/Dayspring.sf",
dirs["janus"]+"data/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
os.environ.get('FWL_DATA')+"/spectral_files/Dayspring/256/Dayspring.sf",
os.environ.get('FWL_DATA')+"/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
dirs["output"]
)

Expand Down
11 changes: 9 additions & 2 deletions examples/demo_instellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import janus.utils.StellarSpectrum as StellarSpectrum
import janus.utils.phys as phys
from janus.utils.ReadSpectralFile import ReadBandEdges
from janus.utils.data import DownloadSpectralFiles

if __name__=='__main__':

Expand All @@ -26,6 +27,8 @@
# Set up dirs
if os.environ.get('RAD_DIR') == None:
raise Exception("Socrates environment variables not set! Have you installed Socrates and sourced set_rad_env?")
if os.environ.get('FWL_DATA') == None:
raise Exception("The FWL_DATA environment variable where spectral data will be downloaded needs to be set up!")
dirs = {
"janus": str(files("janus"))+"/",
"output": os.path.abspath(os.getcwd())+"/output/"
Expand All @@ -36,11 +39,15 @@
shutil.rmtree(dirs["output"])
os.mkdir(dirs["output"])

#Download required spectral files
DownloadSpectralFiles("/Oak")
DownloadSpectralFiles("/stellar_spectra")

# Setup spectral file
print("Inserting stellar spectrum")
StellarSpectrum.InsertStellarSpectrum(
dirs["janus"]+"data/spectral_files/Oak/Oak.sf",
dirs["janus"]+"data/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
os.environ.get('FWL_DATA')+"/spectral_files/Oak/318/Oak.sf",
os.environ.get('FWL_DATA')+"/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
dirs["output"]
)
band_edges = ReadBandEdges(dirs["output"]+"star.sf")
Expand Down
11 changes: 9 additions & 2 deletions examples/demo_runaway_greenhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from janus.utils.atmosphere_column import atmos
import janus.utils.StellarSpectrum as StellarSpectrum
from janus.utils.ReadSpectralFile import ReadBandEdges
from janus.utils.data import DownloadSpectralFiles

if __name__=='__main__':

Expand All @@ -26,6 +27,8 @@
# Set up dirs
if os.environ.get('RAD_DIR') == None:
raise Exception("Socrates environment variables not set! Have you installed Socrates and sourced set_rad_env?")
if os.environ.get('FWL_DATA') == None:
raise Exception("The FWL_DATA environment variable where spectral data will be downloaded needs to be set up!")
dirs = {
"janus": str(files("janus"))+"/",
"output": os.path.abspath(os.getcwd())+"/output/"
Expand All @@ -36,11 +39,15 @@
shutil.rmtree(dirs["output"])
os.mkdir(dirs["output"])

#Download required spectral files
DownloadSpectralFiles("/Oak")
DownloadSpectralFiles("/stellar_spectra")

# Setup spectral file
print("Inserting stellar spectrum")
StellarSpectrum.InsertStellarSpectrum(
dirs["janus"]+"data/spectral_files/Oak/Oak.sf",
dirs["janus"]+"data/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
os.environ.get('FWL_DATA')+"/spectral_files/Oak/318/Oak.sf",
os.environ.get('FWL_DATA')+"/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
dirs["output"]
)
print(" ")
Expand Down
78 changes: 78 additions & 0 deletions src/janus/utils/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import os
from osfclient.api import OSF

#project ID of the stellar evolution tracks folder in the OSF
project_id = 'vehxg'

basic_list =[
"/Dayspring/256",
"/Frostflow/256",
"/Legacy",
"/Mallard",
"/Oak",
"/Reach",
"/stellar_spectra"
]

def download_folder(storage, folder_name, local_path):
''''
Download a specific folder in the OSF repository
Inputs :
- storage : OSF storage name
- folder_name : folder name to be downloaded
- local_path : local repository where data are saved
'''
for file in storage.files:
if file.path.startswith(folder_name):
local_file_path = local_path + file.path
#Create local directory if needed
os.makedirs(os.path.dirname(local_file_path), exist_ok=True)
#Download the file
with open(local_file_path, 'wb') as local_file:
file.write_to(local_file)
return

def DownloadSpectralFiles(fname="",nband=256):
''''
Download spectral files data
Inputs :
- fname (optional) : folder name, i.e. "/Dayspring"
if not provided download all the basic list
- nband (optional) : number of band = 16, 48, 256, 4096
(only relevant for Dayspring, Frostflow and Honeyside)
'''

#Check if data environment variable is set up
fwl_data_dir = os.getenv('FWL_DATA')
if os.environ.get("FWL_DATA") == None:
raise Exception("The FWL_DATA environment variable where spectral data will be downloaded needs to be set up!")

#Create spectral file data repository if not existing
data_dir = fwl_data_dir + "/spectral_files"
if not os.path.exists(data_dir):
os.makedirs(data_dir)

#Link with OSF project repository
osf = OSF()
project = osf.project(project_id)
storage = project.storage('osfstorage')

#If no folder specified download all basic list
if not fname:
for folder in basic_list:
print(folder)
download_folder(storage,folder,data_dir)
elif fname in ["/Dayspring","/Frostflow","/Honeyside"]:
print("HERE ")
folder = fname + "/" + str(nband)
print(folder)
download_folder(storage,folder,data_dir)
elif fname in ["/Kynesgrove","/Legacy","/Mallard","/Oak","/Reach","/stellar_spectra"]:
folder = fname
download_folder(storage,folder,data_dir)
else:
print("Unrecognised folder name in DownloadSpectralFiles")

return
11 changes: 9 additions & 2 deletions tests/test_instellation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
import janus.utils.StellarSpectrum as StellarSpectrum
import janus.utils.phys as phys
from janus.utils.ReadSpectralFile import ReadBandEdges
from janus.utils.data import DownloadSpectralFiles

def test_instellation():

# Set up dirs
if os.environ.get('RAD_DIR') == None:
raise Exception("Socrates environment variables not set! Have you installed Socrates and sourced set_rad_env?")
if os.environ.get('FWL_DATA') == None:
raise Exception("The FWL_DATA environment variable where spectral data will be downloaded needs to be set up!")
dirs = {
"janus": str(files("janus"))+"/",
"output": os.path.abspath(os.getcwd())+"/output/"
Expand All @@ -27,11 +30,15 @@ def test_instellation():
shutil.rmtree(dirs["output"])
os.mkdir(dirs["output"])

#Download required spectral files
DownloadSpectralFiles("/Oak")
DownloadSpectralFiles("/stellar_spectra")

# Setup spectral file
print("Inserting stellar spectrum")
StellarSpectrum.InsertStellarSpectrum(
dirs["janus"]+"data/spectral_files/Oak/Oak.sf",
dirs["janus"]+"data/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
os.environ.get('FWL_DATA')+"/spectral_files/Oak/318/Oak.sf",
os.environ.get('FWL_DATA')+"/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
dirs["output"]
)
band_edges = ReadBandEdges(dirs["output"]+"star.sf")
Expand Down
11 changes: 9 additions & 2 deletions tests/test_runaway_greenhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
from janus.utils.atmosphere_column import atmos
import janus.utils.StellarSpectrum as StellarSpectrum
from janus.utils.ReadSpectralFile import ReadBandEdges
from janus.utils.data import DownloadSpectralFiles

def test_runaway_greenhouse():

# Set up dirs
if os.environ.get('RAD_DIR') == None:
raise Exception("Socrates environment variables not set! Have you installed Socrates and sourced set_rad_env?")
if os.environ.get('FWL_DATA') == None:
raise Exception("The FWL_DATA environment variable where spectral data will be downloaded needs to be set up!")
dirs = {
"janus": str(files("janus"))+"/",
"output": os.path.abspath(os.getcwd())+"/output/"
Expand All @@ -28,11 +31,15 @@ def test_runaway_greenhouse():
shutil.rmtree(dirs["output"])
os.mkdir(dirs["output"])

#Download required spectral files
DownloadSpectralFiles("/Oak")
DownloadSpectralFiles("/stellar_spectra")

# Setup spectral file
print("Inserting stellar spectrum")
StellarSpectrum.InsertStellarSpectrum(
dirs["janus"]+"data/spectral_files/Oak/Oak.sf",
dirs["janus"]+"data/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
os.environ.get('FWL_DATA')+"/spectral_files/Oak/318/Oak.sf",
os.environ.get('FWL_DATA')+"/spectral_files/stellar_spectra/Sun_t4_4Ga_claire_12.txt",
dirs["output"]
)
band_edges = ReadBandEdges(dirs["output"]+"star.sf")
Expand Down

0 comments on commit 05b0744

Please sign in to comment.