pyissm 0.0.1.dev1__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.
Files changed (87) hide show
  1. pyissm/__init__.py +1 -0
  2. pyissm/analysis/__init__.py +1 -0
  3. pyissm/analysis/ismip.py +103 -0
  4. pyissm/data/__init__.py +1 -0
  5. pyissm/data/interp.py +256 -0
  6. pyissm/learn/__init__.py +1 -0
  7. pyissm/learn/tutorials.py +3 -0
  8. pyissm/model/Model.py +586 -0
  9. pyissm/model/__init__.py +2 -0
  10. pyissm/model/bc.py +167 -0
  11. pyissm/model/classes/__init__.py +52 -0
  12. pyissm/model/classes/amr.py +189 -0
  13. pyissm/model/classes/autodiff.py +303 -0
  14. pyissm/model/classes/balancethickness.py +120 -0
  15. pyissm/model/classes/basalforcings.py +1246 -0
  16. pyissm/model/classes/calving.py +752 -0
  17. pyissm/model/classes/class_registry.py +388 -0
  18. pyissm/model/classes/class_utils.py +824 -0
  19. pyissm/model/classes/cluster.py +904 -0
  20. pyissm/model/classes/constants.py +109 -0
  21. pyissm/model/classes/damage.py +247 -0
  22. pyissm/model/classes/debris.py +205 -0
  23. pyissm/model/classes/debug.py +94 -0
  24. pyissm/model/classes/dependent.py +121 -0
  25. pyissm/model/classes/dsl.py +228 -0
  26. pyissm/model/classes/esa.py +175 -0
  27. pyissm/model/classes/flowequation.py +232 -0
  28. pyissm/model/classes/friction.py +1484 -0
  29. pyissm/model/classes/frontalforcings.py +633 -0
  30. pyissm/model/classes/geometry.py +136 -0
  31. pyissm/model/classes/groundingline.py +168 -0
  32. pyissm/model/classes/hydrology.py +1573 -0
  33. pyissm/model/classes/independent.py +105 -0
  34. pyissm/model/classes/initialization.py +286 -0
  35. pyissm/model/classes/inversion.py +742 -0
  36. pyissm/model/classes/issmsettings.py +135 -0
  37. pyissm/model/classes/levelset.py +126 -0
  38. pyissm/model/classes/love.py +518 -0
  39. pyissm/model/classes/lovenumbers.py +179 -0
  40. pyissm/model/classes/mask.py +100 -0
  41. pyissm/model/classes/massfluxatgate.py +130 -0
  42. pyissm/model/classes/masstransport.py +179 -0
  43. pyissm/model/classes/materials.py +996 -0
  44. pyissm/model/classes/mesh.py +909 -0
  45. pyissm/model/classes/miscellaneous.py +94 -0
  46. pyissm/model/classes/misfit.py +148 -0
  47. pyissm/model/classes/nodalvalue.py +110 -0
  48. pyissm/model/classes/offlinesolidearthsolution.py +129 -0
  49. pyissm/model/classes/organizer.py +0 -0
  50. pyissm/model/classes/outputdefinition.py +106 -0
  51. pyissm/model/classes/private.py +74 -0
  52. pyissm/model/classes/qmu.py +124 -0
  53. pyissm/model/classes/radaroverlay.py +72 -0
  54. pyissm/model/classes/regionaloutput.py +140 -0
  55. pyissm/model/classes/results.py +331 -0
  56. pyissm/model/classes/rifts.py +153 -0
  57. pyissm/model/classes/rotational.py +100 -0
  58. pyissm/model/classes/sampling.py +190 -0
  59. pyissm/model/classes/smb.py +3266 -0
  60. pyissm/model/classes/solidearth.py +828 -0
  61. pyissm/model/classes/steadystate.py +157 -0
  62. pyissm/model/classes/stochasticforcing.py +364 -0
  63. pyissm/model/classes/stressbalance.py +311 -0
  64. pyissm/model/classes/surfaceload.py +118 -0
  65. pyissm/model/classes/thermal.py +228 -0
  66. pyissm/model/classes/timestepping.py +309 -0
  67. pyissm/model/classes/toolkits.py +168 -0
  68. pyissm/model/classes/transient.py +262 -0
  69. pyissm/model/classes/verbose.py +172 -0
  70. pyissm/model/execute.py +1920 -0
  71. pyissm/model/io.py +1241 -0
  72. pyissm/model/mesh.py +2503 -0
  73. pyissm/model/param.py +669 -0
  74. pyissm/plot.py +883 -0
  75. pyissm/tools/__init__.py +1 -0
  76. pyissm/tools/config.py +312 -0
  77. pyissm/tools/exp.py +271 -0
  78. pyissm/tools/general.py +541 -0
  79. pyissm/tools/geometry.py +76 -0
  80. pyissm/tools/interp.py +150 -0
  81. pyissm/tools/materials.py +173 -0
  82. pyissm/tools/wrappers.py +965 -0
  83. pyissm-0.0.1.dev1.dist-info/METADATA +34 -0
  84. pyissm-0.0.1.dev1.dist-info/RECORD +87 -0
  85. pyissm-0.0.1.dev1.dist-info/WHEEL +5 -0
  86. pyissm-0.0.1.dev1.dist-info/licenses/LICENSE +201 -0
  87. pyissm-0.0.1.dev1.dist-info/top_level.txt +1 -0
