1+ import argparse
2+ import json
13import os
2- import sys
3- import tempfile
44import shutil
5- import numpy as np
6- from netCDF4 import Dataset , num2date , stringtochar
7- import json
5+ import tempfile
86from datetime import datetime
9- import argparse
107
8+ import numpy as np
9+ import pandas as pd
10+ import xarray as xr
11+ from netCDF4 import Dataset , num2date , stringtochar
1112from pkg_resources import resource_filename
12- from aodntools import __version__
13+
1314import aodntools .timeseries_products .aggregated_timeseries as utils
15+ from aodntools import __version__
1416from aodntools .timeseries_products .velocity_aggregated_timeseries import check_file
1517
16- import xarray as xr
17- import pandas as pd
18-
19-
2018TEMPLATE_JSON = resource_filename (__name__ , 'velocity_hourly_timeseries_template.json' )
21-
19+ QC_FLAG_MAX = 2
2220
2321def cell_velocity_resample (df , binning_function , is_WCUR ):
2422 """
2523 Resample a dataset to a specific time_interval.
2624 if WCUR not present, returns nan
2725 :param df: grouped dataframe
28- :param binning_function: function used for binning as non string standard numpy function
26+ :param binning_function: name of standard numpy function used for binning
2927 :param is_WCUR: True if WCUR is present in nc, False otherwise
3028 :return: binned U, v, W CUR according to the binning function
3129 """
@@ -41,14 +39,14 @@ def cell_velocity_resample(df, binning_function, is_WCUR):
4139 return UCUR , VCUR , WCUR , DEPTH
4240
4341
44- def get_resampled_values (nc_cell , ds , slice_start , varlist , binning_fun , epoch , one_day , is_WCUR ):
42+ def get_resampled_values (nc_cell , ds , slice_start , varlist , binning_function , epoch , one_day , is_WCUR ):
4543 """
4644 get U, V, W current values resampled
4745 :param nc_cell: xarray DATASET
4846 :param ds: netcdf4 dataset
4947 :param slice_start: start index of the slice
5048 :param varlist: list of variable names to subset the dataset
51- :param binnig_fun : list of function names for binning
49+ :param binning_function : list of numpy function names for binning
5250 :param one_day: timedelta one day
5351 :param epoch: base epoch
5452 :param is_WCUR: flag indicating if WCUR is present
@@ -73,7 +71,7 @@ def get_resampled_values(nc_cell, ds, slice_start, varlist, binning_fun, epoch,
7371 ds ['WCUR' ][slice_start :slice_end ], \
7472 ds ['DEPTH' ][slice_start :slice_end ] = cell_velocity_resample (nc_cell_1H , 'mean' , is_WCUR )
7573
76- for method in binning_fun :
74+ for method in binning_function :
7775 ds ['UCUR_' + method ][slice_start :slice_end ], \
7876 ds ['VCUR_' + method ][slice_start :slice_end ], \
7977 ds ['WCUR_' + method ][slice_start :slice_end ], \
@@ -132,7 +130,7 @@ def velocity_hourly_aggregated(files_to_agg, site_code, input_dir='', output_dir
132130 files_to_agg = utils .sort_files (files_to_agg , input_dir = input_dir )
133131
134132 ## create ncdf file, dimensions (unlimited) and variables
135- ds = Dataset (os . path . join ( output_dir , temp_outfile ) , 'w' , format = 'NETCDF4_CLASSIC' )
133+ ds = Dataset (temp_outfile , 'w' , format = 'NETCDF4_CLASSIC' )
136134 OBSERVATION = ds .createDimension ('OBSERVATION' , size = None )
137135 INSTRUMENT = ds .createDimension ('INSTRUMENT' , size = len (files_to_agg ))
138136 STRING256 = ds .createDimension ("strlen" , size = 256 )
@@ -190,20 +188,12 @@ def velocity_hourly_aggregated(files_to_agg, site_code, input_dir='', output_dir
190188
191189 with xr .open_dataset (os .path .join (input_dir , file )) as nc :
192190
193- if 'HEIGHT_ABOVE_SENSOR' in list (nc .variables ):
194- is_2D = True
195- else :
196- is_2D = False
197-
198- if 'WCUR' in list (nc .data_vars ):
199- is_WCUR = True
200- else :
201- is_WCUR = False
191+ is_2D = 'HEIGHT_ABOVE_SENSOR' in list (nc .variables )
192+ is_WCUR = 'WCUR' in list (nc .data_vars )
202193
203194 ## mask values with QC flag>2
204195 for var in varlist :
205- nc [var ] = nc [var ].where (nc [var + '_quality_control' ]< 3 )
206-
196+ nc [var ] = nc [var ].where (nc [var + '_quality_control' ] <= QC_FLAG_MAX )
207197
208198 ## process in chunks
209199 ## in water only
@@ -218,12 +208,12 @@ def velocity_hourly_aggregated(files_to_agg, site_code, input_dir='', output_dir
218208 nc_chunk = nc .where ((nc .TIME >= chunk_start ) & (nc .TIME < chunk_partial ), drop = True )
219209 if is_2D :
220210 ## process all cells, one by one
221- cells = nc_chunk .HEIGHT_ABOVE_SENSOR .values
222- for cell_idx , cell in enumerate (cells ):
211+ heights = nc_chunk .HEIGHT_ABOVE_SENSOR .values
212+ for cell_idx , cell_height in enumerate (heights ):
223213 ## get cell data, drop HEIGHT_ABOVE_SENSOR dim
224- nc_cell = nc_chunk .sel (HEIGHT_ABOVE_SENSOR = cell )
214+ nc_cell = nc_chunk .sel (HEIGHT_ABOVE_SENSOR = cell_height )
225215 ## convert to absolute DEPTH
226- nc_cell ['DEPTH' ] = nc_cell ['DEPTH' ] - cell
216+ nc_cell ['DEPTH' ] = nc_cell ['DEPTH' ] - cell_height
227217 slice_end = get_resampled_values (nc_cell , ds , slice_start , varlist , binning_fun ,
228218 epoch , one_day , is_WCUR )
229219 CELL_INDEX [slice_start :slice_end ] = np .full (slice_end - slice_start , cell_idx , dtype = np .uint32 )
0 commit comments