Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# pycache
# Subdirectories
__pycache__/

# vscode
sample_data/
.vscode/

# python bytecode
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.9.6
hooks:
- id: ruff # run the linter
args: [ --fix ]
- id: ruff-format # run the formatter
45 changes: 23 additions & 22 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@

import os
import sys
sys.path.insert(0, os.path.abspath('../pygem/'))

project = 'PyGEM'
copyright = '2023, David Rounce'
author = 'David Rounce'
release = '1.0.1'
sys.path.insert(0, os.path.abspath("../pygem/"))

project = "PyGEM"
copyright = "2023, David Rounce"
author = "David Rounce"
release = "1.0.1"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ['sphinx_book_theme',
'myst_parser',
'sphinx.ext.autodoc',
'sphinx.ext.autosummary',
'sphinx.ext.intersphinx',
'numpydoc',
'sphinx.ext.viewcode',
'sphinx_togglebutton',
]
extensions = [
"sphinx_book_theme",
"myst_parser",
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.intersphinx",
"numpydoc",
"sphinx.ext.viewcode",
"sphinx_togglebutton",
]

myst_enable_extensions = [
"amsmath",
Expand All @@ -39,21 +41,20 @@
"html_image",
]

#templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']

# templates_path = ['_templates']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output


html_theme = 'sphinx_book_theme'
html_static_path = ['_static']
html_theme = "sphinx_book_theme"
html_static_path = ["_static"]

html_theme_options = {
"repository_url": "https://github.com/PyGEM-Community/PyGEM",
"use_repository_button": True,
"show_nav_level":2,
"navigation_depth":3,
}
"show_nav_level": 2,
"navigation_depth": 3,
}
4 changes: 3 additions & 1 deletion pygem/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

Distrubted under the MIT lisence
"""

from importlib.metadata import version

try:
__version__ = version(__name__)
except:
__version__ = None
__version__ = None
51 changes: 36 additions & 15 deletions pygem/bin/op/duplicate_gdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,64 @@

duplicate OGGM glacier directories
"""

import argparse
import os
import shutil

# pygem imports
import pygem.setup.config as config

# check for config
config.ensure_config()
# read the config
pygem_prms = config.read_config()


def main():
parser = argparse.ArgumentParser(description="Script to make duplicate oggm glacier directories - primarily to avoid corruption if parellelizing runs on a single glacier")
parser = argparse.ArgumentParser(
description="Script to make duplicate oggm glacier directories - primarily to avoid corruption if parellelizing runs on a single glacier"
)
# add arguments
parser.add_argument('-rgi_glac_number', type=str, default=None,
help='Randoph Glacier Inventory region')
parser.add_argument('-num_copies', type=int, default=1,
help='Number of copies to create of the glacier directory data')
parser.add_argument(
"-rgi_glac_number",
type=str,
default=None,
help="Randoph Glacier Inventory region",
)
parser.add_argument(
"-num_copies",
type=int,
default=1,
help="Number of copies to create of the glacier directory data",
)
args = parser.parse_args()
num_copies = args.num_copies
glac_num = args.rgi_glac_number

if (glac_num is not None) and (num_copies)>1:
reg,id = glac_num.split('.')
if (glac_num is not None) and (num_copies) > 1:
reg, id = glac_num.split(".")
reg = reg.zfill(2)
thous = id[:2]

root = pygem_prms['root'] + '/' + pygem_prms['oggm']['oggm_gdir_relpath']
sfix = '/per_glacier/' + f'RGI60-{reg}/' + f'RGI60-{reg}.{thous}/'

root = (
pygem_prms["root"] + "/" + pygem_prms["oggm"]["oggm_gdir_relpath"]
)
sfix = "/per_glacier/" + f"RGI60-{reg}/" + f"RGI60-{reg}.{thous}/"

for n in range(num_copies):
nroot = os.path.abspath(root.replace('gdirs',f'gdirs_{n+1}'))
nroot = os.path.abspath(root.replace("gdirs", f"gdirs_{n + 1}"))
# duplicate structure
os.makedirs(nroot + sfix + f'RGI60-{reg}.{id}', exist_ok=True)
os.makedirs(nroot + sfix + f"RGI60-{reg}.{id}", exist_ok=True)
# copy directory data
shutil.copytree(root + sfix + f'RGI60-{reg}.{id}', nroot + sfix + f'RGI60-{reg}.{id}', dirs_exist_ok=True)
shutil.copytree(
root + sfix + f"RGI60-{reg}.{id}",
nroot + sfix + f"RGI60-{reg}.{id}",
dirs_exist_ok=True,
)

return

if __name__ == '__main__':
main()

