npp/23.histogram.ncl
author Forrest Hoffman <forrest@climatemodeling.org>
Mon, 26 Jan 2009 22:08:20 -0500
changeset 0 0c6405ab2ff4
permissions -rw-r--r--
Initial commit of C-LAMP Diagnostics from Jeff Lee
forrest@0
     1
;********************************************************
forrest@0
     2
; histogram normalized by rain 
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
  data_types    = (/ "Obs",        "Model"       /)
forrest@0
    21
; data_names    = (/ "data.81.nc" , "i01.03cn_1545-1569_ANN_climo.nc" /)
forrest@0
    22
; data_names    = (/ "data.933.nc", "i01.03cn_1545-1569_ANN_climo.nc" /)
forrest@0
    23
; data_names    = (/ "data.81.nc" , "i01.04casa_1605-1629_ANN_climo.nc" /)
forrest@0
    24
  data_names    = (/ "data.933.nc", "i01.04casa_1605-1629_ANN_climo.nc" /)
forrest@0
    25
  filevar_names = (/ (/"PREC_ANN","TNPP_C"/), (/"RAIN","NPP"/) /)
forrest@0
    26
  ndata_types   = dimsizes(data_types)
forrest@0
    27
      
forrest@0
    28
  data_file_obs = addfile(data_names(0),"r")    ; Open obs file
forrest@0
    29
  data_file_mod = addfile(data_names(1),"r")    ; Open model file
forrest@0
    30
forrest@0
    31
;************************************************
forrest@0
    32
; read in data: observed
forrest@0
    33
;************************************************
forrest@0
    34
;RAIN1 = tofloat(data_file_obs->PREC_ANN) ; for data.81
forrest@0
    35
 RAIN1 = data_file_obs->PREC              ; for data.933
forrest@0
    36
 NPP1  = data_file_obs->TNPP_C
forrest@0
    37
 xo    = data_file_obs->LONG_DD  
forrest@0
    38
 yo    = data_file_obs->LAT_DD   
forrest@0
    39
forrest@0
    40
; change longitude from (-180,180) to (0,360)
forrest@0
    41
 print (xo)
forrest@0
    42
 nx = dimsizes(xo)
forrest@0
    43
 do i= 0,nx-1
forrest@0
    44
    if (xo(i) .lt. 0.) then
forrest@0
    45
        xo(i) = xo(i)+ 360.
forrest@0
    46
    end if
forrest@0
    47
 end do
forrest@0
    48
 print (xo)
forrest@0
    49
 
forrest@0
    50
;************************************************
forrest@0
    51
; read in data: model       
forrest@0
    52
;************************************************
forrest@0
    53
 ai    = data_file_mod->RAIN
forrest@0
    54
 bi    = data_file_mod->NPP
forrest@0
    55
 xi    = data_file_mod->lon      
forrest@0
    56
 yi    = data_file_mod->lat      
forrest@0
    57
forrest@0
    58
;************************************************
forrest@0
    59
; interpolate from model grid to observed grid
forrest@0
    60
;************************************************
forrest@0
    61
 RAIN2 = linint2_points(xi,yi,ai,True,xo,yo,0) 
forrest@0
    62
 NPP2  = linint2_points(xi,yi,bi,True,xo,yo,0) 
forrest@0
    63
 
forrest@0
    64
;************************************************
forrest@0
    65
; convert unit
forrest@0
    66
;************************************************
forrest@0
    67
; Units for these four variables are:
forrest@0
    68
;
forrest@0
    69
; RAIN1 : mm/year
forrest@0
    70
; RAIN2 : mm/s
forrest@0
    71
; NPP1  : g C/m^2/year
forrest@0
    72
; NPP2  : g C/m^2/s
forrest@0
    73
;
forrest@0
    74
; We want to convert these to "m/year" and "g C/m^2/year".
forrest@0
    75
;
forrest@0
    76
  nsec_per_year = 60*60*24*365                  ; # seconds per year
forrest@0
    77
forrest@0
    78
; Do the necessary conversions.
forrest@0
    79
  RAIN1 = RAIN1 / 1000.
forrest@0
    80
  RAIN2 = (RAIN2/ 1000.) * nsec_per_year
forrest@0
    81
  NPP2     = NPP2 * nsec_per_year
forrest@0
    82
forrest@0
    83
; Redo the units.
forrest@0
    84
  RAIN1@units = "m/yr"
forrest@0
    85
  RAIN2@units = "m/yr"
forrest@0
    86
  NPP1@units  = "gC/m^2/yr"
forrest@0
    87
  NPP2@units  = "gC/m^2/yr"
forrest@0
    88
forrest@0
    89
;************************************************
forrest@0
    90
; print min/max and unit
forrest@0
    91
;************************************************
forrest@0
    92
  pminmax(RAIN1,"RAIN1")
forrest@0
    93
  pminmax(RAIN2,"RAIN2")
forrest@0
    94
  pminmax(NPP1,"NPP1")
forrest@0
    95
  pminmax(NPP2,"NPP2")
forrest@0
    96
forrest@0
    97
  RAIN1_1D = ndtooned(RAIN1)
forrest@0
    98
  RAIN2_1D = ndtooned(RAIN2)
forrest@0
    99
  NPP1_1D  = ndtooned(NPP1)
forrest@0
   100
  NPP2_1D  = ndtooned(NPP2)
forrest@0
   101
;
forrest@0
   102
; Calculate some "nice" bins for binning the data in equally spaced
forrest@0
   103
; ranges.
forrest@0
   104
;
forrest@0
   105
  nbins       = 15     ; Number of bins to use.
forrest@0
   106
forrest@0
   107
  nicevals    = nice_mnmxintvl(min(RAIN2_1D),max(RAIN2_1D),nbins,True)
forrest@0
   108
  nvals       = floattoint((nicevals(1) - nicevals(0))/nicevals(2) + 1)
forrest@0
   109
  range       = fspan(nicevals(0),nicevals(1),nvals)
forrest@0
   110
;
forrest@0
   111
; Use this range information to grab all the values in a
forrest@0
   112
; particular range, and then take an average.
forrest@0
   113
;
forrest@0
   114
  nr      = dimsizes(range)
forrest@0
   115
  nx      = nr-1
forrest@0
   116
  xvalues     = new((/2,nx/),typeof(RAIN2_1D))
forrest@0
   117
  xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
forrest@0
   118
  dx           = xvalues(0,1) - xvalues(0,0)       ; range width
forrest@0
   119
  dx4          = dx/4                              ; 1/4 of the range
forrest@0
   120
  xvalues(1,:) = xvalues(0,:) - dx/5.
forrest@0
   121
  yvalues      = new((/2,nx/),typeof(RAIN2_1D))
forrest@0
   122
  mn_yvalues   = new((/2,nx/),typeof(RAIN2_1D))
forrest@0
   123
  mx_yvalues   = new((/2,nx/),typeof(RAIN2_1D))
forrest@0
   124
forrest@0
   125
  do nd=0,1
forrest@0
   126
;
forrest@0
   127
; See if we are doing model or observational data.
forrest@0
   128
;
forrest@0
   129
    if(nd.eq.0) then
forrest@0
   130
      data     = RAIN1_1D
forrest@0
   131
      npp_data = NPP1_1D
forrest@0
   132
    else
forrest@0
   133
      data     = RAIN2_1D
forrest@0
   134
      npp_data = NPP2_1D
forrest@0
   135
    end if
forrest@0
   136
;
forrest@0
   137
; Loop through each range and check for values.
forrest@0
   138
;
forrest@0
   139
    do i=0,nr-2
forrest@0
   140
      if (i.ne.(nr-2)) then
forrest@0
   141
;        print("")
forrest@0
   142
;        print("In range ["+range(i)+","+range(i+1)+")")
forrest@0
   143
        idx = ind((range(i).le.data).and.(data.lt.range(i+1)))
forrest@0
   144
      else
forrest@0
   145
;        print("")
forrest@0
   146
;        print("In range ["+range(i)+",)")
forrest@0
   147
        idx = ind(range(i).le.data)
forrest@0
   148
      end if
forrest@0
   149
;
forrest@0
   150
; Calculate average, and get min and max.
forrest@0
   151
;
forrest@0
   152
      if(.not.any(ismissing(idx))) then
forrest@0
   153
        yvalues(nd,i)    = avg(npp_data(idx))
forrest@0
   154
        mn_yvalues(nd,i) = min(npp_data(idx))
forrest@0
   155
        mx_yvalues(nd,i) = max(npp_data(idx))
forrest@0
   156
        count = dimsizes(idx)
forrest@0
   157
      else
forrest@0
   158
        count            = 0
forrest@0
   159
        yvalues(nd,i)    = yvalues@_FillValue
forrest@0
   160
        mn_yvalues(nd,i) = yvalues@_FillValue
forrest@0
   161
        mx_yvalues(nd,i) = yvalues@_FillValue
forrest@0
   162
      end if
forrest@0
   163
