Finding the closest or interpolated positions at given times (e.g. wave observations).#

from pathlib import Path
from trajan.readers.omb import read_omb_csv
import xarray as xr
import coloredlogs

coloredlogs.install(level='debug')

Read the data

data = Path.cwd().parent / "tests" / "test_data" / "csv" / "omb3.csv"
ds = read_omb_csv(data)
print(ds)
2024-12-16 16:26:13 fv-az1766-447 trajan.readers.omb[2121] DEBUG reading /home/runner/work/trajan/trajan/tests/test_data/csv/omb3.csv..
2024-12-16 16:26:13 fv-az1766-447 trajan.readers.omb[2121] DEBUG omb_dataframe at index 94 is:
Date Time (UTC)    16/Jun/2022 18:27:19
Device                        drifter_2
Direction                            MO
Payload                             NaN
Approx Lat/Lng          74.36745,3.3274
Payload (Text)                      NaN
Length (Bytes)                        0
Credits                               1
Name: 94, dtype: object
this is empty (Length (Bytes) is 0), drop
2024-12-16 16:26:13 fv-az1766-447 trajan.readers.omb[2121] DEBUG start applying sliding_filter_nsigma
2024-12-16 16:26:13 fv-az1766-447 trajan.readers.omb[2121] DEBUG done applying sliding_filter_nsigma
2024-12-16 16:26:13 fv-az1766-447 trajan.readers.omb[2121] DEBUG start applying sliding_filter_nsigma
2024-12-16 16:26:13 fv-az1766-447 trajan.readers.omb[2121] DEBUG done applying sliding_filter_nsigma
2024-12-16 16:26:13 fv-az1766-447 trajan.readers.omb[2121] DEBUG start applying sliding_filter_nsigma
2024-12-16 16:26:13 fv-az1766-447 trajan.readers.omb[2121] DEBUG done applying sliding_filter_nsigma
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting trajectory dimension
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting time-variable for "obs"..
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected obs-dim: obs, detected time-variable: time.
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected un-structured (2D) trajectory dataset
2024-12-16 16:26:13 fv-az1766-447 trajan.traj[2121] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2024-12-16 16:26:13 fv-az1766-447 trajan.traj[2121] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2024-12-16 16:26:13 fv-az1766-447 trajan.traj[2121] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
2024-12-16 16:26:13 fv-az1766-447 trajan.traj[2121] DEBUG No grid-mapping specified, checking if coordinates are lon/lat..
<xarray.Dataset> Size: 119kB
Dimensions:                              (trajectory: 3,
                                          frequencies_waves_imu: 55, obs: 150,
                                          obs_waves_imu: 26)
Coordinates:
  * trajectory                           (trajectory) <U9 108B 'drifter_1' .....
  * frequencies_waves_imu                (frequencies_waves_imu) float64 440B ...
Dimensions without coordinates: obs, obs_waves_imu
Data variables: (12/14)
    time                                 (trajectory, obs) datetime64[ns] 4kB ...
    lat                                  (trajectory, obs) float64 4kB 79.4 ....
    lon                                  (trajectory, obs) float64 4kB 3.031 ...
    time_waves_imu                       (trajectory, obs_waves_imu) datetime64[ns] 624B ...
    accel_energy_spectrum                (trajectory, obs_waves_imu, frequencies_waves_imu) float64 34kB ...
    elevation_energy_spectrum            (trajectory, obs_waves_imu, frequencies_waves_imu) float64 34kB ...
    ...                                   ...
    pHs0                                 (trajectory, obs_waves_imu) float64 624B ...
    pT02                                 (trajectory, obs_waves_imu) float64 624B ...
    pT24                                 (trajectory, obs_waves_imu) float64 624B ...
    Hs0                                  (trajectory, obs_waves_imu) float64 624B ...
    T02                                  (trajectory, obs_waves_imu) float64 624B ...
    T24                                  (trajectory, obs_waves_imu) float64 624B ...
Attributes: (12/14)
    Conventions:          CF-1.10
    featureType:          trajectory
    geospatial_lat_min:   74.1878338
    geospatial_lat_max:   79.7326024
    geospatial_lon_min:   2.9766619
    geospatial_lon_max:   7.5205089
    ...                   ...
    creator_name:         XX:TODO
    creator_email:        XX:TODO
    title:                XX:TODO
    summary:              XX:TODO
    creator_institution:  XX:TODO
    history:              created with trajan.reader.omb from a Rock7 Iridium...

The wave data in the variable pHs0 is given along a different observation dimension. Because it is also a observation, i.e. in the style of 2D trajectory datasets, we need to iterate over the trajectories:

def gridwaves(tds):
    t = tds[['lat', 'lon',
             'time']].traj.gridtime(tds['time_waves_imu'].squeeze())
    return t.traj.to_2d(obs_dim='obs_waves_imu')


dsw = ds.groupby('trajectory').map(gridwaves)

print(dsw)
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting trajectory dimension
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting time-variable for "obs"..
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected obs-dim: obs, detected time-variable: time.
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected un-structured (2D) trajectory dataset
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting trajectory dimension
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected obs-dim: time, detected time-variable: time.
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected structured (1D) trajectory dataset
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting trajectory dimension
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting time-variable for "obs"..
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected obs-dim: obs, detected time-variable: time.
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected un-structured (2D) trajectory dataset
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting trajectory dimension
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected obs-dim: time, detected time-variable: time.
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected structured (1D) trajectory dataset
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting trajectory dimension
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting time-variable for "obs"..
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected obs-dim: obs, detected time-variable: time.
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected un-structured (2D) trajectory dataset
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detecting trajectory dimension
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected obs-dim: time, detected time-variable: time.
2024-12-16 16:26:13 fv-az1766-447 trajan.accessor[2121] DEBUG Detected structured (1D) trajectory dataset
<xarray.Dataset> Size: 2kB
Dimensions:        (trajectory: 3, obs_waves_imu: 26)
Coordinates:
  * obs_waves_imu  (obs_waves_imu) int64 208B 0 1 2 3 4 5 ... 20 21 22 23 24 25
  * trajectory     (trajectory) <U9 108B 'drifter_1' 'drifter_2' 'drifter_3'
Data variables:
    lat            (trajectory, obs_waves_imu) float64 624B nan 79.38 ... nan
    lon            (trajectory, obs_waves_imu) float64 624B nan 3.101 ... nan
    time           (trajectory, obs_waves_imu) datetime64[ns] 624B 2022-06-15...
Attributes: (12/14)
    Conventions:          CF-1.10
    featureType:          trajectory
    geospatial_lat_min:   74.1878338
    geospatial_lat_max:   79.7326024
    geospatial_lon_min:   2.9766619
    geospatial_lon_max:   7.5205089
    ...                   ...
    creator_name:         XX:TODO
    creator_email:        XX:TODO
    title:                XX:TODO
    summary:              XX:TODO
    creator_institution:  XX:TODO
    history:              created with trajan.reader.omb from a Rock7 Iridium...

We now have the positions interpolated to the IMU (wave) observations. We could also merge these together to one dataset again:

ds = xr.merge((ds, dsw.rename({
    'lon': 'lon_waves',
    'lat': 'lat_waves'
}).drop('time')))
print(ds)
/home/runner/work/trajan/trajan/examples/example_find_positions_at_obs_times.py:42: DeprecationWarning: dropping variables using `drop` is deprecated; use drop_vars.
  }).drop('time')))
<xarray.Dataset> Size: 121kB
Dimensions:                              (trajectory: 3,
                                          frequencies_waves_imu: 55, obs: 150,
                                          obs_waves_imu: 26)
Coordinates:
  * trajectory                           (trajectory) <U9 108B 'drifter_1' .....
  * frequencies_waves_imu                (frequencies_waves_imu) float64 440B ...
  * obs_waves_imu                        (obs_waves_imu) int64 208B 0 1 ... 25
Dimensions without coordinates: obs
Data variables: (12/16)
    time                                 (trajectory, obs) datetime64[ns] 4kB ...
    lat                                  (trajectory, obs) float64 4kB 79.4 ....
    lon                                  (trajectory, obs) float64 4kB 3.031 ...
    time_waves_imu                       (trajectory, obs_waves_imu) datetime64[ns] 624B ...
    accel_energy_spectrum                (trajectory, obs_waves_imu, frequencies_waves_imu) float64 34kB ...
    elevation_energy_spectrum            (trajectory, obs_waves_imu, frequencies_waves_imu) float64 34kB ...
    ...                                   ...
    pT24                                 (trajectory, obs_waves_imu) float64 624B ...
    Hs0                                  (trajectory, obs_waves_imu) float64 624B ...
    T02                                  (trajectory, obs_waves_imu) float64 624B ...
    T24                                  (trajectory, obs_waves_imu) float64 624B ...
    lat_waves                            (trajectory, obs_waves_imu) float64 624B ...
    lon_waves                            (trajectory, obs_waves_imu) float64 624B ...
Attributes: (12/14)
    Conventions:          CF-1.10
    featureType:          trajectory
    geospatial_lat_min:   74.1878338
    geospatial_lat_max:   79.7326024
    geospatial_lon_min:   2.9766619
    geospatial_lon_max:   7.5205089
    ...                   ...
    creator_name:         XX:TODO
    creator_email:        XX:TODO
    title:                XX:TODO
    summary:              XX:TODO
    creator_institution:  XX:TODO
    history:              created with trajan.reader.omb from a Rock7 Iridium...

Total running time of the script: (0 minutes 0.241 seconds)

Gallery generated by Sphinx-Gallery