Viewing

Example 006




setwd("/Users/rockel/work/R/examples/example006")

#... Example for NCAR palette (http://www.ncl.ucar.edu/Document/Graphics/color_table_gallery.shtml)
col<-get_ncar_palette("temp_19lev")$colors

ncfile<-"init_DOM02_ML_20170112T000000Z.nc"
varname<-"t_g"

#... pdf output file
pdffile<-"exp006.pdf"

#... title on the top of the plot
title<-"ICON Test"

#... open plot device and decide whether landscape or portrait looks prettier
pdf(pdffile, width=11.69,height=8.27,paper="special")

#... overall plot margins
par(oma=c(3,2,5,2))

#... read vertices (for automatic orientation of the plot) and the data field (for calculating the color bar levels)
nc<-nc_open(ncfile)
if ( is.null(nc$var$clon_bnds$id) ) {
  clon_bnds<-ncvar_get(nc,'clon_vertices')
  clat_bnds<-ncvar_get(nc,'clat_vertices')
} else {
clon_bnds<-ncvar_get(nc,'clon_bnds')
clat_bnds<-ncvar_get(nc,'clat_bnds')
}
dat<-ncvar_get(nc,varname)
v3<-nc$var[[3]]
varsize <- v3$varsize
ndims   <- v3$ndims
nc_close(nc)

xyratio<-(max(clon_bnds)-min(clon_bnds))/(max(clat_bnds)-min(clat_bnds))
layout(matrix(c(1,2),2,1, byrow=T), width=c(xyratio,xyratio), height=c(1,lcm(2.5)),respect=T)

#... The color bar levels are computed automatically from the minimum and maximum of the field and the number of colors.
#... This works best for single level fields. For plotting a level of a 3D field the level should be given as an index of dat
datmin<-min(dat)
datmax<-max(dat)
ddat<-(datmax-datmin)/length(col)
levels<-seq(datmin,datmax,ddat)
levels<-pretty(levels,n=length(col), min.n=length(col))

#... plot margins for the main plot
par(mar=c(0.5,0.8,0.5,0.8))
tmp <- plotmap(file=ncfile, varname=varname,col=col,levels=levels,rivers=F,cities=F,sponge=0,grid.txt=T,hires=F,map.lwd=1,interior=F)

#... plot margins for the color bar
par(mai=c(0.5,0.5,0.35,0.5))
plot_colourbar(tmp)

#... plot the title on the top of the plot
mtext(title, 3, line=2,outer=T, font=1, cex=1.4)

#... close plot device
dev.off()
 Viewing