beta/04.biome.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
; required command line input parameters:
forrest@0
     3
;  ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
forrest@0
     4
;
forrest@0
     5
; histogram normalized by rain and compute correleration
forrest@0
     6
;**************************************************************
forrest@0
     7
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl.test"
forrest@0
     8
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl.test"
forrest@0
     9
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
forrest@0
    10
;**************************************************************
forrest@0
    11
procedure set_line(lines:string,nline:integer,newlines:string) 
forrest@0
    12
begin
forrest@0
    13
; add line to ascci/html file
forrest@0
    14
    
forrest@0
    15
  nnewlines = dimsizes(newlines)
forrest@0
    16
  if(nline+nnewlines-1.ge.dimsizes(lines))
forrest@0
    17
    print("set_line: bad index, not setting anything.") 
forrest@0
    18
    return
forrest@0
    19
  end if 
forrest@0
    20
  lines(nline:nline+nnewlines-1) = newlines
forrest@0
    21
;  print ("lines = " + lines(nline:nline+nnewlines-1))
forrest@0
    22
  nline = nline + nnewlines
forrest@0
    23
  return 
forrest@0
    24
end
forrest@0
    25
;**************************************************************
forrest@0
    26
; Main code.
forrest@0
    27
begin
forrest@0
    28
 
forrest@0
    29
  nclass = 20
forrest@0
    30
forrest@0
    31
  plot_type     = "ps"
forrest@0
    32
  plot_type_new = "png"
forrest@0
    33
;************************************************
forrest@0
    34
; read data: model       
forrest@0
    35
;************************************************
forrest@0
    36
 co2_i = 283.1878
forrest@0
    37
 co2_f = 364.1252
forrest@0
    38
forrest@0
    39
 model_grid = "T42"
forrest@0
    40
forrest@0
    41
;model_name_i = "i01.07cn"
forrest@0
    42
;model_name_f = "i01.10cn"
forrest@0
    43
forrest@0
    44
 model_name_i = "i01.07casa"
forrest@0
    45
 model_name_f = "i01.10casa"
forrest@0
    46
forrest@0
    47
 model_name = model_name_f
forrest@0
    48
forrest@0
    49
 dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
    50
 film_i = model_name_i + "_1990-2004_ANN_climo.nc"
forrest@0
    51
 film_f = model_name_f + "_1990-2004_ANN_climo.nc"
forrest@0
    52
forrest@0
    53
 fm_i   = addfile (dirm+film_i,"r")
forrest@0
    54
 fm_f   = addfile (dirm+film_f,"r")
forrest@0
    55
  
forrest@0
    56
 npp_i  = fm_i->NPP
forrest@0
    57
 npp_f  = fm_f->NPP
forrest@0
    58
      
forrest@0
    59
;************************************************
forrest@0
    60
; read data: observed
forrest@0
    61
;************************************************
forrest@0
    62
forrest@0
    63
  ob_name = "MODIS MOD 15A2 2000-2005"
forrest@0
    64
forrest@0
    65
  diro  = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/"
forrest@0
    66
  filo  = "land_class_"+model_grid+".nc"
forrest@0
    67
forrest@0
    68
  fo = addfile(diro+filo,"r")
forrest@0
    69
 
forrest@0
    70
  classob = tofloat(fo->LAND_CLASS)                
forrest@0
    71
forrest@0
    72
;*******************************************************************
forrest@0
    73
; Calculate "nice" bins for binning the data in equally spaced ranges
forrest@0
    74
;********************************************************************
forrest@0
    75
  nclassn     = nclass + 1
forrest@0
    76
  range       = fspan(0,nclassn-1,nclassn)
forrest@0
    77
; print (range)
forrest@0
    78
forrest@0
    79
; Use this range information to grab all the values in a
forrest@0
    80
; particular range, and then take an average.
forrest@0
    81
forrest@0
    82
  nr           = dimsizes(range)
