Climatology & Anomalies#

Climatology, Anomalies, Standard Deviation (Monthly)#

In this section, we compute monthly climatology, anomalies, and standard deviation using two different approaches. These diagnostics are foundational for understanding climate variability and change.


Method 1: Using groupby#

# --- Climatology, Anomalies, and Standard Deviation ---

# Compute monthly climatology
clm1 = dat.groupby("time.month").mean(dim="time")

# Compute monthly anomalies
anm1 = dat.groupby("time.month") - clm1

# Compute monthly standard deviation
std1 = anm1.groupby("time.month").std(dim="time")

# Check dimensions
print(clm1.sizes)
print(anm1.sizes)
print(std1.sizes)

This method leverages xarray’s groupby functionality and is concise and efficient. The result retains the full time dimension for anomaly analysis.


Method 2: Using stack and unstack#

# Add year and month as coordinates
dat = dat.assign_coords(year=dat.time.dt.year, month=dat.time.dt.month)

# Reshape into (year, month, lat, lon)
dat4D = dat.set_index(time=["year", "month"]).unstack("time")
dat4D = dat4D.transpose("year", "month", "lat", "lon")
print(dat4D.sizes)

# Compute monthly climatology
clm2 = dat4D.mean(dim="year")

# Compute anomalies
anm2 = dat4D - clm2

# Compute standard deviation
std2 = anm2.std(dim="year")

# Check dimensions
print(clm2.sizes)
print(anm2.sizes)
print(std2.sizes)

This method explicitly separates year and month dimensions, which is helpful for advanced applications such as composites or month-wise trend analysis.


Both methods produce equivalent results. Choose the one that fits your analysis goals.