all/02.lai.ncl
author Forrest Hoffman <forrest@climatemodeling.org>
Thu, 26 Mar 2009 14:02:21 -0400
changeset 1 4be95183fbcd
parent 0 0c6405ab2ff4
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
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
forrest@0
     5
;**************************************************************
forrest@0
     6
procedure set_line(lines:string,nline:integer,newlines:string) 
forrest@0
     7
begin
forrest@0
     8
; add line to ascci/html file
forrest@0
     9
    
forrest@0
    10
  nnewlines = dimsizes(newlines)
forrest@0
    11
  if(nline+nnewlines-1.ge.dimsizes(lines))
forrest@0
    12
    print("set_line: bad index, not setting anything.") 
forrest@0
    13
    return
forrest@0
    14
  end if 
forrest@0
    15
  lines(nline:nline+nnewlines-1) = newlines
forrest@0
    16
;  print ("lines = " + lines(nline:nline+nnewlines-1))
forrest@0
    17
  nline = nline + nnewlines
forrest@0
    18
  return 
forrest@0
    19
end
forrest@0
    20
;**************************************************************
forrest@0
    21
; Main code.
forrest@0
    22
begin
forrest@0
    23
 
forrest@0
    24
  plot_type     = "ps"
forrest@0
    25
  plot_type_new = "png"
forrest@0
    26
forrest@0
    27
;-----------------------------------------------------
forrest@0
    28
; edit table.html of current model for movel1_vs_model2
forrest@0
    29
forrest@0
    30
  if (isvar("compare")) then
forrest@0
    31
     html_name2 = compare+"/table.html"  
forrest@0
    32
     html_new2  = html_name2 +".new"
forrest@0
    33
  end if
forrest@0
    34
forrest@0
    35
;------------------------------------------------------
forrest@0
    36
; edit table.html for current model
forrest@0
    37
forrest@0
    38
  html_name = model_name+"/table.html"  
forrest@0
    39
  html_new  = html_name +".new"
forrest@0
    40
forrest@0
    41
;------------------------------------------------------
forrest@0
    42
; read data: model       
forrest@0
    43
forrest@0
    44
  fm = addfile(dirm+film10,"r")      
forrest@0
    45
  laimod = fm->TLAI
forrest@0
    46
forrest@0
    47
  delete (fm)
forrest@0
    48
forrest@0
    49
  dsizes = dimsizes(laimod)
forrest@0
    50
  ntime  = dsizes(0)
forrest@0
    51
  nlat   = dsizes(1)
forrest@0
    52
  nlon   = dsizes(2)
forrest@0
    53
forrest@0
    54
;-----------------------------------
forrest@0
    55
; get landfrac data
forrest@0
    56
forrest@0
    57
 film_l   = "lnd_"+ model_grid + ".nc"
forrest@0
    58
 fm_l     = addfile (dirs+film_l,"r")  
forrest@0
    59
 landfrac = fm_l->landfrac
forrest@0
    60
forrest@0
    61
 delete (fm_l)
forrest@0
    62
;----------------------------------
forrest@0
    63
; read biome data: model
forrest@0
    64
forrest@0
    65
  biome_name_mod = "Model PFT Class"
forrest@0
    66
forrest@0
    67
  film_c   = "class_pft_"+model_grid+".nc"
forrest@0
    68
  fm_c     = addfile(dirs+film_c,"r") 
forrest@0
    69
  classmod = fm_c->CLASS_PFT               
forrest@0
    70
forrest@0
    71
  delete (fm_c)
forrest@0
    72
forrest@0
    73
; model data has 17 land-type classes
forrest@0
    74
  nclass_mod = 17
forrest@0
    75
forrest@0
    76
;----------------------------------------------------------
forrest@0
    77
; read data: ob       
forrest@0
    78
forrest@0
    79
;----------------------------------
forrest@0
    80
; read biome data: observed
forrest@0
    81
forrest@0
    82
  biome_name_ob = "MODIS LandCover"
forrest@0
    83
forrest@0
    84
  dir_c = diro + "lai/"
forrest@0
    85
  filo_c = "land_class_"+model_grid+".nc"
forrest@0
    86
  fo = addfile(dir_c+filo_c,"r")
forrest@0
    87
  classob = tofloat(fo->LAND_CLASS)               
forrest@0
    88
forrest@0
    89
  delete (fo)
forrest@0
    90
forrest@0
    91
; input observed data has 20 land-type classes
forrest@0
    92
  nclass_ob = 20
forrest@0
    93
forrest@0
    94
;---------------------------------
forrest@0
    95
; read lai data: observed
forrest@0
    96
forrest@1
    97
  ;ob_name = "MODIS MOD15A2 (2000-2005)"
forrest@1
    98
  ob_name = "MODIS MOD15A2 (2000-2004)"
forrest@0
    99
forrest@0
   100
  dir_l = diro + "lai/"
forrest@0
   101
  ;filo = "LAI_2000-2005_MONS_"+model_grid+".nc"
forrest@0
   102
  filo = "LAI_2000-2004_MONS_"+model_grid+".nc"
forrest@0
   103
  fo = addfile(dir_l+filo,"r")                
forrest@0
   104
  laiob = fo->LAI
forrest@0
   105
forrest@0
   106
  delete (fo)
forrest@0
   107
forrest@0
   108
;-------------------------------------------------
forrest@0
   109
; take into account landfrac
forrest@0
   110
forrest@0
   111
  laimod = laimod * conform(laimod,landfrac,(/1,2/))
forrest@0
   112
  laiob  = laiob  * conform(laiob ,landfrac,(/1,2/))