forrest@0
    83
  nx           = nr-1
forrest@0
    84
  xvalues      = new((/2,nx/),float)
forrest@0
    85
  xvalues(0,:) = range(0:nr-2) + (range(1:)-range(0:nr-2))/2.
forrest@0
    86
  dx           = xvalues(0,1) - xvalues(0,0)       ; range width
forrest@0
    87
  dx4          = dx/4                              ; 1/4 of the range
forrest@0
    88
  xvalues(1,:) = xvalues(0,:) - dx/5.
forrest@0
    89
forrest@0
    90
; get data
forrest@0
    91
forrest@0
    92
  DATA11_1D = ndtooned(classob)
forrest@0
    93
  DATA12_1D = ndtooned(npp_i)
forrest@0
    94
  DATA22_1D = ndtooned(npp_f)
forrest@0
    95
forrest@0
    96
  yvalues      = new((/2,nx/),float)
forrest@0
    97
  mn_yvalues   = new((/2,nx/),float)
forrest@0
    98
  mx_yvalues   = new((/2,nx/),float)
forrest@0
    99
forrest@0
   100
  do nd=0,1
forrest@0
   101
forrest@0
   102
; See if we are doing model or observational data.
forrest@0
   103
forrest@0
   104
    if(nd.eq.0) then
forrest@0
   105
      data_ob  = DATA11_1D
forrest@0
   106
      data_mod = DATA12_1D
forrest@0
   107
    else
forrest@0
   108
      data_ob  = DATA11_1D
forrest@0
   109
      data_mod = DATA22_1D
forrest@0
   110
    end if
forrest@0
   111
forrest@0
   112
; Loop through each range and check for values.
forrest@0
   113
forrest@0
   114
    do i=0,nr-2
forrest@0
   115
      if (i.ne.(nr-2)) then
forrest@0
   116
;        print("")
forrest@0
   117
;        print("In range ["+range(i)+","+range(i+1)+")")
forrest@0
   118
         idx = ind((data_ob.ge.range(i)).and.(data_ob.lt.range(i+1)))
forrest@0
   119
      else
forrest@0
   120
;        print("")
forrest@0
   121
;        print("In range ["+range(i)+",)")
forrest@0
   122
         idx = ind(data_ob.ge.range(i))
forrest@0
   123
      end if
forrest@0
   124
forrest@0
   125
; Calculate average, and get min and max.
forrest@0
   126
forrest@0
   127
      if(.not.any(ismissing(idx))) then
forrest@0
   128
        yvalues(nd,i)    = avg(data_mod(idx))
forrest@0
   129
        mn_yvalues(nd,i) = min(data_mod(idx))
forrest@0
   130
        mx_yvalues(nd,i) = max(data_mod(idx))
forrest@0
   131
        count = dimsizes(idx)
forrest@0
   132
      else
forrest@0
   133
        count            = 0
forrest@0
   134
        yvalues(nd,i)    = yvalues@_FillValue
forrest@0
   135
        mn_yvalues(nd,i) = yvalues@_FillValue
forrest@0
   136
        mx_yvalues(nd,i) = yvalues@_FillValue
forrest@0
   137
      end if
forrest@0
   138
forrest@0
   139
;     print(nd + ": " + count + " points, avg = " + yvalues(nd,i))
forrest@0
   140
;     print("Min/Max:  " + mn_yvalues(nd,i) + "/" + mx_yvalues(nd,i))
forrest@0
   141
forrest@0
   142
; Clean up for next time in loop.
forrest@0
   143
forrest@0
   144
      delete(idx)
forrest@0
   145
    end do
forrest@0
   146
    delete(data_ob)
forrest@0
   147
    delete(data_mod)
forrest@0
   148
  end do
forrest@0
   149
;============================
forrest@0
   150
;compute beta
forrest@0
   151
;============================
forrest@0
   152
forrest@0
   153
 nsec_per_year = 60*60*24*365
