tonik 0.0.11__tar.gz → 0.0.12__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: tonik
3
- Version: 0.0.11
3
+ Version: 0.0.12
4
4
  Summary: Store time series data as HDF5 files and access them through an API.
5
5
  Project-URL: Homepage, https://tsc-tools.github.io/tonik
6
6
  Project-URL: Issues, https://github.com/tsc-tools/tonik/issues
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "tonik"
7
- version = "0.0.11"
7
+ version = "0.0.12"
8
8
  authors = [
9
9
  { name="Yannik Behr", email="y.behr@gns.cri.nz" },
10
10
  { name="Christof Mueller", email="c.mueller@gns.cri.nz" }
@@ -6,7 +6,47 @@ import xarray as xr
6
6
  logger = logging.getLogger(__name__)
7
7
 
8
8
 
9
- def xarray2zarr(xds, path, mode='a'):
9
+ def merge_arrays(xds_old: xr.DataArray, xds_new: xr.DataArray) -> xr.DataArray:
10
+ """
11
+ Merge two xarray datasets with the same datetime index.
12
+
13
+ Parameters
14
+ ----------
15
+ xds_old : xr.DataArray
16
+ Old array.
17
+ xds_new : xr.DataArray
18
+ New array.
19
+
20
+ Returns
21
+ -------
22
+ xr.DataArray
23
+ Merged array.
24
+ """
25
+ xda_old = xds_old.drop_duplicates(
26
+ 'datetime', keep='last')
27
+ xda_new = xds_new.drop_duplicates(
28
+ 'datetime', keep='last')
29
+ xda_new = xda_new.combine_first(xda_old)
30
+ return xda_new
31
+
32
+
33
+ def xarray2zarr(xds: xr.Dataset, path: str, mode: str = 'a'):
34
+ """
35
+ Write xarray dataset to zarr files.
36
+
37
+ Parameters
38
+ ----------
39
+ xds : xr.Dataset
40
+ Dataset to write.
41
+ path : str
42
+ Path to write the dataset.
43
+ mode : str, optional
44
+ Write mode, by default 'a'.
45
+
46
+ Returns
47
+ -------
48
+ None
49
+ """
10
50
  for feature in xds.data_vars.keys():
11
51
  fout = os.path.join(path, feature + '.zarr')
12
52
  if not os.path.exists(fout) or mode == 'w':
@@ -15,8 +55,8 @@ def xarray2zarr(xds, path, mode='a'):
15
55
  else:
16
56
  xds_existing = xr.open_zarr(fout, group='original')
17
57
  if xds_existing.datetime[0] > xds.datetime[0] or xds_existing.datetime[-1] > xds.datetime[-1]:
18
- xds_new = xr.merge([xds_existing[feature], xds[feature]])
19
- xds_new.to_zarr(fout, group='original', mode='w')
58
+ xda_new = merge_arrays(xds_existing[feature], xds[feature])
59
+ xda_new.to_zarr(fout, group='original', mode='w')
20
60
  else:
21
61
  try:
22
62
  overlap = xds_existing.datetime.where(
@@ -34,9 +74,5 @@ def xarray2zarr(xds, path, mode='a'):
34
74
  msg += "Attempting to merge the two datasets."
35
75
  logger.error(msg)
36
76
  # remove duplicate datetime entries
37
- xda_existing = xds_existing[feature].drop_duplicates(
38
- 'datetime', keep='last')
39
- xda_new = xds[feature].drop_duplicates(
40
- 'datetime', keep='last')
41
- xda_new = xda_new.combine_first(xda_existing)
77
+ xda_new = merge_arrays(xds_existing[feature], xds[feature])
42
78
  xda_new.to_zarr(fout, group='original', mode='w')
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes