pyadps 0.1.0b0__py3-none-any.whl → 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. pyadps/Home_Page.py +11 -5
  2. pyadps/pages/01_Read_File.py +191 -16
  3. pyadps/pages/02_View_Raw_Data.py +69 -33
  4. pyadps/pages/03_Download_Raw_File.py +161 -60
  5. pyadps/pages/04_Sensor_Health.py +905 -0
  6. pyadps/pages/05_QC_Test.py +476 -0
  7. pyadps/pages/06_Profile_Test.py +971 -0
  8. pyadps/pages/07_Velocity_Test.py +600 -0
  9. pyadps/pages/08_Write_File.py +587 -0
  10. pyadps/pages/09_Auto_process.py +64 -0
  11. pyadps/pages/__pycache__/__init__.cpython-312.pyc +0 -0
  12. pyadps/utils/__init__.py +3 -3
  13. pyadps/utils/__pycache__/__init__.cpython-312.pyc +0 -0
  14. pyadps/utils/__pycache__/autoprocess.cpython-312.pyc +0 -0
  15. pyadps/utils/__pycache__/cutbin.cpython-312.pyc +0 -0
  16. pyadps/utils/__pycache__/plotgen.cpython-312.pyc +0 -0
  17. pyadps/utils/__pycache__/profile_test.cpython-312.pyc +0 -0
  18. pyadps/utils/__pycache__/pyreadrdi.cpython-312.pyc +0 -0
  19. pyadps/utils/__pycache__/readrdi.cpython-312.pyc +0 -0
  20. pyadps/utils/__pycache__/regrid.cpython-312.pyc +0 -0
  21. pyadps/utils/__pycache__/script.cpython-312.pyc +0 -0
  22. pyadps/utils/__pycache__/sensor_health.cpython-312.pyc +0 -0
  23. pyadps/utils/__pycache__/signal_quality.cpython-312.pyc +0 -0
  24. pyadps/utils/__pycache__/velocity_test.cpython-312.pyc +0 -0
  25. pyadps/utils/__pycache__/writenc.cpython-312.pyc +0 -0
  26. pyadps/utils/autoprocess.py +548 -0
  27. pyadps/utils/metadata/config.ini +99 -0
  28. pyadps/utils/metadata/demo.000 +0 -0
  29. pyadps/utils/plotgen.py +505 -3
  30. pyadps/utils/profile_test.py +526 -145
  31. pyadps/utils/pyreadrdi.py +27 -17
  32. pyadps/utils/readrdi.py +167 -20
  33. pyadps/utils/script.py +197 -147
  34. pyadps/utils/sensor_health.py +120 -0
  35. pyadps/utils/signal_quality.py +344 -24
  36. pyadps/utils/velocity_test.py +103 -20
  37. pyadps/utils/writenc.py +223 -27
  38. {pyadps-0.1.0b0.dist-info → pyadps-0.1.1.dist-info}/METADATA +56 -24
  39. pyadps-0.1.1.dist-info/RECORD +47 -0
  40. {pyadps-0.1.0b0.dist-info → pyadps-0.1.1.dist-info}/WHEEL +1 -1
  41. pyadps-0.1.1.dist-info/entry_points.txt +5 -0
  42. pyadps/pages/04_QC_Test.py +0 -283
  43. pyadps/pages/05_Profile_Test.py +0 -389
  44. pyadps/pages/06_Velocity_Test.py +0 -293
  45. pyadps/pages/07_Write_File.py +0 -367
  46. pyadps/utils/cutbin.py +0 -413
  47. pyadps/utils/regrid.py +0 -122
  48. pyadps-0.1.0b0.dist-info/RECORD +0 -29
  49. pyadps-0.1.0b0.dist-info/entry_points.txt +0 -3
  50. {pyadps-0.1.0b0.dist-info → pyadps-0.1.1.dist-info}/LICENSE +0 -0
@@ -1,20 +1,47 @@
1
1
  import numpy as np
2
2
  from pyadps.utils.plotgen import PlotNoise
3
+ from pyadps.utils.readrdi import ReadFile
3
4
 
4
5
 
5
6
  def qc_check(var, mask, cutoff=0):