forrest@0
   113
forrest@0
   114
  delete (landfrac)
forrest@0
   115
forrest@0
   116
;************************************************
forrest@0
   117
; global res
forrest@0
   118
;************************************************
forrest@0
   119
  resg                      = True             ; Use plot options
forrest@0
   120
  resg@cnFillOn             = True             ; Turn on color fill
forrest@0
   121
  resg@gsnSpreadColors      = True             ; use full colormap
forrest@0
   122
  resg@cnLinesOn            = False            ; Turn off contourn lines
forrest@0
   123
  resg@mpFillOn             = False            ; Turn off map fill
forrest@0
   124
  resg@cnLevelSelectionMode = "ManualLevels"   ; Manual contour invtervals
forrest@0
   125
      
forrest@0
   126
;************************************************
forrest@0
   127
; plot global biome class: (1) observed
forrest@0
   128
;************************************************
forrest@0
   129
forrest@0
   130
  resg@cnMinLevelValF       = 1.              ; Min level
forrest@0
   131
  resg@cnMaxLevelValF       = 19.             ; Max level
forrest@0
   132
  resg@cnLevelSpacingF      = 1.              ; interval
forrest@0
   133
forrest@0
   134
  classob@_FillValue = 1.e+36
forrest@0
   135
  classob = where(classob.eq.0,classob@_FillValue,classob)
forrest@0
   136
  
forrest@0
   137
  plot_name = "global_class_ob"
forrest@0
   138
  title     = biome_name_ob
forrest@0
   139
  resg@tiMainString  = title
forrest@0
   140
forrest@0
   141
  wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
forrest@0
   142
  gsn_define_colormap(wks,"gui_default")     ; choose colormap
forrest@0
   143
forrest@0
   144
  plot = gsn_csm_contour_map_ce(wks,classob,resg)   
forrest@0
   145
forrest@0
   146
  delete (wks)
forrest@0
   147
forrest@1
   148
  system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   149
         "rm "+plot_name+"."+plot_type)
forrest@0
   150
 
forrest@0
   151
;************************************************
forrest@0
   152
; plot global biome class: (2) model
forrest@0
   153
;************************************************
forrest@0
   154
forrest@0
   155
  resg@cnMinLevelValF       = 0.              ; Min level
forrest@0
   156
  resg@cnMaxLevelValF       = 16.             ; Max level
forrest@0
   157
  resg@cnLevelSpacingF      = 1.              ; interval
forrest@0
   158
  
forrest@0
   159
  plot_name = "global_class_model"
forrest@0
   160
  title     = biome_name_mod
forrest@0
   161
  resg@tiMainString  = title
forrest@0
   162
forrest@0
   163
  wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
forrest@0
   164
  gsn_define_colormap(wks,"gui_default")     ; choose colormap
forrest@0
   165
forrest@0
   166
  plot = gsn_csm_contour_map_ce(wks,classmod,resg)   
forrest@0
   167
forrest@0
   168
  delete (wks)
forrest@0
   169
forrest@1
   170
  system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   171
         "rm "+plot_name+"."+plot_type)
forrest@0
   172
 
forrest@0
   173
;*******************************************************************
forrest@0
   174
; for html table : all 3 components (Mean, Max, Phase)
forrest@0
   175
;*******************************************************************
forrest@0
   176
forrest@0
   177
; column (not including header column)
forrest@0
   178
forrest@0
   179
  component = (/"Mean","Max","Phase"/)
forrest@0
   180
forrest@0
   181
  col_head  = (/"observed",model_name,"M_score" \
forrest@0
   182
               ,"observed",model_name,"M_score" \
forrest@0
   183
               ,"observed",model_name,"M_score" \
forrest@0
   184
               /)
forrest@0
   185
  
forrest@0
   186
  n_comp = dimsizes(component) 
forrest@0
   187
  ncol   = dimsizes(col_head )
forrest@0
   188
forrest@0
   189
; row (not including header row)
forrest@0
   190
forrest@0
   191
;----------------------------------------------------
forrest@0
   192
; using model biome class:  
forrest@0
   193
  row_head  = (/"Not Vegetated" \
forrest@0
   194
               ,"Needleleaf Evergreen Temperate Tree" \
forrest@0
   195
               ,"Needleleaf Evergreen Boreal Tree" \
forrest@0
   196
;              ,"Needleleaf Deciduous Boreal Tree" \
forrest@0
   197
               ,"Broadleaf Evergreen Tropical Tree" \
forrest@0
   198
               ,"Broadleaf Evergreen Temperate Tree" \
forrest@0
   199
               ,"Broadleaf Deciduous Tropical Tree" \
forrest@0
   200
               ,"Broadleaf Deciduous Temperate Tree" \
forrest@0
   201
;              ,"Broadleaf Deciduous Boreal Tree" \
forrest@0
   202
;              ,"Broadleaf Evergreen Shrub" \
forrest@0
   203
               ,"Broadleaf Deciduous Temperate Shrub" \
forrest@0
   204
               ,"Broadleaf Deciduous Boreal Shrub" \
forrest@0
   205
               ,"C3 Arctic Grass" \
forrest@0
   206
               ,"C3 Non-Arctic Grass" \
forrest@0
   207
               ,"C4 Grass" \
forrest@0
   208
               ,"Corn" \
forrest@0
   209
;              ,"Wheat" \                      
forrest@0
   210
               ,"All Biomes" \                
forrest@0
   211
               /)  
forrest@0
   212
  
forrest@0
   213
  nrow = dimsizes(row_head)                  
forrest@0
   214
forrest@0
   215
