"""Post hoc addition to Test 5 (not pre-registered): the polar-cap mean of p_diff (the Mansurov sign: lower Arctic and higher Antarctic pressure on By >= 3 nT days), R1 2010-2023, against the same IAAFT null. usage: py -3.12 -I by_pressure_polar.py [n]. Needs numpy, netCDF4.""" import datetime as dt, json, os, sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import numpy as np, netCDF4 as nc import common as C dl, outdir = sys.argv[1], sys.argv[2]; NN = int(sys.argv[3]) if len(sys.argv) > 3 else 1000 rng = np.random.default_rng(2026) dates = C.daterange(dt.date(2010, 1, 1), dt.date(2023, 12, 31)); N = len(dates) _, _, by = C.read_omni_daily(os.path.join(dl, 'omni', 'omni_01_av.dat'), 2010, 2023) P = [] for y in range(2010, 2024): f = nc.Dataset(os.path.join(dl, 'ncep-r1', f'pres.sfc.{y}.nc')) lat = np.array(f.variables['lat'][:], dtype=float) P.append(np.array(f.variables['pres'][:], dtype=np.float64) * 0.01); f.close() P = np.concatenate(P); nlat = len(lat); X = P.reshape(N, -1) doy = np.array([d.timetuple().tm_yday for d in dates], float) G = np.array([np.ones(N)] + [g for k in range(1, 5) for g in (np.cos(2*np.pi*k*doy/365.25), np.sin(2*np.pi*k*doy/365.25))]).T A = X - G @ np.linalg.lstsq(G, X, rcond=None)[0] nlon = X.shape[1] // nlat cl = np.cos(np.deg2rad(lat)) capN = lat >= 70; capS = lat <= -70 def capmeans(field): f = field.reshape(nlat, nlon).mean(axis=1) return (float((f[capN]*cl[capN]).sum()/cl[capN].sum()), float((f[capS]*cl[capS]).sum()/cl[capS].sum())) def w_of(b): w = np.zeros(N); p = b >= 3; m = b <= -3 w[p] = 1/p.sum(); w[m] = -1/m.sum(); return w def iaaft(x, it=50): xs = np.sort(x); amp = np.abs(np.fft.rfft(x)); y = rng.permutation(x) for _ in range(it): Y = np.fft.rfft(y); y = np.fft.irfft(amp*np.exp(1j*np.angle(Y)), n=len(x)); y = xs[np.argsort(np.argsort(y))] return y obs = capmeans(w_of(by) @ A) W = np.array([w_of(iaaft(by)) for _ in range(NN)]) F = W @ A nul = np.array([capmeans(F[i]) for i in range(NN)]) res = {'cap_N_ge70_obs_hPa': round(obs[0], 3), 'cap_S_le-70_obs_hPa': round(obs[1], 3), 'p_capN_negative_one_sided': round(float((1+np.sum(nul[:, 0] <= obs[0]))/(1+NN)), 4), 'p_capS_positive_one_sided': round(float((1+np.sum(nul[:, 1] >= obs[1]))/(1+NN)), 4), 'null_sd_capN': round(float(nul[:, 0].std()), 3), 'null_sd_capS': round(float(nul[:, 1].std()), 3), 'n_null': NN} json.dump(res, open(os.path.join(outdir, 'by_pressure_polar.json'), 'w', encoding='utf-8', newline='\n'), indent=1) print(json.dumps(res, indent=1))