public
Created

Comparison of Northern Hemisphere Perennial and Seasonal Sea Ice

  • Download Gist
ice-code.R
R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
ssq = function (x) {sum(x ^ 2)}
 
plotTrend = function (x, st = 1978, en = c(2012, 12), y.pos = NA, x.pos = NA, main.t = "Untitled") {
 
### Get trend
trend = lm(window(x, st, en) ~ I(time(window(x, st, en))))
 
### Initialize variables
N = length(window(x, st, en))
I = seq(1:N) / frequency(x)
rmI = ssq(I - mean(I))
 
### Calculate sum of squared errors
SSE = ssq(trend[[2]]) / (N - 2)
 
### Calculate OLS standard errors
seOLS = sqrt(SSE / rmI) * 10
 
### Calculate two-tailed 95% CI
ci95 = seOLS * 1.964
 
### Get lag-1 ACF
resids = residuals(trend)
r1 = acf(resids, lag.max=1, plot=FALSE)$acf[[2]]
 
### Calculate CIs with Quenouille (Santer) adjustment for autocorrelation
Q = (1 - r1) / (1 + r1)
ciQ = ci95 / sqrt(Q)
 
### Plot data
plot(window(x, st, en), main=main.t, ylab="Anomaly Km^2")
grid()
 
### Add trendline and x-axis
abline(h = 0, col = 2)
abline(trend, col = 4, lwd = 2)
 
### Annotate text
text(x = ifelse(is.na(x.pos), st, x.pos), y = ifelse(is.na(y.pos), max(x, na.rm = TRUE) - 0.2 * max(x, na.rm = TRUE), y.pos), paste("Slope: ", signif(trend$coef[2] * 10, 3), "+/-", signif(ciQ, 3), "Km^2/Decade\n(corrected for AR(1) serial correlation)\nLag-1 value:", signif(r1, 3)), cex = 0.8, col = 4, pos = 4)
 
### Return
r = list(trend$coef[2] * 10, ciQ)
}
 
 
cal.Isig=function(dat=dat, st=0,en=2020)
{
### Get trend
fm=lm(window(dat, st, en)~I(time(window(dat, st, en))))
 
### Initialize variables
N=length(window(dat, st, en))
I=seq(1:N)/frequency(dat)
rmI=ssq(I-mean(I))
 
### Calculate sum of squared errors
SSE=ssq(fm[[2]])/(N-2)
 
### Calculate OLS standard errors
seOLS=sqrt(SSE/rmI)
 
### Calculate two-tailed 95% CI
ci95=seOLS*1.964
 
### Get lag-1 ACF
resids=residuals(lm(window(dat, st, en)~seq(1:N)))
r1=acf(resids, lag.max=1, plot=FALSE)$acf[[2]]
 
### Calculate CIs with Quenouille (Santer) adjustment for autocorrelation
Q=(1-r1)/(1+r1)
ciQ=ci95/sqrt(Q)
 
return(c(fm$coef[2],ciQ))
}
 
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
##################################################################################
#calculate northern sea ice area from gridded satelite data
 
Nfilenames=list.files(path="C:/agw/sea ice data/north/", pattern = NULL, all.files = TRUE, full.names = FALSE, recursive = TRUE)
startday=as.integer(unclass(as.POSIXct(strptime("1977-12-31", '%Y-%m-%d' )))/86400)
year= substr(Nfilenames,9,12)
month=substr(Nfilenames,13,14)
day=substr(Nfilenames,15,16)
dat=paste(year,month,day,sep="-")
oday=as.integer(unclass(as.POSIXct(strptime(dat, '%Y-%m-%d' )))/86400)-startday
 
area=array(NA,dim=c(366,35))
offsetday=array(NA,dim=c(366,35))
 
#set up hole mask here
getnorthicedata=function(file=file)
{
a=file(file,"rb")
header= readBin(a,n=300,what=integer(),size=1,endian="little",signed=FALSE)
data=readBin(a,n=304*448,what=integer(),size=1,endian="little",signed=FALSE)
close(a)
data
}
 
starthmask=!(getnorthicedata(paste("C:/agw/sea ice data/north/",Nfilenames[10],sep=""))==251)
endhmask=!(getnorthicedata(paste("C:/agw/sea ice data/north/",Nfilenames[9000],sep=""))==251)
diffmask=!(starthmask==endhmask)
holemask=starthmask #insures no step
 