6
7
  """
7
- The module returns the modified mask file after checking the cutoff criteria.
8
- All values less than the cuttoff are masked.
8
+ Perform a quality control check on the provided data and update the mask
9
+ based on a cutoff threshold. Values in `var` that are less than the cutoff
10
+ are marked as invalid in the mask.
9
11
 
10
- Args:
11
- var (numpy.ndarray):
12
- mask (numpy.ndarray): A mask file having same array size as var
13
- cutoff (int): Default cutoff is 0
12
+ Parameters
13
+ ----------
14
+ var : numpy.ndarray
15
+ The input array containing data to be checked against the cutoff.
16
+ mask : numpy.ndarray
17
+ An integer array of the same shape as `var`, where `1` indicates
18
+ invalid data and `0` indicates valid data.
19
+ cutoff : int, optional
20
+ The threshold value for quality control. Any value in `var` less than
21
+ or equal to this cutoff will be marked as invalid in the mask. Default is 0.
14
22
 
15
- Returns:
16
- mask (numpy.ndarray): Modified mask file based on cutoff
23
+ Returns
24
+ -------
25
+ numpy.ndarray
26
+ An updated integer mask array of the same shape as `var`, with `1`
27
+ indicating invalid data and `0` indicating valid data.
28
+
29
+ Notes
30
+ -----
31
+ - The function modifies the `mask` by applying the cutoff condition.
32
+ Values in `var` that are less than or equal to the cutoff will be
33
+ marked as invalid (`1`), while all other values will remain valid (`0`).
34
+ - Ensure that `var` and `mask` are compatible in shape for element-wise
35
+ operations.
36
+
37
+ Example
38
+ -------
39
+ >>> import pyadps
40
+ >>> ds = pyadps.Readfile('dummy.000')
41
+ >>> var = ds.echo.data
42
+ >>> mask = qc_check(var, mask, cutoff=40)
17
43
  """
44
+
18
45
  shape = np.shape(var)
19
46
  if len(shape) == 2:
20
47
  mask[var[:, :] < cutoff] = 1
@@ -22,16 +49,138 @@ def qc_check(var, mask, cutoff=0):
22
49
  beam = shape[0]
23
50
  for i in range(beam):
24
51
  mask[var[i, :, :] < cutoff] = 1
25
- values, counts = np.unique(mask, return_counts=True)
52
+ # values, counts = np.unique(mask, return_counts=True)
26
53
  # print(values, counts, np.round(counts[1] * 100 / np.sum(counts)))
27
54
  return mask
28
55
 
29
56
 
