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