if __name__ == "__main__":
main()
72 changes: 47 additions & 25 deletions pygem/bin/op/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,57 @@

initialization script (ensure config.yaml and get sample datasets)
"""
import requests
import zipfile
import os,sys

import os
import shutil
import zipfile

import requests
from ruamel.yaml import YAML

# set up config.yaml
import pygem.setup.config as config

config.ensure_config(overwrite=True)


def update_config_root(conf_path, datapath):
yaml = YAML()
yaml.preserve_quotes = True # Preserve quotes around string values

# Read the YAML file
with open(conf_path, 'r') as file:
with open(conf_path, "r") as file:
config = yaml.load(file)

# Update the key with the new value
config['root'] = datapath
config["root"] = datapath

# Save the updated configuration back to the file
with open(conf_path, 'w') as file:
with open(conf_path, "w") as file:
yaml.dump(config, file)


def print_file_tree(start_path, indent=""):
# Loop through all files and directories in the current directory
for item in os.listdir(start_path):
path = os.path.join(start_path, item)

# Print the current item with indentation
print(indent + "|-- " + item)

# Recursively call this function if the item is a directory
if os.path.isdir(path):
print_file_tree(path, indent + " ")



def get_confirm_token(response):
"""Extract confirmation token for Google Drive large file download."""
for key, value in response.cookies.items():
if key.startswith("download_warning"):
return value
return None


def save_response_content(response, destination):
"""Save the response content to a file."""
chunk_size = 32768
Expand All @@ -58,6 +66,7 @@ def save_response_content(response, destination):
if chunk: # Filter out keep-alive chunks
file.write(chunk)


def get_unique_folder_name(dir):
"""Generate a unique folder name by appending a suffix if the folder already exists."""
counter = 1
Expand All @@ -67,14 +76,15 @@ def get_unique_folder_name(dir):
counter += 1
return unique_dir


def download_and_unzip_from_google_drive(file_id, output_dir):
"""
Download a ZIP file from Google Drive and extract its contents.

Args:
file_id (str): The Google Drive file ID.
output_dir (str): The directory to save and extract the contents of the ZIP file.

Returns:
int: 1 if the ZIP file was successfully downloaded and extracted, 0 otherwise.
"""
Expand All @@ -90,19 +100,29 @@ def download_and_unzip_from_google_drive(file_id, output_dir):
try:
# Start the download process
with requests.Session() as session:
response = session.get(base_url, params={"id": file_id}, stream=True)
response = session.get(
base_url, params={"id": file_id}, stream=True
)
token = get_confirm_token(response)
if token:
response = session.get(base_url, params={"id": file_id, "confirm": token}, stream=True)
response = session.get(
base_url,
params={"id": file_id, "confirm": token},
stream=True,
)
save_response_content(response, zip_path)

# Unzip the file
tmppath = os.path.join(output_dir, 'tmp')
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
tmppath = os.path.join(output_dir, "tmp")
with zipfile.ZipFile(zip_path, "r") as zip_ref:
zip_ref.extractall(tmppath)

# get root dir name of zipped files
dir = [item for item in os.listdir(tmppath) if os.path.isdir(os.path.join(tmppath, item))][0]
dir = [
item
for item in os.listdir(tmppath)
if os.path.isdir(os.path.join(tmppath, item))
][0]
unzip_dir = os.path.join(tmppath, dir)
# get unique name if root dir name already exists in output_dir
output_dir = get_unique_folder_name(os.path.join(output_dir, dir))
Expand All @@ -112,33 +132,35 @@ def download_and_unzip_from_google_drive(file_id, output_dir):
os.remove(zip_path)
return output_dir # Success

except (requests.RequestException, zipfile.BadZipFile, Exception) as e:
except (requests.RequestException, zipfile.BadZipFile, Exception):
return None # Failure



def main():
# Define the base directory
basedir = os.path.join(os.path.expanduser('~'), 'PyGEM')
basedir = os.path.join(os.path.expanduser("~"), "PyGEM")
# Google Drive file id for sample dataset
file_id = "1Wu4ZqpOKxnc4EYhcRHQbwGq95FoOxMfZ"
# download and unzip
out = download_and_unzip_from_google_drive(file_id, basedir)

if out:
print(f"Downloaded PyGEM sample dataset:")
print("Downloaded PyGEM sample dataset:")
print(os.path.abspath(out))
try:
print_file_tree(out)
except:
pass

else:
print(f'Error downloading PyGEM sample dataset.')
print("Error downloading PyGEM sample dataset.")

# update root path in config.yaml
try:
update_config_root(config.config_file, out+'/sample_data/')
update_config_root(config.config_file, out + "/sample_data/")
except:
pass



if __name__ == "__main__":
main()
main()
Loading