30
- cor_check = qc_check
31
- echo_check = qc_check
57
+ def correlation_check(ds, mask, cutoff=64):
58
+ """
59
+ Perform an correlation check on the provided variable and update the
60
+ mask to mark valid and invalid values based on a cutoff threshold.
61
+
62
+ Parameters
63
+ ----------
64
+ ds : pyadps.dataset
65
+ The input pyadps dataframe containing correlation data to be checked.
66
+ Accepts 2-D or 3-D masks.
67
+ mask : numpy.ndarray
68
+ An integer array of the same shape as `var`, where `1` indicates invalid
69
+ data or masked data and `0` indicates valid data.
70
+ cutoff : float, optional
71
+ The threshold value for echo intensity. Any value in `ds.correlation.data` below
72
+ this cutoff will be considered invalid and marked as `1` in the mask.
73
+ Default is 64.
74
+
75
+ Returns
76
+ -------
77
+ numpy.ndarray
78
+ An updated integer mask array of the same shape as `var`, with `1`
79
+ indicating invalid or masked data (within the cutoff limit) and `0` indicating
80
+ valid.
81
+
82
+ Notes
83
+ -----
84
+ - The function modifies the `mask` based on the cutoff condition. Valid
85
+ values in `var` retain their corresponding mask value as `0`, while
86
+ invalid values or previously masked elements are marked as `1`.
87
+ operations.
88
+
89
+ Example
90
+ -------
91
+ >>> import pyadps
92
+ >>> ds = pyadps.Readfile('dummy.000')
93
+ >>> outmask = correlation_check(ds, mask, cutoff=9999)
94
+ """
95
+ correlation = ds.correlation.data
96
+ mask = qc_check(correlation, mask, cutoff=cutoff)
97
+ return mask
98
+
99
+ def echo_check(ds, mask, cutoff=40):
100
+ """
101
+ Perform an echo intensity check on the provided variable and update the
102
+ mask to mark valid and invalid values based on a cutoff threshold.
103
+
104
+ Parameters
105
+ ----------
106
+ ds : pyadps.dataset
107
+ The input pyadps dataframe containing echo intensity data to be checked.
108
+ Accepts 2-D or 3-D masks.
109
+ mask : numpy.ndarray
110
+ An integer array of the same shape as `var`, where `1` indicates invalid
111
+ data or masked data and `0` indicates valid data.
112
+ cutoff : float, optional
113
+ The threshold value for echo intensity. Any value in `ds.echo.data` below
114
+ this cutoff will be considered invalid and marked as `1` in the mask.
115
+ Default is 40.
116
+
117
+ Returns
118
+ -------
119
+ numpy.ndarray
120
+ An updated integer mask array of the same shape as `var`, with `1`
121
+ indicating invalid or masked data (within the cutoff limit) and `0` indicating
122
+ valid.
32
123
 
124
+ Notes
125
+ -----
126
+ - The function modifies the `mask` based on the cutoff condition. Valid
127
+ values in `var` retain their corresponding mask value as `0`, while
128
+ invalid values or previously masked elements are marked as `1`.
129
+ - Ensure that `var` and `mask` are compatible in shape for element-wise
130
+ operations.
33
131
 
34
- def ev_check(var, mask, cutoff=9999):
132
+ Example
133
+ -------
134
+ >>> import pyadps
135
+ >>> ds = pyadps.Readfile('dummy.000')
136
+ >>> outmask = echo_check(ds, mask, cutoff=9999)
137
+ """
138
+
139
+ echo = ds.echo.data
140
+ mask = qc_check(echo, mask, cutoff=cutoff)
141
+ return mask
142
+
143
+
144
+ def ev_check(ds, mask, cutoff=9999):
145
+ """
146
+ Perform an error velocity check on the provided variable and update the
147
+ mask to mark valid and invalid values based on a cutoff threshold.
148
+
149
+ Parameters
150
+ ----------
151
+ ds : pyadps.dataset
152
+ The input pyadps dataframe containing error velocity data to be checked.
153
+ mask : numpy.ndarray
154
+ An integer array of the same shape as `var`, where `1` indicates invalid
155
+ data or masked data and `0` indicates valid data.
156
+ cutoff : float, optional
157
+ The threshold value for error velocity. Any value in `var` exceeding
158
+ this cutoff will be considered invalid and marked as `0` in the mask.
159
+ Default is 9999.
160
+
161
+ Returns
162
+ -------
163
+ numpy.ndarray
164
+ An updated integer mask array of the same shape as `var`, with `1`
165
+ indicating invalid or masked data (within the cutoff limit) and `0` indicating
166
+ valid.
167
+
168
+ Notes
169
+ -----
170
+ - The function modifies the `mask` based on the cutoff condition. Valid
171
+ values in `var` retain their corresponding mask value as `0`, while
172
+ invalid values or previously masked elements are marked as `1`.
173
+ - Ensure that `var` and `mask` are compatible in shape for element-wise
174
+ operations.
175
+
176
+ Example
177
+ -------
178
+ >>> import pyadps
179
+ >>> ds = pyadps.Readfile('dummy.000')
180
+ >>> outmask = ev_check(ds, mask, cutoff=9999)
181
+ """
182
+
183
+ var = ds.velocity.data[3, :, :]
35
184
  shape = np.shape(var)
36
185
  var = abs(var)
37
186
  if len(shape) == 2:
@@ -40,24 +189,110 @@ def ev_check(var, mask, cutoff=9999):
40
189
  beam = shape[2]
41
190
  for i in range(beam):
42
191
  mask[(var[i, :, :] >= cutoff) & (var[i, :, :] < 32768)] = 1
43
- values, counts = np.unique(mask, return_counts=True)
44
- # print(values, counts, np.round(counts[1] * 100 / np.sum(counts)))
45
192
  return mask
46
193
 
47
194
 