forrest@0
   154
forrest@0
   155
 u = yvalues(0,:)
forrest@0
   156
 v = yvalues(1,:)
forrest@0
   157
forrest@0
   158
 good = ind(.not.ismissing(u) .and. .not.ismissing(v))
forrest@0
   159
 uu = u(good)* nsec_per_year 
forrest@0
   160
 vv = v(good)* nsec_per_year 
forrest@0
   161
forrest@0
   162
 n_biome = dimsizes(uu)
forrest@0
   163
forrest@0
   164
 beta_biome = new((/n_biome/),float)
forrest@0
   165
forrest@0
   166
 beta_biome = ((vv/uu) - 1.)/log(co2_f/co2_i)
forrest@0
   167
forrest@0
   168
 beta_biome_avg = avg(beta_biome)
forrest@0
   169
  
forrest@0
   170
 print (beta_biome_avg)
forrest@0
   171
;*******************************************************************
forrest@0
   172
; for html table
forrest@0
   173
;*******************************************************************
forrest@0
   174
forrest@0
   175
; column (not including header column)
forrest@0
   176
forrest@0
   177
  col_head = (/"CO2_i","CO2_f","NPP_i","NPP_f","Beta"/)
forrest@0
   178
forrest@0
   179
  ncol = dimsizes(col_head)
forrest@0
   180
forrest@0
   181
; row (not including header row)
forrest@0
   182
  row_head  = (/"Water Bodies" \
forrest@0
   183
               ,"Evergreen Needleleaf Forests" \
forrest@0
   184
               ,"Evergreen Broadleaf Forests" \
forrest@0
   185
               ,"Deciduous Needleleaf Forest" \
forrest@0
   186
               ,"Deciduous Broadleaf Forests" \
forrest@0
   187
               ,"Mixed Forests" \                      
forrest@0
   188
               ,"Closed Bushlands" \                   
forrest@0
   189
               ,"Open Bushlands" \                     
forrest@0
   190
               ,"Woody Savannas (S. Hem.)" \           
forrest@0
   191
               ,"Savannas (S. Hem.)" \                 
forrest@0
   192
               ,"Grasslands" \                         
forrest@0
   193
               ,"Permanent Wetlands" \                 
forrest@0
   194
               ,"Croplands" \                                           
forrest@0
   195
               ,"Cropland/Natural Vegetation Mosaic" \ 
forrest@0
   196
               ,"Permanent Snow and Ice" \             
forrest@0
   197
               ,"Barren or Sparsely Vegetated" \                             
forrest@0
   198
               ,"Woody Savannas (N. Hem.)" \           
forrest@0
   199
               ,"Savannas (N. Hem.)" \
forrest@0
   200
               ,"All Biome" \                
forrest@0
   201
               /)  
forrest@0
   202
  nrow = dimsizes(row_head)                  
forrest@0
   203
forrest@0
   204
; arrays to be passed to table. 
forrest@0
   205
  text4 = new ((/nrow, ncol/),string )
forrest@0
   206
forrest@0
   207
 do i=0,nrow-2
forrest@0
   208
  text4(i,0) = sprintf("%.2f",co2_i)
forrest@0
   209
  text4(i,1) = sprintf("%.2f",co2_f)
forrest@0
   210
  text4(i,2) = sprintf("%.2f",uu(i))
forrest@0
   211
  text4(i,3) = sprintf("%.2f",vv(i))
forrest@0
   212
  text4(i,4) = sprintf("%.2f",beta_biome(i))
forrest@0
   213
 end do
forrest@0
   214
  text4(nrow-1,0) = "-"
forrest@0
   215
  text4(nrow-1,1) = "-"
forrest@0
   216
  text4(nrow-1,2) = "-"
forrest@0
   217
  text4(nrow-1,3) = "-"
forrest@0
   218
  text4(nrow-1,4) = sprintf("%.2f",beta_biome_avg)
forrest@0
   219
