lai/27.histogram+bias_grow.ncl
author Forrest Hoffman <forrest@climatemodeling.org>
Thu, 26 Mar 2009 14:02:21 -0400
changeset 1 4be95183fbcd
permissions -rw-r--r--
Modifications to scoring and graphics production for the final version of code for the C-LAMP paper in GCB.
forrest@0
     1
;********************************************************
forrest@0
     2
; histogram normalized by rain and compute correleration
forrest@0
     3
;********************************************************
forrest@0
     4
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
forrest@0
     5
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
forrest@0
     6
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
forrest@0
     7
forrest@0
     8
procedure pminmax(data:numeric,name:string)
forrest@0
     9
begin
forrest@0
    10
  print ("min/max " + name + " = " + min(data) + "/" + max(data))
forrest@0
    11
  if(isatt(data,"units")) then
forrest@0
    12
    print (name + " units = " + data@units)
forrest@0
    13
  end if
forrest@0
    14
end
forrest@0
    15
forrest@0
    16
;
forrest@0
    17
; Main code.
forrest@0
    18
;
forrest@0
    19
begin
forrest@0
    20
 
forrest@0
    21
;nclass = 18
forrest@0
    22
 nclass = 20
forrest@0
    23
 day_of_data = (/31,28,31,30,31,30,31,31,30,31,30,31/)
forrest@0
    24
forrest@0
    25
;************************************************
forrest@0
    26
; read in data: observed
forrest@0
    27
;************************************************
forrest@0
    28
 diri1  = "/fis/cgd/cseg/people/jeff/clamp_data/lai/"
forrest@0
    29
;fili1  = "land_class_T42.nc"
forrest@0
    30
 fili1  = "land_class_T42_new.nc"
forrest@0
    31
 fili2  = "LAI_2000-2005_ensemble_T42.nc"
forrest@0
    32
 data_file_ob1 = addfile(diri1+fili1,"r")
forrest@0
    33
 data_file_ob2 = addfile(diri1+fili2,"r")
forrest@0
    34
 
forrest@0
    35
 RAIN1 = tofloat(data_file_ob1->LAND_CLASS)
forrest@0
    36
forrest@0
    37
  z     = data_file_ob2->LAI
forrest@0
    38
  y     = z(0,:,:)
forrest@0
    39
  y@long_name = "Days of Growing Season"
forrest@0
    40
 
forrest@0
    41
  dsizes_z = dimsizes(z)
forrest@0
    42
  ntime    = dsizes_z(0)
forrest@0
    43
  nlat     = dsizes_z(1)
forrest@0
    44
  nlon     = dsizes_z(2)
forrest@0
    45
  
forrest@0
    46
  do j = 0,nlat-1
forrest@0
    47
  do i = 0,nlon-1
forrest@0
    48
     nday = 0.
forrest@0
    49
     do k = 0,ntime-1
forrest@0
    50
        if (.not. ismissing(z(k,j,i)) .and. z(k,j,i) .gt. 1.0) then
forrest@0
    51
           nday = nday + day_of_data(k)
forrest@0
    52
        end if
forrest@0
    53
     end do
forrest@0
    54
     y(j,i) = nday
forrest@0
    55
  end do
forrest@0
    56
  end do
forrest@0
    57
forrest@0
    58
  print (min(y)+"/"+max(y))
forrest@0
    59
               
forrest@0
    60
  NPP1  = y
forrest@0
    61
forrest@0
    62
  delete (z)
forrest@0
    63
  delete (y)      
forrest@0
    64
;************************************************
forrest@0
    65
; read in data: model       
forrest@0
    66
;************************************************
forrest@0
    67
 diri2  = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
    68
;fili3  = "i01.03cn_1545-1569_MONS_climo.nc"
forrest@0
    69
 fili3  = "i01.04casa_1605-1629_MONS_climo.nc"
forrest@0
    70
 data_file_model = addfile(diri2+fili3,"r")