; arrays to be passed to table. 
forrest@0
   216
  text = new ((/nrow, ncol/),string ) 
forrest@0
   217
forrest@0
   218
; total M_score
forrest@0
   219
  M_total = 0.
forrest@0
   220
forrest@0
   221
;********************************************************************
forrest@0
   222
; use land-type class to bin the data in equally spaced ranges
forrest@0
   223
;********************************************************************
forrest@0
   224
forrest@0
   225
; using model biome class
forrest@0
   226
  nclass = nclass_mod
forrest@0
   227
forrest@0
   228
  range  = fspan(0,nclass,nclass+1)
forrest@0
   229
forrest@0
   230
; Use this range information to grab all the values in a
forrest@0
   231
; particular range, and then take an average.
forrest@0
   232
forrest@0
   233
  nx = dimsizes(range) - 1
forrest@0
   234
forrest@0
   235
; for 2 data: model and observed
forrest@0
   236
  data_n = 2
forrest@0
   237
forrest@0
   238
; using model biome class
forrest@0
   239
forrest@0
   240
  base = ndtooned(classmod)
forrest@0
   241
forrest@0
   242
; output
forrest@0
   243
forrest@0
   244
  yvalues = new((/data_n,nx/),float)
forrest@0
   245
  count   = new((/data_n,nx/),float)
forrest@0
   246
forrest@0
   247
;************************************************************************
forrest@0
   248
; go through all components
forrest@0
   249
;************************************************************************
forrest@0
   250
forrest@0
   251
 do m = 0,n_comp-1
forrest@0
   252
forrest@0
   253
;===================
forrest@0
   254
; get data:
forrest@0
   255
;===================
forrest@0
   256
; (A) Mean
forrest@0
   257
 
forrest@0
   258
  if (m .eq. 0) then
forrest@0
   259
     data_ob  = dim_avg_Wrap(laiob (lat|:,lon|:,time|:))
forrest@0
   260
     data_mod = dim_avg_Wrap(laimod(lat|:,lon|:,time|:))
forrest@1
   261
     score_max = 4.0
forrest@0
   262
  end if
forrest@0
   263
forrest@0
   264
; (B) Max
forrest@0
   265
forrest@0
   266
  if (m .eq. 1) then
forrest@0
   267
forrest@0
   268
;    observed  
forrest@0
   269
     data_ob = laiob(0,:,:)
forrest@0
   270
     data_ob@long_name = "Leaf Area Index Max"
forrest@0
   271
  
forrest@0
   272
     do j = 0,nlat-1
forrest@0
   273
     do i = 0,nlon-1
forrest@0
   274
        data_ob(j,i) = max(laiob(:,j,i))
forrest@0
   275
     end do
forrest@0
   276
     end do          
forrest@0
   277
forrest@0
   278
;    model  
forrest@0
   279
     data_mod = laimod(0,:,:)
forrest@0
   280
     data_mod@long_name = "Leaf Area Index Max"
forrest@0
   281
  
forrest@0
   282
     do j = 0,nlat-1
forrest@0
   283
     do i = 0,nlon-1
forrest@0
   284
        data_mod(j,i) = max(laimod(:,j,i))
forrest@0
   285
     end do
forrest@0
   286
     end do
forrest@1
   287
forrest@1
   288
     score_max = 5.0
forrest@0
   289
          
forrest@0
   290
  end if
forrest@0
   291
forrest@0
   292
; (C) phase
forrest@0
   293
forrest@0
   294
  if (m .eq. 2) then  
forrest@0
   295
forrest@0
   296
;    observed
forrest@0
   297
     data_ob = laiob(0,:,:)
forrest@0
   298
     data_ob@long_name = "Leaf Area Index Max Month"
forrest@0
   299
  
forrest@0
   300
     do j = 0,nlat-1
forrest@0
   301
     do i = 0,nlon-1 
forrest@0
   302
        data_ob(j,i) = maxind(laiob(:,j,i)) + 1
forrest@0
   303
     end do
forrest@0
   304
     end do          
forrest@0
   305
forrest@0
   306
;    model
forrest@0
   307
     data_mod = laimod(0,:,:)
forrest@0
   308
     data_mod@long_name = "Leaf Area Index Max Month"
forrest@0
   309
  
forrest@0
   310
     do j = 0,nlat-1
forrest@0
   311
     do i = 0,nlon-1 
forrest@0
   312
        data_mod(j,i) = maxind(laimod(:,j,i)) + 1
forrest@0
   313
     end do
forrest@0
   314
     end do
forrest@1
   315
forrest@1
   316
     score_max = 6.0
forrest@0
   317
        
forrest@0
   318
  end if
forrest@0
   319
forrest@0
   320
;==============================
forrest@0
   321
; put data into bins
forrest@0
   322
;==============================
forrest@0
   323
forrest@0
   324
; Loop through each range, using base
forrest@0
   325
forrest@0
   326
  do i=0,nx-1
forrest@0
   327
forrest@0
   328
     if (i.ne.(nx-1)) then
forrest@0
   329
        idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
forrest@0
   330
     else
forrest@0
   331
        idx = ind(base.ge.range(i))
forrest@0
   332
     end if
forrest@0
   333
forrest@0
   334
;    loop through each dataset
forrest@0
   335
 
forrest@0
   336
     do n = 0,data_n-1
forrest@0
   337
forrest@0
   338
        if (n .eq. 0) then
forrest@0
   339
           data = ndtooned(data_ob)
forrest@0
   340
        end if
forrest@0
   341
forrest@0
   342
        if (n .eq. 1) then
forrest@0
   343
           data = ndtooned(data_mod)
