Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

deltamath.py 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  1. """
  2. DeepIceDrain functions for calculating delta changes, such as for ice elevation
  3. differencing (dh), measuring lengths of time (dt), and related measures.
  4. """
  5. import numpy as np
  6. import scipy.stats
  7. import xarray as xr
  8. def calculate_delta(
  9. dataset: xr.Dataset,
  10. oldcyclenum: int = 5,
  11. newcyclenum: int = 6,
  12. variable: str = "h_corr",
  13. ) -> xr.DataArray:
  14. """
  15. Calculates the change in some quantity variable between two ICESat-2 cycles
  16. i.e. new minus old.
  17. Example ATL11 variables to use:
  18. h_corr - corrected height
  19. delta_time - GPS time for the segments for each pass
  20. """
  21. oldcycle: xr.Dataset = dataset.sel(cycle_number=oldcyclenum)
  22. newcycle: xr.Dataset = dataset.sel(cycle_number=newcyclenum)
  23. delta_quantity: xr.DataArray = newcycle[variable] - oldcycle[variable]
  24. return delta_quantity
  25. def nanptp(a, axis=None) -> np.ndarray:
  26. """
  27. Range of values (maximum - minimum) along an axis, ignoring any NaNs.
  28. When slices with less than two non-NaN values are encountered,
  29. a ``RuntimeWarning`` is raised and Nan is returned for that slice.
  30. Adapted from https://github.com/numpy/numpy/pull/13220
  31. """
  32. return np.nanmax(a=a, axis=axis) - np.nanmin(a=a, axis=axis)
  33. def nan_linregress(x, y) -> np.ndarray:
  34. """
  35. Linear Regression function that handles NaN and NaT values.
  36. Hardcoded so that x is expected to be the time array.
  37. Stacking the outputs (slope, intercept, rvalue, pvalue, stderr)
  38. into one numpy.ndarray to keep xarray.apply_ufuncs happy.
  39. Kudos to https://stackoverflow.com/a/60524715/6611055
  40. """
  41. x = np.atleast_2d(x) # shape of at least (1, 6) instead of (6,)
  42. y = np.atleast_2d(y) # shape of at least (1, 6) instead of (6,)
  43. mask = ~np.isnan(y)
  44. x = x[mask] # .where(cond=mask, drop=True)
  45. y = y[mask] # .where(cond=mask, drop=True)
  46. try:
  47. linregress_result = np.array(scipy.stats.linregress(x=x, y=y))
  48. except ValueError:
  49. # if x.size == 0 or y.size == 0:
  50. linregress_result = np.full(shape=(5,), fill_value=np.NaN)
  51. return linregress_result
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...