lat=readBin("C:/agw/sea ice data/sea ice coordinates/psn25lats_v3.dat", integer(), endian = "little",n=304*448)/100000
lon=readBin("C:/agw/sea ice data/sea ice coordinates/psn25lons_v3.dat", integer(), endian = "little",n=304*448)/100000
latmask=lat>66.5 #arctic circle
 
 
#################################
#load area values into 2d matrix 366days each year x 35 days
for(i in 1:(length(Nfilenames)-1))
{
#open filename
fn=paste("C:/agw/sea ice data/north/",Nfilenames[i],sep="")
data=getnorthicedata(fn)
print(dat[i])
#Limit data/land mask to all info <250 - 100% and >25 - 10%
datamask=data<251# & data>25
#store sum in trend value
#area[ydayindex[i],yearindex[i]]=sum(data[(datamask&holemask&latmask)])/250*625
area[ydayindex[i],yearindex[i]]=sum(data[(datamask&holemask)])/250*625
offsetday[ydayindex[i],yearindex[i]]=oday[i]
}
 
areatimeseries=area
dim(areatimeseries)=c(366*35,1)
plot(areatimeseries)
 
fareatimeseries=rep(NA,length(areatimeseries))
######filter timeseries for anomaly calculation only
######Use more recent years to avoid missing values from early record
fwid=10
for (i in 4000:length(areatimeseries))
{
if(!is.na(areatimeseries[i]))
{
miave=i-10
maave=i+10
fareatimeseries[i]=mean(areatimeseries[miave:maave],na.rm=TRUE)
}
}
 
#resequence all matrix data into time series to complete correction for leap years
dim(fareatimeseries)=c(366,35)
anomval=rowMeans(fareatimeseries,na.rm=TRUE)
NIAnom=area-anomval
NIArea=area
dim(NIAnom)=c(366*35,1)
dim(NIArea)=c(366*35,1)
dim(offsetday)=c(366*35,1)
NorthIceArea=rep(NA,366*35)
NorthIceAreaAnomaly=rep(NA,366*35)
 
for (i in 1:length(NorthIceArea))
{
if(!is.na(offsetday[i]))
{
NorthIceArea[offsetday[i]]=NIArea[i]
NorthIceAreaAnomaly[offsetday[i]]=NIAnom[i]
 
}
}
 
NorthIceArea=ts(NorthIceArea,start=1978,deltat=1/365.25) #set time series to get time values only
decdate=time(NorthIceArea) #copy time values
NorthIceArea=approx(decdate,NorthIceArea,xout=decdate) #interpolate missing values
NorthIceArea=ts(NorthIceArea$y,start=1978,deltat=1/365.25) #reassemble series
NorthIceArea=NorthIceArea + sum(!holemask)*625
NorthIceAreaAnomaly=approx(decdate,NorthIceAreaAnomaly,xout=decdate) #interpolate missing values
NorthIceAreaAnomaly=ts(NorthIceAreaAnomaly$y,start=1978,deltat=1/365.25) #reassemble series
NorthIceAreaAnomaly=NorthIceAreaAnomaly-mean(NorthIceAreaAnomaly,na.rm=TRUE) #center series
 
 
 
########## plotting calls may be a little messy due to continuous changes
plotTrend(NorthIceAreaAnomaly,y.pos=-1.4e6,main.t = "Arctic Ice Area Anomaly")
#arcticcircleanomaly=NorthIceAreaAnomaly
#arcticcirclearea=NorthIceArea
#totalanomaly=NorthIceAreaAnomaly
#totalarea=NorthIceArea
 
plotTrend(NorthIceAreaAnomaly,y.pos=-1.3e6,main.t = "Sea Ice Area North")
 
plot(arcticcirclearea,y.pos=-.7e6,main = "Arctic Circle Sea Ice Area",ylim=c(0,1.3e7),ylab="Ice Area km^2")
grid()
plotTrend(arcticcircleanomaly,y.pos=-.9e6,main.t = "Arctic Circle Sea Ice Area Anomaly")
plotTrend(NorthIceArea-above60area,y.pos=-.7e6,main.t = "Sea Ice Area Below Arctic Circle")
plotTrend(NorthIceAreaAnomaly-above60anomaly,y.pos=-.7e6,main.t = "Arctic Sea Ice Area Anomaly Below Arctic Circle")
lines(totalanomaly,col="red")
 
plotTrend(NorthIceAreaAnomaly,y.pos=-1.2e6,main.t = "Northern Hemisphere Sea Ice Area Anomaly Total vs Non-Arctic")
lines(NorthIceAreaAnomaly-above60anomaly,col="red")
 
plotTrend(NorthIceAreaAnomaly,y.pos=-1.2e6,main.t = "Northern Hemisphere Sea Ice Area Anomaly")
 
nonarctic=NorthIceArea-arcticcirclearea
 
