"""TRMM 3B42 daily V7 precipitation on the 1 deg OLR CDR grid, all days, as CESM PRECT (m s-1). Source: GES DISC TRMM_3B42_Daily.7 (downloaded by trmm_download.sh to scratch), variable 'precipitation' (combined microwave-IR with gauge calibration over land, mm per day, dims lon, lat; 0.25 deg, 50S-50N). Each daily file covers 01:30 UTC that day to 01:30 UTC the next day (3-hourly 3B42 03..21 UTC and next 00 UTC); it is stamped here as that date (noon). Regridding: each 1x1 deg cell is the area-weighted mean of the 4x4 TRMM cells inside it (the grids nest exactly); missing TRMM cells are left out; a 1 deg cell with no valid TRMM cell is missing. Poleward of 50 deg is missing. Longitudes shifted from -180..180 to 0..360. Output: data/PRECT.trmm_3b42v7.day.nc Usage: python -I trmm_daily.py [NDAYS for testing -> ./trmm_test.nc]. Written 2026-11-12. """ import datetime as dt import glob import os import sys import netCDF4 import numpy as np BASE = "/glade/work/bundy/mdtf/obs_wk_mjo" SRC = "/glade/derecho/scratch/bundy/trmm_3b42_daily_v7" FILL = 1.0e20 def main(): ndays = int(sys.argv[1]) if len(sys.argv) > 1 else None out = f"{BASE}/data/PRECT.trmm_3b42v7.day.nc" if ndays is None else "trmm_test.nc" if os.path.exists(out): sys.exit(f"{out} exists, nothing to do") files = sorted(glob.glob(f"{SRC}/*/3B42_Daily.*.7.nc4"))[:ndays] days = [dt.datetime.strptime(os.path.basename(f).split(".")[1], "%Y%m%d").date() for f in files] gaps = [(a, b) for a, b in zip(days, days[1:]) if (b - a).days != 1] if gaps: sys.exit(f"days are not consecutive: {gaps[:10]}") # area weights of the 0.25 deg cells 50S-50N, in blocks of 4 edges = np.radians(np.linspace(-50, 50, 401)) wl = (np.sin(edges[1:]) - np.sin(edges[:-1])).reshape(100, 4) pr = np.full((len(days), 180, 360), FILL, dtype="f4") nmiss = 0 for i, f in enumerate(files): with netCDF4.Dataset(f) as nc: nc.set_auto_mask(False) lat = nc.variables["lat"][:] lon = nc.variables["lon"][:] if not (abs(lat[0] + 49.875) < 1e-4 and abs(lon[0] + 179.875) < 1e-4 and len(lat) == 400 and len(lon) == 1440): sys.exit(f"{f}: unexpected grid") a = np.asarray(nc.variables["precipitation"][:], dtype="f8").T # (lat, lon) ok = a > -9000.0 a = np.where(ok, a, 0.0).reshape(100, 4, 360, 4) w = ok.reshape(100, 4, 360, 4) * wl[:, :, None, None] num = (a * w).sum(axis=(1, 3)) den = w.sum(axis=(1, 3)) m = np.where(den > 0, num / np.where(den > 0, den, 1) / 86400.0 / 1000.0, FILL) # mm/day -> m s-1 nmiss += int((den == 0).sum()) pr[i, 40:140] = np.roll(m, 180, axis=1) # rows 40..139 = 49.5S..49.5N; lon -180..180 -> 0..360 if i % 365 == 0: print(f"{dt.datetime.now():%H:%M:%S} {days[i]}", flush=True) tmp = out + ".tmp" with netCDF4.Dataset(tmp, "w") as o: o.createDimension("time", None) o.createDimension("lat", 180) o.createDimension("lon", 360) o.createDimension("nbnd", 2) t = o.createVariable("time", "f8", ("time",)) t.units, t.calendar, t.standard_name, t.axis = "days since 1970-01-01", "proleptic_gregorian", "time", "T" t.bounds = "time_bnds" base = dt.date(1970, 1, 1) t[:] = [(d - base).days + 0.5 for d in days] tb = o.createVariable("time_bnds", "f8", ("time", "nbnd")) tb[:] = [[(d - base).days, (d - base).days + 1] for d in days] la = o.createVariable("lat", "f4", ("lat",)) la.units, la.standard_name, la.axis = "degrees_north", "latitude", "Y" la[:] = np.arange(-89.5, 90.0) lo = o.createVariable("lon", "f4", ("lon",)) lo.units, lo.standard_name, lo.axis = "degrees_east", "longitude", "X" lo[:] = np.arange(0.5, 360.0) x = o.createVariable("PRECT", "f4", ("time", "lat", "lon"), zlib=True, complevel=1, fill_value=FILL) x.units, x.standard_name, x.cell_methods = "m s-1", "precipitation_rate", "time: mean" x.long_name = "Total precipitation rate (TRMM 3B42 V7 daily 'precipitation', mm/day / 86400 / 1000)" x.comment = ("TRMM covers 50S-50N only; missing poleward. Each day is 01:30 UTC to 01:30 UTC the next day " "(TRMM_3B42_Daily.7 definition).") x[:] = pr o.source = ("NASA TRMM Multi-satellite Precipitation Analysis 3B42 V7 daily (TRMM_3B42_Daily.7, GES DISC); " "0.25 deg cells area-averaged onto the 1x1 deg NOAA OLR CDR grid") os.rename(tmp, out) print(f"DONE {out}: {len(days)} days {days[0]}..{days[-1]}, all-missing 1 deg cells in 50S-50N: {nmiss}", flush=True) if __name__ == "__main__": main()