ticoi 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ticoi might be problematic. Click here for more details.

ticoi/example.py ADDED
@@ -0,0 +1,81 @@
1
+ """Utility functions to download and find example data."""
2
+
3
+ import os
4
+ import shutil
5
+ import tarfile
6
+ import tempfile
7
+ import urllib
8
+
9
+ # Define the location of the data in the example directory
10
+ _EXAMPLES_DIRECTORY = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "test_data"))
11
+
12
+ # Absolute filepaths to the example files.
13
+ _FILEPATHS_DATA = {
14
+ "ITS_LIVE_Lowell_Lower": os.path.join(_EXAMPLES_DIRECTORY, "Lowell", "ITS_LIVE_Lowell_Lower_test.nc"),
15
+ "Argentiere_example_interp": os.path.join(_EXAMPLES_DIRECTORY, "Argentiere", "Argentiere_example_interp.nc"),
16
+ "Argentiere_static": os.path.join(_EXAMPLES_DIRECTORY, "Argentiere", "Argentiere_static.gpkg"),
17
+ "Argentiere_iceflow": os.path.join(_EXAMPLES_DIRECTORY, "Argentiere", "Argentiere_iceflow.gpkg"),
18
+ }
19
+
20
+ available = list(_FILEPATHS_DATA.keys())
21
+
22
+
23
+ def download_examples(overwrite: bool = False) -> None:
24
+ """
25
+ Fetch the example files.
26
+
27
+ :param overwrite: Do not download the files again if they already exist.
28
+ """
29
+ if not overwrite and all(map(os.path.isfile, list(_FILEPATHS_DATA.values()))):
30
+ print("Datasets exist")
31
+ return
32
+
33
+ # Static commit hash to be bumped every time it needs to be.
34
+ commit = "3121f37e8de767cb7ea21cbd93b4dd59a81b1ced"
35
+ # The URL from which to download the repository
36
+ url = f"https://github.com/ticoi/ticoi_data/tarball/main#commit={commit}"
37
+
38
+ # Create a temporary directory to extract the tarball in.
39
+ with tempfile.TemporaryDirectory() as tmp_dir:
40
+ tar_path = os.path.join(tmp_dir, "data.tar.gz")
41
+
42
+ response = urllib.request.urlopen(url)
43
+ # If the response was right, download the tarball to the temporary directory
44
+ if response.getcode() == 200:
45
+ with open(tar_path, "wb") as outfile:
46
+ outfile.write(response.read())
47
+ else:
48
+ raise ValueError(f"Example data fetch gave non-200 response: {response.status_code}")
49
+
50
+ # Extract the tarball
51
+ with tarfile.open(tar_path) as tar:
52
+ try:
53
+ tar.extractall(tmp_dir, filter="tar")
54
+ except TypeError: # For compatibility with different versions of python: The filter argument, which was added in Python 3.10.12, specifies how members are modified or rejected before extraction.
55
+ tar.extractall(tmp_dir)
56
+
57
+ # Find the first directory in the temp_dir (should only be one) and construct the example data dir paths.
58
+ for dir_name in ["Argentiere", "Lowell"]:
59
+ tmp_dir_name = os.path.join(
60
+ tmp_dir,
61
+ [dirname for dirname in os.listdir(tmp_dir) if os.path.isdir(os.path.join(tmp_dir, dirname))][0],
62
+ dir_name,
63
+ )
64
+
65
+ # Copy the temporary extracted data to the example directory.
66
+ shutil.copytree(tmp_dir_name, os.path.join(_EXAMPLES_DIRECTORY, dir_name))
67
+
68
+
69
+ def get_path(name: str) -> str:
70
+ """
71
+ Get path of example data. List of available files can be found in "examples.available".
72
+
73
+ :param name: Name of test data.
74
+ :return:
75
+ """
76
+
77
+ if name in list(_FILEPATHS_DATA.keys()):
78
+ download_examples()
79
+ return _FILEPATHS_DATA[name]
80
+ else:
81
+ raise ValueError('Data name should be one of "' + '" , "'.join(list(_FILEPATHS_DATA.keys())) + '".')