npp/21.histogram.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
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
forrest@0
     2
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
forrest@0
     3
forrest@0
     4
procedure pminmax(data:numeric,name:string)
forrest@0
     5
begin
forrest@0
     6
  print ("min/max " + name + " = " + min(data) + "/" + max(data))
forrest@0
     7
  if(isatt(data,"units")) then
forrest@0
     8
    print (name + " units = " + data@units)
forrest@0
     9
  end if
forrest@0
    10
end
forrest@0
    11
forrest@0
    12
;
forrest@0
    13
; Main code.
forrest@0
    14
;
forrest@0
    15
begin
forrest@0
    16
  data_types    = (/ "Obs",        "Model"       /)
forrest@0
    17
  data_names    = (/ "data.81.nc", "i01.03cn_1545-1569_ANN_climo.nc" /)
forrest@0
    18
; data_names    = (/ "data.81.nc", "i01.04casa_1605-1629_ANN_climo.nc" /)
forrest@0
    19
  filevar_names = (/ (/"PREC_ANN","TNPP_C"/), (/"RAIN","NPP"/) /)
forrest@0
    20
  ndata_types   = dimsizes(data_types)
forrest@0
    21
forrest@0
    22
  data_file_obs = addfile(data_names(0),"r")    ; Open obs file
forrest@0
    23
  data_file_mod = addfile(data_names(1),"r")    ; Open model file
forrest@0
    24
forrest@0
    25
;
forrest@0
    26
; Read four variables from files.
forrest@0
    27
;
forrest@0
    28
  PREC_ANN = tofloat(data_file_obs->PREC_ANN)
forrest@0
    29
  TNPP_C   = data_file_obs->TNPP_C
forrest@0
    30
  RAIN     = data_file_mod->RAIN
forrest@0
    31
  NPP    = data_file_mod->NPP
forrest@0
    32
;
forrest@0
    33
; Units for these four variables are:
forrest@0
    34
;
forrest@0
    35
; PREC_ANN : mm/year
forrest@0
    36
; TNPP_C   : g C/m^2/year
forrest@0
    37
; RAIN     : mm/s
forrest@0
    38
; NPP    : g C/m^2/s
forrest@0
    39
;
forrest@0
    40
; We want to convert these to "m/year" and "g C/m^2/year".
forrest@0
    41
;
forrest@0
    42
  nsec_per_year = 60*60*24*365                  ; # seconds per year
forrest@0
    43
forrest@0
    44
; Do the necessary conversions.
forrest@0
    45
  PREC_ANN = PREC_ANN / 1000.
forrest@0
    46
  RAIN     = (RAIN / 1000.) * nsec_per_year
forrest@0
    47
  NPP    = NPP * nsec_per_year
forrest@0
    48
forrest@0
    49
; Redo the units.
forrest@0
    50
  PREC_ANN@units = "m/yr"
forrest@0
    51
  RAIN@units     = "m/yr"
forrest@0
    52
  NPP@units    = "gC/m^2/yr"
forrest@0
    53
  TNPP_C@units   = "gC/m^2/yr"
forrest@0
    54
forrest@0
    55
  pminmax(PREC_ANN,"PREC_ANN")
forrest@0
    56
  pminmax(TNPP_C,"TNPP_C")
forrest@0
    57
  pminmax(RAIN,"RAIN")
forrest@0
    58
  pminmax(NPP,"NPP")
forrest@0
    59
forrest@0
    60
  RAIN_1D     = ndtooned(RAIN)
forrest@0
    61
  NPP_1D    = ndtooned(NPP)
forrest@0
    62
  PREC_ANN_1D = ndtooned(PREC_ANN)
forrest@0
    63
  TNPP_C_1D   = ndtooned(TNPP_C)
forrest@0
    64
forrest@0
    65
;
forrest@0
    66
; Calculate some "nice" bins for binning the data in equally spaced
forrest@0
    67
; ranges.
forrest@0
    68
