Numpy N-dimensional Array to Xarray
The following python 3 modules will be used:
import numpy as np
import xarray as xr
import pandas as pd
An outline of the data format as derived from
We know that the shape of the data is (136, 994), this data will be stored in an ndarray across all 4 frequencies. Using the following guide
we know that the data needs to have dimension:
136 vertical samples x 994 horizonal samples x 4 frequencies
numOberservations = data.shape[0] # represents vertical pixels
numWatercolumns = data.shape[1] # represents horizontal pixels
numFrequencies = 5
frequencyNames = ['18 kHz', '38 kHz', '70 kHz', '120 kHz', '200 kHz']
Generate some synthetic data:
## generate some synthetic data
data_00 = np.random.rand(numFrequencies, numOberservations, numWatercolumns)
data_01 = np.random.rand(numFrequencies, numOberservations*5, numWatercolumns)
data_02 = np.random.rand(numFrequencies, numOberservations*5, numWatercolumns)
time = pd.date_range('2000-01-01', periods=numWatercolumns)
latitude = np.arange(numWatercolumns) + 1
longitude = np.arange(numWatercolumns) + 2
raw_depth = (np.arange(numOberservations) + 1.) / 2
resampled_depth = (np.arange(numOberservations*5) + 1.) / 2
#cruise_name = "gu1002"
ds = xr.Dataset(
data_vars={
"level_00": (("frequency", "raw_depth", "time"), data_00),
"level_01": (("frequency", "resampled_depth", "time"), data_01),
"level_02": (("frequency", "resampled_depth", "time"), data_02),
"latitude": ("time", latitude),
"longitude": ("time", longitude)
},
coords={
"frequency": frequency,
"raw_depth": raw_depth,
"resampled_depth": resampled_depth,
"time": time
},
attrs={
'Title': 'Sub-Surface Oil Monitoring Cruise (GU1002, EK60)',
'Organization Name': 'DOC/NOAA/NESDIS/NCEI > National Centers for Environmental Information, NESDIS, NOAA, U.S. Department of Commerce',
'File Identifier': 'gov.noaa.ngdc.mgg.wcd:GU1002_EK60',
'E-Mail Address': 'wcd.info@example.gov',
'Version': '0.1.0'
}
)
ds = ds.chunk({
"frequency": 1,
"raw_depth": 2,
"resampled_depth": 2
"time": 1
})
>>> ds
<xarray.Dataset>
Dimensions: (frequency: 4, raw_depth: 5, resampled_depth: 25, time: 3)
Coordinates:
* frequency (frequency) <U7 '18 kHz' '38 kHz' '120 kHz' '200 kHz'
* raw_depth (raw_depth) float64 0.5 1.0 1.5 2.0 2.5
* resampled_depth (resampled_depth) float64 0.5 1.0 1.5 ... 11.5 12.0 12.5
* time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03
Data variables:
level_00 (frequency, raw_depth, time) float64 dask.array<chunksize=(1, 2, 1), meta=np.ndarray>
level_01 (frequency, resampled_depth, time) float64 dask.array<chunksize=(1, 2, 1), meta=np.ndarray>
level_02 (frequency, resampled_depth, time) float64 dask.array<chunksize=(1, 2, 1), meta=np.ndarray>
Attributes:
Title: Sub-Surface Oil Monitoring Cruise (GU1002, EK60)
Organization Name: DOC/NOAA/NESDIS/NCEI > National Centers for Environme...
File Identifier: gov.noaa.ngdc.mgg.wcd:GU1002_EK60
E-Mail Address: wcd.info@example.gov
Version: 0.1.0
>>>
To read an products at level 0, level 1, etc., the user can parse those entries as follows:
>>> ds.data_vars.get('level_00')
<xarray.DataArray 'level_00' (frequency: 4, depth1: 5, time: 3)>
dask.array<xarray-level_00, shape=(4, 5, 3), dtype=float64, chunksize=(1, 2, 1), chunktype=numpy.ndarray>
Coordinates:
* frequency (frequency) <U7 '18 kHz' '38 kHz' '120 kHz' '200 kHz'
* depth1 (depth1) float64 0.5 1.0 1.5 2.0 2.5
* time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03
>>> ds.data_vars.get('level_01')
<xarray.DataArray 'level_01' (frequency: 4, depth2: 25, time: 3)>
dask.array<xarray-level_01, shape=(4, 25, 3), dtype=float64, chunksize=(1, 2, 1), chunktype=numpy.ndarray>
Coordinates:
* frequency (frequency) <U7 '18 kHz' '38 kHz' '120 kHz' '200 kHz'
* depth2 (depth2) float64 0.5 1.0 1.5 2.0 2.5 ... 10.5 11.0 11.5 12.0 12.5
* time (time) datetime64[ns] 2000-01-01 2000-01-02 2000-01-03
The dtype of the data can be modified for products beyond level 0. See here for reference:
To get the raw data from the level 1 product:
ds.data_vars.get('level_01').values