forrest@0
   344
        end if
forrest@0
   345
forrest@0
   346
;       Calculate average 
forrest@0
   347
forrest@0
   348
        if (.not.any(ismissing(idx))) then
forrest@0
   349
           yvalues(n,i) = avg(data(idx))
forrest@0
   350
           count(n,i)   = dimsizes(idx)
forrest@0
   351
        else
forrest@0
   352
           yvalues(n,i) = yvalues@_FillValue
forrest@0
   353
           count(n,i)   = 0
forrest@0
   354
        end if
forrest@0
   355
forrest@0
   356
;#############################################################
forrest@0
   357
; using model biome class:
forrest@0
   358
;
forrest@0
   359
;     set the following 4 classes to _FillValue:
forrest@0
   360
;     (3)Needleleaf Deciduous Boreal Tree,
forrest@0
   361
;     (8)Broadleaf Deciduous Boreal Tree,
forrest@0
   362
;     (9)Broadleaf Evergreen Shrub,
forrest@0
   363
;     (16)Wheat
forrest@0
   364
forrest@0
   365
      if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
forrest@0
   366
         yvalues(n,i) = yvalues@_FillValue
forrest@0
   367
         count(n,i)   = 0
forrest@0
   368
      end if
forrest@0
   369
;############################################################# 
forrest@0
   370
forrest@0
   371
      delete(data)
forrest@0
   372
    end do                 ; n-loop
forrest@0
   373
forrest@0
   374
    delete(idx)
forrest@0
   375
  end do                   ; i-loop
forrest@0
   376
forrest@0
   377
;=====================================
forrest@0
   378
; compute correlation coef and M score 
forrest@0
   379
;=====================================
forrest@0
   380
forrest@1
   381
  ; FMH: Max scores are depend on which component is being analyzed, so this
forrest@1
   382
  ; is now set in each if statement above.
forrest@1
   383
  ;score_max = 5.0
forrest@0
   384
forrest@0
   385
  u       = yvalues(0,:)
forrest@0
   386
  v       = yvalues(1,:)
forrest@0
   387
  u_count = count(0,:)
forrest@0
   388
  v_count = count(1,:)
forrest@0
   389
forrest@0
   390
  good = ind(.not.ismissing(u) .and. .not.ismissing(v))
forrest@0
   391
 
forrest@0
   392
  uu = u(good)
forrest@0
   393
  vv = v(good)
forrest@0
   394
  uu_count = u_count(good)
forrest@0
   395
  vv_count = v_count(good)
forrest@0
   396
forrest@0
   397
; compute correlation coef
forrest@0
   398
  cc = esccr(uu,vv,0)
forrest@0
   399
forrest@0
   400
  if (n .eq. 2) then
forrest@0
   401
     bias   = avg(abs(vv-uu))
forrest@0
   402
     bias   = where((bias.gt. 6.),12.-bias,bias)
forrest@0
   403
     Mscore = ((6. - bias)/6.)*score_max
forrest@0
   404
  else
forrest@0
   405
     bias  = sum(abs(vv-uu)/abs(vv+uu))
forrest@0
   406
     Mscore = (1.- (bias/dimsizes(uu)))*score_max
forrest@0
   407
  end if
forrest@0
   408
forrest@0
   409
  M_score = sprintf("%.2f", Mscore)
forrest@0
   410
forrest@0
   411
; compute M_total
forrest@0
   412
  
forrest@0
   413
  M_total = M_total + Mscore
forrest@0
   414
forrest@0
   415
;================================
forrest@0
   416
; output M_score to score sheet
forrest@0
   417
;===============================
forrest@0
   418
forrest@0
   419
  if (isvar("compare")) then
forrest@0
   420
     system("sed -e '1,/M_lai_"+component(m)+"/s/M_lai_"+component(m)+"/"+M_score+"/' "+html_name2+" > "+html_new2+";"+ \
forrest@0
   421
            "mv -f "+html_new2+" "+html_name2)
forrest@0
   422
  end if
forrest@0
   423
forrest@0
   424
  system("sed s#M_lai_"+component(m)+"#"+M_score+"# "+html_name+" > "+html_new+";"+ \
forrest@0
   425
         "mv -f "+html_new+" "+html_name)
forrest@0
   426
forrest@0
   427
;==============================
forrest@0
   428
; output M_score to html table
forrest@0
   429
;==============================
forrest@0
   430
forrest@0
   431
  n = m*3
forrest@0
   432
forrest@0
   433
  do i=0,nrow-2
forrest@0
   434
     text(i,n)   = sprintf("%.1f",uu(i))
forrest@0
   435
     text(i,n+1) = sprintf("%.1f",vv(i))
forrest@0
   436
     text(i,n+2) = "-"
forrest@0
   437
  end do
forrest@0
   438
  text(nrow-1,n)   = sprintf("%.1f",sum(uu*uu_count)/sum(uu_count))
forrest@0
   439
  text(nrow-1,n+1) = sprintf("%.1f",sum(vv*vv_count)/sum(vv_count))
forrest@0
   440
  text(nrow-1,n+2) = M_score
forrest@0
   441
forrest@0
   442
  delete (u)
forrest@0
   443
  delete (v)
forrest@0
   444
  delete (uu)
forrest@0
   445
  delete (vv)
forrest@0
   446
  delete (u_count)
forrest@0
   447
  delete (v_count)
forrest@0
   448
  delete (uu_count)
forrest@0
   449
  delete (vv_count)
forrest@0
   450
  delete (good)
forrest@0
   451
forrest@0
   452
;======================================== 
forrest@0
   453
