Alaea 2.6.7__tar.gz → 2.6.9__tar.gz
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.
- alaea-2.6.9/.VERSION +1 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/baysalt/erosion_coastline.py +166 -41
- {alaea-2.6.7 → alaea-2.6.9/Alaea.egg-info}/PKG-INFO +58 -1
- {alaea-2.6.7 → alaea-2.6.9}/Alaea.egg-info/SOURCES.txt +1 -0
- {alaea-2.6.7 → alaea-2.6.9}/ChangeLog.md +15 -0
- {alaea-2.6.7/Alaea.egg-info → alaea-2.6.9}/PKG-INFO +58 -1
- {alaea-2.6.7 → alaea-2.6.9}/README.md +57 -0
- {alaea-2.6.7 → alaea-2.6.9}/setup.py +1 -1
- alaea-2.6.9/tests/test_erosion_dataarray.py +270 -0
- alaea-2.6.7/.VERSION +0 -1
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Blogging.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Pyshell/RS_File.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Pyshell/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/S_DateTime.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/SameAsMATLAB/CoorTrans.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/SameAsMATLAB/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/SameAsMATLAB/calculatefuns.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/vars_name.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/xr_atmos.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/xr_attrs.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/xr_ice.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/xr_netcdf.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/xr_vars.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/xr_wave.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/Standard/xr_wind.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/baysalt/Forage/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/baysalt/Forage/erosion_data/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/baysalt/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/commonCode.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/cprintfs.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/exutils/Interp/Interp_g2s.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/exutils/Interp/Interp_s2g.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/exutils/Interp/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/exutils/Interp/interpolate.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/exutils/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/netcdf_input.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/pFVCOM/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/pFVCOM/fvcom_class.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/pFVCOM/fvcom_image.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/pFVCOM/fvcom_load.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/pFVCOM/fvcom_tools.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/pFVCOM/writeFile.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/processBar.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/read_conf.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/server_info.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/storage.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/tools/P_params.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/tools/Read.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/tools/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/tools/download_check.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/tools/geo_distance.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/tools/kml2shp.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea/warningTools/__init__.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea.egg-info/dependency_links.txt +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea.egg-info/requires.txt +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/Alaea.egg-info/top_level.txt +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/LICENSE +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/MANIFEST.in +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/pyproject.toml +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/setup.cfg +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/tests/test_atmos_weather.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/tests/test_core.py +0 -0
- {alaea-2.6.7 → alaea-2.6.9}/tests/test_logging_multiprocess.py +0 -0
alaea-2.6.9/.VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v2.6.9
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
"""
|
|
17
17
|
import os
|
|
18
18
|
import time
|
|
19
|
-
import warnings
|
|
20
19
|
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
|
|
21
20
|
from typing import List
|
|
22
21
|
|
|
@@ -29,6 +28,7 @@ __all__ = [
|
|
|
29
28
|
'LOGGER',
|
|
30
29
|
'erosion_cal_id',
|
|
31
30
|
'erosion_via_id',
|
|
31
|
+
'erosion_fill_dataarray',
|
|
32
32
|
'erosion_cal_id_multi_times',
|
|
33
33
|
'erosion_via_id_multi_times',
|
|
34
34
|
'erosion_via_id_multi_vars',
|
|
@@ -276,56 +276,181 @@ def judge_nan_txtmask(latli, lonli, value, k, judge_num):
|
|
|
276
276
|
return latli_2[select_mask], lonli_2[select_mask]
|
|
277
277
|
|
|
278
278
|
|
|
279
|
-
def erosion_via_id(_weight, value):
|
|
280
|
-
"""
|
|
281
|
-
根据 erosion_cal_id 生成的索引说明书,对目标点执行回填。
|
|
282
|
-
|
|
283
|
-
核心思想:
|
|
284
|
-
- 对每个待修改点,取出它对应邻居点的值
|
|
285
|
-
- 对这些邻居值做平均(np.nanmean 会自动跳过 NaN)
|
|
286
|
-
- 再把平均值写回目标点
|
|
279
|
+
def erosion_via_id(_weight, value, is_direction=False, min_valid=None):
|
|
280
|
+
"""根据预先计算的邻居索引回填NumPy数组,支持任意前导维。
|
|
287
281
|
|
|
288
|
-
|
|
282
|
+
先筛选满足有效供点数的邻域再求均值,不对空邻域归约。
|
|
283
|
+
默认行为保持普通邻域均值及空邻域的NaN/掩膜结果;方向及有效阈值由调用方显式启用。
|
|
289
284
|
|
|
290
|
-
:param _weight: erosion_cal_id
|
|
291
|
-
:param value:
|
|
292
|
-
:
|
|
285
|
+
:param _weight: dict,erosion_cal_id返回的目标点和邻居索引。
|
|
286
|
+
:param value: numpy.ndarray或MaskedArray,末两维为纬度、经度;不修改输入。
|
|
287
|
+
:param is_direction: bool,默认False;True时按角度制单位向量平均,方向相消写为NaN。
|
|
288
|
+
整型方向输入提升为Float64,浮点输入保持类型。
|
|
289
|
+
:param min_valid: int或None,默认None保持旧接口行为;正整数时逐帧检查有限邻居数,
|
|
290
|
+
不足则保留目标原值。方向相消且启用阈值时同样保留目标原值。
|
|
291
|
+
:return: numpy.ndarray或MaskedArray,同形状回填副本;标量默认保持原类型。
|
|
292
|
+
:raises ValueError: 输入不足二维、方向开关非布尔或邻居阈值不是正整数。
|
|
293
293
|
"""
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
294
|
+
if not isinstance(is_direction, (bool, np.bool_)):
|
|
295
|
+
raise ValueError('is_direction must be boolean')
|
|
296
|
+
if min_valid is not None:
|
|
297
|
+
if isinstance(min_valid, (bool, np.bool_)) or not isinstance(min_valid, (int, float, np.integer, np.floating)) or not np.isfinite(min_valid) or int(min_valid) != min_valid or min_valid < 1:
|
|
298
|
+
raise ValueError('min_valid must be a positive integer or None')
|
|
299
|
+
min_valid = int(min_valid)
|
|
301
300
|
value = np.asanyarray(value)
|
|
302
301
|
if value.ndim < 2:
|
|
303
302
|
raise ValueError('value must have latitude and longitude as its last two dimensions')
|
|
304
|
-
if
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
# 拷贝一份,避免直接改原数组
|
|
303
|
+
if is_direction and not np.issubdtype(value.dtype, np.floating):
|
|
304
|
+
value = value.astype(np.float64)
|
|
308
305
|
new = value.copy()
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
#
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
306
|
+
if len(_weight['change_lat']) == 0:
|
|
307
|
+
return new
|
|
308
|
+
value_lat = np.asarray(_weight['value_lat'], dtype=np.intp)
|
|
309
|
+
value_lon = np.asarray(_weight['value_lon'], dtype=np.intp)
|
|
310
|
+
change_lat = np.asarray(_weight['change_lat'], dtype=np.intp)
|
|
311
|
+
change_lon = np.asarray(_weight['change_lon'], dtype=np.intp)
|
|
312
|
+
samples = value[..., value_lat, value_lon]
|
|
313
|
+
if is_direction or min_valid is not None:
|
|
314
|
+
samples = np.ma.filled(np.asanyarray(samples, dtype=np.float64), np.nan)
|
|
315
|
+
samples = np.where(np.isfinite(samples), samples, np.nan)
|
|
316
|
+
# 先筛选合格邻域,避免对全空或供点不足的行求均值;不修改线程共享的 warning 过滤器。
|
|
317
|
+
if is_direction or min_valid is not None:
|
|
318
|
+
valid_count = np.isfinite(samples).sum(axis=-1)
|
|
319
|
+
else:
|
|
320
|
+
valid_count = (~np.ma.getmaskarray(samples) & ~np.isnan(np.ma.getdata(samples))).sum(axis=-1)
|
|
321
|
+
usable = valid_count >= (1 if min_valid is None else min_valid)
|
|
322
|
+
replacement = value[..., change_lat, change_lon].copy()
|
|
323
|
+
if min_valid is None:
|
|
324
|
+
empty = ~usable
|
|
325
|
+
if np.ma.isMaskedArray(samples):
|
|
326
|
+
all_masked = np.ma.getmaskarray(samples).all(axis=-1)
|
|
327
|
+
replacement[empty & all_masked] = np.ma.masked
|
|
328
|
+
empty &= ~all_masked
|
|
329
|
+
if np.any(empty):
|
|
330
|
+
replacement[empty] = np.nan
|
|
331
|
+
if np.any(usable):
|
|
332
|
+
selected = samples[usable]
|
|
333
|
+
# 保持原邻居轴的连续性,避免 float16 标量均值因归约顺序变化而改变舍入结果。
|
|
334
|
+
if samples.strides[-1] != samples.dtype.itemsize:
|
|
335
|
+
selected = np.array(selected, order='F', subok=True)
|
|
336
|
+
with np.errstate(invalid='ignore', over='ignore'):
|
|
337
|
+
if is_direction:
|
|
338
|
+
radians = np.deg2rad(selected)
|
|
339
|
+
sine = np.nanmean(np.sin(radians), axis=-1)
|
|
340
|
+
cosine = np.nanmean(np.cos(radians), axis=-1)
|
|
341
|
+
has_direction = np.hypot(sine, cosine) > 1e-12
|
|
342
|
+
calculated = np.where(has_direction, np.rad2deg(np.arctan2(sine, cosine)) % 360, np.nan)
|
|
343
|
+
calculated = calculated.astype(value.dtype) % value.dtype.type(360)
|
|
344
|
+
if min_valid is not None:
|
|
345
|
+
calculated = np.ma.where(has_direction, calculated, replacement[usable]) if np.ma.isMaskedArray(value) else np.where(has_direction, calculated, replacement[usable])
|
|
346
|
+
else:
|
|
347
|
+
calculated = np.nanmean(selected, axis=-1)
|
|
348
|
+
replacement[usable] = calculated
|
|
349
|
+
new[..., change_lat, change_lon] = replacement
|
|
326
350
|
return new
|
|
327
351
|
|
|
328
352
|
|
|
353
|
+
def erosion_fill_dataarray(value, _times, k, judge_num, is_direction=False, weight_cache=None, reference=None):
|
|
354
|
+
"""按静态缺测边界对二维或时间序列 DataArray 做有限轮回填。
|
|
355
|
+
|
|
356
|
+
默认只填整个输入时间轴中始终无有效值的格点;指定reference时只使用参考场的缺测边界。
|
|
357
|
+
保留已有值,参考场有效区域内的目标变量缺测不会被填补。
|
|
358
|
+
每轮复用 erosion_cal_id,并在每帧回填时重新检查有效邻居数量。
|
|
359
|
+
经度/纬度使用现有平面距离,不进行球面修正或日期变更线绕回。
|
|
360
|
+
|
|
361
|
+
:param value: xarray.DataArray,浮点二维格点或三维时间序列;末两维为纬度、经度,
|
|
362
|
+
且各自具备有限、不重复的一维数字坐标。三维首维为时间,不改变其时区或值。
|
|
363
|
+
:param _times: int,回填轮数,必须非负;0返回原DataArray,不读取数据。
|
|
364
|
+
:param k: int,近邻总数(包含目标自身),至少2;小网格自动截到格点总数。
|
|
365
|
+
:param judge_num: int,每帧至少需要的有效邻居数,满足1 <= judge_num < k。
|
|
366
|
+
:param is_direction: bool,默认False使用算术平均;True按角度制圆周平均,向量抵消仍缺测。
|
|
367
|
+
:param weight_cache: dict或None,默认每次创建独立缓存;可在多个变量间显式复用,
|
|
368
|
+
键绑定坐标、掩膜和参数,调用方负责在批次结束后释放。
|
|
369
|
+
:param reference: xarray.DataArray或None,默认按value计算边界;可显式传入同网格二维浮点
|
|
370
|
+
参考场,有限值表示有效区域、非有限值表示待填区域。其他变量共用该场时
|
|
371
|
+
即可复用相同索引,但仍按各自邻居的有效值回填。
|
|
372
|
+
:return: xarray.DataArray,维度顺序、坐标、属性和输入浮点类型保持;Dask仅立即计算二维
|
|
373
|
+
静态掩膜,按原时间分块延迟回填,空间维合并为完整网格;不修改输入。
|
|
374
|
+
:raises TypeError: 输入不是DataArray、浮点数组,或缓存不是dict。
|
|
375
|
+
:raises ValueError: 参数、维度或坐标不符合上述合同。
|
|
376
|
+
"""
|
|
377
|
+
import xarray as xr
|
|
378
|
+
|
|
379
|
+
for name, number, minimum in (('_times', _times, 0), ('k', k, 2), ('judge_num', judge_num, 1)):
|
|
380
|
+
if isinstance(number, (bool, np.bool_)) or not isinstance(number, (int, float, np.integer, np.floating)) or not np.isfinite(number) or int(number) != number or number < minimum:
|
|
381
|
+
raise ValueError(f'{name} must be an integer >= {minimum}')
|
|
382
|
+
_times, k, judge_num = int(_times), int(k), int(judge_num)
|
|
383
|
+
if judge_num >= k or not isinstance(is_direction, (bool, np.bool_)):
|
|
384
|
+
raise ValueError('judge_num must be less than k and is_direction must be boolean')
|
|
385
|
+
if not isinstance(value, xr.DataArray) or not np.issubdtype(value.dtype, np.floating):
|
|
386
|
+
raise TypeError('value must be a floating-point xarray.DataArray')
|
|
387
|
+
if value.ndim not in (2, 3) or any(size == 0 for size in value.shape):
|
|
388
|
+
raise ValueError('value must be a non-empty spatial grid or time/spatial grid')
|
|
389
|
+
if weight_cache is not None and not isinstance(weight_cache, dict):
|
|
390
|
+
raise TypeError('weight_cache must be a dict or None')
|
|
391
|
+
if _times == 0:
|
|
392
|
+
return value
|
|
393
|
+
latitude_dim, longitude_dim = value.dims[-2:]
|
|
394
|
+
coordinates = []
|
|
395
|
+
for dimension in (latitude_dim, longitude_dim):
|
|
396
|
+
if dimension not in value.coords or value.coords[dimension].dims != (dimension,):
|
|
397
|
+
raise ValueError(f'{dimension} requires a one-dimensional coordinate')
|
|
398
|
+
coordinate = np.asarray(value.coords[dimension].values, dtype=np.float64)
|
|
399
|
+
if not np.isfinite(coordinate).all() or np.unique(coordinate).size != coordinate.size:
|
|
400
|
+
raise ValueError(f'{dimension} coordinates must be finite and unique')
|
|
401
|
+
coordinates.append(coordinate)
|
|
402
|
+
latitudes, longitudes = coordinates
|
|
403
|
+
if reference is not None:
|
|
404
|
+
if not isinstance(reference, xr.DataArray) or not np.issubdtype(reference.dtype, np.floating):
|
|
405
|
+
raise TypeError('reference must be a floating-point xarray.DataArray or None')
|
|
406
|
+
if reference.dims != value.dims[-2:] or reference.shape != value.shape[-2:]:
|
|
407
|
+
raise ValueError('reference must have the same spatial dimensions as value')
|
|
408
|
+
for dimension in reference.dims:
|
|
409
|
+
if dimension not in reference.coords or not np.array_equal(reference.coords[dimension].values, value.coords[dimension].values):
|
|
410
|
+
raise ValueError('reference spatial coordinates must match value exactly')
|
|
411
|
+
valid = np.isfinite(reference).compute().values
|
|
412
|
+
else:
|
|
413
|
+
finite = np.isfinite(value)
|
|
414
|
+
valid = (finite.any(value.dims[0]) if value.ndim == 3 else finite).compute().values
|
|
415
|
+
if valid.all() or not valid.any() or valid.size <= judge_num:
|
|
416
|
+
return value
|
|
417
|
+
if weight_cache is None:
|
|
418
|
+
weight_cache = {}
|
|
419
|
+
key = (_times, k, judge_num, latitudes.tobytes(), longitudes.tobytes(), valid.tobytes())
|
|
420
|
+
if key not in weight_cache:
|
|
421
|
+
reference = np.where(valid, 1.0, np.nan)
|
|
422
|
+
weights = []
|
|
423
|
+
for _ in range(_times):
|
|
424
|
+
weight = erosion_cal_id(longitudes, latitudes, reference, k, judge_num, method='np.nan')
|
|
425
|
+
if not weight['change_lat']:
|
|
426
|
+
break
|
|
427
|
+
weight = {name: np.asarray(indices, dtype=np.intp) for name, indices in weight.items()}
|
|
428
|
+
weights.append(weight)
|
|
429
|
+
reference = erosion_via_id(weight, reference)
|
|
430
|
+
weight_cache[key] = weights
|
|
431
|
+
weights = weight_cache[key]
|
|
432
|
+
if not weights:
|
|
433
|
+
return value
|
|
434
|
+
|
|
435
|
+
def fill_block(values):
|
|
436
|
+
"""依次回填一个空间完整的NumPy块,不改变输入。
|
|
437
|
+
|
|
438
|
+
:param values: numpy.ndarray,二维格点或按原时间分块的三维浮点数组。
|
|
439
|
+
:return: numpy.ndarray,同形状、同类型的回填结果。
|
|
440
|
+
:raises ValueError: 底层回填参数或数组形状不符合合同。
|
|
441
|
+
"""
|
|
442
|
+
result = values
|
|
443
|
+
for weight in weights:
|
|
444
|
+
filled = erosion_via_id(weight, result, is_direction=is_direction, min_valid=judge_num)
|
|
445
|
+
result = np.where(np.isfinite(result), result, filled)
|
|
446
|
+
return result
|
|
447
|
+
|
|
448
|
+
prepared = value.chunk({latitude_dim: -1, longitude_dim: -1}) if value.chunks is not None else value
|
|
449
|
+
result = xr.apply_ufunc(fill_block, prepared, input_core_dims=[[latitude_dim, longitude_dim]], output_core_dims=[[latitude_dim, longitude_dim]], dask='parallelized', output_dtypes=[value.dtype], keep_attrs=True)
|
|
450
|
+
result.encoding = value.encoding.copy()
|
|
451
|
+
return result
|
|
452
|
+
|
|
453
|
+
|
|
329
454
|
def erosion_cal_id_multi_times(_times: int,
|
|
330
455
|
g_lon,
|
|
331
456
|
g_lat,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: Alaea
|
|
3
|
-
Version: 2.6.
|
|
3
|
+
Version: 2.6.9
|
|
4
4
|
Summary: Some simple tools by Christmas
|
|
5
5
|
Author: Christmas
|
|
6
6
|
Author-email: 273519355@qq.com
|
|
@@ -63,6 +63,63 @@ does not attempt upload; a failed upload retains the new archives for diagnosis
|
|
|
63
63
|
and retry. Directly rerunning the script skips files already present on PyPI and
|
|
64
64
|
continues uploading the remaining archives.
|
|
65
65
|
|
|
66
|
+
## Coastline boundary filling (2.6.8)
|
|
67
|
+
|
|
68
|
+
`Alaea.baysalt.erosion_coastline.erosion_fill_dataarray` fills persistent
|
|
69
|
+
missing boundaries in a floating-point xarray DataArray. The last two
|
|
70
|
+
axes must be latitude and longitude with finite, unique 1-D coordinates;
|
|
71
|
+
a 3-D array uses its first axis as time. Dimension names may vary.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from Alaea.baysalt.erosion_coastline import erosion_fill_dataarray
|
|
75
|
+
|
|
76
|
+
filled = erosion_fill_dataarray(wave, _times=1, k=16, judge_num=3)
|
|
77
|
+
direction = erosion_fill_dataarray(wave_direction, _times=1, k=16, judge_num=3, is_direction=True)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Only cells missing throughout the supplied time series are candidates.
|
|
81
|
+
Existing values and transient gaps are preserved, and each frame must meet
|
|
82
|
+
`judge_num` valid neighbors. `k` includes the target itself; `_times=0`
|
|
83
|
+
disables filling. Directions use circular averaging and remain missing
|
|
84
|
+
when the mean direction is undefined. Distances use the existing planar
|
|
85
|
+
longitude/latitude metric, without periodic seam wrapping; this is numerical
|
|
86
|
+
boundary extrapolation, not a physical erosion model.
|
|
87
|
+
|
|
88
|
+
Dask inputs retain time chunks and fill lazily after the 2-D validity mask
|
|
89
|
+
is computed; each spatial grid is rechunked as a whole. Callers can share
|
|
90
|
+
`weight_cache={}` within one batch of variables. Cache keys include the
|
|
91
|
+
coordinates, mask and parameters, and the caller releases the cache at the
|
|
92
|
+
end of the batch. No global cache or new dependency is added.
|
|
93
|
+
|
|
94
|
+
To apply one set of significant-wave-height boundary indices to wind sea,
|
|
95
|
+
swell, directions and periods, pass the same 2-D `reference` and batch cache:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
import numpy as np
|
|
99
|
+
import xarray as xr
|
|
100
|
+
|
|
101
|
+
reference = xr.where(np.isfinite(swh).any("time"), 1.0, np.nan).compute()
|
|
102
|
+
weights = {}
|
|
103
|
+
filled_height = erosion_fill_dataarray(swh, 1, 16, 3, reference=reference, weight_cache=weights)
|
|
104
|
+
filled_swell = erosion_fill_dataarray(swell, 1, 16, 3, reference=reference, weight_cache=weights)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The reference must have exactly the same spatial dimensions and coordinates.
|
|
108
|
+
Its finite cells define the existing domain; only its missing boundary cells
|
|
109
|
+
can become targets. Component-only holes within the valid reference domain
|
|
110
|
+
are preserved. Every field keeps existing valid values, checks its own donor
|
|
111
|
+
count and uses its own donor values; directions still require circular means.
|
|
112
|
+
Providing a reference does not replace missing component values with total
|
|
113
|
+
wave-height values. Omitting it preserves the field-specific default above.
|
|
114
|
+
|
|
115
|
+
For NumPy or masked arrays, `erosion_via_id(weights, values,
|
|
116
|
+
is_direction=True, min_valid=3)` exposes the same circular mean and
|
|
117
|
+
per-frame threshold. Omitting the new options preserves existing behavior.
|
|
118
|
+
|
|
119
|
+
These APIs require Alaea 2.6.8 or newer. Earlier versions do not include
|
|
120
|
+
`erosion_fill_dataarray` or the new optional direction/threshold arguments.
|
|
121
|
+
Update downstream dependency constraints and lock files when upgrading.
|
|
122
|
+
|
|
66
123
|
## Standard atmosphere datasets
|
|
67
124
|
|
|
68
125
|
`xr_atmos_dataset` builds any supported combination of wind, 2 metre air
|
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## v2.6.9
|
|
4
|
+
|
|
5
|
+
- 版本从2.6.8提升到2.6.9。源码包与wheel的版本、Python>=3.9、文件清单、隐藏元数据及Twine检查通过;隔离安装wheel后的14项岸边回归通过。
|
|
6
|
+
|
|
7
|
+
- 岸边填补先筛选满足有效供点数的邻域,再计算算术/圆周均值,避免空邻域 `Mean of empty slice` 警告和无效计算;移除该路径对进程共享 warning 过滤器的修改。有效点不足仍保留原值,旧接口空邻域的 NaN/掩膜结果、方向抵消规则、类型与共享权重保持。 保留旧 float16 标量归约的舍入行为;新增空邻域、阈值、掩膜、多线程和精度回归,Alaea 64 项测试、GFS/ECMWF 各 5 项集成测试及 2,592 组新旧数值对照通过。
|
|
8
|
+
|
|
9
|
+
## v2.6.8
|
|
10
|
+
|
|
11
|
+
- 发布前源码包与wheel通过版本、Python下限、文件清单、隐藏元数据和Twine检查;独立临时目录安装wheel后,共享参考接口最小验证通过。
|
|
12
|
+
|
|
13
|
+
- 新增`erosion_fill_dataarray`:支持二维/三维浮点DataArray、Dask时间分块、已有值与缺测保护,以及按坐标/掩膜/参数绑定的显式共享索引缓存;不增加依赖或全局缓存。
|
|
14
|
+
- 可传入同网格二维`reference`,让有效波高swh统一计算邻居索引后供全部波场复用;各变量只使用自己的有效邻居,保留swh有效海域内的风浪/涌浪独有缺测。GFS和ECMWF共用参数次数1/查询16点/至少3有效点。
|
|
15
|
+
- `erosion_via_id`新增可选圆周平均及逐帧有效邻居阈值;原调用默认算术均值保持,方向抵消、邻居不足与MaskedArray掩膜受保护。
|
|
16
|
+
- 本库60项、GFS107项、ECMWF86项源码回归全部通过(合计253项),覆盖旧接口、共享参考与索引复用、分区空洞、Dask分块、Int16写盘回读和发布状态失效。既有经纬度平面距离与不跨日期变更线绕回的限制保持。
|
|
17
|
+
|
|
3
18
|
## v2.6.7
|
|
4
19
|
|
|
5
20
|
- 22种标准大气变量补齐纯英文物理定义(按用户要求不写入中文或 `_zh` 属性)、单位含义、时间语义、物理范围和缺测说明;降水类型增加ECMWF/ecCodes 0–14完整分类表及保留/本地/缺测码说明。CAPE气块定义、LI温差与CIN源符号约定明确区分,属性可经两种NetCDF引擎完整写入和读回,不修改原数组;本次本库完整50项回归通过。
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: Alaea
|
|
3
|
-
Version: 2.6.
|
|
3
|
+
Version: 2.6.9
|
|
4
4
|
Summary: Some simple tools by Christmas
|
|
5
5
|
Author: Christmas
|
|
6
6
|
Author-email: 273519355@qq.com
|
|
@@ -63,6 +63,63 @@ does not attempt upload; a failed upload retains the new archives for diagnosis
|
|
|
63
63
|
and retry. Directly rerunning the script skips files already present on PyPI and
|
|
64
64
|
continues uploading the remaining archives.
|
|
65
65
|
|
|
66
|
+
## Coastline boundary filling (2.6.8)
|
|
67
|
+
|
|
68
|
+
`Alaea.baysalt.erosion_coastline.erosion_fill_dataarray` fills persistent
|
|
69
|
+
missing boundaries in a floating-point xarray DataArray. The last two
|
|
70
|
+
axes must be latitude and longitude with finite, unique 1-D coordinates;
|
|
71
|
+
a 3-D array uses its first axis as time. Dimension names may vary.
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from Alaea.baysalt.erosion_coastline import erosion_fill_dataarray
|
|
75
|
+
|
|
76
|
+
filled = erosion_fill_dataarray(wave, _times=1, k=16, judge_num=3)
|
|
77
|
+
direction = erosion_fill_dataarray(wave_direction, _times=1, k=16, judge_num=3, is_direction=True)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Only cells missing throughout the supplied time series are candidates.
|
|
81
|
+
Existing values and transient gaps are preserved, and each frame must meet
|
|
82
|
+
`judge_num` valid neighbors. `k` includes the target itself; `_times=0`
|
|
83
|
+
disables filling. Directions use circular averaging and remain missing
|
|
84
|
+
when the mean direction is undefined. Distances use the existing planar
|
|
85
|
+
longitude/latitude metric, without periodic seam wrapping; this is numerical
|
|
86
|
+
boundary extrapolation, not a physical erosion model.
|
|
87
|
+
|
|
88
|
+
Dask inputs retain time chunks and fill lazily after the 2-D validity mask
|
|
89
|
+
is computed; each spatial grid is rechunked as a whole. Callers can share
|
|
90
|
+
`weight_cache={}` within one batch of variables. Cache keys include the
|
|
91
|
+
coordinates, mask and parameters, and the caller releases the cache at the
|
|
92
|
+
end of the batch. No global cache or new dependency is added.
|
|
93
|
+
|
|
94
|
+
To apply one set of significant-wave-height boundary indices to wind sea,
|
|
95
|
+
swell, directions and periods, pass the same 2-D `reference` and batch cache:
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
import numpy as np
|
|
99
|
+
import xarray as xr
|
|
100
|
+
|
|
101
|
+
reference = xr.where(np.isfinite(swh).any("time"), 1.0, np.nan).compute()
|
|
102
|
+
weights = {}
|
|
103
|
+
filled_height = erosion_fill_dataarray(swh, 1, 16, 3, reference=reference, weight_cache=weights)
|
|
104
|
+
filled_swell = erosion_fill_dataarray(swell, 1, 16, 3, reference=reference, weight_cache=weights)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The reference must have exactly the same spatial dimensions and coordinates.
|
|
108
|
+
Its finite cells define the existing domain; only its missing boundary cells
|
|
109
|
+
can become targets. Component-only holes within the valid reference domain
|
|
110
|
+
are preserved. Every field keeps existing valid values, checks its own donor
|
|
111
|
+
count and uses its own donor values; directions still require circular means.
|
|
112
|
+
Providing a reference does not replace missing component values with total
|
|
113
|
+
wave-height values. Omitting it preserves the field-specific default above.
|
|
114
|
+
|
|
115
|
+
For NumPy or masked arrays, `erosion_via_id(weights, values,
|
|
116
|
+
is_direction=True, min_valid=3)` exposes the same circular mean and
|
|
117
|
+
per-frame threshold. Omitting the new options preserves existing behavior.
|
|
118
|
+
|
|
119
|
+
These APIs require Alaea 2.6.8 or newer. Earlier versions do not include
|
|
120
|
+
`erosion_fill_dataarray` or the new optional direction/threshold arguments.
|
|
121
|
+
Update downstream dependency constraints and lock files when upgrading.
|
|
122
|
+
|
|
66
123
|
## Standard atmosphere datasets
|
|
67
124
|
|
|
68
125
|
`xr_atmos_dataset` builds any supported combination of wind, 2 metre air
|
|
@@ -26,6 +26,63 @@ does not attempt upload; a failed upload retains the new archives for diagnosis
|
|
|
26
26
|
and retry. Directly rerunning the script skips files already present on PyPI and
|
|
27
27
|
continues uploading the remaining archives.
|
|
28
28
|
|
|
29
|
+
## Coastline boundary filling (2.6.8)
|
|
30
|
+
|
|
31
|
+
`Alaea.baysalt.erosion_coastline.erosion_fill_dataarray` fills persistent
|
|
32
|
+
missing boundaries in a floating-point xarray DataArray. The last two
|
|
33
|
+
axes must be latitude and longitude with finite, unique 1-D coordinates;
|
|
34
|
+
a 3-D array uses its first axis as time. Dimension names may vary.
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from Alaea.baysalt.erosion_coastline import erosion_fill_dataarray
|
|
38
|
+
|
|
39
|
+
filled = erosion_fill_dataarray(wave, _times=1, k=16, judge_num=3)
|
|
40
|
+
direction = erosion_fill_dataarray(wave_direction, _times=1, k=16, judge_num=3, is_direction=True)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Only cells missing throughout the supplied time series are candidates.
|
|
44
|
+
Existing values and transient gaps are preserved, and each frame must meet
|
|
45
|
+
`judge_num` valid neighbors. `k` includes the target itself; `_times=0`
|
|
46
|
+
disables filling. Directions use circular averaging and remain missing
|
|
47
|
+
when the mean direction is undefined. Distances use the existing planar
|
|
48
|
+
longitude/latitude metric, without periodic seam wrapping; this is numerical
|
|
49
|
+
boundary extrapolation, not a physical erosion model.
|
|
50
|
+
|
|
51
|
+
Dask inputs retain time chunks and fill lazily after the 2-D validity mask
|
|
52
|
+
is computed; each spatial grid is rechunked as a whole. Callers can share
|
|
53
|
+
`weight_cache={}` within one batch of variables. Cache keys include the
|
|
54
|
+
coordinates, mask and parameters, and the caller releases the cache at the
|
|
55
|
+
end of the batch. No global cache or new dependency is added.
|
|
56
|
+
|
|
57
|
+
To apply one set of significant-wave-height boundary indices to wind sea,
|
|
58
|
+
swell, directions and periods, pass the same 2-D `reference` and batch cache:
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
import numpy as np
|
|
62
|
+
import xarray as xr
|
|
63
|
+
|
|
64
|
+
reference = xr.where(np.isfinite(swh).any("time"), 1.0, np.nan).compute()
|
|
65
|
+
weights = {}
|
|
66
|
+
filled_height = erosion_fill_dataarray(swh, 1, 16, 3, reference=reference, weight_cache=weights)
|
|
67
|
+
filled_swell = erosion_fill_dataarray(swell, 1, 16, 3, reference=reference, weight_cache=weights)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The reference must have exactly the same spatial dimensions and coordinates.
|
|
71
|
+
Its finite cells define the existing domain; only its missing boundary cells
|
|
72
|
+
can become targets. Component-only holes within the valid reference domain
|
|
73
|
+
are preserved. Every field keeps existing valid values, checks its own donor
|
|
74
|
+
count and uses its own donor values; directions still require circular means.
|
|
75
|
+
Providing a reference does not replace missing component values with total
|
|
76
|
+
wave-height values. Omitting it preserves the field-specific default above.
|
|
77
|
+
|
|
78
|
+
For NumPy or masked arrays, `erosion_via_id(weights, values,
|
|
79
|
+
is_direction=True, min_valid=3)` exposes the same circular mean and
|
|
80
|
+
per-frame threshold. Omitting the new options preserves existing behavior.
|
|
81
|
+
|
|
82
|
+
These APIs require Alaea 2.6.8 or newer. Earlier versions do not include
|
|
83
|
+
`erosion_fill_dataarray` or the new optional direction/threshold arguments.
|
|
84
|
+
Update downstream dependency constraints and lock files when upgrading.
|
|
85
|
+
|
|
29
86
|
## Standard atmosphere datasets
|
|
30
87
|
|
|
31
88
|
`xr_atmos_dataset` builds any supported combination of wind, 2 metre air
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""Alaea岸边回填公共API的NumPy/xarray/Dask保护回归。"""
|
|
2
|
+
|
|
3
|
+
import importlib
|
|
4
|
+
import unittest
|
|
5
|
+
import warnings
|
|
6
|
+
from unittest import mock
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
import pandas as pd
|
|
10
|
+
import xarray as xr
|
|
11
|
+
from Alaea.baysalt.erosion_coastline import erosion_fill_dataarray, erosion_via_id
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def field(values):
|
|
15
|
+
"""建立time/latitude/longitude合成场,不访问外部数据。
|
|
16
|
+
|
|
17
|
+
:param values: np.ndarray,三维浮点数值。
|
|
18
|
+
:return: xr.DataArray,内部时间统一表示UTC,不做时区转换。
|
|
19
|
+
"""
|
|
20
|
+
return xr.DataArray(values, dims=('time', 'latitude', 'longitude'), coords={'time': pd.date_range('2026-01-01', periods=len(values), freq='2h'), 'latitude': np.arange(values.shape[1]), 'longitude': np.arange(values.shape[2])})
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ErosionDataArrayTest(unittest.TestCase):
|
|
24
|
+
"""保护旧调用默认值,同时验证新入口的通用边界。"""
|
|
25
|
+
|
|
26
|
+
def test_scalar_boundary_iterations_and_transient_gaps(self):
|
|
27
|
+
"""填补静态边界并保留临时缺测、原值及远处空白;返回 None。"""
|
|
28
|
+
values = np.full((3, 9, 9), np.nan, dtype=np.float32)
|
|
29
|
+
values[:2, :, :4] = 2
|
|
30
|
+
values[1, 4, 2] = np.nan
|
|
31
|
+
source = field(values).chunk({'time': 1, 'latitude': 3, 'longitude': 3})
|
|
32
|
+
result = erosion_fill_dataarray(source, 1, 9, 3, False, {}).compute()
|
|
33
|
+
self.assertEqual(float(result[0, 4, 4]), 2)
|
|
34
|
+
self.assertTrue(np.isnan(result[0, 4, 5]))
|
|
35
|
+
self.assertTrue(np.isnan(result[1, 4, 2]))
|
|
36
|
+
self.assertTrue(np.isnan(result[2]).all())
|
|
37
|
+
np.testing.assert_array_equal(result.values[np.isfinite(values)], values[np.isfinite(values)])
|
|
38
|
+
expanded = erosion_fill_dataarray(source, 2, 9, 3, False, {}).compute()
|
|
39
|
+
self.assertEqual(float(expanded[0, 4, 5]), 2)
|
|
40
|
+
self.assertTrue(np.isnan(expanded[0, 4, 7]))
|
|
41
|
+
np.testing.assert_array_equal(source.compute(), values)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_minimum_neighbors_checked_each_time(self):
|
|
45
|
+
"""静态索引不能让临时失去邻居的时刻绕过阈值;返回 None。"""
|
|
46
|
+
values = np.full((2, 9, 9), np.nan, dtype=np.float32)
|
|
47
|
+
values[:, :, :4] = 2
|
|
48
|
+
values[1, 3:6, 3] = np.nan
|
|
49
|
+
result = erosion_fill_dataarray(field(values), 1, 9, 3, False, {})
|
|
50
|
+
self.assertEqual(float(result[0, 4, 4]), 2)
|
|
51
|
+
self.assertTrue(np.isnan(result[1, 4, 4]))
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def test_direction_crosses_north_and_opposites_remain_missing(self):
|
|
55
|
+
"""邻域方向绕过北向,完全相反的方向不生成虚构角度;返回 None。"""
|
|
56
|
+
values = np.full((1, 3, 3), np.nan, dtype=np.float32)
|
|
57
|
+
values[0, 0, 0], values[0, 1, 0] = 359, 1
|
|
58
|
+
result = erosion_fill_dataarray(field(values), 1, 9, 2, True, {})
|
|
59
|
+
self.assertLess(min(abs(float(result[0, 1, 1])), abs(float(result[0, 1, 1]) - 360)), 1e-4)
|
|
60
|
+
values[0, 0, 0], values[0, 1, 0] = 90, 270
|
|
61
|
+
opposite = erosion_fill_dataarray(field(values), 1, 9, 2, True, {})
|
|
62
|
+
self.assertTrue(np.isnan(opposite[0, 1, 1]))
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_shared_mask_reuses_alaea_indices_and_all_missing_is_unchanged(self):
|
|
66
|
+
"""同一标准化批次按静态掩膜共享索引;返回 None。"""
|
|
67
|
+
values = np.full((1, 5, 5), np.nan, dtype=np.float32)
|
|
68
|
+
values[:, :, :2] = 2
|
|
69
|
+
module = importlib.import_module('Alaea.baysalt.erosion_coastline')
|
|
70
|
+
cache = {}
|
|
71
|
+
with mock.patch.object(module, 'erosion_cal_id', wraps=module.erosion_cal_id) as calculate:
|
|
72
|
+
erosion_fill_dataarray(field(values), 1, 9, 3, False, cache)
|
|
73
|
+
erosion_fill_dataarray(field(values * 2), 1, 9, 3, False, cache)
|
|
74
|
+
self.assertEqual(calculate.call_count, 1)
|
|
75
|
+
empty = field(np.full((1, 5, 5), np.nan, dtype=np.float32))
|
|
76
|
+
self.assertIs(erosion_fill_dataarray(empty, 1, 9, 3, False, {}), empty)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_validation_and_disabled_operation(self):
|
|
80
|
+
"""参数、形状、坐标和缓存不合法时拒绝,关闭时返回原场;返回None。"""
|
|
81
|
+
source = field(np.ones((1, 3, 3), dtype=np.float32))
|
|
82
|
+
self.assertIs(erosion_fill_dataarray(source, 0, 16, 3), source)
|
|
83
|
+
for args in ((-1, 16, 3), (1.5, 16, 3), (True, 16, 3), (1, 1, 1), (1, 3, 3), (1, 16, 0), (1, float('inf'), 3)):
|
|
84
|
+
with self.subTest(args=args), self.assertRaises(ValueError):
|
|
85
|
+
erosion_fill_dataarray(source, *args)
|
|
86
|
+
with self.assertRaises(TypeError):
|
|
87
|
+
erosion_fill_dataarray(np.ones((3, 3)), 1, 16, 3)
|
|
88
|
+
with self.assertRaises(TypeError):
|
|
89
|
+
erosion_fill_dataarray(source.astype(int), 1, 16, 3)
|
|
90
|
+
with self.assertRaises(TypeError):
|
|
91
|
+
erosion_fill_dataarray(source, 1, 16, 3, weight_cache=[])
|
|
92
|
+
for coordinates in ([0, 0, 1], [0, np.nan, 2]):
|
|
93
|
+
with self.subTest(coordinates=coordinates), self.assertRaises(ValueError):
|
|
94
|
+
erosion_fill_dataarray(source.assign_coords(longitude=coordinates), 1, 16, 3)
|
|
95
|
+
|
|
96
|
+
def test_two_dimensions_arbitrary_dimension_names_and_metadata(self):
|
|
97
|
+
"""二维规则网格支持任意维名,保留坐标、属性和编码;返回None。"""
|
|
98
|
+
values = np.ones((1, 3, 3), dtype=np.float32)
|
|
99
|
+
values[0, 1, 1] = np.nan
|
|
100
|
+
source = field(values).isel(time=0, drop=True).rename(latitude='y', longitude='x')
|
|
101
|
+
source.attrs['units'] = 'm'
|
|
102
|
+
source.encoding['dtype'] = 'float32'
|
|
103
|
+
result = erosion_fill_dataarray(source, 1, 16, 3)
|
|
104
|
+
self.assertEqual(float(result[1, 1]), 1)
|
|
105
|
+
self.assertEqual(result.dims, source.dims)
|
|
106
|
+
self.assertEqual(result.attrs, source.attrs)
|
|
107
|
+
self.assertEqual(result.encoding, source.encoding)
|
|
108
|
+
self.assertTrue(np.isnan(source[1, 1]))
|
|
109
|
+
|
|
110
|
+
def test_cache_identity_includes_grid_and_settings(self):
|
|
111
|
+
"""跨网格或参数不能复用错误索引,标量和方向可共享几何索引;返回None。"""
|
|
112
|
+
values = np.ones((1, 3, 3), dtype=np.float32)
|
|
113
|
+
values[0, 1, 1] = np.nan
|
|
114
|
+
source = field(values)
|
|
115
|
+
cache = {}
|
|
116
|
+
module = importlib.import_module('Alaea.baysalt.erosion_coastline')
|
|
117
|
+
with mock.patch.object(module, 'erosion_cal_id', wraps=module.erosion_cal_id) as calculate:
|
|
118
|
+
erosion_fill_dataarray(source, 1, 9, 3, weight_cache=cache)
|
|
119
|
+
erosion_fill_dataarray(source, 1, 9, 3, is_direction=True, weight_cache=cache)
|
|
120
|
+
erosion_fill_dataarray(source.assign_coords(longitude=[0, 1, 10]), 1, 9, 3, weight_cache=cache)
|
|
121
|
+
erosion_fill_dataarray(source, 1, 8, 3, weight_cache=cache)
|
|
122
|
+
self.assertEqual(calculate.call_count, 3)
|
|
123
|
+
|
|
124
|
+
def test_numpy_direction_and_per_frame_threshold_are_public(self):
|
|
125
|
+
"""NumPy调用也可直接启用圆周均值和逐帧阈值;返回None。"""
|
|
126
|
+
weight = {'change_lat': [1], 'change_lon': [1], 'value_lat': [[0, 0]], 'value_lon': [[0, 1]]}
|
|
127
|
+
values = np.full((2, 2, 2), np.nan, dtype=np.float32)
|
|
128
|
+
values[0, 0] = [359, 1]
|
|
129
|
+
values[1, 0] = [359, np.nan]
|
|
130
|
+
result = erosion_via_id(weight, values, is_direction=True, min_valid=2)
|
|
131
|
+
self.assertLess(float(result[0, 1, 1]), 1e-4)
|
|
132
|
+
self.assertTrue(np.isnan(result[1, 1, 1]))
|
|
133
|
+
legacy = erosion_via_id(weight, values)
|
|
134
|
+
self.assertEqual(float(legacy[0, 1, 1]), 180)
|
|
135
|
+
self.assertEqual(float(legacy[1, 1, 1]), 359)
|
|
136
|
+
values[0, 0] = [90, 270]
|
|
137
|
+
self.assertTrue(np.isnan(erosion_via_id(weight, values, is_direction=True)[0, 1, 1]))
|
|
138
|
+
masked = np.ma.masked_invalid(values)
|
|
139
|
+
self.assertTrue(np.ma.is_masked(erosion_via_id(weight, masked, min_valid=2)[1, 1, 1]))
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_reductions_skip_insufficient_neighbors_without_changing_warning_filters(self):
|
|
143
|
+
"""四维输入仅归约合格邻域;全空、稀疏邻域及方向抵消保留原值。
|
|
144
|
+
|
|
145
|
+
:return: None;均值收到不合格邻域、全局过滤器变化或数值不正确时断言失败。
|
|
146
|
+
"""
|
|
147
|
+
weight = {'change_lat': [1], 'change_lon': [1], 'value_lat': [[0, 0, 0]], 'value_lon': [[0, 1, 2]]}
|
|
148
|
+
values = np.full((2, 2, 2, 3), np.nan, dtype=np.float32)
|
|
149
|
+
values.reshape(4, 2, 3)[:, 0] = [[359, 0, 1], [np.nan, np.nan, np.nan], [30, np.nan, np.nan], [0, 120, 240]]
|
|
150
|
+
values.reshape(4, 2, 3)[:, 1, 1] = [8, 7, 11, 17]
|
|
151
|
+
original = values.copy()
|
|
152
|
+
original_mean = np.nanmean
|
|
153
|
+
for direction in (False, True):
|
|
154
|
+
with self.subTest(direction=direction), warnings.catch_warnings():
|
|
155
|
+
warnings.simplefilter('error', RuntimeWarning)
|
|
156
|
+
filters_before = warnings.filters.copy()
|
|
157
|
+
|
|
158
|
+
def checked_mean(samples, axis):
|
|
159
|
+
"""检查归约输入与线程共享的警告过滤器,再调用 NumPy 均值。
|
|
160
|
+
|
|
161
|
+
:param samples: numpy.ndarray,仅包含满足最少供点数的邻域。
|
|
162
|
+
:param axis: int,邻居所在的归约轴。
|
|
163
|
+
:return: numpy.ndarray,独立 NumPy 算出的邻域均值。
|
|
164
|
+
"""
|
|
165
|
+
self.assertEqual(samples.shape, (2, 3))
|
|
166
|
+
self.assertTrue(np.all(np.isfinite(samples).sum(axis=axis) >= 3))
|
|
167
|
+
self.assertEqual(warnings.filters, filters_before)
|
|
168
|
+
return original_mean(samples, axis=axis)
|
|
169
|
+
|
|
170
|
+
with mock.patch.object(np, 'nanmean', side_effect=checked_mean):
|
|
171
|
+
result = erosion_via_id(weight, values, is_direction=direction, min_valid=3)
|
|
172
|
+
targets = result.reshape(4, 2, 3)[:, 1, 1]
|
|
173
|
+
self.assertEqual(result.dtype, values.dtype)
|
|
174
|
+
np.testing.assert_array_equal(targets[1:3], [7, 11])
|
|
175
|
+
if direction:
|
|
176
|
+
self.assertLess(min(abs(float(targets[0])), abs(float(targets[0]) - 360)), 1e-4)
|
|
177
|
+
self.assertEqual(targets[3], 17)
|
|
178
|
+
else:
|
|
179
|
+
np.testing.assert_allclose(targets[[0, 3]], [120, 120])
|
|
180
|
+
np.testing.assert_array_equal(values, original)
|
|
181
|
+
with mock.patch.object(np, 'nanmean', side_effect=AssertionError('ineligible neighborhood reduced')):
|
|
182
|
+
np.testing.assert_array_equal(erosion_via_id(weight, values, min_valid=4), values)
|
|
183
|
+
|
|
184
|
+
def test_empty_legacy_neighbors_keep_nan_and_mask_semantics_without_warnings(self):
|
|
185
|
+
"""旧标量接口保留全空结果与全掩膜结果,整数和浮点类型不变。
|
|
186
|
+
|
|
187
|
+
:return: None;空均值产生警告、旧接口数值或掩膜语义变化时断言失败。
|
|
188
|
+
"""
|
|
189
|
+
weight = {'change_lat': [1], 'change_lon': [1], 'value_lat': [[0, 0]], 'value_lon': [[0, 1]]}
|
|
190
|
+
with warnings.catch_warnings():
|
|
191
|
+
warnings.simplefilter('error', RuntimeWarning)
|
|
192
|
+
for direction in (False, True):
|
|
193
|
+
values = np.full((2, 2), np.nan, dtype=np.float32)
|
|
194
|
+
values[1, 1] = 7
|
|
195
|
+
result = erosion_via_id(weight, values, is_direction=direction)
|
|
196
|
+
self.assertTrue(np.isnan(result[1, 1]))
|
|
197
|
+
self.assertEqual(values[1, 1], 7)
|
|
198
|
+
masked = np.ma.array([[1, 2], [3, 4]], mask=[[True, True], [False, False]])
|
|
199
|
+
result = erosion_via_id(weight, masked)
|
|
200
|
+
self.assertTrue(np.ma.is_masked(result[1, 1]))
|
|
201
|
+
self.assertEqual(result.dtype, masked.dtype)
|
|
202
|
+
partial = np.ma.array([[np.nan, 2], [3, 4]], mask=[[False, True], [False, False]])
|
|
203
|
+
result = erosion_via_id(weight, partial)
|
|
204
|
+
self.assertFalse(np.ma.is_masked(result[1, 1]))
|
|
205
|
+
self.assertTrue(np.isnan(result[1, 1]))
|
|
206
|
+
|
|
207
|
+
def test_legacy_float16_preserves_reduction_rounding(self):
|
|
208
|
+
"""筛选邻域后仍保持旧 float16 标量接口的求和顺序与舍入结果。
|
|
209
|
+
|
|
210
|
+
:return: None;连续/跨步邻居轴的结果与原归约不同则断言失败。
|
|
211
|
+
"""
|
|
212
|
+
weight = {'change_lat': [1], 'change_lon': [0], 'value_lat': [[0, 0, 0, 0]], 'value_lon': [[0, 1, 2, 3]]}
|
|
213
|
+
values = np.zeros((2, 2, 4), dtype=np.float16)
|
|
214
|
+
values[:, 0] = [10.195, 44.75, 138.1, np.nan]
|
|
215
|
+
for source in (values, values[0], np.ma.array(values, mask=False)):
|
|
216
|
+
with self.subTest(shape=source.shape, masked=np.ma.isMaskedArray(source)):
|
|
217
|
+
expected = np.nanmean(source[..., weight['value_lat'], weight['value_lon']], axis=-1)
|
|
218
|
+
result = erosion_via_id(weight, source)
|
|
219
|
+
np.testing.assert_array_equal(result[..., weight['change_lat'], weight['change_lon']], expected)
|
|
220
|
+
|
|
221
|
+
def test_threaded_direction_blocks_allow_entirely_missing_component_frames(self):
|
|
222
|
+
"""共享 swh 索引的 Dask 多线程方向回填不对全空分量帧发出警告。
|
|
223
|
+
|
|
224
|
+
:return: None;合法岸边填补错误、全空帧被填值或运行警告出现时断言失败。
|
|
225
|
+
"""
|
|
226
|
+
heights = np.full((1, 9, 9), np.nan, dtype=np.float32)
|
|
227
|
+
heights[:, :, :4] = 1
|
|
228
|
+
reference = field(heights).isel(time=0, drop=True)
|
|
229
|
+
values = np.full((12, 9, 9), np.nan, dtype=np.float32)
|
|
230
|
+
values[::3, :, :4] = 45
|
|
231
|
+
with warnings.catch_warnings(record=True) as captured:
|
|
232
|
+
warnings.simplefilter('always', RuntimeWarning)
|
|
233
|
+
result = erosion_fill_dataarray(field(values).chunk({'time': 1}), 1, 16, 3, is_direction=True, reference=reference).compute(scheduler='threads', num_workers=4)
|
|
234
|
+
self.assertFalse([item for item in captured if issubclass(item.category, RuntimeWarning)])
|
|
235
|
+
np.testing.assert_allclose(result.values[::3, 4, 4], 45)
|
|
236
|
+
self.assertTrue(np.isnan(result.values[np.arange(12) % 3 != 0]).all())
|
|
237
|
+
|
|
238
|
+
def test_shared_reference_does_not_fill_component_only_ocean_holes(self):
|
|
239
|
+
"""全部变量复用swh索引,分区独有海上缺测不成为填补目标;返回None。"""
|
|
240
|
+
heights = np.full((2, 9, 9), np.nan, dtype=np.float32)
|
|
241
|
+
heights[:, :, :4] = 1
|
|
242
|
+
reference = field(heights).isel(time=0, drop=True)
|
|
243
|
+
components = heights * 2
|
|
244
|
+
components[:, 4, 2] = np.nan
|
|
245
|
+
components[:, 0, 4] = 7
|
|
246
|
+
module = importlib.import_module('Alaea.baysalt.erosion_coastline')
|
|
247
|
+
cache = {}
|
|
248
|
+
with mock.patch.object(module, 'erosion_cal_id', wraps=module.erosion_cal_id) as calculate:
|
|
249
|
+
erosion_fill_dataarray(field(heights), 1, 16, 3, weight_cache=cache, reference=reference)
|
|
250
|
+
result = erosion_fill_dataarray(field(components).chunk({'time': 1}), 1, 16, 3, weight_cache=cache, reference=reference).compute()
|
|
251
|
+
absent = erosion_fill_dataarray(field(np.full_like(components, np.nan)), 1, 16, 3, weight_cache=cache, reference=reference)
|
|
252
|
+
self.assertEqual(calculate.call_count, 1)
|
|
253
|
+
self.assertTrue(np.isnan(result[:, 4, 2]).all())
|
|
254
|
+
np.testing.assert_array_equal(result[:, 0, 4], [7, 7])
|
|
255
|
+
np.testing.assert_array_equal(result[:, 4, 4], [2, 2])
|
|
256
|
+
self.assertTrue(np.isnan(absent).all())
|
|
257
|
+
|
|
258
|
+
def test_reference_grid_must_match_and_valid_sea_stays_missing(self):
|
|
259
|
+
"""参考场必须精确对齐;参考场为有效海域时不填分区空洞;返回None。"""
|
|
260
|
+
source = field(np.full((1, 3, 3), np.nan, dtype=np.float32))
|
|
261
|
+
reference = field(np.ones((1, 3, 3), dtype=np.float32)).isel(time=0, drop=True)
|
|
262
|
+
self.assertIs(erosion_fill_dataarray(source, 1, 16, 3, reference=reference), source)
|
|
263
|
+
with self.assertRaises(ValueError):
|
|
264
|
+
erosion_fill_dataarray(source, 1, 16, 3, reference=reference.assign_coords(longitude=[1, 2, 3]))
|
|
265
|
+
with self.assertRaises(ValueError):
|
|
266
|
+
erosion_fill_dataarray(source, 1, 16, 3, reference=reference.expand_dims(time=[0]))
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
if __name__ == '__main__':
|
|
270
|
+
unittest.main()
|
alaea-2.6.7/.VERSION
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
v2.6.7
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|