;
forrest@0
   164
; Print out information.
forrest@0
   165
;
forrest@0
   166
;      print(data_types(nd) + ": " + count + " points, avg = " + yvalues(nd,i))
forrest@0
   167
;      print("Min/Max:  " + mn_yvalues(nd,i) + "/" + mx_yvalues(nd,i))
forrest@0
   168
forrest@0
   169
;
forrest@0
   170
; Clean up for next time in loop.
forrest@0
   171
;
forrest@0
   172
      delete(idx)
forrest@0
   173
    end do
forrest@0
   174
    delete(data)
forrest@0
   175
    delete(npp_data)
forrest@0
   176
  end do
forrest@0
   177
forrest@0
   178
;
forrest@0
   179
; Start the graphics.
forrest@0
   180
;
forrest@0
   181
  wks = gsn_open_wks("png","npp")
forrest@0
   182
forrest@0
   183
  res             = True
forrest@0
   184
  res@gsnMaximize = True
forrest@0
   185
  res@gsnDraw     = False
forrest@0
   186
  res@gsnFrame    = False
forrest@0
   187
  res@xyMarkLineMode = "Markers"
forrest@0
   188
  res@xyMarkerSizeF   = 0.014
forrest@0
   189
  res@xyMarker       = 16
forrest@0
   190
  res@xyMarkerColors = (/"Brown","Blue"/)
forrest@0
   191
  res@trYMinF        = min(mn_yvalues) - 10.
forrest@0
   192
  res@trYMaxF        = max(mx_yvalues) + 10.
forrest@0
   193
forrest@0
   194
; res@tiMainString  = "Observerd vs i01.03cn 81site"
forrest@0
   195
; res@tiMainString  = "Observerd vs i01.03cn 933site"
forrest@0
   196
; res@tiMainString  = "Observerd vs i01.04casa_81site"
forrest@0
   197
  res@tiMainString  = "Observerd vs i01.04casa 933site"
forrest@0
   198
  res@tiYAxisString = "NPP (g C/m2/year)"
forrest@0
   199
  res@tiXAxisString = "Precipitation (m/year)"
forrest@0
   200
forrest@0
   201
;
forrest@0
   202
; Add a boxed legend using the more simple method, which won't have
forrest@0
   203
; vertical lines going through the markers.
forrest@0
   204
;
forrest@0
   205
  res@pmLegendDisplayMode    = "Always"
forrest@0
   206
; res@pmLegendWidthF         = 0.1
forrest@0
   207
  res@pmLegendWidthF         = 0.08
forrest@0
   208
  res@pmLegendHeightF        = 0.05
forrest@0
   209
  res@pmLegendOrthogonalPosF = -1.17
forrest@0
   210
; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
forrest@0
   211
; res@pmLegendParallelPosF   =  0.18
forrest@0
   212
  res@pmLegendParallelPosF   =  0.23  ;(rightward)
forrest@0
   213
forrest@0
   214
;  res@lgPerimOn             = False
forrest@0
   215
  res@lgLabelFontHeightF     = 0.015
forrest@0
   216
; res@xyExplicitLegendLabels = (/"observed","model_i01.03cn"/)
forrest@0
   217
  res@xyExplicitLegendLabels = (/"observed","model_i01.04casa"/)
forrest@0
   218
forrest@0
   219
  xy = gsn_csm_xy(wks,xvalues,yvalues,res)
forrest@0
   220
forrest@0
   221
  max_bar = new((/2,nx/),graphic)
forrest@0
   222
  min_bar = new((/2,nx/),graphic)
forrest@0
   223
  max_cap = new((/2,nx/),graphic)
forrest@0
   224
  min_cap = new((/2,nx/),graphic)
forrest@0
   225
forrest@0
   226
  lnres = True
forrest@0
   227
forrest@0
   228
  line_colors = (/"brown","blue"/)
forrest@0
   229
  do nd=0,1
forrest@0
   230
    lnres@gsLineColor = line_colors(nd)
forrest@0
   231
    do i=0,nx-1
forrest@0
   232
     
forrest@0
   233
      if(.not.ismissing(mn_yvalues(nd,i)).and. \
forrest@0
   234
         .not.ismissing(mx_yvalues(nd,i))) then
forrest@0
   235
;
forrest@0
   236
; Attach the vertical bar, both above and below the marker.
forrest@0
   237
;
forrest@0
   238
        x1 = xvalues(nd,i)
forrest@0
   239
        y1 = yvalues(nd,i)
forrest@0
   240
        y2 = mn_yvalues(nd,i)
