|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import xarray as xr |
| 4 | +import numpy as np |
| 5 | + |
| 6 | + |
| 7 | +def global_mean(variable): |
| 8 | + """ |
| 9 | + Description: compute the global mean (weigthed) of a variable |
| 10 | + Argument: |
| 11 | + ds: variable |
| 12 | + Return: |
| 13 | + ds_ann: annual mean |
| 14 | + """ |
| 15 | + weights = np.cos(np.deg2rad(variable.lat)) |
| 16 | + variable_weighted = variable.weighted(weights) |
| 17 | + variable_mean = variable_weighted.mean(("lon", "lat")) |
| 18 | + return variable_mean |
| 19 | + |
| 20 | + |
| 21 | +def compute_ann_mean(filepath, case, var, lat1=None, lat2=None, lon1=None, lon2=None): |
| 22 | + """ |
| 23 | + Description: Compute the annual mean within a specified latitude (lat1-lat2) |
| 24 | + and longitude (lon1-lon2) range of the variable "var". |
| 25 | +
|
| 26 | + Arguments: |
| 27 | + filepath = filepath |
| 28 | + case = casename |
| 29 | + var = variable name |
| 30 | + lat1 = starting latitude |
| 31 | + lat2 = ending latitude |
| 32 | + lon1 = starting longitude |
| 33 | + lon2 = ending longitude |
| 34 | + """ |
| 35 | + filename = filepath + case + "/" + case + "." + var + ".nc" |
| 36 | + ds = xr.open_dataset(filename) |
| 37 | + |
| 38 | + # Select the subset of data within the specified latitude and longitude ranges before calculating the annual mean |
| 39 | + ds_subset = ds.sel(lat=slice(lat1, lat2), lon=slice(lon1, lon2)) |
| 40 | + |
| 41 | + var_ann = ds_subset[var].groupby("time.year").mean() |
| 42 | + var_lat_lon_ann = lat_lon_mean(var_ann, lat1, lat2, lon1, lon2) |
| 43 | + |
| 44 | + return var_lat_lon_ann |
| 45 | + |
| 46 | + |
| 47 | +def compute_var_g_ann(filepath, case, var): |
| 48 | + """ |
| 49 | + Compute the weighted annual global mean of |
| 50 | + a given variable. |
| 51 | + Argument: |
| 52 | + case = casename |
| 53 | + var = variable name |
| 54 | + """ |
| 55 | + filename = filepath + case + "/" + case + "." + var + ".nc" |
| 56 | + ds = xr.open_dataset(filename) |
| 57 | + return global_mean(ds[var].groupby("time.year").mean()) |
| 58 | + |
| 59 | + |
| 60 | +def lat_lon_mean(variable, lat1, lat2, lon1, lon2): |
| 61 | + """ |
| 62 | + Description: Compute the mean (weighted by cosine of latitude) of a variable |
| 63 | + within a specified latitude (lat1-lat2) and longitude (lon1-lon2) range. |
| 64 | +
|
| 65 | + Arguments: |
| 66 | + variable: xarray DataArray representing the variable to be averaged. |
| 67 | + lat1: Starting latitude for the range. |
| 68 | + lat2: Ending latitude for the range. |
| 69 | + lon1: Starting longitude for the range. |
| 70 | + lon2: Ending longitude for the range. |
| 71 | +
|
| 72 | + Return: |
| 73 | + variable_mean: The mean value of the variable within the specified lat/lon range. |
| 74 | + """ |
| 75 | + # Select the subset of data within the specified latitude and longitude ranges |
| 76 | + variable_subset = variable.sel(lat=slice(lat1, lat2), lon=slice(lon1, lon2)) |
| 77 | + |
| 78 | + # Compute weights as the cosine of the latitude, adjusted for the selected range |
| 79 | + weights = np.cos(np.deg2rad(variable_subset.lat)) |
| 80 | + |
| 81 | + # Apply weighted mean over the subset |
| 82 | + variable_weighted = variable_subset.weighted(weights) |
| 83 | + variable_mean = variable_weighted.mean(("lon", "lat")) |
| 84 | + |
| 85 | + return variable_mean |
| 86 | + |
| 87 | + |
| 88 | +def lat_lon_mean_norm(variable, lat1, lat2, lon1, lon2): |
| 89 | + # TODO: This could be combined with lat_lon_mean by adding a normalize_weights argument |
| 90 | + """ |
| 91 | + Description: Compute the mean (weighted by cosine of latitude) of a variable |
| 92 | + within a specified latitude (lat1-lat2) and longitude (lon1-lon2) range. |
| 93 | +
|
| 94 | + Arguments: |
| 95 | + variable: xarray DataArray representing the variable to be averaged. |
| 96 | + lat1: Starting latitude for the range. |
| 97 | + lat2: Ending latitude for the range. |
| 98 | + lon1: Starting longitude for the range. |
| 99 | + lon2: Ending longitude for the range. |
| 100 | +
|
| 101 | + Return: |
| 102 | + variable_mean: The mean value of the variable within the specified lat/lon range. |
| 103 | + """ |
| 104 | + # Select the data subset within the specified latitude and longitude ranges |
| 105 | + variable_subset = variable.sel(lat=slice(lat1, lat2), lon=slice(lon1, lon2)) |
| 106 | + |
| 107 | + # Compute weights as the cosine of the latitude, adjusted for the selected range |
| 108 | + # Ensure weights are normalized (sum to 1) over the selected latitude range for accurate weighting |
| 109 | + latitudes = variable_subset.lat |
| 110 | + weights = np.cos(np.deg2rad(latitudes)) |
| 111 | + weights /= weights.sum(dim="lat") |
| 112 | + |
| 113 | + # Apply weighted mean over the subset |
| 114 | + variable_weighted = variable_subset.weighted(weights) |
| 115 | + variable_mean = variable_weighted.mean(("lon", "lat")) |
| 116 | + |
| 117 | + return variable_mean |
| 118 | + |
| 119 | + |
| 120 | +def compute_var_zonal_ann(filepath, case, var): |
| 121 | + """ |
| 122 | + Description: compute the annual global mean (weighted) of |
| 123 | + a variable var |
| 124 | + Argument: |
| 125 | + case = casename |
| 126 | + var = variable name |
| 127 | + """ |
| 128 | + filename = filepath + case + "/" + case + "." + var + ".nc" |
| 129 | + ds = xr.open_dataset(filename) |
| 130 | + ds.mean(["lon"]) |
| 131 | + var_zonal_ann = ds[var].groupby("time.year").mean().mean(["lon"]) |
| 132 | + return var_zonal_ann |
0 commit comments