forrest@0
    71
forrest@0
    72
  z     = data_file_model->TLAI
forrest@0
    73
  y     = z(0,:,:)
forrest@0
    74
  y@long_name = "Days of Growing Season"
forrest@0
    75
 
forrest@0
    76
  dsizes_z = dimsizes(z)
forrest@0
    77
  ntime    = dsizes_z(0)
forrest@0
    78
  nlat     = dsizes_z(1)
forrest@0
    79
  nlon     = dsizes_z(2)
forrest@0
    80
  
forrest@0
    81
  do j = 0,nlat-1
forrest@0
    82
  do i = 0,nlon-1
forrest@0
    83
     nday = 0.
forrest@0
    84
     do k = 0,ntime-1
forrest@0
    85
        if (.not. ismissing(z(k,j,i)) .and. z(k,j,i) .gt. 1.0) then
forrest@0
    86
           nday = nday + day_of_data(k)
forrest@0
    87
        end if
forrest@0
    88
     end do
forrest@0
    89
     y(j,i) = nday
forrest@0
    90
  end do
forrest@0
    91
  end do
forrest@0
    92
forrest@0
    93
  print (min(y)+"/"+max(y))
forrest@0
    94
               
forrest@0
    95
  NPP2  = y
forrest@0
    96
forrest@0
    97
  delete (z)
forrest@0
    98
  delete (y)            
forrest@0
    99
;************************************************
forrest@0
   100
; print min/max and unit
forrest@0
   101
;************************************************
forrest@0
   102
  pminmax(RAIN1,"RAIN1")
forrest@0
   103
  pminmax(NPP1,"NPP1")
forrest@0
   104
  pminmax(NPP2,"NPP2")
forrest@0
   105
forrest@0
   106
  RAIN1_1D = ndtooned(RAIN1)
forrest@0
   107
  NPP1_1D  = ndtooned(NPP1)
forrest@0
   108
  NPP2_1D  = ndtooned(NPP2)
forrest@0
   109
;
forrest@0
   110
; Calculate some "nice" bins for binning the data in equally spaced
forrest@0
   111
; ranges.
forrest@0
   112
;
forrest@0
   113
forrest@0
   114
; nbins       = nclass + 1         ; Number of bins to use.
forrest@0
   115
; nicevals    = nice_mnmxintvl(min(RAIN1_1D),max(RAIN1_1D),nbins,False)
forrest@0
   116
; nvals       = floattoint((nicevals(1) - nicevals(0))/nicevals(2) + 1)
forrest@0
   117
; range       = fspan(nicevals(0),nicevals(1),nvals)
forrest@0
   118
forrest@0
   119
  nclass      = nclass + 1
forrest@0
   120
  range       = fspan(0,nclass-1,nclass)
forrest@0
   121
forrest@0
   122
; print (nicevals)
forrest@0
   123
; print (nvals)
forrest@0
   124
  print (range)
forrest@0
   125
; exit
forrest@0
   126
forrest@0
   127
;
forrest@0
   128
; Use this range information to grab all the values in a
forrest@0
   129
; particular range, and then take an average.
forrest@0
   130
;
forrest@0
   131
  nr      = dimsizes(range)
forrest@0
   132
  nx      = nr-1
forrest@0
   133
  xvalues     = new((/2,nx/),typeof(RAIN1_1D))
forrest@0
   134
  xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
forrest@0
   135
  dx           = xvalues(0,1) - xvalues(0,0)       ; range width
forrest@0
   136
  dx4          = dx/4                              ; 1/4 of the range
forrest@0
   137
  xvalues(1,:) = xvalues(0,:) - dx/5.
forrest@0
   138
  yvalues      = new((/2,nx/),typeof(RAIN1_1D))
forrest@0
   139
  mn_yvalues   = new((/2,nx/),typeof(RAIN1_1D))
forrest@0
   140
  mx_yvalues   = new((/2,nx/),typeof(RAIN1_1D))
forrest@0
   141
forrest@0
   142
  do nd=0,1
forrest@0
   143
;
forrest@0
   144
; See if we are doing model or observational data.
forrest@0
   145
;
forrest@0
   146
    if(nd.eq.0) then
forrest@0
   147
      data     = RAIN1_1D
forrest@0
   148
      npp_data = NPP1_1D
forrest@0
   149
    else
forrest@0
   150
      data     = RAIN1_1D
forrest@0
   151
      npp_data = NPP2_1D
forrest@0
   152
    end if
forrest@0
   153
;
forrest@0
   154
; Loop through each range and check for values.
forrest@0
   155
;
forrest@0
   156
    do i=0,nr-2
forrest@0
   157
      if (i.ne.(nr-2)) then
forrest@0
   158
         print("")
forrest@0
   159
         print("In range ["+range(i)+","+range(i+1)+")")
forrest@0
   160
        idx = ind((range(i).le.data).and.(data.lt.range(i+1)))
forrest@0
   161
      else
forrest@0
   162
         print("")
forrest@0
   163
         print("In range ["+range(i)+",)")
forrest@0
   164
        idx = ind(range(i).le.data)
forrest@0
   165
      end if
forrest@0
   166
;
forrest@0
   167
; Calculate average, and get min and max.
forrest@0
   168
;
forrest@0
   169
      if(.not.any(ismissing(idx))) then
forrest@0
   170
        yvalues(nd,i)    = avg(npp_data(idx))
forrest@0
   171
        mn_yvalues(nd,i) = min(npp_data(idx))
forrest@0
   172
        mx_yvalues(nd,i) = max(npp_data(idx))
forrest@0
   173
        count = dimsizes(idx)
forrest@0
   174
      else
forrest@0
   175
        count            = 0
forrest@0
   176
        yvalues(nd,i)    = yvalues@_FillValue
forrest@0
   177
        mn_yvalues(nd,i) = yvalues@_FillValue
forrest@0
   178
        mx_yvalues(nd,i) = yvalues@_FillValue
forrest@0
   179
      end if
forrest@0
   180
;
forrest@0
   181
; Print out information.
forrest@0
   182
;
forrest@0
   183
       print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
forrest@0
   184
       print("Min/Max:  " + mn_yvalues(nd,i) + "/" + mx_yvalues(nd,i))
forrest@0
   185
forrest@0
   186
;
forrest@0
   187
; Clean up for next time in loop.
forrest@0
   188
;
forrest@0
   189
      delete(idx)
forrest@0
   190
    end do
forrest@0
   191
    delete(data)
forrest@0
   192
    delete(npp_data)
forrest@0
   193
  end do
forrest@0
   194
forrest@0
   195
;
forrest@0
   196
; Start the graphics.
forrest@0
   197
;
forrest@0
   198
  wks = gsn_open_wks("ps","xy")
forrest@0
   199
forrest@0
   200
  res             = True
forrest@0
   201
  res@gsnMaximize = True
forrest@0
   202
  res@gsnDraw     = False
forrest@0
   203
  res@gsnFrame    = False
forrest@0
   204
  res@xyMarkLineMode = "Markers"
forrest@0
   205
  res@xyMarkerSizeF   = 0.014
forrest@0
   206
  res@xyMarker       = 16
forrest@0
   207
  res@xyMarkerColors = (/"Brown","Blue"/)
forrest@0
   208
; res@trYMinF        = min(mn_yvalues) - 10.
forrest@0
   209
; res@trYMaxF        = max(mx_yvalues) + 10.
forrest@0
   210
  res@trYMinF        = min(mn_yvalues) - 20.
forrest@0
   211
  res@trYMaxF        = max(mx_yvalues) + 100.
forrest@0
   212
forrest@0
   213
; res@tiMainString  = "Observed vs i01.03cn"
forrest@0
   214
  res@tiMainString  = "Observed vs i01.04casa"