48
- def pg_check(pgood, mask, cutoff=0, threebeam=True):
195
+ def pg_check(ds, mask, cutoff=0, threebeam=True):
196
+ """
197
+ Perform a percent-good check on the provided data and update the mask
198
+ to mark valid and invalid values based on a cutoff threshold.
199
+
200
+ Parameters
201
+ ----------
202
+ ds : pyadps.dataset
203
+ The input pyadps dataframe containing percent-good data, where values range from
204
+ 0 to 100 (maximum percent good).
205
+ mask : numpy.ndarray
206
+ An integer array of the same shape as `pgood`, where `1` indicates
207
+ invalid data and `0` indicates valid data.
208
+ cutoff : float, optional
209
+ The threshold value for percent good. Any value in `pgood` greater than
210
+ or equal to this cutoff will be considered valid (marked as `0`),
211
+ while values not exceeding the cutoff are marked as invalid (`1`).
212
+ Default is 0.
213
+ threebeam : bool, optional
214
+ If `True`, sums up Percent Good 1 and Percent Good 4 for the check.
215
+
216
+ Returns
217
+ -------
218
+ numpy.ndarray
219
+ An updated integer mask array of the same shape as `pgood`, with `1`
220
+ indicating invalid data and `0` indicating valid data.
221
+
222
+ Notes
223
+ -----
224
+ - The function modifies the `mask` based on the cutoff condition. Valid
225
+ values in `pgood` are marked as `0`, while invalid values are marked
226
+ as `1` in the mask.
227
+ - Ensure that `pgood` and `mask` are compatible in shape for element-wise
228
+ operations.
229
+ - If `threebeam` is `True`, the logic may be adjusted to allow partial
230
+ validity based on specific criteria.
231
+
232
+ Example
233
+ -------
234
+ >>> import pyadps
235
+ >>> ds = pyadps.Readfile('dummy.000')
236
+ >>> outmask = pg_check(ds, mask, cutoff=50, threebeam=True)
237
+ """
238
+
239
+ pgood = ds.percentgood.data
49
240
  if threebeam:
50
241
  pgood1 = pgood[0, :, :] + pgood[3, :, :]
51
242
  else:
52
- pgood1 = pgood[:, :, :]
243
+ pgood1 = pgood[3, :, :]
53
244
 
54
245
  mask[pgood1[:, :] < cutoff] = 1
55
- values, counts = np.unique(mask, return_counts=True)
56
- # print(values, counts, np.round(counts[1] * 100 / np.sum(counts)))
57
246
  return mask
58
247
 
59
248
 
60
- def false_target(echo, mask, cutoff=255, threebeam=True):
249
+ def false_target(ds, mask, cutoff=255, threebeam=True):
250
+ """
251
+ Apply a false target detection algorithm based on echo intensity values.
252
+ This function identifies invalid or false targets in the data and updates
253
+ the mask accordingly based on a specified cutoff threshold.
254
+
255
+ Parameters
256
+ ----------
257
+ ds : pyadps.dataset
258
+ The input pyadps dataframe containing echo intensity values, which are used to
259
+ detect false targets.
260
+ mask : numpy.ndarray
261
+ An integer array of the same shape as `echo`, where `1` indicates
262
+ invalid or false target data and `0` indicates valid data.
263
+ cutoff : int, optional
264
+ The threshold value for echo intensity. Any value in `echo` greater
265
+ than or equal to this cutoff will be considered a false target (invalid),
266
+ marked as `1` in the mask. Default is 255.
267
+ threebeam : bool, optional
268
+ If `True`, applies a relaxed check that considers data valid even
269
+ when only three beams report valid data. Default is `True`.
270
+
271
+ Returns
272
+ -------
273
+ numpy.ndarray
274
+ An updated integer mask array of the same shape as `echo`, with `1`
275
+ indicating false target or invalid data and `0` indicating valid data.
276
+
277
+ Notes
278
+ -----
279
+ - The function modifies the `mask` by applying the cutoff condition.
280
+ Echo values greater than or equal to the cutoff are marked as false
281
+ targets (`1`), while values below the cutoff are considered valid (`0`).
282
+ - If `threebeam` is `True`, a more lenient check may be applied to handle
283
+ data with fewer valid beams.
284
+ - Ensure that `echo` and `mask` are compatible in shape for element-wise
285
+ operations.
286
+
287
+ Example
288
+ -------
289
+ >>> import pyadps
290
+ >>> ds = pyadps.Readfile('dummy.000')
291
+ >>> mask = false_target(echo, mask, cutoff=255)
292
+ """
293
+
294
+ echo = ds.echo.data
295
+
61
296
  shape = np.shape(echo)
62
297
  for i in range(shape[1]):
63
298
  for j in range(shape[2]):