;
forrest@0
    69
  nbins       = 15     ; Number of bins to use.
forrest@0
    70
forrest@0
    71
  nicevals    = nice_mnmxintvl(min(RAIN_1D),max(RAIN_1D),nbins,True)
forrest@0
    72
  nvals       = floattoint((nicevals(1) - nicevals(0))/nicevals(2) + 1)
forrest@0
    73
  range       = fspan(nicevals(0),nicevals(1),nvals)
forrest@0
    74
;
forrest@0
    75
; Use this range information to grab all the values in a
forrest@0
    76
; particular range, and then take an average.
forrest@0
    77
;
forrest@0
    78
  nr      = dimsizes(range)
forrest@0
    79
  nx      = nr-1
forrest@0
    80
  xvalues     = new((/2,nx/),typeof(RAIN_1D))
forrest@0
    81
  xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
forrest@0
    82
  dx           = xvalues(0,1) - xvalues(0,0)       ; range width
forrest@0
    83
  dx4          = dx/4                              ; 1/4 of the range
forrest@0
    84
  xvalues(1,:) = xvalues(0,:) - dx/5.
forrest@0
    85
  yvalues      = new((/2,nx/),typeof(RAIN_1D))
forrest@0
    86
  mn_yvalues   = new((/2,nx/),typeof(RAIN_1D))
forrest@0
    87
  mx_yvalues   = new((/2,nx/),typeof(RAIN_1D))
forrest@0
    88
forrest@0
    89
  do nd=0,1
forrest@0
    90
;
forrest@0
    91
; See if we are doing model or observational data.
forrest@0
    92
;
forrest@0
    93
    if(nd.eq.0) then
forrest@0
    94
      data     = PREC_ANN_1D
forrest@0
    95
      npp_data = TNPP_C_1D
forrest@0
    96
    else
forrest@0
    97
      data     = RAIN_1D
forrest@0
    98
      npp_data = NPP_1D
forrest@0
    99
    end if
forrest@0
   100
;
forrest@0
   101
; Loop through each range and check for values.
forrest@0
   102
;
forrest@0
   103
    do i=0,nr-2
forrest@0
   104
      if (i.ne.(nr-2)) then
forrest@0
   105
        print("")
forrest@0
   106
        print("In range ["+range(i)+","+range(i+1)+")")
forrest@0
   107
        idx = ind((range(i).le.data).and.(data.lt.range(i+1)))
forrest@0
   108
      else
forrest@0
   109
        print("")
forrest@0
   110
        print("In range ["+range(i)+",)")
forrest@0
   111
        idx = ind(range(i).le.data)
forrest@0
   112
      end if
forrest@0
   113
;
forrest@0
   114
; Calculate average, and get min and max.
forrest@0
   115
;
forrest@0
   116
      if(.not.any(ismissing(idx))) then
forrest@0
   117
        yvalues(nd,i)    = avg(npp_data(idx))
forrest@0
   118
        mn_yvalues(nd,i) = min(npp_data(idx))
forrest@0
   119
        mx_yvalues(nd,i) = max(npp_data(idx))
forrest@0
   120
        count = dimsizes(idx)
forrest@0
   121
      else
forrest@0
   122
        count            = 0
forrest@0
   123
        yvalues(nd,i)    = yvalues@_FillValue
forrest@0
   124
        mn_yvalues(nd,i) = yvalues@_FillValue
forrest@0
   125
        mx_yvalues(nd,i) = yvalues@_FillValue
forrest@0
   126
      end if
forrest@0
   127
;
forrest@0
   128
; Print out information.
forrest@0
   129
;
forrest@0
   130
      print(data_types(nd) + ": " + count + " points, avg = " + yvalues(nd,i))
forrest@0
   131
      print("Min/Max:  " + mn_yvalues(nd,i) + "/" + mx_yvalues(nd,i))
forrest@0
   132
forrest@0
   133
;
forrest@0
   134
