; ============================================================== ; eof_1.ncl ; ; Concepts illustrated: ; - Calculating EOFs ; - Using coordinate subscripting to read a specified geographical region ; - Rearranging longitude data to span -180 to 180 ; - Calculating symmetric contour intervals ; - Drawing filled bars above and below a given reference line ; - Drawing subtitles at the top of a plot ; ; ============================================================== ; Calculate EOFs of the Sea Level Pressure over the North Atlantic. ; ============================================================== ; The slp.mon.mean file can be downloaded from: ; http://www.esrl.noaa.gov/psd/data/gridded/data.ncep.reanalysis.surface.html ; ============================================================== load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" begin ; ============================================================== ; User defined parameters that specify region of globe and ; ============================================================== latS = 25. latN = 80. lonL = -70. lonR = 40. yrStrt = 1970 yrLast = 2000 season = "DJF" ; choose Dec-Jan-Feb seasonal mean neof = 2 ; number of EOFs optEOF = True optEOF@jopt = 0 ; This is the default; most commonly used; no need to specify. ;;optEOF@jopt = 1 ; **only** if the correlation EOF is desired optETS = False ; ============================================================== ; Open the file: Read only the user specified period ; ============================================================== f = addfile ("prmsl.1969-2000.nc", "r") TIME = f->time YYYY = cd_calendar(TIME,-1)/100 ; entire file iYYYY = ind(YYYY.ge.yrStrt .and. YYYY.le.yrLast) slp = short2flt(f->prmsl(iYYYY,:,:)) ; printVarSummary(slp) ; variable overview ; ============================================================== ; dataset longitudes span 0=>357.5 ; Because EOFs of the North Atlantic Oscillation are desired ; use the "lonFlip" (contributed.ncl) to reorder ; longitudes to span -180 to 177.5: facilitate coordinate subscripting ; ============================================================== slp = lonFlip( slp ) ; printVarSummary(slp) ; note the longitude coord ; ============================================================== ; Hourly data conversion ; ============================================================== nyrs = yrLast - yrStrt+1 ;yrst and yren are the forst and last years in x SLP = slp(:nyrs-1,:,:) ; nyr is the number of years in your data set year = ispan(yrStrt, yrLast,1) ; integer array of all years in the range tdat = cd_calendar(slp&time, -5) ; converts time to a multidimensional array ; giving year, month, day, time etc for ; each time step do yy = 0, nyrs-1 ii = ind(tdat(:,0).eq.year(yy).and.tdat(:,1).ge.1.and.tdat(:,1).le.3) ; location of all times in given year, that ; are between Jan (1) and March(3) SLP(yy,:,:) = dim_avg_n(slp(ii,:,:), 0) ; get average of all those data points delete(ii) end do ; ============================================================== ; compute desired global seasonal mean: month_to_season (contributed.ncl) ; ============================================================== ; SLP = month_to_season (slp, season) ; nyrs = dimsizes(SLP&time) ;;printVarSummary(SLP) ; ================================================================= ; create weights: sqrt(cos(lat)) [or sqrt(gw) ] ; ================================================================= rad = 4.*atan(1.)/180. clat = f->lat clat = sqrt( cos(rad*clat) ) ; gw for gaussian grid ; ================================================================= ; weight all observations ; ================================================================= wSLP = SLP ; copy meta data wSLP = SLP*conform(SLP, clat, 1) wSLP@long_name = "Wgt: "+wSLP@long_name ; ================================================================= ; Reorder (lat,lon,time) the *weighted* input data ; Access the area of interest via coordinate subscripting ; ================================================================= x = wSLP({lat|latS:latN},{lon|lonL:lonR},time|:) eof = eofunc_Wrap(x, neof, optEOF) eof_ts = eofunc_ts_Wrap (x, eof, optETS) ; printVarSummary( eof ) ; examine EOF variables ; printVarSummary( eof_ts ) ; ================================================================= ; Normalize time series: Sum spatial weights over the area of used ; ================================================================= dimx = dimsizes( x ) mln = dimx(1) sumWgt = mln*sum( clat({lat|latS:latN}) ) eof_ts = eof_ts/sumWgt ; ================================================================= ; Extract the YYYYMM from the time coordinate ; associated with eof_ts [same as SLP&time] ; ================================================================= yyyymm = cd_calendar(eof_ts&time,-2)/100 ;;yrfrac = yyyymm_to_yyyyfrac(yyyymm, 0.0); not used here ;============================================================ ; PLOTS ;============================================================ wks = gsn_open_wks("pdf","eof") gsn_define_colormap(wks,"BlWhRe") ; choose colormap plot = new(neof,graphic) ; create graphic array ; only needed if paneling ; EOF patterns res = True res@gsnDraw = False ; don't draw yet res@gsnFrame = False ; don't advance frame yet ;---This resource not needed in V6.1.0 res@gsnSpreadColors = True ; spread out color table res@gsnAddCyclic = False ; plotted dataa are not cyclic res@mpFillOn = False ; turn off map fill res@mpMinLatF = latS ; zoom in on map res@mpMaxLatF = latN res@mpMinLonF = lonL res@mpMaxLonF = lonR res@cnFillOn = True ; turn on color fill res@cnLinesOn = False ; True is default ;res@cnLineLabelsOn = False ; True is default res@lbLabelBarOn = False ; turn off individual lb's ; set symmetric plot min/max symMinMaxPlt(eof, 16, False, res) ; contributed.ncl ; panel plot only resources resP = True ; modify the panel plot resP@gsnMaximize = True ; large format resP@gsnPanelLabelBar = True ; add common colorbar resP@lbLabelAutoStride = True ; auto stride on labels yStrt = yyyymm(0)/100 yLast = yyyymm(nyrs-1)/100 resP@txString = "SLP: "+season+": "+yStrt+"-"+yLast ;******************************************* ; first plot ;******************************************* do n=0,neof-1 res@gsnLeftString = "EOF "+(n+1) res@gsnRightString = sprintf("%5.1f", eof@pcvar(n)) +"%" plot(n)=gsn_csm_contour_map_ce(wks,eof(n,:,:),res) end do gsn_panel(wks,plot,(/neof,1/),resP) ; now draw as one plot ;;******************************************* ;; second plot ;;******************************************* ;; EOF time series [bar form] ; ; rts = True ; rts@gsnDraw = False ; don't draw yet ; rts@gsnFrame = False ; don't advance frame yet ; rts@gsnScale = True ; force text scaling ; ;; these four rtsources allow the user to stretch the plot size, and ;; decide exactly where on the page to draw it. ; ; rts@vpHeightF = 0.40 ; Changes the aspect ratio ; rts@vpWidthF = 0.85 ; rts@vpXF = 0.10 ; change start locations ; rts@vpYF = 0.75 ; the plot ; ; ; rts@tiYAxisString = "Pa" ; y-axis label ; ; rts@gsnYRefLine = 0. ; reference line ; rts@gsnXYBarChart = True ; create bar chart ; rts@gsnAboveYRefLineColor = "red" ; above ref line fill red ; rts@gsnBelowYRefLineColor = "blue" ; below ref line fill blue ; ;; panel plot only resources ; rtsP = True ; modify the panel plot ; rtsP@gsnMaximize = True ; large format ; rtsP@txString = "SLP: "+season+": "+yStrt+"-"+yLast ; ; year = yyyymm/100 ; ;; create individual plots ; do n=0,neof-1 ; rts@gsnLeftString = "EOF "+(n+1) ; rts@gsnRightString = sprintf("%5.1f", eof@pcvar(n)) +"%" ; plot(n) = gsn_csm_xy (wks,year,eof_ts(n,:),rts) ; end do ; gsn_panel(wks,plot,(/neof,1/),rtsP) ; now draw as one plot ; ;============================================================ ; TEXT OUTPUT ;============================================================ year = yyyymm/100 ntimes = dimsizes(year) ; number of times in the file line = new((/neof,ntimes/),string) ; declare a string array do n=0,neof-1 do t=0,ntimes-1 line(n,t) = sprintf("%5.0f",year(t)) +" "+sprintf("%21.2f", eof_ts(n,t)) end do asciiwrite("EOF"+(n+1)+".txt",line) ; write output to file end do end