forrest@0
   215
forrest@0
   216
  res@tiYAxisString = "Days of Growing season"
forrest@0
   217
  res@tiXAxisString = "Land Cover Type"
forrest@0
   218
;
forrest@0
   219
; Add a boxed legend using the more simple method, which won't have
forrest@0
   220
; vertical lines going through the markers.
forrest@0
   221
;
forrest@0
   222
  res@pmLegendDisplayMode    = "Always"
forrest@0
   223
; res@pmLegendWidthF         = 0.1
forrest@0
   224
  res@pmLegendWidthF         = 0.08
forrest@0
   225
  res@pmLegendHeightF        = 0.05
forrest@0
   226
  res@pmLegendOrthogonalPosF = -1.17
forrest@0
   227
; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
forrest@0
   228
; res@pmLegendParallelPosF   =  0.18
forrest@0
   229
  res@pmLegendParallelPosF   =  0.23  ;(rightward)
forrest@0
   230
forrest@0
   231
;  res@lgPerimOn             = False
forrest@0
   232
  res@lgLabelFontHeightF     = 0.015
forrest@0
   233
; res@xyExplicitLegendLabels = (/"observed","model_i01.03cn"/)
forrest@0
   234
  res@xyExplicitLegendLabels = (/"observed","model_i01.04casa"/)
forrest@0
   235
forrest@0
   236
  xy = gsn_csm_xy(wks,xvalues,yvalues,res)
forrest@0
   237
forrest@0
   238
  max_bar = new((/2,nx/),graphic)
forrest@0
   239
  min_bar = new((/2,nx/),graphic)
forrest@0
   240
  max_cap = new((/2,nx/),graphic)
forrest@0
   241
  min_cap = new((/2,nx/),graphic)
forrest@0
   242
forrest@0
   243
  lnres = True
forrest@0
   244
forrest@0
   245
  line_colors = (/"brown","blue"/)
forrest@0
   246
  do nd=0,1
forrest@0
   247
    lnres@gsLineColor = line_colors(nd)
forrest@0
   248
    do i=0,nx-1
forrest@0
   249
     
forrest@0
   250
      if(.not.ismissing(mn_yvalues(nd,i)).and. \
forrest@0
   251
         .not.ismissing(mx_yvalues(nd,i))) then
forrest@0
   252
;
forrest@0
   253
; Attach the vertical bar, both above and below the marker.
forrest@0
   254
;
forrest@0
   255
        x1 = xvalues(nd,i)
forrest@0
   256
        y1 = yvalues(nd,i)
forrest@0
   257
        y2 = mn_yvalues(nd,i)
forrest@0
   258
        min_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
forrest@0
   259
forrest@0
   260
        y2 = mx_yvalues(nd,i)
forrest@0
   261
        max_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
forrest@0
   262
;
forrest@0
   263
; Attach the horizontal cap line, both above and below the marker.
forrest@0
   264
;
forrest@0
   265
        x1 = xvalues(nd,i) - dx4
forrest@0
   266
        x2 = xvalues(nd,i) + dx4
forrest@0
   267
        y1 = mn_yvalues(nd,i)
forrest@0
   268
        min_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
forrest@0
   269
forrest@0
   270
        y1 = mx_yvalues(nd,i)
forrest@0
   271
        max_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
forrest@0
   272
      end if
forrest@0
   273
    end do
forrest@0
   274
  end do
forrest@0
   275
forrest@0
   276
;
forrest@0
   277
; Here's how to do the legend by hand.
forrest@0
   278
;
forrest@0
   279
; mkres               = True    ; Marker resources
forrest@0
   280
; txres               = True    ; Text resources
forrest@0
   281
; mkres@gsMarkerIndex = 16
forrest@0
   282
; mkres@gsMarkerSizeF = 0.02
forrest@0
   283
; txres@txFontHeightF = 0.02
forrest@0
   284
; txres@txJust        = "CenterLeft"
forrest@0
   285