pyissm/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from . import analysis, data, learn, plot, model, tools
@@ -0,0 +1 @@
1
+ from . import ismip
@@ -0,0 +1,103 @@
1
+ """
2
+ ISMIP-related functions for ISSM
3
+
4
+ This module contains functions for handling ISMIP-related data and operations.
5
+ """
6
+
7
+ from pyissm import tools
8
+
9
+ def calc_perc_ice_cover(total_ice_area, ice_area):
10
+ """
11
+ Calculate the percentage ice cover, relative to the of total ice area.
12
+
13
+ This function computes the fraction of the ice area that is grounded (i.e.,
14
+ not floating). The result is returned as a percentage.
15
+
16
+ Parameters
17
+ ----------
18
+ total_ice_area : ndarray
19
+ Array representing the total ice area.
20
+
21
+ ice_area : ndarray
22
+ Array representing the ice area for percentage coverage to be computed.
23
+
24
+ Returns
25
+ -------
26
+ ice_area_perc : ndarray
27
+ Array with the same shape as `ice_area`, where each
28
+ element represents the percentage of the total_ice_area that is covered by the ice_area.
29
+
30
+ """
31
+
32
+ ## Calculate ice area fraction
33
+ ice_area_perc = (ice_area / total_ice_area) * 100
34
+
35
+ return ice_area_perc
36
+
37
+
38
+ def get_ismip_variable(md, ismip_variable_name):
39
+ """
40
+ Retrieve a specific ISMIP variable from a model object.
41
+
42
+ This function extracts and calculates a requested ISMIP (Ice Sheet Model Intercomparison Project)
43
+ variable from a given model object, if the necessary data is available. It supports area-based
44
+ variables calculated from grounded and floating ice components stored in the model's
45
+ transient solution.
46
+
47
+ Parameters
48
+ ----------
49
+ md : object
50
+ The model object containing transient solution results, typically with
51
+ attributes like `md.results.TransientSolution.GroundedArea` and `FloatingArea`.
52
+
53
+ ismip_variable_name : str
54
+ Name of the ISMIP variable to compute. Must be one of:
55
+ - 'land_ice_area_fraction' (assumes all ice in ISSM is land ice and returns 100 %)
56
+ - 'floating_ice_shelf_area_fraction'
57
+ - 'grounded_ice_sheet_area_fraction'
58
+
59
+ Returns
60
+ -------
61
+ variable : array-like or None
62
+ The computed variable as a percentage, or `None` if the required data is
63
+ not available in the model object.
64
+
65
+ Raises
66
+ ------
67
+ None explicitly, but prints a warning if the requested variable cannot be computed.
68
+
69
+ Notes
70
+ -----
71
+ - 'land_ice_area_fraction' assumes all ice is land ice (returns 100% where data is available).
72
+ - 'floating_ice_shelf_area_fraction' computes the fraction of total ice that is floating.
73
+ - 'grounded_ice_sheet_area_fraction' computes the fraction of total ice that is grounded.
74
+ - Missing required attributes in `md` will result in the variable being skipped.
75
+ """
76
+
77
+ variable = None
78
+
79
+ if ismip_variable_name == 'land_ice_area_fraction':
80
+ if tools.general.has_nested_attr(md, 'results', 'TransientSolution', 'GroundedArea') and tools.general.has_nested_attr(md, 'results', 'TransientSolution', 'FloatingArea'):
81
+ total_area = md.results.TransientSolution.GroundedArea + md.results.TransientSolution.FloatingArea
82
+ variable = calc_perc_ice_cover(total_area, total_area)
83
+ else:
84
+ print("\033[1mland_ice_area_fraction\033[0m cannot be computed and will be skipped.")
85
+
86
+ elif ismip_variable_name == 'floating_ice_shelf_area_fraction':
87
+ if tools.general.has_nested_attr(md, 'results', 'TransientSolution', 'FloatingArea'):
88
+ total_area = md.results.TransientSolution.GroundedArea + md.results.TransientSolution.FloatingArea
89
+ variable = calc_perc_ice_cover(total_area, md.results.TransientSolution.FloatingArea)
90
+ else:
91
+ print("\033[1mfloating_ice_shelf_area_fraction\033[0m cannot be computed and will be skipped.")
92
+
93
+ elif ismip_variable_name == 'grounded_ice_sheet_area_fraction':
94
+ if tools.general.has_nested_attr(md, 'results', 'TransientSolution', 'GroundedArea'):
95
+ total_area = md.results.TransientSolution.GroundedArea + md.results.TransientSolution.FloatingArea
96
+ variable = calc_perc_ice_cover(total_area, md.results.TransientSolution.GroundedArea)
97
+ else:
98
+ print("\033[1mgrounded_ice_sheet_area_fraction\033[0m cannot be computed and will be skipped.")
99
+
100
+ else:
101
+ print(f"\033[1m{ismip_variable_name}\033[0m is not recognized and will be skipped.")
102
+
103
+ return variable
@@ -0,0 +1 @@
1
+ from . import interp
pyissm/data/interp.py ADDED
@@ -0,0 +1,256 @@
1
+ """
2
+ Tools to interpolate data to/from ISSM model mesh.
3
+
4
+ This module contains various interpolation functions that can be used in conjunction with ISSM models.
5
+ """
6
+
7
+ import xarray as xr
8
+ import numpy as np
9
+ from scipy.interpolate import RegularGridInterpolator, griddata
10
+ from pyissm import tools
11
+
12
+ def xr_to_mesh(data,
13
+ var_name,
14
+ mesh_x,
15
+ mesh_y,
16
+ x_var = 'x',
17
+ y_var = 'y',
18
+ default_value = np.nan,
19
+ interpolation_type = 'bilinear',
20
+ issm_wrapper = True):
21
+
22
+ """
23
+ Interpolate a variable from an xarray dataset onto mesh nodes.
24
+
25
+ Assumes rectilinear (structured) grid in the xarray dataset.
26
+
27
+ Parameters
28
+ ----------
29
+ data : str or xr.Dataset
30
+ Path to a netCDF file or an xarray Dataset containing the gridded data.
31
+ var_name : str
32
+ Name of the variable to interpolate.
33
+ mesh_x : ndarray
34
+ X-coordinates of mesh nodes.
35
+ mesh_y : ndarray
36
+ Y-coordinates of mesh nodes.
37
+ x_var : str, optional
38
+ Name of the x-coordinate variable in the dataset. Default is 'x'.
39
+ y_var : str, optional
40
+ Name of the y-coordinate variable in the dataset. Default is 'y'.
41
+ default_value : float, optional
42
+ Value to assign to points outside the grid domain. Default is np.nan.
43
+ interpolation_type : str, optional
44
+ Type of interpolation method. For ISSM wrapper: 'bilinear', 'nearest', etc.
45
+ For scipy: 'linear', 'nearest', 'slinear', 'cubic', 'quintic', 'pchip'.
46
+ Default is 'bilinear'.
47
+ issm_wrapper : bool, optional
48
+ If True, use ISSM wrapper functions for interpolation. If False, use scipy.
49
+ Default is True.
50
+
51
+ Returns
52
+ -------
53
+ ndarray
54
+ Interpolated variable values at mesh nodes.
55
+
56
+ Raises
57
+ ------
58
+ TypeError
59
+ If data is neither a file path nor an xarray Dataset.
60
+ ValueError
61
+ If variable is not 2D, coordinates are inconsistent, or grid is not rectilinear.
62
+ ImportError
63
+ If issm_wrapper is True but ISSM wrappers are not installed.
64
+ """
65
+
66
+ # Load xarray dataset if a filepath was given
67
+ if isinstance(data, str):
68
+ data = xr.open_dataset(data)
69
+ close = True
70
+ elif isinstance(data, xr.Dataset):
71
+ close = False
72
+ else:
73
+ raise TypeError("pyissm.data.interp.xr_to_mesh: data must be a file path or an xarray Dataset")
74
+
75
+ # Extract and squeeze arrays
76
+ x = np.asarray(data[x_var].values).squeeze()
77
+ y = np.asarray(data[y_var].values).squeeze()
78
+ var_data = np.asarray(data[var_name].values).squeeze()
79
+
80
+ if close:
81
+ data.close()
82
+
83
+ # Convert everything to float64 (but keep shapes)
84
+ x = x.astype(np.float64, copy = False)
85
+ y = y.astype(np.float64, copy = False)
86
+ var_data = var_data.astype(np.float64, copy = False)
87
+ mesh_x = mesh_x.astype(np.float64, copy = False)
88
+ mesh_y = mesh_y.astype(np.float64, copy = False)
89
+
90
+ # Check for rectilinear grid
91
+ if var_data.ndim != 2:
92
+ raise ValueError(f"pyissm.data.interp.xr_to_mesh: variable '{var_name}' must be 2D on a rectilinear grid")
93
+
94
+ # If coordinates are 1D arrays, check shapes
95
+ if x.ndim == 1 and y.ndim == 1:
96
+ if var_data.shape != (y.size, x.size):
97
+ raise ValueError(f"pyissm.data.interp.xr_to_mesh: variable '{var_name}' has shape {var_data.shape}, "
98
+ f"expected ({y.size}, {x.size})")
99
+
100
+ # If coordinates are 2D arrays, check shapes and rectilinearity
101
+ # If 2D, they should be repeated 1D arrays
102
+ elif x.ndim == 2 and y.ndim == 2:
103
+ if x.shape != var_data.shape or y.shape != var_data.shape:
104
+ raise ValueError("pyissm.data.interp.xr_to_mesh: x, y, and variable must have identical shapes "
105
+ "for 2D coordinate grids")
106
+
107
+ # Check rectilinearity
108
+ if not np.allclose(x, x[0, :][None, :]):
109
+ raise ValueError("pyissm.data.interp.xr_to_mesh: 2D x-coordinate is not rectilinear")
110
+
111
+ if not np.allclose(y, y[:, 0][:, None]):
112
+ raise ValueError("pyissm.data.interp.xr_to_mesh: 2D y-coordinate is not rectilinear")
113
+
114
+ # Reduce to 1D
115
+ x = x[0, :]
116
+ y = y[:, 0]
117
+
118
+ else:
119
+ raise ValueError(
120
+ "pyissm.data.interp.xr_to_mesh: x and y must both be 1D or both be 2D"
121
+ )
122
+
123
+
124
+ # Ensure monotonic increasing coordinates
125
+ if np.any(np.diff(x) < 0):
126
+ x = x[::-1]
127
+ var_data = var_data[:, ::-1]
128
+
129
+ if np.any(np.diff(y) < 0):
130
+ y = y[::-1]
131
+ var_data = var_data[::-1, :]
132
+
133
+ # If use_wrapper is True, use ISSM wrappers for interpolation
134
+ if issm_wrapper:
135
+
136
+ # Check that wrappers are installed
137
+ if not tools.wrappers.check_wrappers_installed():
138
+ raise ImportError("pyissm.data.interp.xr_to_mesh: ISSM wrappers are not installed. Please install them or set issm_wrapper = False to use scipy interpolation.")
139
+
140
+ # Interpolate using ISSM wrapper
141
+ var_on_mesh = tools.wrappers.InterpFromGridToMesh(
142
+ x,
143
+ y,
144
+ var_data,
145
+ mesh_x,
146
+ mesh_y,
147
+ default_value,
148
+ interpolation_type
149
+ )
150
+
151
+ # Otherwise, use scipy for interpolation
152
+ else:
153
+
154
+ scipy_method_list = ['linear', 'nearest', 'slinear', 'cubic', 'quintic', 'pchip']
155
+ if interpolation_type not in scipy_method_list:
156
+ raise ValueError(f"pyissm.data.interp.xr_to_mesh: interpolation_type '{interpolation_type}' is not supported by scipy. Choose from {scipy_method_list}.")
157
+
158
+ interp = RegularGridInterpolator(
159
+ (y, x),
160
+ var_data,
161
+ method = interpolation_type,
162
+ bounds_error = False,
163
+ fill_value = default_value,
164
+ )
165
+
166
+ # scipy expects points as (y, x)
167
+ mesh_points = np.column_stack((mesh_y, mesh_x))
168
+
169
+ # Extract variable on mesh
170
+ var_on_mesh = interp(mesh_points)
171
+
172
+ return var_on_mesh
173
+
174
+ def points_to_mesh(data_x,
175
+ data_y,
176
+ data_values,
177
+ mesh_x,
178
+ mesh_y,
179
+ default_value = np.nan,
180
+ interpolation_type = 'linear'):
181
+ """
182
+ Interpolate scattered points onto mesh node coordinates using scipy.
183
+
184
+ Parameters
185
+ ----------
186
+ data_x : array_like
187
+ X-coordinates of the scattered data points. Can be 1D or 2D; if 2D it will be flattened.
188
+ data_y : array_like
189
+ Y-coordinates of the scattered data points. Must have the same shape as `data_x`.
190
+ data_values : array_like
191
+ Values at the scattered data points. Must have the same shape as `data_x`.
192
+ mesh_x : array_like
193
+ X-coordinates of mesh nodes. 1D array.
194
+ mesh_y : array_like
195
+ Y-coordinates of mesh nodes. 1D array.
196
+ default_value : float, optional
197
+ Value used to fill points outside the convex hull of the input data. Default is `np.nan`.
198
+ interpolation_type : str, optional
199
+ Interpolation method passed to `scipy.interpolate.griddata`. Supported options: 'linear', 'nearest', 'cubic'.
200
+ Default is 'linear'.
201
+
202
+ Returns
203
+ -------
204
+ ndarray
205
+ 1D array of interpolated values at the mesh nodes (shape equals `mesh_x`/`mesh_y`).
206
+ Points outside the convex hull of the input data are assigned `default_value`.
207
+
208
+ Raises
209
+ ------
210
+ ValueError
211
+ If `interpolation_type` is not supported by scipy, if input shapes are inconsistent, or if no valid
212
+ data points remain after removing NaNs/Infs.
213
+ """
214
+
215
+ # Check interpolation type
216
+ scipy_method_list = ['linear', 'nearest', 'cubic']
217
+ if interpolation_type not in scipy_method_list:
218
+ raise ValueError(f"pyissm.data.interp.points_to_mesh: interpolation_type '{interpolation_type}' is not supported by scipy. Choose from {scipy_method_list}.")
219
+
220
+ # Validate input shapes
221
+ if data_x.shape != data_y.shape or data_x.shape != data_values.shape:
222
+ raise ValueError("pyissm.data.interp.points_to_mesh: data_x, data_y, and data_values must have the same shape.")
223
+
224
+ # Flatten if needed:
225
+ if data_values.ndim == 2:
226
+ data_x = data_x.flatten()
227
+ data_y = data_y.flatten()
228
+ data_values = data_values.flatten()
229
+ elif data_values.ndim == 1:
230
+ pass
231
+ else:
232
+ raise ValueError("pyissm.data.interp.points_to_mesh: data_values must be 1D or 2D array.")
233
+
234
+ # Remove NaNs/Inf from input data
235
+ mask = np.isfinite(data_x) & np.isfinite(data_y) & np.isfinite(data_values)
236
+
237
+ if not np.any(mask):
238
+ raise ValueError("pyissm.data.interp.points_to_mesh: no valid data points available for interpolation")
239
+
240
+ data_x = data_x[mask]
241
+ data_y = data_y[mask]
242
+ data_values = data_values[mask]
243
+
244
+ # Prepare points for interpolation
245
+ data_points = np.column_stack((data_y, data_x))
246
+ interp_points = np.column_stack((mesh_y, mesh_x))
247
+
248
+ # Perform interpolation
249
+ data_on_mesh = griddata(data_points,
250
+ data_values,
251
+ interp_points,
252
+ method = interpolation_type,
253
+ fill_value = default_value)
254
+
255
+ return data_on_mesh
256
+
@@ -0,0 +1 @@
1
+ from . import tutorials
@@ -0,0 +1,3 @@
1
+ """
2
+ ISSM tutorials and help functions
3
+ """