seasonalratio = nonarctic/arcticcirclearea*100
plot(seasonalratio, main="NH Ratio of Non-Arctic Ice Area to Arctic Ice Area",ylab="Percent")
grid()
 
##########################################
##########################################
#southern hemisphere sea ice
Sfilenames=list.files(path="C:/agw/sea ice data/south/", pattern = NULL, all.files = TRUE, full.names = FALSE, recursive = TRUE)
startday=as.integer(unclass(as.POSIXct(strptime("1977-12-31", '%Y-%m-%d' )))/86400)
year= substr(Sfilenames,9,12)
month=substr(Sfilenames,13,14)
day=substr(Sfilenames,15,16)
dat=paste(year,month,day,sep="-")
oday=as.integer(unclass(as.POSIXct(strptime(dat, '%Y-%m-%d' )))/86400)-startday
 
area=array(NA,dim=c(366,35))
offsetday=array(NA,dim=c(366,35))
 
getsouthicedata=function(file=file)
{
a=file(file,"rb")
header= readBin(a,n=300,what=integer(),size=1,endian="little",signed=FALSE)
data=readBin(a,n=332*316,what=integer(),size=1,endian="little",signed=FALSE)
close(a)
data
}
 
#################################
#load area values into 2d matrix 366days each year x 35 days
for(i in 1:(length(Sfilenames)-1))
{
#open filename
fn=paste("C:/agw/sea ice data/south/",Sfilenames[i],sep="")
data=getsouthicedata(fn)
print(dat[i])
#Limit data/land mask to all info <250 - 100% and >25 - 10%
datamask=data<251# & data>25
#store sum in trend value
area[ydayindex[i],yearindex[i]]=sum(data[datamask])/250*625
offsetday[ydayindex[i],yearindex[i]]=oday[i]
}
 
areatimeseries=area
dim(areatimeseries)=c(366*35,1)
fareatimeseries=rep(NA,length(areatimeseries))
 
######filter timeseries for anomaly calculation only
######Use more recent years to avoid missing values from early record
fwid=10
for (i in 4000:length(areatimeseries))
{
if(!is.na(areatimeseries[i]))
{
miave=i-10
maave=i+10
fareatimeseries[i]=mean(areatimeseries[miave:maave],na.rm=TRUE)
}
}
 
#resequence all matrix data into time series to complete correction for leap years
dim(fareatimeseries)=c(366,35)
anomval=rowMeans(fareatimeseries,na.rm=TRUE)
SIAnom=area-anomval
SIArea=area
dim(SIAnom)=c(366*35,1)
dim(SIArea)=c(366*35,1)
dim(offsetday)=c(366*35,1)
SouthIceArea=rep(NA,366*35)
SouthIceAreaAnomaly=rep(NA,366*35)
 
#remove two extreme values which are obviously errors
mask= SIAnom< -5000000 | SIAnom>3000000
SIAnom[mask]=NA
SIArea[mask]=NA
 
for (i in 1:length(SouthIceArea))
{
if(!is.na(offsetday[i]))
{
SouthIceArea[offsetday[i]]=SIArea[i]
SouthIceAreaAnomaly[offsetday[i]]=SIAnom[i]
 
}
}
 
 
 
SouthIceArea=ts(SouthIceArea,start=1978,deltat=1/365.25) #set time series to get time values only
decdate=time(SouthIceArea) #copy time values
SouthIceArea=approx(decdate,SouthIceArea,xout=decdate) #interpolate missing values
SouthIceArea=ts(SouthIceArea$y,start=1978,deltat=1/365.25) #reassemble series
SouthIceAreaAnomaly=approx(decdate,SouthIceAreaAnomaly,xout=decdate) #interpolate missing values
SouthIceAreaAnomaly=ts(SouthIceAreaAnomaly$y,start=1978,deltat=1/365.25) #reassemble series
SouthIceAreaAnomaly=SouthIceAreaAnomaly-mean(SouthIceAreaAnomaly,na.rm=TRUE) #center series
 
plotTrend(SouthIceAreaAnomaly,y.pos=-2e6,main.t = "Antarctic Ice Area Anomaly")
 
 
 
GlobalIceArea=NorthIceArea+SouthIceArea
mean(GlobalIceArea,na.rm=TRUE)#19020751
GlobalIceAreaAnomaly= NorthIceAreaAnomaly+SouthIceAreaAnomaly
plotTrend(GlobalIceAreaAnomaly,y.pos=-2e6,main.t = "Global Ice Area Anomaly")
 
plot(GlobalIceAreaAnomaly+19000000,ylim=c(0,22000000),main="Global Sea Ice Area Anomaly\nOffset by Average Global Ice 19M Km^2", ylab="Km^2 Sea Ice")
grid()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.