; global res changes for each component
forrest@0
   454
;========================================
forrest@0
   455
  delta = 0.00001  
forrest@0
   456
forrest@0
   457
  if (m .eq. 0) then
forrest@0
   458
     resg@cnMinLevelValF       = 0.             
forrest@0
   459
     resg@cnMaxLevelValF       = 10.             
forrest@0
   460
     resg@cnLevelSpacingF      = 1.
forrest@0
   461
forrest@0
   462
     data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
forrest@0
   463
  end if
forrest@0
   464
forrest@0
   465
  if (m .eq. 1) then
forrest@0
   466
     resg@cnMinLevelValF       = 0.             
forrest@0
   467
     resg@cnMaxLevelValF       = 10.             
forrest@0
   468
     resg@cnLevelSpacingF      = 1.
forrest@0
   469
forrest@0
   470
     data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
forrest@0
   471
  end if
forrest@0
   472
forrest@0
   473
  if (m .eq. 2) then
forrest@0
   474
     resg@cnMinLevelValF       = 1.             
forrest@0
   475
     resg@cnMaxLevelValF       = 12.             
forrest@0
   476
     resg@cnLevelSpacingF      = 1.
forrest@0
   477
forrest@0
   478
     data_mod = where(ismissing(data_ob).or.(data_mod.lt.delta),data_mod@_FillValue,data_mod)
forrest@0
   479
     data_ob = where(ismissing(data_ob).and.(ismissing(data_mod).or.(data_mod.lt.delta)),0.,data_ob)
forrest@0
   480
forrest@0
   481
  end if
forrest@0
   482
forrest@0
   483
;=========================
forrest@0
   484
; global contour : ob
forrest@0
   485
;=========================
forrest@0
   486
  
forrest@0
   487
  plot_name = "global_"+component(m)+"_ob"
forrest@0
   488
  title     = ob_name
forrest@0
   489
  resg@tiMainString  = title
forrest@0
   490
forrest@0
   491
  wks = gsn_open_wks (plot_type,plot_name)   ; open workstation
forrest@0
   492
  gsn_define_colormap(wks,"gui_default")     ; choose colormap
forrest@0
   493
forrest@0
   494
  plot = gsn_csm_contour_map_ce(wks,data_ob,resg)   
forrest@0
   495
forrest@0
   496
  delete (wks)
forrest@0
   497
  delete (plot)
forrest@0
   498
forrest@1
   499
  system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   500
         "rm "+plot_name+"."+plot_type)
forrest@1
   501
  ;system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
forrest@1
   502
  
forrest@0
   503
forrest@0
   504
;============================
forrest@0
   505
; global contour : model
forrest@0
   506
;============================
forrest@0
   507
forrest@0
   508
  plot_name = "global_"+component(m)+"_model"  
forrest@1
   509
  title     = "Model " + model_name + " (2000-2004)"
forrest@0
   510
  resg@tiMainString  = title
forrest@0
   511
forrest@0
   512
  wks = gsn_open_wks (plot_type,plot_name)
forrest@0
   513
  gsn_define_colormap(wks,"gui_default")     
forrest@0
   514
forrest@0
   515
  plot = gsn_csm_contour_map_ce(wks,data_mod,resg)   
forrest@0
   516
forrest@0
   517
  delete (wks)
forrest@0
   518
  delete (plot)
forrest@0
   519
forrest@1
   520
  system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   521
         "rm "+plot_name+"."+plot_type)
forrest@1
   522
  ;system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
forrest@0
   523
forrest@0
   524
;================================
forrest@0
   525
; global contour: model vs ob
forrest@0
   526
;================================
forrest@0
   527
forrest@0
   528
  plot_name = "global_"+component(m)+"_model_vs_ob"
forrest@0
   529
forrest@0
   530
  wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   531
  gsn_define_colormap(wks,"gui_default")     
forrest@0
   532
forrest@0
   533
  plot=new(3,graphic)                        ; create graphic array
forrest@0
   534
forrest@0
   535
  resg@gsnFrame             = False          ; Do not draw plot 
forrest@0
   536
  resg@gsnDraw              = False          ; Do not advance frame
forrest@0
   537
forrest@0
   538
; plot correlation coef
forrest@0
   539
forrest@0
   540
  gRes               = True
forrest@0
   541
  gRes@txFontHeightF = 0.02
forrest@0
   542
  gRes@txAngleF      = 90
forrest@0
   543
forrest@0
   544
  correlation_text = "(correlation coef = "+sprintf("%.2f", cc)+")"
forrest@0
   545
forrest@0
   546
  gsn_text_ndc(wks,correlation_text,0.20,0.50,gRes)
forrest@0
   547
  
forrest@0
   548
; plot ob
forrest@0
   549
forrest@0
   550
  title     = ob_name
forrest@0
   551
  resg@tiMainString  = title
forrest@0
   552
forrest@0
   553
  plot(0) = gsn_csm_contour_map_ce(wks,data_ob,resg)       
forrest@0
   554
forrest@0
   555
; plot model
forrest@0
   556
forrest@1
   557
  title     = "Model "+ model_name + " (2000-2004)"
forrest@0
   558
  resg@tiMainString  = title
forrest@0
   559
forrest@0
   560
  plot(1) = gsn_csm_contour_map_ce(wks,data_mod,resg) 
forrest@0
   561
forrest@0
   562
; plot model-ob
forrest@0
   563
forrest@0
   564
  if (m .eq. 0) then
forrest@0
   565
     resg@cnMinLevelValF  = -2.           
forrest@0
   566
     resg@cnMaxLevelValF  =  2.            