; Clean up for next time in loop.
forrest@0
   135
;
forrest@0
   136
      delete(idx)
forrest@0
   137
    end do
forrest@0
   138
    delete(data)
forrest@0
   139
    delete(npp_data)
forrest@0
   140
  end do
forrest@0
   141
forrest@0
   142
  xvalues@long_name = "Mean Annual precipitation (m/year)"
forrest@0
   143
  yvalues@long_name = "NPP (g C/m2/year)"                 
forrest@0
   144
 
forrest@0
   145
;
forrest@0
   146
; Start the graphics.
forrest@0
   147
;
forrest@0
   148
; wks = gsn_open_wks("x11","npp")
forrest@0
   149
  wks = gsn_open_wks("png","npp")
forrest@0
   150
forrest@0
   151
  res             = True
forrest@0
   152
  res@tiMainString = "Observed vs i01.03cn"
forrest@0
   153
; res@tiMainString = "Observed vs i01.04casa"
forrest@0
   154
  res@gsnMaximize = False
forrest@0
   155
  res@gsnDraw     = False
forrest@0
   156
  res@gsnFrame    = False
forrest@0
   157
  res@xyMarkLineMode = "Markers"
forrest@0
   158
  res@xyMarkerSizeF   = 0.014
forrest@0
   159
  res@xyMarker       = 16
forrest@0
   160
; res@xyMarkerColors = (/"Gray25","Gray50"/)
forrest@0
   161
  res@xyMarkerColors = (/"brown","blue"/)
forrest@0
   162
  res@trYMinF        = min(mn_yvalues) - 10.
forrest@0
   163
  res@trYMaxF        = max(mx_yvalues) + 10.
forrest@0
   164
forrest@0
   165
  xy = gsn_csm_xy(wks,xvalues,yvalues,res)
forrest@0
   166
forrest@0
   167
  max_bar = new((/2,nx/),graphic)
forrest@0
   168
  min_bar = new((/2,nx/),graphic)
forrest@0
   169
  max_cap = new((/2,nx/),graphic)
forrest@0
   170
  min_cap = new((/2,nx/),graphic)
forrest@0
   171
forrest@0
   172
  lnres = True
forrest@0
   173
forrest@0
   174
  line_colors = (/"brown","blue"/)
forrest@0
   175
  do nd=0,1
forrest@0
   176
    lnres@gsLineColor = line_colors(nd)
forrest@0
   177
    do i=0,nx-1
forrest@0
   178
     
forrest@0
   179
      if(.not.ismissing(mn_yvalues(nd,i)).and. \
forrest@0
   180
         .not.ismissing(mx_yvalues(nd,i))) then
forrest@0
   181
;
forrest@0
   182
; Attach the vertical bar, both above and below the marker.
forrest@0
   183
;
forrest@0
   184
        x1 = xvalues(nd,i)
forrest@0
   185
        y1 = yvalues(nd,i)
forrest@0
   186
        y2 = mn_yvalues(nd,i)
forrest@0
   187
        min_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
forrest@0
   188
forrest@0
   189
        y2 = mx_yvalues(nd,i)
forrest@0
   190
        max_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
forrest@0
   191
;
forrest@0
   192
; Attach the horizontal cap line, both above and below the marker.
forrest@0
   193
;
forrest@0
   194
        x1 = xvalues(nd,i) - dx4
forrest@0
   195
        x2 = xvalues(nd,i) + dx4
forrest@0
   196
        y1 = mn_yvalues(nd,i)
forrest@0
   197
        min_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
forrest@0
   198
forrest@0
   199
        y1 = mx_yvalues(nd,i)
forrest@0
   200
        max_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
forrest@0
   201
      end if
forrest@0
   202
    end do
forrest@0
   203
  end do
forrest@0
   204
forrest@0
   205
  draw(xy)
forrest@0
   206
  frame(wks)
forrest@0
   207
forrest@0
   208
end
forrest@0
   209