Skip to content

Commit

Permalink
Merge pull request #185 from c-proof/fix-mission-number
Browse files Browse the repository at this point in the history
Fix mission number
  • Loading branch information
jklymak authored Aug 8, 2024
2 parents b1a2614 + 4655a10 commit 06804a9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pyglider/ncprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def make_gridfiles(inname, outdir, deploymentyaml, *, fnamesuffix='', dz=1, star
# 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'] = np.int32(1)
dsout['mission_number'].attrs['cf_role'] = 'trajectory_id'
dsout = dsout.set_coords(['latitude', 'longitude', 'time'])
for k in dsout:
Expand Down
11 changes: 10 additions & 1 deletion pyglider/slocum.py
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,8 @@ def raw_to_timeseries(indir, outdir, deploymentyaml, *,
def binary_to_timeseries(indir, cachedir, outdir, deploymentyaml, *,
search='*.[D|E]BD', fnamesuffix='',
time_base='sci_water_temp', profile_filt_time=100,
profile_min_time=300, maxgap=300):
profile_min_time=300, maxgap=300,
replace_attrs=None):
"""
Convert directly from binary files to netcdf timeseries file. Requires
dbdreader to be installed.
Expand Down Expand Up @@ -821,6 +822,11 @@ def binary_to_timeseries(indir, cachedir, outdir, deploymentyaml, *,
profile_min_time : float
minimum time to consider a profile an actual profile (seconds)
replace_attrs : dict or None
replace global attributes in the metadata after reading the metadata
file in. Helpful when processing runs with only a couple things that
change.
Returns
-------
outname : string
Expand All @@ -831,6 +837,9 @@ def binary_to_timeseries(indir, cachedir, outdir, deploymentyaml, *,
raise ImportError('Cannot import dbdreader')

deployment = utils._get_deployment(deploymentyaml)
if replace_attrs:
for att in replace_attrs:
deployment['metadata'][att] = replace_attrs[att]

ncvar = deployment['netcdf_variables']
device_data = deployment['glider_devices']
Expand Down
62 changes: 54 additions & 8 deletions pyglider/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from scipy.signal import argrelextrema
import gsw
import logging
from pathlib import Path
import yaml


Expand All @@ -28,11 +29,15 @@ def get_distance_over_ground(ds):
ds : `.xarray.Dataset`
With ``distance_over_ground`` key.
"""

good = ~np.isnan(ds.latitude + ds.longitude)
dist = gsw.distance(ds.longitude[good].values, ds.latitude[good].values)/1000
dist = np.roll(np.append(dist, 0), 1)
dist = np.cumsum(dist)
dist = np.interp(ds.time, ds.time[good], dist)
if np.any(good):
dist = gsw.distance(ds.longitude[good].values, ds.latitude[good].values)/1000
dist = np.roll(np.append(dist, 0), 1)
dist = np.cumsum(dist)
dist = np.interp(ds.time, ds.time[good], dist)
else:
dist = 0 * ds.latitude.values
attr = {'long_name': 'distance over ground flown since mission start',
'method': 'get_distance_over_ground',
'units': 'km',
Expand Down Expand Up @@ -464,10 +469,17 @@ def fill_metadata(ds, metadata, sensor_data):
"""
good = ~np.isnan(ds.latitude.values + ds.longitude.values)
ds.attrs['geospatial_lat_max'] = np.max(ds.latitude.values[good])
ds.attrs['geospatial_lat_min'] = np.min(ds.latitude.values[good])
ds.attrs['geospatial_lon_max'] = np.max(ds.longitude.values[good])
ds.attrs['geospatial_lon_min'] = np.min(ds.longitude.values[good])
if np.any(good):
ds.attrs['geospatial_lat_max'] = np.max(ds.latitude.values[good])
ds.attrs['geospatial_lat_min'] = np.min(ds.latitude.values[good])
ds.attrs['geospatial_lon_max'] = np.max(ds.longitude.values[good])
ds.attrs['geospatial_lon_min'] = np.min(ds.longitude.values[good])
else:
ds.attrs['geospatial_lat_max'] = np.nan
ds.attrs['geospatial_lat_min'] = np.nan
ds.attrs['geospatial_lon_max'] = np.nan
ds.attrs['geospatial_lon_min'] = np.nan

ds.attrs['geospatial_lat_units'] = 'degrees_north'
ds.attrs['geospatial_lon_units'] = 'degrees_east'
ds.attrs['netcdf_version'] = '4.0' # TODO get this somehow...
Expand Down Expand Up @@ -711,6 +723,40 @@ def _get_deployment(deploymentyaml):
return deployment


def _any_newer(dirname, filename):
"""
Check if any files in dirname are newer than filename
"""
filename = Path(filename)
dirname = Path(dirname)
print(filename, filename.exists())
if not filename.exists():
return True

mod_time = filename.stat().st_mtime
is_newer = False
for file_path in dirname.iterdir():
if file_path.is_file():
if file_path.stat().st_mtime > mod_time:
is_newer = True
break

return is_newer


def _get_glider_name_slocum(current_directory):
glider = current_directory.parts[-2]
mission = current_directory.parts[-1]
print(f'Glider {glider} and mission: {mission}')
slocum_glider = glider[4:]
if slocum_glider[-4:-3].isnumeric():
slocum_glider = slocum_glider[:-4] + '_' + slocum_glider[-4:]
else:
slocum_glider = slocum_glider[:-3] + '_' + slocum_glider[-3:]

return glider, mission, slocum_glider


__all__ = ['get_distance_over_ground', 'get_glider_depth', 'get_profiles_new',
'get_derived_eos_raw', "fill_metadata", "nmea2deg",
"gappy_fill_vertical", "oxygen_concentration_correction"]

0 comments on commit 06804a9

Please sign in to comment.