Skip to content

Commit

Permalink
Add obstime for profiling CTD (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnowacki-usgs authored Jun 17, 2024
1 parent c5350e2 commit 3b5dbdc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion stglib/rsk/cdf2nc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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


Expand Down
6 changes: 6 additions & 0 deletions stglib/rsk/csv2cdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

"""
Expand Down

0 comments on commit 3b5dbdc

Please sign in to comment.