forrest@0
   220
;**************************************************
forrest@0
   221
; html table
forrest@0
   222
;**************************************************
forrest@0
   223
  output_html = "table_biome.html"
forrest@0
   224
forrest@0
   225
  header_text = "<H1>Beta Factor: Model "+model_name+"</H1>" 
forrest@0
   226
forrest@0
   227
  header = (/"<HTML>" \
forrest@0
   228
            ,"<HEAD>" \
forrest@0
   229
            ,"<TITLE>CLAMP metrics</TITLE>" \
forrest@0
   230
            ,"</HEAD>" \
forrest@0
   231
            ,header_text \
forrest@0
   232
            /) 
forrest@0
   233
  footer = "</HTML>"
forrest@0
   234
forrest@0
   235
  table_header = (/ \
forrest@0
   236
        "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
forrest@0
   237
       ,"<tr>" \
forrest@0
   238
       ,"   <th bgcolor=DDDDDD >Biome Class</th>" \
forrest@0
   239
       ,"   <th bgcolor=DDDDDD >CO2_i</th>" \
forrest@0
   240
       ,"   <th bgcolor=DDDDDD >CO2_f</th>" \
forrest@0
   241
       ,"   <th bgcolor=DDDDDD >NPP_i</th>" \
forrest@0
   242
       ,"   <th bgcolor=DDDDDD >NPP_f</th>" \
forrest@0
   243
       ,"   <th bgcolor=DDDDDD >Beta</th>" \
forrest@0
   244
       ,"</tr>" \
forrest@0
   245
       /)
forrest@0
   246
  table_footer = "</table>"
forrest@0
   247
  row_header = "<tr>"
forrest@0
   248
  row_footer = "</tr>"
forrest@0
   249
forrest@0
   250
  lines = new(50000,string)
forrest@0
   251
  nline = 0
forrest@0
   252
forrest@0
   253
  set_line(lines,nline,header)
forrest@0
   254
  set_line(lines,nline,table_header)
forrest@0
   255
;-----------------------------------------------
forrest@0
   256
;row of table
forrest@0
   257
forrest@0
   258
  do n = 0,nrow-1
forrest@0
   259
     set_line(lines,nline,row_header)
forrest@0
   260
forrest@0
   261
     txt1  = row_head(n)
forrest@0
   262
     txt2  = text4(n,0)
forrest@0
   263
     txt3  = text4(n,1)
forrest@0
   264
     txt4  = text4(n,2)
forrest@0
   265
     txt5  = text4(n,3)
forrest@0
   266
     txt6  = text4(n,4)
forrest@0
   267
forrest@0
   268
     set_line(lines,nline,"<th>"+txt1+"</th>")
forrest@0
   269
     set_line(lines,nline,"<th>"+txt2+"</th>")
forrest@0
   270
     set_line(lines,nline,"<th>"+txt3+"</th>")
forrest@0
   271
     set_line(lines,nline,"<th>"+txt4+"</th>")
forrest@0
   272
     set_line(lines,nline,"<th>"+txt5+"</th>")
forrest@0
   273
     set_line(lines,nline,"<th>"+txt6+"</th>")
forrest@0
   274
forrest@0
   275
     set_line(lines,nline,row_footer)
forrest@0
   276
  end do
forrest@0
   277
;-----------------------------------------------
forrest@0
   278
  set_line(lines,nline,table_footer)
forrest@0
   279
  set_line(lines,nline,footer) 
forrest@0
   280
forrest@0
   281
; Now write to an HTML file.
forrest@0
   282
  idx = ind(.not.ismissing(lines))
forrest@0
   283
  if(.not.any(ismissing(idx))) then
forrest@0
   284
    asciiwrite(output_html,lines(idx))
forrest@0
   285
  else
forrest@0
   286
   print ("error?")
forrest@0
   287
  end if
forrest@0
   288
forrest@0
   289
end
forrest@0
   290