diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 166bf56..b503a72 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -48,5 +48,6 @@ jobs: - name: Test with pytest run: | pip install pytest + export FWL_DATA="./fwl_data" source SOCRATES/set_rad_env pytest diff --git a/examples/SocRadConv.py b/examples/SocRadConv.py index 42cb000..3c00373 100755 --- a/examples/SocRadConv.py +++ b/examples/SocRadConv.py @@ -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 @@ -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/" @@ -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"] ) diff --git a/examples/demo_instellation.py b/examples/demo_instellation.py index 95d1797..9d35d7f 100755 --- a/examples/demo_instellation.py +++ b/examples/demo_instellation.py @@ -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__': @@ -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/" @@ -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") diff --git a/examples/demo_runaway_greenhouse.py b/examples/demo_runaway_greenhouse.py index 2bc0d64..348b6de 100755 --- a/examples/demo_runaway_greenhouse.py +++ b/examples/demo_runaway_greenhouse.py @@ -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__': @@ -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/" @@ -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(" ") diff --git a/src/janus/utils/data.py b/src/janus/utils/data.py new file mode 100644 index 0000000..af018ac --- /dev/null +++ b/src/janus/utils/data.py @@ -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 diff --git a/tests/test_instellation.py b/tests/test_instellation.py index 94d233d..7b8de11 100755 --- a/tests/test_instellation.py +++ b/tests/test_instellation.py @@ -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/" @@ -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") diff --git a/tests/test_runaway_greenhouse.py b/tests/test_runaway_greenhouse.py index 46fbb4d..23e8aba 100755 --- a/tests/test_runaway_greenhouse.py +++ b/tests/test_runaway_greenhouse.py @@ -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/" @@ -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")