@@ -74,10 +309,49 @@ def false_target(echo, mask, cutoff=255, threebeam=True):
74
309
  return mask
75
310
 
76
311
 
77
- def default_mask(flobj, velocity):
78
- cells = flobj.field()["Cells"]
79
- beams = flobj.field()["Beams"]
80
- ensembles = flobj.ensembles
312
+ def default_mask(ds):
313
+ """
314
+ Create a default 2-D mask file based on the velocity data.
315
+ This function generates a mask where values are marked as valid or invalid
316
+ based on the missing values from the velocity data.
317
+
318
+ Parameters
319
+ ----------
320
+ ds : pyadps.dataset or numpy.ndarray
321
+ A pyadps data frame is used to extract velocity and dimensions for the mask.
322
+ If numpy.ndarray, enter the values for beams, cells and ensembles.
323
+
324
+ Returns
325
+ -------
326
+ numpy.ndarray
327
+ A mask array of the same shape as `velocity`, where `1` indicates invalid
328
+ data and `0` indicates valid data.
329
+
330
+ Notes
331
+ -----
332
+ - The function uses the velocity data along with the information from the
333
+ Fixed Leader object to determine which values are valid and which are invalid.
334
+
335
+ Example
336
+ -------
337
+ >>> import pyadps
338
+ >>> ds = pyadps.ReadFile('demo.000')
339
+ >>> mask = pyadps.default_mask(ds)
340
+ """
341
+ if isinstance(ds, ReadFile) or ds.__class__.__name__ == "ReadFile":
342
+ flobj = ds.fixedleader
343
+ velocity = ds.velocity.data
344
+ cells = flobj.field()["Cells"]
345
+ beams = flobj.field()["Beams"]
346
+ ensembles = flobj.ensembles
347
+ elif isinstance(ds, np.ndarray) and ds.ndim == 3:
348
+ velocity = ds
349
+ beams = ds.shape[0]
350
+ cells = ds.shape[1]
351
+ ensembles = ds.shape[2]
352
+ else:
353
+ raise ValueError("Input must be a 3-D numpy array or a PyADPS instance")
354
+
81
355
  mask = np.zeros((cells, ensembles))
82
356
  # Ignore mask for error velocity
83
357
  for i in range(beams - 1):
@@ -85,7 +359,53 @@ def default_mask(flobj, velocity):
85
359
  return mask
86
360
 
87
361
 
88
- def qc_prompt(flobj, name, data=None):
362
+ def qc_prompt(ds, name, data=None):
363
+ """
364
+ Prompt the user to confirm or adjust the quality control threshold for a specific
365
+ parameter based on predefined ranges. The function provides an interactive interface
366
+ for the user to adjust thresholds for various quality control criteria, with options
367
+ for certain thresholds like "Echo Intensity Thresh" to check the noise floor.
368
+
369
+ Parameters
370
+ ----------
371
+ flobj : FixedLeader
372
+ An instance of the FixedLeader class that holds metadata and configuration
373
+ data. The `flobj` is used to retrieve the current threshold values based on
374
+ the provided parameter name.
375
+ name : str
376
+ The name of the parameter for which the threshold is being adjusted. Examples
377
+ include "Echo Intensity Thresh", "Correlation Thresh", "Percent Good Min", etc.
378
+ data : numpy.ndarray, optional
379
+ The data associated with the threshold. This is required for parameters like
380
+ "Echo Intensity Thresh" where a noise floor check might be performed. Default is None.
381
+
382
+ Returns
383
+ -------
384
+ int
385
+ The updated threshold value, either the default or the new value entered by the user.
386
+
387
+ Notes
388
+ -----
389
+ - The function will prompt the user to change the threshold for the given `name` parameter.
390
+ - For certain parameters, the user may be asked if they would like to check the noise floor
391
+ (for example, for "Echo Intensity Thresh"). This triggers the display of a plot and lets
392
+ the user select a new threshold.
393
+ - The function ensures that the new threshold is within the acceptable range for each parameter.
394
+ - The default thresholds are provided if the user chooses not to change them.
395
+
396
+ Example
397
+ -------
398
+ >>> import pyadps
399
+ >>> ds = pyadps.ReadFile('demo.000')
400
+ >>> name = "Echo Intensity Thresh"
401
+ >>> threshold = qc_prompt(ds, name, data)
402
+ The default threshold for echo intensity thresh is 0
403
+ Would you like to change the threshold [y/n]: y
404
+ Would you like to check the noise floor [y/n]: y
405
+ Threshold changed to 50
406
+ """
407
+
408
+ flobj = ds.fixedleader
89
409
  cutoff = 0
