forrest@0: ;******************************************************** forrest@0: ; histogram normalized by rain and compute correleration forrest@0: ;******************************************************** 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: load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.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: forrest@0: ;nclass = 18 forrest@0: nclass = 20 forrest@0: forrest@0: ;************************************************ forrest@0: ; read in data: observed forrest@0: ;************************************************ forrest@0: diri1 = "/fis/cgd/cseg/people/jeff/clamp_data/lai/" forrest@0: ;fili1 = "land_class_T42.nc" forrest@0: fili1 = "land_class_T42_new.nc" forrest@0: fili2 = "LAI_2000-2005_ensemble_T42.nc" forrest@0: data_file_ob1 = addfile(diri1+fili1,"r") forrest@0: data_file_ob2 = addfile(diri1+fili2,"r") forrest@0: forrest@0: RAIN1 = tofloat(data_file_ob1->LAND_CLASS) forrest@0: forrest@0: z = data_file_ob2->LAI forrest@0: y = z(0,:,:) forrest@0: y@long_name = "Leaf Area Index Max Month" forrest@0: s = z(:,0,0) forrest@0: forrest@0: dsizes_z = dimsizes(z) forrest@0: ntime = dsizes_z(0) forrest@0: nlat = dsizes_z(1) forrest@0: nlon = dsizes_z(2) forrest@0: forrest@0: do j = 0,nlat-1 forrest@0: do i = 0,nlon-1 forrest@0: s = z(:,j,i) forrest@0: y(j,i) = maxind(s) + 1 forrest@0: end do forrest@0: end do forrest@0: forrest@0: print (min(y)+"/"+max(y)) forrest@0: forrest@0: NPP1 = y forrest@0: forrest@0: delete (z) forrest@0: delete (s) forrest@0: delete (y) forrest@0: ;************************************************ forrest@0: ; read in data: model forrest@0: ;************************************************ forrest@0: diri2 = "/fis/cgd/cseg/people/jeff/clamp_data/model/" forrest@0: ;fili3 = "i01.03cn_1545-1569_MONS_climo.nc" forrest@0: fili3 = "i01.04casa_1605-1629_MONS_climo.nc" forrest@0: data_file_model = addfile(diri2+fili3,"r") forrest@0: forrest@0: z = data_file_model->TLAI forrest@0: y = z(0,:,:) forrest@0: y@long_name = "Leaf Area Index Max Month" forrest@0: s = z(:,0,0) forrest@0: forrest@0: dsizes_z = dimsizes(z) forrest@0: ntime = dsizes_z(0) forrest@0: nlat = dsizes_z(1) forrest@0: nlon = dsizes_z(2) forrest@0: forrest@0: do j = 0,nlat-1 forrest@0: do i = 0,nlon-1 forrest@0: s = z(:,j,i) forrest@0: y(j,i) = maxind(s) + 1 forrest@0: end do forrest@0: end do forrest@0: forrest@0: print (min(y)+"/"+max(y)) forrest@0: forrest@0: NPP2 = y forrest@0: forrest@0: delete (z) forrest@0: delete (s) forrest@0: delete (y) forrest@0: ;************************************************ forrest@0: ; print min/max and unit forrest@0: ;************************************************ forrest@0: pminmax(RAIN1,"RAIN1") forrest@0: pminmax(NPP1,"NPP1") forrest@0: pminmax(NPP2,"NPP2") forrest@0: forrest@0: RAIN1_1D = ndtooned(RAIN1) forrest@0: NPP1_1D = ndtooned(NPP1) forrest@0: NPP2_1D = ndtooned(NPP2) forrest@0: ; forrest@0: ; Calculate some "nice" bins for binning the data in equally spaced forrest@0: ; ranges. forrest@0: ; forrest@0: forrest@0: ; nbins = nclass + 1 ; Number of bins to use. forrest@0: ; nicevals = nice_mnmxintvl(min(RAIN1_1D),max(RAIN1_1D),nbins,False) forrest@0: ; nvals = floattoint((nicevals(1) - nicevals(0))/nicevals(2) + 1) forrest@0: ; range = fspan(nicevals(0),nicevals(1),nvals) forrest@0: forrest@0: nclass = nclass + 1 forrest@0: range = fspan(0,nclass-1,nclass) forrest@0: forrest@0: ; print (nicevals) forrest@0: ; print (nvals) forrest@0: print (range) forrest@0: ; exit forrest@0: 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(RAIN1_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(RAIN1_1D)) forrest@0: mn_yvalues = new((/2,nx/),typeof(RAIN1_1D)) forrest@0: mx_yvalues = new((/2,nx/),typeof(RAIN1_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 = RAIN1_1D forrest@0: npp_data = NPP1_1D forrest@0: else forrest@0: data = RAIN1_1D forrest@0: npp_data = NPP2_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(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: ; forrest@0: ; Start the graphics. forrest@0: ; forrest@0: wks = gsn_open_wks("ps","xy") forrest@0: forrest@0: res = True forrest@0: res@gsnMaximize = True 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 = (/"Brown","Blue"/) forrest@0: ; res@trYMinF = min(mn_yvalues) - 10. forrest@0: ; res@trYMaxF = max(mx_yvalues) + 10. forrest@0: res@trYMinF = min(mn_yvalues) - 2 forrest@0: res@trYMaxF = max(mx_yvalues) + 4 forrest@0: forrest@0: ; res@tiMainString = "Observed vs i01.03cn" forrest@0: res@tiMainString = "Observed vs i01.04casa" forrest@0: forrest@0: res@tiYAxisString = "Max LAI (Leaf Area Index) Month" forrest@0: res@tiXAxisString = "Land Cover Type" forrest@0: ; forrest@0: ; Add a boxed legend using the more simple method, which won't have forrest@0: ; vertical lines going through the markers. forrest@0: ; forrest@0: res@pmLegendDisplayMode = "Always" forrest@0: ; res@pmLegendWidthF = 0.1 forrest@0: res@pmLegendWidthF = 0.08 forrest@0: res@pmLegendHeightF = 0.05 forrest@0: res@pmLegendOrthogonalPosF = -1.17 forrest@0: ; res@pmLegendOrthogonalPosF = -1.00 ;(downward) forrest@0: ; res@pmLegendParallelPosF = 0.18 forrest@0: res@pmLegendParallelPosF = 0.23 ;(rightward) forrest@0: forrest@0: ; res@lgPerimOn = False forrest@0: res@lgLabelFontHeightF = 0.015 forrest@0: ; res@xyExplicitLegendLabels = (/"observed","model_i01.03cn"/) forrest@0: res@xyExplicitLegendLabels = (/"observed","model_i01.04casa"/) 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: ; forrest@0: ; Here's how to do the legend by hand. forrest@0: ; forrest@0: ; mkres = True ; Marker resources forrest@0: ; txres = True ; Text resources forrest@0: ; mkres@gsMarkerIndex = 16 forrest@0: ; mkres@gsMarkerSizeF = 0.02 forrest@0: ; txres@txFontHeightF = 0.02 forrest@0: ; txres@txJust = "CenterLeft" forrest@0: ; forrest@0: ; Change these values if you want to move the marker legend location. forrest@0: ; These values are in the same data space as the plot. forrest@0: ; forrest@0: ; xlg1_cen = 0.2 forrest@0: ; ylg1_cen = 900. forrest@0: forrest@0: ; xlg2_cen = 0.2 forrest@0: ; ylg2_cen = 760. forrest@0: forrest@0: ; mkres@gsMarkerColor = "brown" forrest@0: ; lnres@gsLineColor = "brown" forrest@0: forrest@0: ; lg_mark_legend1 = gsn_add_polymarker(wks,xy,xlg1_cen,ylg1_cen,mkres) forrest@0: ; lg_line_legend1 = gsn_add_polyline(wks,xy,(/xlg1_cen,xlg1_cen/), \ forrest@0: ; (/ylg1_cen-60,ylg1_cen+60/),lnres) forrest@0: ; lg_cap_legend11 = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \ forrest@0: ; (/ylg1_cen-60,ylg1_cen-60/),lnres) forrest@0: ; lg_cap_legend12 = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \ forrest@0: ; (/ylg1_cen+60,ylg1_cen+60/),lnres) forrest@0: forrest@0: ; tx_legend1 = gsn_add_text(wks,xy,"observed",xlg1_cen+0.15,ylg1_cen,txres) forrest@0: forrest@0: ; mkres@gsMarkerColor = "blue" forrest@0: ; lnres@gsLineColor = "blue" forrest@0: forrest@0: ; lg_mark_legend2 = gsn_add_polymarker(wks,xy,xlg2_cen,ylg2_cen,mkres) forrest@0: ; lg_line_legend2 = gsn_add_polyline(wks,xy,(/xlg2_cen,xlg2_cen/), \ forrest@0: ; (/ylg2_cen-60,ylg2_cen+60/),lnres) forrest@0: ; lg_cap_legend21 = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \ forrest@0: ; (/ylg2_cen-60,ylg2_cen-60/),lnres) forrest@0: ; lg_cap_legend22 = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \ forrest@0: ; (/ylg2_cen+60,ylg2_cen+60/),lnres) forrest@0: ; tx_legend2 = gsn_add_text(wks,xy,"model_i01.03cn",xlg2_cen+0.15,ylg2_cen,txres) forrest@0: forrest@0: draw(xy) forrest@0: frame(wks) forrest@0: system("convert xy.ps xy.png") forrest@0: forrest@0: u = yvalues(0,:) forrest@0: v = yvalues(1,:) forrest@0: forrest@0: good = ind(.not.ismissing(u) .and. .not.ismissing(v)) forrest@0: uu = u(good) forrest@0: vv = v(good) forrest@0: nz = dimsizes(uu) forrest@0: print (nz) forrest@0: forrest@0: ccr = esccr(uu,vv,0) forrest@0: print (ccr) forrest@0: forrest@0: ;old eq forrest@0: ;bias = sum(((vv-uu)/uu)^2) forrest@0: ;M = (1.- sqrt(bias/nz))*5. forrest@0: forrest@0: ;new eq forrest@0: bias = abs(avg(vv)-avg(uu)) forrest@0: bias = where((bias.gt. 6.),12.-bias,bias) forrest@0: M = ((6. - bias)/6.)*5. forrest@0: print (bias) forrest@0: print (M) forrest@0: forrest@0: end forrest@0: