Skip to content

Commit

Permalink
Merge pull request #181 from jklymak/fix-cf-cmpliance
Browse files Browse the repository at this point in the history
Fix cf compliance
  • Loading branch information
jklymak committed Jul 26, 2024
2 parents 70746b5 + c0a62b7 commit 96fb528
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
60 changes: 49 additions & 11 deletions pyglider/ncprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,17 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1, star
_log.info(f'Nprofiles {Nprofiles}')
depth_bins = np.arange(0, 1100.1, dz)
depths = depth_bins[:-1] + 0.5

xdimname = 'time'
dsout = xr.Dataset(
coords={'depth': ('depth', depths),
'profile': ('time', profiles)})
print('Booo', ds.time, ds.temperature)
'profile': (xdimname, profiles)})
dsout['depth'].attrs = {'units': 'm',
'long_name': 'Depth',
'standard_name': 'depth',
'positive': 'down',
'coverage_content_type': 'coordinate',
'comment': 'center of depth bins'}

ds['time_1970'] = ds.temperature.copy()
ds['time_1970'].values = ds.time.values.astype(np.float64)
for td in ('time_1970', 'longitude', 'latitude'):
Expand All @@ -241,12 +247,12 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1, star
good = np.where(~np.isnan(ds['time']) & (ds['profile_index'] % 1 == 0))[0]
_log.info(f'Done times! {len(dat)}')
dsout['profile_time_start'] = (
('time'), dat, profile_meta['profile_time_start'])
(xdimname), dat, profile_meta['profile_time_start'])
dsout['profile_time_end'] = (
('time'), dat, profile_meta['profile_time_end'])
(xdimname), dat, profile_meta['profile_time_end'])

for k in ds.keys():
if k in ['time', 'longitude', 'latitude', 'depth'] or 'time' in k:
if k in ['time', 'profile', 'longitude', 'latitude', 'depth'] or 'time' in k:
continue
_log.info('Gridding %s', k)
good = np.where(~np.isnan(ds[k]) & (ds['profile_index'] % 1 == 0))[0]
Expand All @@ -263,15 +269,14 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1, star
"scipy.stats.gmean")
else:
average_method = "mean"

dat, xedges, yedges, binnumber = stats.binned_statistic_2d(
ds['profile_index'].values[good],
ds['depth'].values[good],
values=ds[k].values[good], statistic=average_method,
bins=[profile_bins, depth_bins])

_log.debug(f'dat{np.shape(dat)}')
dsout[k] = (('depth', 'time'), dat.T, ds[k].attrs)
dsout[k] = (('depth', xdimname), dat.T, ds[k].attrs)

# fill gaps in data:
dsout[k].values = utils.gappy_fill_vertical(dsout[k].values)
Expand All @@ -287,12 +292,45 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1, star
dsout = dsout.drop(['water_velocity_eastward',
'water_velocity_northward'])
dsout.attrs = ds.attrs
dsout.attrs.pop('cdm_data_type')
# fix to be ISO parsable:
if len(dsout.attrs['deployment_start']) > 18:
dsout.attrs['deployment_start'] = dsout.attrs['deployment_start'][:19]
dsout.attrs['deployment_end'] = dsout.attrs['deployment_end'][:19]
dsout.attrs['time_coverage_start'] = dsout.attrs['time_coverage_start'][:19]
dsout.attrs['time_coverage_end'] = dsout.attrs['time_coverage_end'][:19]
# fix standard_name so they don't overlap!
try:
dsout['waypoint_latitude'].attrs.pop('standard_name')
dsout['waypoint_longitude'].attrs.pop('standard_name')
dsout['profile_time_start'].attrs.pop('standard_name')
dsout['profile_time_end'].attrs.pop('standard_name')
except:
pass
# set some attributes for cf guidance
# see H.6.2. Profiles along a single trajectory
# https://cfconventions.org/Data/cf-conventions/cf-conventions-1.7/build/aphs06.html
dsout.attrs['featureType'] = 'trajectoryProfile'
dsout['profile'].attrs['cf_role'] = 'profile_id'
dsout['mission_number'] = int(1)
dsout['mission_number'].attrs['cf_role'] = 'trajectory_id'
dsout = dsout.set_coords(['latitude', 'longitude', 'time'])
for k in dsout:
if k in ['profile', 'depth', 'latitude', 'longitude', 'time', 'mission_number']:
dsout[k].attrs['coverage_content_type'] = 'coordinate'
else:
dsout[k].attrs['coverage_content_type'] = 'physicalMeasurement'


outname = outdir + '/' + ds.attrs['deployment_name'] + '_grid' + fnamesuffix + '.nc'
_log.info('Writing %s', outname)
dsout.to_netcdf(outname,
encoding={'time': {'units': 'seconds since 1970-01-01T00:00:00Z',
'dtype': 'float64'}})
# timeunits = 'nanoseconds since 1970-01-01T00:00:00Z'
dsout.to_netcdf(
outname,
encoding={'time': {'units': 'seconds since 1970-01-01T00:00:00Z',
'_FillValue': np.NaN,
'calendar': 'gregorian',
'dtype': 'float64'}})
_log.info('Done gridding')

return outname
Expand Down
5 changes: 3 additions & 2 deletions pyglider/slocum.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,9 @@ def binary_to_timeseries(indir, cachedir, outdir, deploymentyaml, *,
end = ds.time.values[0]
_log.debug('Long')
_log.debug(ds.longitude.values[-2000:])
ds.attrs['deployment_start'] = str(start)
ds.attrs['deployment_end'] = str(end)
# make sure this is ISO readable....
ds.attrs['deployment_start'] = str(start)[:18]
ds.attrs['deployment_end'] = str(end)[:18]
_log.debug(ds.depth.values[:100])
_log.debug(ds.depth.values[2000:2100])

Expand Down
Binary file not shown.

0 comments on commit 96fb528

Please sign in to comment.