DOCUMENT CONTENT
文档内容
"""Case 03 core algorithms: percentile heat extremes."""
from __future__ import annotations
import numpy as np
def percentile_threshold(values, q=90, axis=0): return np.nanpercentile(np.asarray(values,float),q,axis=axis)
def heatwave_mask(values, threshold, min_duration=3):
x=np.asarray(values,float); th=np.asarray(threshold,float); raw=x>th
out=np.zeros_like(raw,bool)
for j in range(x.shape[1]):
start=0
while start<x.shape[0]:
if not raw[start,j]: start+=1; continue
end=start
while end<x.shape[0] and raw[end,j]: end+=1
if end-start>=min_duration: out[start:end,j]=True
start=end
return out
def longest_run(mask):
x=np.asarray(mask,bool); out=[]
for j in range(x.shape[1]):
best=cur=0
for v in x[:,j]: cur=cur+1 if v else 0; best=max(best,cur)
out.append(best)
return np.asarray(out)