From 009edab448cef796df8c74106fafc2421b62b1f1 Mon Sep 17 00:00:00 2001 From: Alister Trabattoni Date: Wed, 4 Sep 2024 14:40:07 +0200 Subject: [PATCH 1/4] Add Terra15 support. --- docs/user-guide/data-formats.md | 3 ++- xdas/io/__init__.py | 2 +- xdas/io/terra15.py | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 xdas/io/terra15.py diff --git a/docs/user-guide/data-formats.md b/docs/user-guide/data-formats.md index 7573cd2..be5b6e1 100644 --- a/docs/user-guide/data-formats.md +++ b/docs/user-guide/data-formats.md @@ -20,7 +20,7 @@ os.chdir("../_data") ## Implemented file formats -The formats that are currently implemented are: ASN, FEBUS, OPTASENSE and SINTELA. To read them you have to specifiy which one you want in the `engine` argument in {py:func}`xdas.open_dataarray` for a single file or {py:func}`xdas.open_mfdataarray` for multiple files: +The formats that are currently implemented are: ASN, FEBUS, OPTASENSE, SINTELA and TERRA15. To read them you have to specifiy which one you want in the `engine` argument in {py:func}`xdas.open_dataarray` for a single file or {py:func}`xdas.open_mfdataarray` for multiple files: | DAS constructor | `engine` argument | |:-----------------:|:-----------------:| @@ -28,6 +28,7 @@ The formats that are currently implemented are: ASN, FEBUS, OPTASENSE and SINTEL | FEBUS | `"febus"` | | OPTASENSE | `"optasense"` | | SINTELA | `"sintela"` | +| TERRA15 | `"terra15"` | ## Extending *xdas* with your file format diff --git a/xdas/io/__init__.py b/xdas/io/__init__.py index e630de2..b6fb426 100644 --- a/xdas/io/__init__.py +++ b/xdas/io/__init__.py @@ -1 +1 @@ -from . import asn, febus, optasense, sintela +from . import asn, febus, optasense, sintela, terra15 diff --git a/xdas/io/terra15.py b/xdas/io/terra15.py new file mode 100644 index 0000000..2421d13 --- /dev/null +++ b/xdas/io/terra15.py @@ -0,0 +1,24 @@ +from datetime import UTC, datetime + +import h5py +import numpy as np + +from ..core.dataarray import DataArray +from ..virtual import VirtualSource + + +def read(fname): + with h5py.File(fname, "r") as file: + ti = np.datetime64( + datetime.fromtimestamp(file["data_product"]["gps_time"][0]), UTC + ).astype("datetime64[ms]") + tf = np.datetime64( + datetime.fromtimestamp(file["data_product"]["gps_time"][-1]), UTC + ).astype("datetime64[ms]") + d0 = file.attrs["sensing_range_start"] + dx = file.attrs["dx"] + data = VirtualSource(file["data_product"]["data"]) + nt, nd = data.shape + t = {"tie_indices": [0, nt - 1], "tie_values": [ti, tf]} + d = {"tie_indices": [0, nd - 1], "tie_values": [d0, d0 + (nd - 1) * dx]} + return DataArray(data, {"time": t, "distance": d}) From d888a0c8cdb221ec1f5f5b5f14df87ac2e86c2e0 Mon Sep 17 00:00:00 2001 From: Alister Trabattoni Date: Wed, 4 Sep 2024 14:51:46 +0200 Subject: [PATCH 2/4] Fix UTC datetime import for python 3.10 compatibility. --- xdas/io/terra15.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xdas/io/terra15.py b/xdas/io/terra15.py index 2421d13..2ddedc5 100644 --- a/xdas/io/terra15.py +++ b/xdas/io/terra15.py @@ -1,4 +1,4 @@ -from datetime import UTC, datetime +from datetime import datetime, timezone import h5py import numpy as np @@ -10,10 +10,10 @@ def read(fname): with h5py.File(fname, "r") as file: ti = np.datetime64( - datetime.fromtimestamp(file["data_product"]["gps_time"][0]), UTC + datetime.fromtimestamp(file["data_product"]["gps_time"][0]), timezone.utc ).astype("datetime64[ms]") tf = np.datetime64( - datetime.fromtimestamp(file["data_product"]["gps_time"][-1]), UTC + datetime.fromtimestamp(file["data_product"]["gps_time"][-1]), timezone.utc ).astype("datetime64[ms]") d0 = file.attrs["sensing_range_start"] dx = file.attrs["dx"] From 2bde9ee9879e0784e278402db927d9195c3824b5 Mon Sep 17 00:00:00 2001 From: chauvetige Date: Tue, 17 Sep 2024 11:16:53 +0200 Subject: [PATCH 3/4] fix bug for timezone Let the choise to select the timezone #15 --- xdas/io/terra15.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xdas/io/terra15.py b/xdas/io/terra15.py index 2ddedc5..84e5ffc 100644 --- a/xdas/io/terra15.py +++ b/xdas/io/terra15.py @@ -7,13 +7,13 @@ from ..virtual import VirtualSource -def read(fname): +def read(fname,tz=timezone.utc): with h5py.File(fname, "r") as file: ti = np.datetime64( - datetime.fromtimestamp(file["data_product"]["gps_time"][0]), timezone.utc + datetime.fromtimestamp(file["data_product"]["gps_time"][0],tz=tz) ).astype("datetime64[ms]") tf = np.datetime64( - datetime.fromtimestamp(file["data_product"]["gps_time"][-1]), timezone.utc + datetime.fromtimestamp(file["data_product"]["gps_time"][-1],tz=tz) ).astype("datetime64[ms]") d0 = file.attrs["sensing_range_start"] dx = file.attrs["dx"] @@ -21,4 +21,4 @@ def read(fname): nt, nd = data.shape t = {"tie_indices": [0, nt - 1], "tie_values": [ti, tf]} d = {"tie_indices": [0, nd - 1], "tie_values": [d0, d0 + (nd - 1) * dx]} - return DataArray(data, {"time": t, "distance": d}) + return DataArray(data, {"time": t, "distance": d}) \ No newline at end of file From 942136179e104109c8b947b4dcf55ae6c7e24f79 Mon Sep 17 00:00:00 2001 From: Alister Trabattoni Date: Tue, 17 Sep 2024 18:43:08 +0200 Subject: [PATCH 4/4] Format code. --- xdas/io/terra15.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xdas/io/terra15.py b/xdas/io/terra15.py index 84e5ffc..7c969ac 100644 --- a/xdas/io/terra15.py +++ b/xdas/io/terra15.py @@ -7,13 +7,13 @@ from ..virtual import VirtualSource -def read(fname,tz=timezone.utc): +def read(fname, tz=timezone.utc): with h5py.File(fname, "r") as file: ti = np.datetime64( - datetime.fromtimestamp(file["data_product"]["gps_time"][0],tz=tz) + datetime.fromtimestamp(file["data_product"]["gps_time"][0], tz=tz) ).astype("datetime64[ms]") tf = np.datetime64( - datetime.fromtimestamp(file["data_product"]["gps_time"][-1],tz=tz) + datetime.fromtimestamp(file["data_product"]["gps_time"][-1], tz=tz) ).astype("datetime64[ms]") d0 = file.attrs["sensing_range_start"] dx = file.attrs["dx"] @@ -21,4 +21,4 @@ def read(fname,tz=timezone.utc): nt, nd = data.shape t = {"tie_indices": [0, nt - 1], "tie_values": [ti, tf]} d = {"tie_indices": [0, nd - 1], "tie_values": [d0, d0 + (nd - 1) * dx]} - return DataArray(data, {"time": t, "distance": d}) \ No newline at end of file + return DataArray(data, {"time": t, "distance": d})