forrest@0
   567
     resg@cnLevelSpacingF =  0.4
forrest@0
   568
  end if
forrest@0
   569
forrest@0
   570
  if (m .eq. 1) then
forrest@0
   571
     resg@cnMinLevelValF  = -6.           
forrest@0
   572
     resg@cnMaxLevelValF  =  6.            
forrest@0
   573
     resg@cnLevelSpacingF =  1.
forrest@0
   574
  end if
forrest@0
   575
forrest@0
   576
  if (m .eq. 2) then
forrest@0
   577
     resg@cnMinLevelValF  = -6.           
forrest@0
   578
     resg@cnMaxLevelValF  =  6.            
forrest@0
   579
     resg@cnLevelSpacingF =  1.
forrest@0
   580
  end if
forrest@0
   581
forrest@0
   582
  zz = data_mod
forrest@0
   583
  zz = data_mod - data_ob
forrest@1
   584
  title = "Model_"+model_name+" - MODIS MOD15A2"
forrest@0
   585
  resg@tiMainString    = title
forrest@0
   586
forrest@0
   587
  plot(2) = gsn_csm_contour_map_ce(wks,zz,resg) 
forrest@0
   588
forrest@0
   589
; plot panel
forrest@0
   590
forrest@0
   591
  pres                            = True        ; panel plot mods desired
forrest@0
   592
  pres@gsnMaximize                = True        ; fill the page
forrest@0
   593
forrest@0
   594
  gsn_panel(wks,plot,(/3,1/),pres)              ; create panel plot
forrest@0
   595
forrest@0
   596
  delete (wks)
forrest@0
   597
  delete (plot)
forrest@0
   598
forrest@1
   599
  system("convert -trim -density 150 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   600
         "rm "+plot_name+"."+plot_type)