;
forrest@0
   286
; Change these values if you want to move the marker legend location.
forrest@0
   287
; These values are in the same data space as the plot. 
forrest@0
   288
;
forrest@0
   289
; xlg1_cen = 0.2
forrest@0
   290
; ylg1_cen = 900.
forrest@0
   291
forrest@0
   292
; xlg2_cen = 0.2
forrest@0
   293
; ylg2_cen = 760.
forrest@0
   294
forrest@0
   295
; mkres@gsMarkerColor = "brown"
forrest@0
   296
; lnres@gsLineColor   = "brown"
forrest@0
   297
forrest@0
   298
; lg_mark_legend1 = gsn_add_polymarker(wks,xy,xlg1_cen,ylg1_cen,mkres)
forrest@0
   299
; lg_line_legend1 = gsn_add_polyline(wks,xy,(/xlg1_cen,xlg1_cen/), \
forrest@0
   300
;                                           (/ylg1_cen-60,ylg1_cen+60/),lnres)
forrest@0
   301
; lg_cap_legend11  = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \
forrest@0
   302
;                                           (/ylg1_cen-60,ylg1_cen-60/),lnres)
forrest@0
   303
; lg_cap_legend12  = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \
forrest@0
   304
;                                           (/ylg1_cen+60,ylg1_cen+60/),lnres)
forrest@0
   305
forrest@0
   306
; tx_legend1 = gsn_add_text(wks,xy,"observed",xlg1_cen+0.15,ylg1_cen,txres)
forrest@0
   307
forrest@0
   308
; mkres@gsMarkerColor = "blue"
forrest@0
   309
; lnres@gsLineColor   = "blue"
forrest@0
   310
forrest@0
   311
; lg_mark_legend2 = gsn_add_polymarker(wks,xy,xlg2_cen,ylg2_cen,mkres)
forrest@0
   312
; lg_line_legend2 = gsn_add_polyline(wks,xy,(/xlg2_cen,xlg2_cen/), \
forrest@0
   313
;                                           (/ylg2_cen-60,ylg2_cen+60/),lnres)
forrest@0
   314
; lg_cap_legend21  = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \
forrest@0
   315
;                                           (/ylg2_cen-60,ylg2_cen-60/),lnres)
forrest@0
   316
; lg_cap_legend22  = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \
forrest@0
   317
;                                           (/ylg2_cen+60,ylg2_cen+60/),lnres)
forrest@0
   318
; tx_legend2 = gsn_add_text(wks,xy,"model_i01.03cn",xlg2_cen+0.15,ylg2_cen,txres)
forrest@0
   319
forrest@0
   320
  draw(xy)
forrest@0
   321
  frame(wks)
forrest@0
   322
  system("convert xy.ps xy.png")
forrest@0
   323
forrest@0
   324
 u = yvalues(0,:)
forrest@0
   325
 v = yvalues(1,:)
forrest@0
   326
forrest@0
   327
 good = ind(.not.ismissing(u) .and. .not.ismissing(v))
forrest@0
   328
 uu = u(good)
forrest@0
   329
 vv = v(good)
forrest@0
   330
 nz = dimsizes(uu)
forrest@0
   331
 print (nz)
forrest@0
   332
forrest@0
   333
 ccr = esccr(uu,vv,0)
forrest@0
   334
 print (ccr)
forrest@0
   335
;old eq
forrest@0
   336
;bias = sum(((vv-uu)/uu)^2)
forrest@0
   337
;M    = (1.- sqrt(bias/nz))*5.
forrest@0
   338
forrest@0
   339
;new eq
forrest@0
   340
 bias = sum(abs(vv-uu)/(vv+uu))
forrest@0
   341
 M    = (1.- (bias/nz))*5.
forrest@0
   342
forrest@0
   343
 print (bias)
forrest@0
   344
 print (M)
forrest@0
   345
forrest@0
   346
end
forrest@0
   347