From 3b5dbdc3f4cf3fcec7bcd710cdc69a190284e063 Mon Sep 17 00:00:00 2001 From: Dan Nowacki Date: Mon, 17 Jun 2024 13:05:57 -0700 Subject: [PATCH] Add obstime for profiling CTD (#237) --- stglib/rsk/cdf2nc.py | 10 +++++++++- stglib/rsk/csv2cdf.py | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/stglib/rsk/cdf2nc.py b/stglib/rsk/cdf2nc.py index c8c12842..a478e1f3 100755 --- a/stglib/rsk/cdf2nc.py +++ b/stglib/rsk/cdf2nc.py @@ -85,6 +85,12 @@ def cdf_to_nc(cdf_filename, atmpres=None, writefile=True, format="NETCDF4"): if len(ds["sample"]) == 1: ds = ds.squeeze(dim="sample") + if "obstime" in ds: + if utils.check_time_fits_in_int32(ds, "obstime"): + ds["obstime"].encoding["dtype"] = "i4" + else: + ds["obstime"].encoding["dtype"] = "float64" + if writefile: # Write to .nc file print("Writing cleaned/trimmed data to .nc file") @@ -126,7 +132,9 @@ def cdf_to_nc(cdf_filename, atmpres=None, writefile=True, format="NETCDF4"): def open_raw_cdf(cdf_filename): ds = xr.load_dataset(cdf_filename) # remove units in case we change and we can use larger time steps - ds.time.encoding.pop("units") + ds["time"].encoding.pop("units") + if "obstime" in ds: + ds["obstime"].encoding.pop("units") return ds diff --git a/stglib/rsk/csv2cdf.py b/stglib/rsk/csv2cdf.py index 4e7f0817..3cc730f9 100644 --- a/stglib/rsk/csv2cdf.py +++ b/stglib/rsk/csv2cdf.py @@ -186,11 +186,17 @@ def csv_to_cdf(metadata): if utils.check_fits_in_int32(ds, "obs"): ds["obs"].encoding["dtype"] = "i4" + obstime = ds["time"].values + ds = ds.drop("time") ds = ds.rename({"obs": "time"}).set_coords("time").rename({"time": "obs"}) ds["obs"].attrs["long_name"] = "sample number" + ds["obstime"] = xr.DataArray(obstime, dims="obs") + ds["obstime"].attrs["long_name"] = "time (UTC)" + ds["obstime"].attrs["standard_name"] = "time" + ds = xr.merge([ds, pr]) """