forrest@1
   601
  ;system("convert -trim -density 150 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new)
forrest@0
   602
forrest@0
   603
  delete (data_ob)
forrest@0
   604
  delete (data_mod)
forrest@0
   605
forrest@0
   606
  resg@gsnFrame             = True          ; Do advance frame 
forrest@0
   607
  resg@gsnDraw              = True          ; Do draw plot
forrest@0
   608
forrest@0
   609
 end do    ; m-loop
forrest@0
   610
forrest@0
   611
;**************************************************
forrest@0
   612
; html table
forrest@0
   613
;**************************************************
forrest@0
   614
  output_html = "table_model_vs_ob.html"
forrest@0
   615
forrest@0
   616
  header_text = "<H1>LAI: Model "+model_name+" vs. MODIS observations</H1>" 
forrest@0
   617
forrest@0
   618
  header = (/"<HTML>" \
forrest@0
   619
            ,"<HEAD>" \
forrest@0
   620
            ,"<TITLE>CLAMP metrics</TITLE>" \
forrest@0
   621
            ,"</HEAD>" \
forrest@0
   622
            ,header_text \
forrest@0
   623
            /) 
forrest@0
   624
  footer = "</HTML>"
forrest@0
   625
forrest@0
   626
  table_header = (/ \
forrest@0
   627
        "<table border=1 cellspacing=0 cellpadding=3 width=100%>" \
forrest@0
   628
       ,"<tr>" \
forrest@0
   629
       ,"   <th bgcolor=DDDDDD rowspan=2>Biome Class</th>" \
forrest@0
   630
       ,"   <th bgcolor=DDDDDD colspan=3>"+component(0)+" (m2/m2)</th>" \
forrest@0
   631
       ,"   <th bgcolor=DDDDDD colspan=3>"+component(1)+" (m2/m2)</th>" \
forrest@0
   632
       ,"   <th bgcolor=DDDDDD colspan=3>"+component(2)+" (months)</th>" \
forrest@0
   633
       ,"   <th bgcolor=DDDDDD rowspan=2>Annual Plot</th>" \
forrest@0
   634
       ,"</tr>" \
forrest@0
   635
       ,"<tr>" \
forrest@0
   636
       ,"   <th bgcolor=DDDDDD >observed</th>" \
forrest@0
   637
       ,"   <th bgcolor=DDDDDD >"+model_name+"</th>" \
forrest@0
   638
       ,"   <th bgcolor=DDDDDD >M_score</th>" \
forrest@0
   639
       ,"   <th bgcolor=DDDDDD >observed</th>" \
forrest@0
   640
       ,"   <th bgcolor=DDDDDD >"+model_name+"</th>" \
forrest@0
   641
       ,"   <th bgcolor=DDDDDD >M_score</th>" \
forrest@0
   642
       ,"   <th bgcolor=DDDDDD >observed</th>" \
forrest@0
   643
       ,"   <th bgcolor=DDDDDD >"+model_name+"</th>" \
forrest@0
   644
       ,"   <th bgcolor=DDDDDD >M_score</th>" \
forrest@0
   645
       ,"</tr>" \
forrest@0
   646
       /)
forrest@0
   647
  table_footer = "</table>"
forrest@0
   648
  row_header = "<tr>"
forrest@0
   649
  row_footer = "</tr>"
forrest@0
   650
forrest@0
   651
  lines = new(50000,string)
forrest@0
   652
  nline = 0
forrest@0
   653
forrest@0
   654
  set_line(lines,nline,header)
forrest@0
   655
  set_line(lines,nline,table_header)
forrest@0
   656
;-----------------------------------------------
forrest@0
   657
;row of table
forrest@0
   658
forrest@0
   659
  do n = 0,nrow-1
forrest@0
   660
     set_line(lines,nline,row_header)
forrest@0
   661
forrest@0
   662
     txt1  = row_head(n)
forrest@0
   663
     txt2  = text(n,0)
forrest@0
   664
     txt3  = text(n,1)
forrest@0
   665
     txt4  = text(n,2)
forrest@0
   666
     txt5  = text(n,3)
forrest@0
   667
     txt6  = text(n,4)
forrest@0
   668
     txt7  = text(n,5)
forrest@0
   669
     txt8  = text(n,6)
forrest@0
   670
     txt9  = text(n,7)
forrest@0
   671
     txt10 = text(n,8)
forrest@0
   672
     txt11 = "<a href=./annual_biome_"+n+".png>model_vs_ob</a>" 
forrest@0
   673
forrest@0
   674
     set_line(lines,nline,"<th>"+txt1+"</th>")
forrest@0
   675
     set_line(lines,nline,"<th>"+txt2+"</th>")
forrest@0
   676
     set_line(lines,nline,"<th>"+txt3+"</th>")
forrest@0
   677
     set_line(lines,nline,"<th>"+txt4+"</th>")
forrest@0
   678
     set_line(lines,nline,"<th>"+txt5+"</th>")
forrest@0
   679
     set_line(lines,nline,"<th>"+txt6+"</th>")
forrest@0
   680
     set_line(lines,nline,"<th>"+txt7+"</th>")
forrest@0
   681
     set_line(lines,nline,"<th>"+txt8+"</th>")
forrest@0
   682
     set_line(lines,nline,"<th>"+txt9+"</th>")
forrest@0
   683
     set_line(lines,nline,"<th>"+txt10+"</th>")
forrest@0
   684
     set_line(lines,nline,"<th>"+txt11+"</th>")
forrest@0
   685
forrest@0
   686
     set_line(lines,nline,row_footer)
forrest@0
   687
  end do
forrest@0
   688
;-----------------------------------------------
forrest@0
   689
  set_line(lines,nline,table_footer)
forrest@0
   690
  set_line(lines,nline,footer) 
forrest@0
   691
forrest@0
   692
; Now write to an HTML file
forrest@0
   693
forrest@0
   694
  idx = ind(.not.ismissing(lines))
forrest@0
   695
  if(.not.any(ismissing(idx))) then
forrest@0
   696
    asciiwrite(output_html,lines(idx))
forrest@0
   697
  else
forrest@0
   698
   print ("error?")
forrest@0
   699
  end if
forrest@0
   700
forrest@0
   701
  delete (yvalues)
forrest@0
   702
  delete (count)
forrest@0
   703
  delete (idx)
forrest@0
   704
forrest@0
   705
;************************************************************************
forrest@0
   706
; go through all ntime
forrest@0
   707
;************************************************************************
forrest@0
   708
forrest@0
   709
; for 2 data: model and observed
forrest@0
   710
  data_n = 2
forrest@0
   711
forrest@0
   712
; using model biome class
forrest@0
   713
forrest@0
   714
  base = ndtooned(classmod)
forrest@0
   715
forrest@0
   716
; output
forrest@0
   717
forrest@0
   718
  yvalues = new((/data_n,nx,ntime/),float)
forrest@0
   719
forrest@0
   720
;==============================
forrest@0
   721
; put data into bins
forrest@0
   722
;==============================
forrest@0
   723
forrest@0
   724
; Loop through each range, using base
forrest@0
   725
forrest@0
   726
  do i=0,nx-1
forrest@0
   727
forrest@0
   728
     if (i.ne.(nx-1)) then
forrest@0
   729
        idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
forrest@0
   730
     else
forrest@0
   731
        idx = ind(base.ge.range(i))
forrest@0
   732
     end if
forrest@0
   733
forrest@0
   734
;    loop through each dataset
forrest@0
   735
 
forrest@0
   736
     do n = 0,data_n-1
forrest@0
   737
forrest@0
   738
        do m = 0,ntime-1
forrest@0
   739
forrest@0
   740
        if (n .eq. 0) then
forrest@0
   741
           data = ndtooned(laiob (m,:,:))
forrest@0
   742
        end if
forrest@0
   743
forrest@0
   744
        if (n .eq. 1) then
forrest@0
   745
           data = ndtooned(laimod(m,:,:))
forrest@0
   746
        end if
forrest@0
   747
forrest@0
   748
;       Calculate average 
forrest@0
   749
forrest@0
   750
        if (.not.any(ismissing(idx))) then
forrest@0
   751
           yvalues(n,i,m) = avg(data(idx))
forrest@0
   752
        else
forrest@0
   753
           yvalues(n,i,m) = yvalues@_FillValue
forrest@0
   754
        end if
forrest@0
   755
forrest@0
   756
;#############################################################
forrest@0
   757
; using model biome class:
forrest@0
   758
;
forrest@0
   759
;     set the following 4 classes to _FillValue:
forrest@0
   760
;     (3)Needleleaf Deciduous Boreal Tree,
forrest@0
   761
;     (8)Broadleaf Deciduous Boreal Tree,
forrest@0
   762
;     (9)Broadleaf Evergreen Shrub,
forrest@0
   763
;     (16)Wheat
forrest@0
   764
forrest@0
   765
      if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
forrest@0
   766
         yvalues(n,i,m) = yvalues@_FillValue
forrest@0
   767
      end if
forrest@0
   768
;############################################################# 
forrest@0
   769
forrest@0
   770
      end do               ; m-loop
forrest@0
   771
forrest@0
   772
      delete(data)
forrest@0
   773
    end do                 ; n-loop
forrest@0
   774
forrest@0
   775
    delete(idx)
forrest@0
   776
  end do                   ; i-loop
forrest@0
   777
forrest@0
   778
  good = ind(.not.ismissing(yvalues(0,:,0)))  
forrest@0
   779
forrest@0
   780
  n_biome = dimsizes(good)
forrest@0
   781
forrest@0
   782
;----------------------------------------------------------------
forrest@0
   783
; data for tseries plot
forrest@0
   784
forrest@0
   785
  yvalues_g = new((/data_n,n_biome,ntime/),float)
forrest@0
   786
forrest@0
   787
  yvalues_g@units = ""
forrest@0
   788
forrest@0
   789
  yvalues_g = yvalues(:,good,:)
forrest@0
   790
forrest@0
   791
  delete (good)
forrest@0
   792
forrest@0
   793
;*******************************************************************
forrest@0
   794
; res for line plot
forrest@0
   795
;*******************************************************************
forrest@0
   796
; for x-axis in xyplot
forrest@0
   797
  mon = ispan(1,12,1)
forrest@0
   798
  mon@long_name = "month"
forrest@0
   799
forrest@0
   800
  res                   = True                      ; plot mods desired
forrest@0
   801
  res@xyLineThicknesses = (/2.0,2.0,2.0/)           ; make 2nd lines thicker
forrest@0
   802
  res@xyLineColors      = (/"blue","red"/)         ; change line color
forrest@0
   803
forrest@0
   804
; res@tiMainFontHeightF = 0.025                     ; size of title 
forrest@0
   805
forrest@0
   806
; Add a boxed legend using the more simple method
forrest@0
   807
  res@pmLegendDisplayMode    = "Always"
forrest@0
   808
; res@pmLegendWidthF         = 0.1
forrest@0
   809
  res@pmLegendWidthF         = 0.08
forrest@0
   810
  res@pmLegendHeightF        = 0.06
forrest@0
   811
; res@pmLegendOrthogonalPosF = -1.17
forrest@0
   812
; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
forrest@0
   813
  res@pmLegendOrthogonalPosF = -0.30  ;(downward)
forrest@0
   814
forrest@0
   815
; res@pmLegendParallelPosF   =  0.18
forrest@0
   816
  res@pmLegendParallelPosF   =  0.23  ;(rightward)
forrest@0
   817
forrest@0
   818
; res@lgPerimOn             = False
forrest@0
   819
  res@lgLabelFontHeightF     = 0.015
forrest@0
   820
  res@xyExplicitLegendLabels = (/"observed",model_name/)
forrest@0
   821
forrest@0
   822
;************************************************************
forrest@0
   823
forrest@0
   824
  plot_data = new((/2,ntime/),float)
forrest@0
   825
  plot_data@long_name = "Leaf Area Index"
forrest@0
   826
forrest@0
   827
;----------------------------------------------
forrest@0
   828
; time series plot : per biome
forrest@0
   829
 
forrest@0
   830
  do m = 0, n_biome-1
forrest@0
   831
forrest@0
   832
     plot_name = "annual_biome_"+ m
forrest@0
   833
forrest@0
   834
     wks = gsn_open_wks (plot_type,plot_name) 
forrest@0
   835
forrest@0
   836
     title = "LAI : "+ row_head(m)
forrest@0
   837
     res@tiMainString = title
forrest@0
   838
forrest@0
   839
     plot_data(0,:) = yvalues_g(0,m,:)
forrest@0
   840
     plot_data(1,:) = yvalues_g(1,m,:)
forrest@0
   841
                                  
forrest@0
   842
     plot = gsn_csm_xy(wks,mon,plot_data,res)
forrest@0
   843
forrest@0
   844
     delete (wks)
forrest@0
   845
     delete (plot)
forrest@0
   846
forrest@1
   847
     system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   848
            "rm "+plot_name+"."+plot_type)
