Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding default option of downloading files from given URL, instead of fixed github one #247

Merged
merged 7 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions python/snewpy/_model_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def load(self) -> Path:
return self.path


def from_zenodo(zenodo_id:str, model:str, filename:str, path:str=model_path):
def from_zenodo(zenodo_id:str, model:str, filename:str):
"""Access files on Zenodo.

Parameters
Expand All @@ -113,7 +113,7 @@ def from_zenodo(zenodo_id:str, model:str, filename:str, path:str=model_path):

Returns
-------
file : FileHandle object.
file_url, md5sum
"""
zenodo_url = f'https://zenodo.org/api/records/{zenodo_id}'
path = Path(path)/str(model)
Sheshuk marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -124,17 +124,13 @@ def from_zenodo(zenodo_id:str, model:str, filename:str, path:str=model_path):
# Search for model file string in Zenodo request for this record.
file = next((_file for _file in record['files'] if _file['key'] == filename), None)

# If matched, return a FileHandle which takes care of downloading and
# checksum (or loads a local data file). Otherwise raise an exception.
# If matched, return a tuple of URL and checksum.Otherwise raise an exception.
if file is not None:
return FileHandle(path = path/file['key'],
remote= file['links']['self'],
md5 = file['checksum'].split(':')[1])
return file['links']['self'], file['checksum'].split(':')[1]
else:
raise MissingFileError(filename)


def from_github(release_version:str, model:str, filename:str, path:str=model_path):
def from_github(release_version:str, model:str, filename:str):
"""Access files on GitHub.

Parameters
Expand All @@ -146,15 +142,10 @@ def from_github(release_version:str, model:str, filename:str, path:str=model_pat

Returns
-------
file : FileHandle object.
file_url, md5sum
"""
github_url = f'https://github.com/SNEWS2/snewpy/raw/v{release_version}/models/{model}/{filename}'
localpath = Path(path)/str(model)
localpath.mkdir(exist_ok=True, parents=True)

return FileHandle(path = localpath/filename,
remote = github_url)

return github_url, None

def get_model_data(model: str, filename: str, path: str = model_path) -> Path:
"""Access model data. Configuration for each model is in a YAML file
Expand All @@ -173,7 +164,7 @@ def get_model_data(model: str, filename: str, path: str = model_path) -> Path:
if os.path.isabs(filename):
return Path(filename)

params = { 'model':model, 'filename':filename, 'path':path }
params = { 'model':model, 'filename':filename }

# Parse YAML file with model repository configurations.
with open_text('snewpy.models', 'model_files.yml') as f:
Expand All @@ -183,16 +174,20 @@ def get_model_data(model: str, filename: str, path: str = model_path) -> Path:
if model in models.keys():
# Get data from GitHub or Zenodo.
modconf = models[model]
repo = modconf['repository']
repo = modconf.pop('repository')

if repo == 'github':
params['release_version'] = modconf['release_version']
fh = from_github(**params)
url, md5 = from_github(**params)
elif repo == 'zenodo':
params['zenodo_id'] = modconf['zenodo_id']
fh = from_zenodo(**params)
url, md5 = from_zenodo(**params)
else:
raise ValueError(f'Repository {repo} not recognized')
#format the url directly
url, md5 = repo.format(**params, **modconf), None
localpath = Path(path)/str(model)
localpath.mkdir(exist_ok=True, parents=True)
fh = FileHandle(path = localpath/filename,remote = url, md5=md5)
return fh.load()
else:
raise KeyError(f'No configuration for {model}')
Expand Down
6 changes: 5 additions & 1 deletion python/snewpy/models/model_files.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Define remote locations of simulation files for each model.
# 1. Model data on GitHub must provide the SNEWPY release version.
# 2. Model data on Zenodo must provide the Zenodo record ID.

config:
- &github "https://github.com/SNEWS2/snewpy/raw/v{release_version}/models/{model}/{filename}"

models:

Bollig_2016:
repository: github
repository: *github
release_version: 1.2

Fornax_2019:
Expand Down