90
410
  if name == "Echo Intensity Thresh":
91
411
  cutoff = 0
@@ -1,28 +1,106 @@
1
1
  from itertools import groupby
2
+ from pygeomag import GeoMag
2
3
 
4
+ import requests
3
5
  import numpy as np
4
6
  import scipy as sp
5
- import wmm2020
6
7
 
7
8
 
8
- def magnetic_declination(velocity, lat, lon, depth, year):
9
+ def magdec(glat, glon, alt, time):
10
+ # Selecting COF file According to given year
11
+ if time >= 2010 and time < 2030:
12
+ var = 2010 + (int(time) - 2010) // 5 * 5
13
+ file_name = "wmm/WMM_{}.COF".format(str(var))
14
+ geo_mag = GeoMag(coefficients_file=file_name)
15
+ else:
16
+ geo_mag = GeoMag("wmm/WMM_2025.COF")
17
+ result = geo_mag.calculate(glat=glat, glon=glon, alt=alt, time=time)
18
+
19
+ return [[result.d]]
20
+
21
+
22
+ def wmm2020api(lat1, lon1, year):
23
+ """
24
+ This function uses the WMM2020 API to retrieve the magnetic field values at a given location
25
+ The API need latitude, longitude and year to perform the calculation. The key in the function
26
+ must be updated time to time since the API is subjected to timely updates and the key may change.
27
+
28
+ Args:
29
+ Latitude (float)
30
+ Longitude (float)
31
+ startYear (int)
32
+
33
+ Returns:
34
+ mag -> magnetic declination at the given location in degree.
35
+ """
36
+ baseurl_wmm = (
37
+ "https://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?"
38
+ )
39
+ baseurl_igrf = (
40
+ "https://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?"
41
+ )
42
+ baseurl_emm = "https://emmcalc.geomag.info/?magneticcomponent=d&"
43
+ key = "zNEw7"
44
+ resultFormat = "json"
45
+ if year >= 2025:
46
+ baseurl = baseurl_wmm
47
+ model = "WMM"
48
+ elif year >= 2019:
49
+ baseurl = baseurl_wmm
50
+ model = "IGRF"
51
+ elif year >= 2000:
52
+ baseurl = baseurl_emm
53
+ model = "EMM"
54
+ elif year >= 1590:
55
+ baseurl = baseurl_igrf
56
+ model = "IGRF"
57
+ url = "{}model={}&lat1={}&lon1={}&key={}&startYear={}&resultFormat={}".format(
58
+ baseurl, model, lat1, lon1, key, year, resultFormat
59
+ )
60
+ response = requests.get(url)
61
+ data = response.json()
62
+ results = data["result"][0]
63
+ mag = [[results["declination"]]]
64
+
65
+ return mag
66
+
67
+
68
+ # Commentin magnetic_declination model since the method is no longer using.
69
+ # def magnetic_declination(lat, lon, depth, year):
70
+ # """
71
+ # The function calculates the magnetic declination at a given location and depth.
72
+ # using a local installation of wmm2020 model.
73
+
74
+
75
+ # Args:
76
+ # lat (parameter, float): Latitude in decimals
77
+ # lon (parameter, float): Longitude in decimals
78
+ # depth (parameter, float): depth in m
79
+ # year (parameter, integer): Year
80
+
81
+ # Returns:
82
+ # mag: Magnetic declination (degrees)
83
+ # """
84
+ # import wmm2020
85
+ # mag = wmm2020.wmm(lat, lon, depth, year)
86
+ # mag = mag.decl.data
87
+
88
+ # return mag
89
+
90
+
91
+ def velocity_modifier(velocity, mag):
9
92
  """
10
93
  The function uses magnetic declination from wmm2020 to correct
11
94
  the horizontal velocities
12
95
 
13
96
  Args:
14
- velocity (numpy array): velocity(beam, depth, time)
15
- lat (parameter, float): Latitude in decimals
16
- lon (parameter, float): Longitude in decimals
17
- depth (parameter, float): depth in m
18
- year (parameter, integer): Year
97
+ velocity (numpy array): velocity array
98
+ mag: magnetic declination (degrees)
19
99
 
20
100
  Returns:
21
101
  velocity (numpy array): Rotated velocity using magnetic declination
22
- mag: Magnetic declination (degrees)
23
102
  """
