forrest@0: load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl" forrest@0: load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl" forrest@0: forrest@0: procedure pminmax(data:numeric,name:string) forrest@0: begin forrest@0: print ("min/max " + name + " = " + min(data) + "/" + max(data)) forrest@0: if(isatt(data,"units")) then forrest@0: print (name + " units = " + data@units) forrest@0: end if forrest@0: end forrest@0: forrest@0: ; forrest@0: ; Main code. forrest@0: ; forrest@0: begin forrest@0: data_types = (/ "Obs", "Model" /) forrest@0: data_names = (/ "data.81.nc", "i01.03cn_1545-1569_ANN_climo.nc" /) forrest@0: ; data_names = (/ "data.81.nc", "i01.04casa_1605-1629_ANN_climo.nc" /) forrest@0: filevar_names = (/ (/"PREC_ANN","TNPP_C"/), (/"RAIN","NPP"/) /) forrest@0: ndata_types = dimsizes(data_types) forrest@0: forrest@0: data_file_obs = addfile(data_names(0),"r") ; Open obs file forrest@0: data_file_mod = addfile(data_names(1),"r") ; Open model file forrest@0: forrest@0: ; forrest@0: ; Read four variables from files. forrest@0: ; forrest@0: PREC_ANN = tofloat(data_file_obs->PREC_ANN) forrest@0: TNPP_C = data_file_obs->TNPP_C forrest@0: RAIN = data_file_mod->RAIN forrest@0: NPP = data_file_mod->NPP forrest@0: ; forrest@0: ; Units for these four variables are: forrest@0: ; forrest@0: ; PREC_ANN : mm/year forrest@0: ; TNPP_C : g C/m^2/year forrest@0: ; RAIN : mm/s forrest@0: ; NPP : g C/m^2/s forrest@0: ; forrest@0: ; We want to convert these to "m/year" and "g C/m^2/year". forrest@0: ; forrest@0: nsec_per_year = 60*60*24*365 ; # seconds per year forrest@0: forrest@0: ; Do the necessary conversions. forrest@0: PREC_ANN = PREC_ANN / 1000. forrest@0: RAIN = (RAIN / 1000.) * nsec_per_year forrest@0: NPP = NPP * nsec_per_year forrest@0: forrest@0: ; Redo the units. forrest@0: PREC_ANN@units = "m/yr" forrest@0: RAIN@units = "m/yr" forrest@0: NPP@units = "gC/m^2/yr" forrest@0: TNPP_C@units = "gC/m^2/yr" forrest@0: forrest@0: pminmax(PREC_ANN,"PREC_ANN") forrest@0: pminmax(TNPP_C,"TNPP_C") forrest@0: pminmax(RAIN,"RAIN") forrest@0: pminmax(NPP,"NPP") forrest@0: forrest@0: RAIN_1D = ndtooned(RAIN) forrest@0: NPP_1D = ndtooned(NPP) forrest@0: PREC_ANN_1D = ndtooned(PREC_ANN) forrest@0: TNPP_C_1D = ndtooned(TNPP_C) forrest@0: forrest@0: ; forrest@0: ; Calculate some "nice" bins for binning the data in equally spaced forrest@0: ; ranges. forrest@0: ; forrest@0: nbins = 15 ; Number of bins to use. forrest@0: forrest@0: nicevals = nice_mnmxintvl(min(RAIN_1D),max(RAIN_1D),nbins,True) forrest@0: nvals = floattoint((nicevals(1) - nicevals(0))/nicevals(2) + 1) forrest@0: range = fspan(nicevals(0),nicevals(1),nvals) forrest@0: ; forrest@0: ; Use this range information to grab all the values in a forrest@0: ; particular range, and then take an average. forrest@0: ; forrest@0: nr = dimsizes(range) forrest@0: nx = nr-1 forrest@0: xvalues = new((/2,nx/),typeof(RAIN_1D)) forrest@0: xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2. forrest@0: dx = xvalues(0,1) - xvalues(0,0) ; range width forrest@0: dx4 = dx/4 ; 1/4 of the range forrest@0: xvalues(1,:) = xvalues(0,:) - dx/5. forrest@0: yvalues = new((/2,nx/),typeof(RAIN_1D)) forrest@0: mn_yvalues = new((/2,nx/),typeof(RAIN_1D)) forrest@0: mx_yvalues = new((/2,nx/),typeof(RAIN_1D)) forrest@0: forrest@0: do nd=0,1 forrest@0: ; forrest@0: ; See if we are doing model or observational data. forrest@0: ; forrest@0: if(nd.eq.0) then forrest@0: data = PREC_ANN_1D forrest@0: npp_data = TNPP_C_1D forrest@0: else forrest@0: data = RAIN_1D forrest@0: npp_data = NPP_1D forrest@0: end if forrest@0: ; forrest@0: ; Loop through each range and check for values. forrest@0: ; forrest@0: do i=0,nr-2 forrest@0: if (i.ne.(nr-2)) then forrest@0: print("") forrest@0: print("In range ["+range(i)+","+range(i+1)+")") forrest@0: idx = ind((range(i).le.data).and.(data.lt.range(i+1))) forrest@0: else forrest@0: print("") forrest@0: print("In range ["+range(i)+",)") forrest@0: idx = ind(range(i).le.data) forrest@0: end if forrest@0: ; forrest@0: ; Calculate average, and get min and max. forrest@0: ; forrest@0: if(.not.any(ismissing(idx))) then forrest@0: yvalues(nd,i) = avg(npp_data(idx)) forrest@0: mn_yvalues(nd,i) = min(npp_data(idx)) forrest@0: mx_yvalues(nd,i) = max(npp_data(idx)) forrest@0: count = dimsizes(idx) forrest@0: else forrest@0: count = 0 forrest@0: yvalues(nd,i) = yvalues@_FillValue forrest@0: mn_yvalues(nd,i) = yvalues@_FillValue forrest@0: mx_yvalues(nd,i) = yvalues@_FillValue forrest@0: end if forrest@0: ; forrest@0: ; Print out information. forrest@0: ; forrest@0: print(data_types(nd) + ": " + count + " points, avg = " + yvalues(nd,i)) forrest@0: print("Min/Max: " + mn_yvalues(nd,i) + "/" + mx_yvalues(nd,i)) forrest@0: forrest@0: ; forrest@0: ; Clean up for next time in loop. forrest@0: ; forrest@0: delete(idx) forrest@0: end do forrest@0: delete(data) forrest@0: delete(npp_data) forrest@0: end do forrest@0: forrest@0: xvalues@long_name = "Mean Annual precipitation (m/year)" forrest@0: yvalues@long_name = "NPP (g C/m2/year)" forrest@0: forrest@0: ; forrest@0: ; Start the graphics. forrest@0: ; forrest@0: ; wks = gsn_open_wks("x11","npp") forrest@0: wks = gsn_open_wks("png","npp") forrest@0: forrest@0: res = True forrest@0: res@tiMainString = "Observed vs i01.03cn" forrest@0: ; res@tiMainString = "Observed vs i01.04casa" forrest@0: res@gsnMaximize = False forrest@0: res@gsnDraw = False forrest@0: res@gsnFrame = False forrest@0: res@xyMarkLineMode = "Markers" forrest@0: res@xyMarkerSizeF = 0.014 forrest@0: res@xyMarker = 16 forrest@0: ; res@xyMarkerColors = (/"Gray25","Gray50"/) forrest@0: res@xyMarkerColors = (/"brown","blue"/) forrest@0: res@trYMinF = min(mn_yvalues) - 10. forrest@0: res@trYMaxF = max(mx_yvalues) + 10. forrest@0: forrest@0: xy = gsn_csm_xy(wks,xvalues,yvalues,res) forrest@0: forrest@0: max_bar = new((/2,nx/),graphic) forrest@0: min_bar = new((/2,nx/),graphic) forrest@0: max_cap = new((/2,nx/),graphic) forrest@0: min_cap = new((/2,nx/),graphic) forrest@0: forrest@0: lnres = True forrest@0: forrest@0: line_colors = (/"brown","blue"/) forrest@0: do nd=0,1 forrest@0: lnres@gsLineColor = line_colors(nd) forrest@0: do i=0,nx-1 forrest@0: forrest@0: if(.not.ismissing(mn_yvalues(nd,i)).and. \ forrest@0: .not.ismissing(mx_yvalues(nd,i))) then forrest@0: ; forrest@0: ; Attach the vertical bar, both above and below the marker. forrest@0: ; forrest@0: x1 = xvalues(nd,i) forrest@0: y1 = yvalues(nd,i) forrest@0: y2 = mn_yvalues(nd,i) forrest@0: min_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres) forrest@0: forrest@0: y2 = mx_yvalues(nd,i) forrest@0: max_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres) forrest@0: ; forrest@0: ; Attach the horizontal cap line, both above and below the marker. forrest@0: ; forrest@0: x1 = xvalues(nd,i) - dx4 forrest@0: x2 = xvalues(nd,i) + dx4 forrest@0: y1 = mn_yvalues(nd,i) forrest@0: min_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres) forrest@0: forrest@0: y1 = mx_yvalues(nd,i) forrest@0: max_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres) forrest@0: end if forrest@0: end do forrest@0: end do forrest@0: forrest@0: draw(xy) forrest@0: frame(wks) forrest@0: forrest@0: end forrest@0: