Author: Abdullah A. Fahad (a.fahad@nasa.gov)
import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
!pip install cartopy
import cartopy.crs as ccrs
!wget https://downloads.psl.noaa.gov/Datasets/noaa.oisst.v2.highres/sst.mon.mean.nc
# Read the data using xarray
ds = xr.open_dataset('/content/sst.mon.mean.nc').sel(time=slice('1982','2022'))
#ds = xr.decode_cf(ds, use_cftime=True) # Ignore this line. It is only needed if time coordinates are given in floating data and you need to convert it to cftime format
ds
# converting the data to numpy array
lon=ds.lon.data
lat=ds.lat.data
time=ds.time.data
nlon=len(lon); nlat=len(lat); nt=len(time)
sst=ds.sst.data
sst.shape
nmonth=12
nyear=nt//nmonth
year=np.arange(1982,2022+1)
#reshaping data to year, mon, lat, lon
sst=np.reshape(sst,(nyear,nmonth,nlat,nlon))
sst.shape
#making annual mean data by taking average ver the month dimension
sst_annual=np.nanmean(sst,1)
sst_annual.shape
#plotting Climatology of Annual mean SST
plt.figure(figsize=(10, 6))
plt.contourf(lon, lat, np.nanmean(sst_annual,0),cmap='YlOrRd')
plt.colorbar(label='SST')
plt.title('Sea Surface Temperature')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()
## function for linear regression
from scipy import stats
from numpy import *
def l_trend(var,lon,lat,time,sig=False):
nlon=len(lon)
nlat=len(lat)
nt=len(time)
vart=zeros(nlat*nlon)
varp=zeros(nlat*nlon)
if len(var.shape)== 3:
var=reshape(var,(nt,nlat*nlon))
print('l_trend: assuming variable as 3D [time,lat,lon]')
for i in range(nlat*nlon):
v=var[:,i]
vart[i], intercept, r_value, varp[i], std_err=stats.linregress(time,v)
vart=reshape(vart,(nlat,nlon))
varp=reshape(varp,(nlat,nlon))
#return (vart,varp)
else:
raise ValueError('Variable shape is not 2D or 3D. plese instert variable in this format var[time,lat,lon] or var[time,lon*lat]')
if sig==False:
return (vart, varp)
else:
for i in range(nlat):
for j in range (nlon):
if varp[i,j]>sig:
vart[i,j]=nan
return (vart, varp)
## calculating SST linear trend and their pvalues
SST_trend, SST_pvalue=l_trend(sst_annual, lon, lat, time=year)
#plotting SST annual mean 41 years of trend, stippling pvalue where it is significant at 95%
plt.figure(figsize=(10, 6))
plt.contourf(lon,lat,SST_pvalue,[0,0.05],colors='none',hatches='...')
plt.contourf(lon, lat, SST_trend*41,cmap='coolwarm',levels=np.arange(-2,2.1,.25),extend='both',alpha=.85)
plt.colorbar(label='degree C')
plt.title('Sea Surface Temperature Trend per 41 years')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.show()