@@ -931,26 +931,49 @@ def get_segment_instrument_depths(
931931):
932932 """
933933 Compute instrument depth from the average time series depth PPSAADCP for each segment
934+ start_instrument_depth is the depth provided by the user in the metadata csv file
934935 """
936+ # Compute new instrument_depth including an offset amount
937+ depth_correction = np .round (
938+ np .nanmean (time_series_depth [start_indices [0 ]: end_indices [0 ]]) - start_instrument_depth ,
939+ 1
940+ )
941+
935942 instrument_depths = np .zeros (len (start_indices ))
936943 instrument_depths [0 ] = start_instrument_depth
937944
938945 for i in range (1 , len (start_indices )):
939946 instrument_depths [i ] = np .round (
940- np .nanmean (time_series_depth [start_indices [i ]: end_indices [i ]]),
947+ np .nanmean (time_series_depth [start_indices [i ]: end_indices [i ]]) - depth_correction ,
941948 1
942949 )
943- return instrument_depths
950+
951+ # Check for any repeating depth values
952+ if len (np .unique ([utils .round_to_int (x ) for x in instrument_depths ])) < len (instrument_depths ):
953+ warnings .warn (f'Segment depths round to same int value: depths = { instrument_depths } ' )
954+
955+ return instrument_depths , depth_correction
944956
945957
946958def make_dataset_from_subset (
947959 ds : xr .Dataset , start_idx : int , end_idx : int ,
948- instrument_depth : float , new_filename : str , num_segments : int ,
960+ instrument_depth : float , depth_correction : float , num_segments : int ,
949961 time_of_strike : str , recovery_lat = None , recovery_lon = None
950962):
951963 """
952964 Create an xarray dataset to contain data from a single segment
953965 """
966+
967+ # Need to subtract 1 from en_idx because +1 is added in
968+ # get_user_segment_start_end_idx_depth() for inclusive ranges
969+ # but ranges are not called here
970+ new_filename = '{}_{}_{}_{}m_L1.adcp.nc' .format (
971+ ds .station .lower (),
972+ utils .numpy_datetime_to_str_utc (ds .time .data [start_idx ])[:10 ].replace ('-' , '' ),
973+ utils .numpy_datetime_to_str_utc (ds .time .data [end_idx ])[:10 ].replace ('-' , '' ),
974+ f'000{ utils .round_to_int (instrument_depth )} ' [- 4 :]
975+ )
976+
954977 # Add variables
955978 var_dict = {}
956979 for key in ds .data_vars .keys ():
@@ -1040,7 +1063,11 @@ def make_dataset_from_subset(
10401063
10411064 # Update processing_history
10421065 dsout .attrs ['processing_history' ] += (f" Dataset split into { num_segments } segments due to a mooring "
1043- f"strike(s) at { time_of_strike } ." )
1066+ f"strike(s) at { time_of_strike } . New instrument depth calculated "
1067+ f"as the sum of the mean instrument depth for this segment and an "
1068+ f"offset of { depth_correction } m, where the offset is the "
1069+ f"difference between the mean instrument depth for segment 1 and "
1070+ f"the instrument depth input by the user." )
10441071
10451072 # Convert start and end idx back to within the context of the original dataset length
10461073 # leading_ens_cut, trailing_ens_cut = utils.parse_processing_history(dsout.attrs['processing_history'])
@@ -1060,7 +1087,7 @@ def make_dataset_from_subset(
10601087 dsout .attrs ['geospatial_lon_max' ] = recovery_lon
10611088 dsout .attrs ['processing_history' ] += ' Longitude updated with coordinates from recovery cruise.'
10621089
1063- return dsout
1090+ return dsout , new_filename
10641091
10651092
10661093def split_ds_by_pressure (input_ds : xr .Dataset , segment_starts : list , segment_ends : list ,
@@ -1076,7 +1103,7 @@ def split_ds_by_pressure(input_ds: xr.Dataset, segment_starts: list, segment_end
10761103 :param verbose: print out progress statements if True; default False
10771104 """
10781105 # Use the input instrument_depth for the first segment
1079- segment_instr_depths = get_segment_instrument_depths (
1106+ segment_instr_depths , depth_correction = get_segment_instrument_depths (
10801107 segment_starts , segment_ends , time_series_depth = input_ds .PPSAADCP .data ,
10811108 start_instrument_depth = input_ds .instrument_depth .data
10821109 )
@@ -1105,33 +1132,18 @@ def split_ds_by_pressure(input_ds: xr.Dataset, segment_starts: list, segment_end
11051132 # only use the "date" part of the datetime and not the time portion
11061133 # format the depth to 4 string characters by adding zeros if necessary
11071134
1108- # Need to subtract 1 from en_idx because +1 is added in
1109- # get_user_segment_start_end_idx_depth() for inclusive ranges
1110- # but ranges are not called here
1111- out_segment_name = '{}_{}_{}_{}m_L1.adcp.nc' .format (
1112- input_ds .station .lower (),
1113- utils .numpy_datetime_to_str_utc (input_ds .time .data [st_idx ])[:10 ].replace ('-' , '' ),
1114- utils .numpy_datetime_to_str_utc (input_ds .time .data [en_idx ])[:10 ].replace ('-' , '' ),
1115- f'000{ utils .round_to_int (segment_instr_depths [i ])} ' [- 4 :]
1116- )
1117-
1118- # File name
1119- absolute_segment_name = os .path .join (dest_dir , out_segment_name )
1120-
1121- netcdf_filenames .append (absolute_segment_name )
1122-
11231135 # split the dataset by creating subsets from the original
11241136 # Only apply the recovery lat and lon to the last segment of data
11251137 if i < len (segment_instr_depths ) - 1 :
1126- ds_segment = make_dataset_from_subset (
1127- input_ds , st_idx , en_idx , segment_instr_depths [i ],
1128- out_segment_name , num_segments , time_of_strike = time_of_split
1138+ ds_segment , out_segment_name = make_dataset_from_subset (
1139+ input_ds , st_idx , en_idx , segment_instr_depths [i ], depth_correction ,
1140+ num_segments , time_of_strike = time_of_split
11291141 )
11301142 else :
11311143 # The last segment of data where i == len(segment_instr_depths) - 1
1132- ds_segment = make_dataset_from_subset (
1133- input_ds , st_idx , en_idx , segment_instr_depths [i ],
1134- out_segment_name , num_segments , time_of_strike = time_of_split ,
1144+ ds_segment , out_segment_name = make_dataset_from_subset (
1145+ input_ds , st_idx , en_idx , segment_instr_depths [i ], depth_correction ,
1146+ num_segments , time_of_strike = time_of_split ,
11351147 recovery_lat = recovery_lat , recovery_lon = recovery_lon
11361148 )
11371149
@@ -1141,6 +1153,11 @@ def split_ds_by_pressure(input_ds: xr.Dataset, segment_starts: list, segment_end
11411153 # print(ds_segment.attrs)
11421154 # print(ds_segment)
11431155
1156+ # File name
1157+ absolute_segment_name = os .path .join (dest_dir , out_segment_name )
1158+
1159+ netcdf_filenames .append (absolute_segment_name )
1160+
11441161 # Export the dataset object as a new netCDF file
11451162 ds_segment .to_netcdf (absolute_segment_name , mode = 'w' , format = 'NETCDF4' )
11461163
0 commit comments