24
- mag = wmm2020.wmm(lat, lon, depth, year)
25
- mag = np.deg2rad(mag.decl.data)
103
+ mag = np.deg2rad(mag[0][0])
26
104
  velocity = np.where(velocity == -32768, np.nan, velocity)
27
105
  velocity[0, :, :] = velocity[0, :, :] * np.cos(mag) + velocity[1, :, :] * np.sin(
28
106
  mag
@@ -32,7 +110,7 @@ def magnetic_declination(velocity, lat, lon, depth, year):
32
110
  ] * np.cos(mag)
33
111
  velocity = np.where(velocity == np.nan, -32768, velocity)
34
112
 
35
- return velocity, np.rad2deg(mag)
113
+ return velocity
36
114
 
37
115
 
38
116
  def velocity_cutoff(velocity, mask, cutoff=250):
@@ -54,7 +132,7 @@ def velocity_cutoff(velocity, mask, cutoff=250):
54
132
  return mask
55
133
 
56
134
 
57
- def despike(velocity, mask, kernal_size=13, cutoff=150):
135
+ def despike(velocity, mask, kernel_size=13, cutoff=3):
58
136
  """
59
137
  Function to remove anomalous spikes in the data over a period of time.
60
138
  A median filter is used to despike the data.
@@ -62,26 +140,31 @@ def despike(velocity, mask, kernal_size=13, cutoff=150):
62
140
  Args:
63
141
  velocity (numpy array, integer): Velocity(depth, time) in mm/s
64
142
  mask (numpy array, integer): Mask file
65
- kernal_size (paramater, integer): Number of ensembles over which the spike has to be checked
66
- cutoff (parameter, integer): [TODO:description]
143
+ kernel_size (paramater, integer): Window size for rolling median filter
144
+ cutoff (parameter, integer): Number of standard deviations to identify spikes
67
145
 
68
146
  Returns:
69
147
  mask
70
148
  """
71
- cutoff = cutoff * 10
72
149
  velocity = np.where(velocity == -32768, np.nan, velocity)
73
150
  shape = np.shape(velocity)
74
151
  for j in range(shape[0]):
75
- filt = sp.signal.medfilt(velocity[j, :], kernal_size)
152
+ # Apply median filter
153
+ filt = sp.signal.medfilt(velocity[j, :], kernel_size)
154
+ # Calculate absolute deviation from the rolling median
76
155
  diff = np.abs(velocity[j, :] - filt)
77
- mask[j, :] = np.where(diff < cutoff, mask[j, :], 1)
156
+ # Calculate threshold for spikes based on standard deviation
157
+ std_dev = np.nanstd(diff)
158
+ spike_threshold = cutoff * std_dev
159
+ # Apply mask after identifying spikes
160
+ mask[j, :] = np.where(diff < spike_threshold, mask[j, :], 1)
78
161
  return mask
79
162
 
80
163
 
81
164
  def flatline(
82
165
  velocity,
83
166
  mask,
84
- kernal_size=4,
167
+ kernel_size=4,
85
168
  cutoff=1,
86
169
  ):
87
170
  """
@@ -91,7 +174,7 @@ def flatline(
91
174
  Args:
92
175
  velocity (numpy arrray, integer): Velocity (depth, time)
93
176
  mask (numpy array, integer): Mask file
94
- kernal_size (parameter, integer): No. of ensembles over which flatline has to be detected
177
+ kernel_size (parameter, integer): No. of ensembles over which flatline has to be detected
95
178
  cutoff (parameter, integer): Permitted deviation in velocity
96
179
 
97
180
  Returns:
@@ -108,7 +191,7 @@ def flatline(
108
191
  for k, g in groupby(dummymask):
109
192
  # subset_size = sum(1 for i in g)
110
193
  subset_size = len(list(g))
111
- if k == 1 and subset_size >= kernal_size:
194
+ if k == 1 and subset_size >= kernel_size:
112
195
  mask[j, index : index + subset_size] = 1
113
196
  index = index + subset_size
114
197
  dummymask = np.zeros(shape[1])