Skip to content

Commit

Permalink
MNT: more time changes - seconds to nano
Browse files Browse the repository at this point in the history
  • Loading branch information
C-PROOF committed Apr 18, 2024
1 parent eff60ad commit 812e746
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions pyglider/ncprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def extract_timeseries_profiles(inname, outdir, deploymentyaml):

# outname = outdir + '/' + utils.get_file_id(dss) + '.nc'
_log.info('Writing %s', outname)
timeunits = 'seconds since 1970-01-01T00:00:00Z'
timeunits = 'nanoseconds since 1970-01-01T00:00:00Z'
timecalendar = 'gregorian'
dss.to_netcdf(outname, encoding={'time': {'units': timeunits,
'calendar': timecalendar},
Expand Down Expand Up @@ -164,7 +164,7 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1):
deployment = yaml.safe_load(fin)
profile_meta = deployment['profile_variables']

ds = xr.open_dataset(inname)
ds = xr.open_dataset(inname, decode_times=True)
_log.info(f'Working on: {inname}')
_log.debug(str(ds))
_log.debug(str(ds.time[0]))
Expand All @@ -183,8 +183,9 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1):
dsout = xr.Dataset(
coords={'depth': ('depth', depths),
'profile': ('time', profiles)})
print('Booo', ds.time, ds.temperature)
ds['time_1970'] = ds.temperature.copy()
ds['time_1970'].values = ds.time.values.astype(np.float64)/1e9
ds['time_1970'].values = ds.time.values.astype(np.float64)
for td in ('time_1970', 'longitude', 'latitude'):
good = np.where(~np.isnan(ds[td]) & (ds['profile_index'] % 1 == 0))[0]
dat, xedges, binnumber = stats.binned_statistic(
Expand All @@ -193,7 +194,7 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1):
bins=[profile_bins])
if td == 'time_1970':
td = 'time'
dat = dat.astype('timedelta64[s]') + np.datetime64('1970-01-01T00:00:00')
dat = dat.astype('timedelta64[ns]') + np.datetime64('1970-01-01T00:00:00')
_log.info(f'{td} {len(dat)}')
dsout[td] = (('time'), dat, ds[td].attrs)
ds.drop('time_1970')
Expand Down Expand Up @@ -249,8 +250,8 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1):

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

return outname
Expand Down
4 changes: 2 additions & 2 deletions pyglider/slocum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ def parse_logfiles(files):

# now parse them
out = xr.Dataset(
coords={'time': ('surfacing', np.zeros(ntimes, dtype='datetime64[s]'))})
coords={'time': ('surfacing', np.zeros(ntimes, dtype='datetime64[ns]'))})
out['ampH'] = ('surfacing', np.zeros(ntimes) * np.NaN)
out['lon'] = ('surfacing', np.zeros(ntimes) * np.NaN)
out['lat'] = ('surfacing', np.zeros(ntimes) * np.NaN)
Expand All @@ -1073,7 +1073,7 @@ def parse_logfiles(files):
timestring = times[i][11:-13]
try:
out['time'][i] = np.datetime64(
datetime.strptime(timestring, '%a %b %d %H:%M:%S %Y'))
datetime.strptime(timestring, '%a %b %d %H:%M:%S %Y'), 'ns')
st = amph[i].index('=')
en = amph[i][st:].index(' ') + st
out['ampH'][i] = float(amph[i][(st+1):en])
Expand Down

0 comments on commit 812e746

Please sign in to comment.