forrest@0
   849
  end do
forrest@0
   850
forrest@0
   851
;--------------------------------------------
forrest@0
   852
; time series plot: all biome
forrest@0
   853
forrest@0
   854
     plot_name = "annual_biome_"+ n_biome
forrest@0
   855
forrest@0
   856
     wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   857
forrest@0
   858
     title = "LAI : "+ row_head(n_biome)
forrest@0
   859
     res@tiMainString = title
forrest@0
   860
forrest@0
   861
     do k = 0,ntime-1
forrest@0
   862
        plot_data(0,k) = avg(yvalues_g(0,:,k))
forrest@0
   863
        plot_data(1,k) = avg(yvalues_g(1,:,k))
forrest@0
   864
     end do
forrest@0
   865
                                  
forrest@0
   866
     plot = gsn_csm_xy(wks,mon,plot_data,res)
forrest@0
   867
forrest@0
   868
     delete (wks)
forrest@0
   869
     delete (plot)
forrest@0
   870
forrest@1
   871
     system("convert -trim -density 100 "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   872
            "rm "+plot_name+"."+plot_type)
forrest@0
   873
forrest@0
   874
  delete (plot_data)
forrest@0
   875
forrest@0
   876
;***************************************************************************
forrest@0
   877
; write total score to file
forrest@0
   878
;***************************************************************************
forrest@0
   879
forrest@0
   880
  asciiwrite("M_save.lai", M_total)
forrest@0
   881
forrest@0
   882
;***************************************************************************
forrest@0
   883
; output plots
forrest@0
   884
;***************************************************************************
forrest@0
   885
  output_dir = model_name+"/lai"
forrest@0
   886
forrest@0
   887
  system("mv *.png *.html " + output_dir) 
forrest@0
   888
;***************************************************************************
forrest@0
   889
end
forrest@0
   890