forrest@0
   241
        min_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
forrest@0
   242
forrest@0
   243
        y2 = mx_yvalues(nd,i)
forrest@0
   244
        max_bar(nd,i) = gsn_add_polyline(wks,xy,(/x1,x1/),(/y1,y2/),lnres)
forrest@0
   245
;
forrest@0
   246
; Attach the horizontal cap line, both above and below the marker.
forrest@0
   247
;
forrest@0
   248
        x1 = xvalues(nd,i) - dx4
forrest@0
   249
        x2 = xvalues(nd,i) + dx4
forrest@0
   250
        y1 = mn_yvalues(nd,i)
forrest@0
   251
        min_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
forrest@0
   252
forrest@0
   253
        y1 = mx_yvalues(nd,i)
forrest@0
   254
        max_cap(nd,i) = gsn_add_polyline(wks,xy,(/x1,x2/),(/y1,y1/),lnres)
forrest@0
   255
      end if
forrest@0
   256
    end do
forrest@0
   257
  end do
forrest@0
   258
forrest@0
   259
;
forrest@0
   260
; Here's how to do the legend by hand.
forrest@0
   261
;
forrest@0
   262
; mkres               = True    ; Marker resources
forrest@0
   263
; txres               = True    ; Text resources
forrest@0
   264
; mkres@gsMarkerIndex = 16
forrest@0
   265
; mkres@gsMarkerSizeF = 0.02
forrest@0
   266
; txres@txFontHeightF = 0.02
forrest@0
   267
; txres@txJust        = "CenterLeft"
forrest@0
   268
;
forrest@0
   269
; Change these values if you want to move the marker legend location.
forrest@0
   270
; These values are in the same data space as the plot. 
forrest@0
   271
;
forrest@0
   272
; xlg1_cen = 0.2
forrest@0
   273
; ylg1_cen = 900.
forrest@0
   274
forrest@0
   275
; xlg2_cen = 0.2
forrest@0
   276
; ylg2_cen = 760.
forrest@0
   277
forrest@0
   278
; mkres@gsMarkerColor = "brown"
forrest@0
   279
; lnres@gsLineColor   = "brown"
forrest@0
   280
forrest@0
   281
; lg_mark_legend1 = gsn_add_polymarker(wks,xy,xlg1_cen,ylg1_cen,mkres)
forrest@0
   282
; lg_line_legend1 = gsn_add_polyline(wks,xy,(/xlg1_cen,xlg1_cen/), \
forrest@0
   283
;                                           (/ylg1_cen-60,ylg1_cen+60/),lnres)
forrest@0
   284
; lg_cap_legend11  = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \
forrest@0
   285
;                                           (/ylg1_cen-60,ylg1_cen-60/),lnres)
forrest@0
   286
; lg_cap_legend12  = gsn_add_polyline(wks,xy,(/xlg1_cen-0.1,xlg1_cen+0.1/), \
forrest@0
   287
;                                           (/ylg1_cen+60,ylg1_cen+60/),lnres)
forrest@0
   288
forrest@0
   289
; tx_legend1 = gsn_add_text(wks,xy,"observed",xlg1_cen+0.15,ylg1_cen,txres)
forrest@0
   290
forrest@0
   291
; mkres@gsMarkerColor = "blue"
forrest@0
   292
; lnres@gsLineColor   = "blue"
forrest@0
   293
forrest@0
   294
; lg_mark_legend2 = gsn_add_polymarker(wks,xy,xlg2_cen,ylg2_cen,mkres)
forrest@0
   295
; lg_line_legend2 = gsn_add_polyline(wks,xy,(/xlg2_cen,xlg2_cen/), \
forrest@0
   296
;                                           (/ylg2_cen-60,ylg2_cen+60/),lnres)
forrest@0
   297
; lg_cap_legend21  = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \
forrest@0
   298
;                                           (/ylg2_cen-60,ylg2_cen-60/),lnres)
forrest@0
   299
; lg_cap_legend22  = gsn_add_polyline(wks,xy,(/xlg2_cen-0.1,xlg2_cen+0.1/), \
forrest@0
   300
;                                           (/ylg2_cen+60,ylg2_cen+60/),lnres)
forrest@0
   301
; tx_legend2 = gsn_add_text(wks,xy,"model_i01.03cn",xlg2_cen+0.15,ylg2_cen,txres)
forrest@0
   302
forrest@0
   303
  draw(xy)
forrest@0
   304
  frame(wks)
forrest@0
   305
forrest@0
   306
end
forrest@0
   307