fire/24x.table+tseries.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
; using observed biome class
forrest@0
     3
; landfrac applied to area only.
forrest@0
     4
;
forrest@0
     5
; required command line input parameters:
forrest@0
     6
;  ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
forrest@0
     7
;
forrest@0
     8
; histogram normalized by rain and compute correleration
forrest@0
     9
;**************************************************************
forrest@0
    10
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
forrest@0
    11
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
forrest@0
    12
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
forrest@0
    13
;**************************************************************
forrest@0
    14
procedure set_line(lines:string,nline:integer,newlines:string) 
forrest@0
    15
begin
forrest@0
    16
; add line to ascci/html file
forrest@0
    17
    
forrest@0
    18
  nnewlines = dimsizes(newlines)
forrest@0
    19
  if(nline+nnewlines-1.ge.dimsizes(lines))
forrest@0
    20
    print("set_line: bad index, not setting anything.") 
forrest@0
    21
    return
forrest@0
    22
  end if 
forrest@0
    23
  lines(nline:nline+nnewlines-1) = newlines
forrest@0
    24
;  print ("lines = " + lines(nline:nline+nnewlines-1))
forrest@0
    25
  nline = nline + nnewlines
forrest@0
    26
  return 
forrest@0
    27
end
forrest@0
    28
;**************************************************************
forrest@0
    29
; Main code.
forrest@0
    30
begin
forrest@0
    31
 
forrest@0
    32
  plot_type     = "ps"
forrest@0
    33
  plot_type_new = "png"
forrest@0
    34
forrest@0
    35
;---------------------------------------------------
forrest@0
    36
; model name and grid       
forrest@0
    37
forrest@0
    38
  model_grid = "T42"
forrest@0
    39
forrest@0
    40
  model_name  = "cn"
forrest@0
    41
  model_name1 = "i01.06cn"
forrest@0
    42
  model_name2 = "i01.10cn"
forrest@0
    43
forrest@0
    44
;------------------------------------------------
forrest@0
    45
; read biome data: observed
forrest@0
    46
forrest@0
    47
  biome_name_ob = "MODIS LandCover"
forrest@0
    48
forrest@0
    49
  diro = "/fis/cgd/cseg/people/jeff/clamp_data/lai/ob/"
forrest@0
    50
  filo = "land_class_"+model_grid+".nc"
forrest@0
    51
forrest@0
    52
  fo = addfile(diro+filo,"r")
forrest@0
    53
 
forrest@0
    54
  classob = tofloat(fo->LAND_CLASS)               
forrest@0
    55
forrest@0
    56
  delete (fo)
forrest@0
    57
forrest@0
    58
; observed data has 20 land-type classes
forrest@0
    59
forrest@0
    60
  nclass_ob = 20
forrest@0
    61
forrest@0
    62
;---------------------------------------------------
forrest@0
    63
; get biome data: model
forrest@0
    64
forrest@0
    65
  biome_name_mod = "Model PFT Class"
forrest@0
    66
forrest@0
    67
  dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
    68
  film = "class_pft_"+model_grid+".nc"
forrest@0
    69
  fm   = addfile(dirm+film,"r")
forrest@0
    70
 
forrest@0
    71
  classmod = fm->CLASS_PFT
forrest@0
    72
forrest@0
    73
  delete (fm)
forrest@0
    74
forrest@0
    75
; model data has 17 land-type classes
forrest@0
    76
forrest@0
    77
  nclass_mod = 17
forrest@0
    78
forrest@0
    79
;--------------------------------------------------
forrest@0
    80
; get model data: landmask, landfrac and area
forrest@0
    81
forrest@0
    82
  dirm = "/fis/cgd/cseg/people/jeff/surface_data/" 
forrest@0
    83
  film = "lnd_T42.nc"
forrest@0
    84
  fm   = addfile (dirm+film,"r")
forrest@0
    85
  
forrest@0
    86
  landmask = fm->landmask
forrest@0
    87
  landfrac = fm->landfrac
forrest@0
    88
  area     = fm->area
forrest@0
    89
forrest@0
    90
  delete (fm)
forrest@0
    91
forrest@0
    92
; change area from km**2 to m**2
forrest@0
    93
  area = area * 1.e6             
forrest@0
    94
forrest@0
    95
;---------------------------------------------------
forrest@0
    96
; take into account landfrac
forrest@0
    97
forrest@0
    98
 area = area * landfrac
forrest@0
    99
forrest@0
   100
;----------------------------------------------------
forrest@0
   101
; read data: time series, model
forrest@0
   102
forrest@0
   103
 dirm = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
forrest@0
   104
 film = model_name2 + "_Fire_C_1979-2004_monthly.nc"
forrest@0
   105
 fm   = addfile (dirm+film,"r")
forrest@0
   106
forrest@0
   107
 data_mod = fm->COL_FIRE_CLOSS(18:25,:,:,:)
forrest@0
   108
forrest@0
   109
 delete (fm)
forrest@0
   110
forrest@0
   111
; Units for these variables are:
forrest@0
   112
; g C/m^2/s
forrest@0
   113
forrest@0
   114
; change unit to g C/m^2/month
forrest@0
   115
forrest@0
   116
  nsec_per_month = 60*60*24*30
forrest@0
   117
 
forrest@0
   118
  data_mod = data_mod * nsec_per_month 
forrest@0
   119
forrest@0
   120
  data_mod@unit = "gC/m2/month"
forrest@0
   121
;----------------------------------------------------
forrest@0
   122
; read data: time series, observed
forrest@0
   123
forrest@0
   124
 dirm = "/fis/cgd/cseg/people/jeff/fire_data/ob/GFEDv2_C/"
forrest@0
   125
 film = "Fire_C_1997-2006_monthly_"+ model_grid+".nc"
forrest@0
   126
 fm   = addfile (dirm+film,"r")
forrest@0
   127
forrest@0
   128
 data_ob = fm->FIRE_C(0:7,:,:,:)
forrest@0
   129
forrest@0
   130
 delete (fm)
forrest@0
   131
forrest@0
   132
 ob_name = "GFEDv2"
forrest@0
   133
forrest@0
   134
; Units for these variables are:
forrest@0
   135
; g C/m^2/month
forrest@0
   136
forrest@0
   137
;-------------------------------------------------------------
forrest@0
   138
; html table1 data
forrest@0
   139
forrest@0
   140
; column (not including header column)
forrest@0
   141
forrest@0
   142
  col_head  = (/"Observed Fire_Flux (PgC/yr)" \
forrest@0
   143
               ,"Model Fire_Flux (PgC/yr)" \
forrest@0
   144
               ,"Correlation Coefficient" \
forrest@0
   145
               ,"Ratio model/observed" \
forrest@0
   146
               ,"M_score" \
forrest@0
   147
               ,"Timeseries plot" \
forrest@0
   148
               /)
forrest@0
   149
forrest@0
   150
  ncol = dimsizes(col_head)
forrest@0
   151
forrest@0
   152
; row (not including header row)                   
forrest@0
   153
forrest@0
   154
;----------------------------------------------------
forrest@0
   155
; using observed biome class:  
forrest@0
   156
  row_head  = (/"Evergreen Needleleaf Forests" \
forrest@0
   157
               ,"Evergreen Broadleaf Forests" \
forrest@0
   158
               ,"Deciduous Needleleaf Forest" \
forrest@0
   159
               ,"Deciduous Broadleaf Forests" \
forrest@0
   160
               ,"Mixed Forests" \                      
forrest@0
   161
               ,"Closed Bushlands" \                   
forrest@0
   162
               ,"Open Bushlands" \                     
forrest@0
   163
               ,"Woody Savannas (S. Hem.)" \           
forrest@0
   164
               ,"Savannas (S. Hem.)" \                 
forrest@0
   165
               ,"Grasslands" \                         
forrest@0
   166
               ,"Permanent Wetlands" \                 
forrest@0
   167
               ,"Croplands" \                                           
forrest@0
   168
               ,"Cropland/Natural Vegetation Mosaic" \             
forrest@0
   169
               ,"Barren or Sparsely Vegetated" \                             
forrest@0
   170
               ,"Woody Savannas (N. Hem.)" \           
forrest@0
   171
               ,"Savannas (N. Hem.)" \
forrest@0
   172
               ,"All Biome" \                
forrest@0
   173
               /)
forrest@0
   174
forrest@0
   175
forrest@0
   176
; using model biome class:  
forrest@0
   177
; row_head  = (/"Not Vegetated" \
forrest@0
   178
;              ,"Needleleaf Evergreen Temperate Tree" \
forrest@0
   179
;              ,"Needleleaf Evergreen Boreal Tree" \
forrest@0
   180
;;             ,"Needleleaf Deciduous Boreal Tree" \
forrest@0
   181
;              ,"Broadleaf Evergreen Tropical Tree" \
forrest@0
   182
;              ,"Broadleaf Evergreen Temperate Tree" \
forrest@0
   183
;              ,"Broadleaf Deciduous Tropical Tree" \
forrest@0
   184
;              ,"Broadleaf Deciduous Temperate Tree" \
forrest@0
   185
;;             ,"Broadleaf Deciduous Boreal Tree" \
forrest@0
   186
;;             ,"Broadleaf Evergreen Shrub" \
forrest@0
   187
;              ,"Broadleaf Deciduous Temperate Shrub" \
forrest@0
   188
;              ,"Broadleaf Deciduous Boreal Shrub" \
forrest@0
   189
;              ,"C3 Arctic Grass" \
forrest@0
   190
;              ,"C3 Non-Arctic Grass" \
forrest@0
   191
;              ,"C4 Grass" \
forrest@0
   192
;              ,"Corn" \
forrest@0
   193
;;             ,"Wheat" \                      
forrest@0
   194
;              ,"All Biome" \                
forrest@0
   195
;              /)  
forrest@0
   196
  nrow = dimsizes(row_head)                  
forrest@0
   197
forrest@0
   198
; arrays to be passed to table. 
forrest@0
   199
  text = new ((/nrow, ncol/),string ) 
forrest@0
   200
forrest@0
   201
;*****************************************************************
forrest@0
   202
; (A) get time-mean
forrest@0
   203
;*****************************************************************
forrest@0
   204
  
forrest@0
   205
  x          = dim_avg_Wrap(data_mod(lat|:,lon|:,month|:,year|:))
forrest@0
   206
  data_mod_m = dim_avg_Wrap(       x(lat|:,lon|:,month|:))
forrest@0
   207
  delete (x)
forrest@0
   208
forrest@0
   209
  x          = dim_avg_Wrap( data_ob(lat|:,lon|:,month|:,year|:))
forrest@0
   210
  data_ob_m  = dim_avg_Wrap(       x(lat|:,lon|:,month|:))
forrest@0
   211
  delete (x)
forrest@0
   212
forrest@0
   213
;----------------------------------------------------
forrest@0
   214
; compute correlation coef: space
forrest@0
   215
forrest@0
   216
  landmask_1d = ndtooned(landmask)
forrest@0
   217
  data_mod_1d = ndtooned(data_mod_m)
forrest@0
   218
  data_ob_1d  = ndtooned(data_ob_m )
forrest@0
   219
  area_1d     = ndtooned(area)
forrest@0
   220
  landfrac_1d = ndtooned(landfrac)
forrest@0
   221
forrest@0
   222
  good = ind(landmask_1d .gt. 0.)
forrest@0
   223
forrest@0
   224
  global_mod = sum(data_mod_1d(good)*area_1d(good)) * 1.e-15 * 12.
forrest@0
   225
  global_ob  = sum(data_ob_1d(good) *area_1d(good)) * 1.e-15 * 12.
forrest@0
   226
forrest@0
   227
  print (global_mod)
forrest@0
   228
  print (global_ob) 
forrest@0
   229
forrest@0
   230
  global_area= sum(area_1d)
forrest@0
   231
  global_land= sum(area_1d(good))
forrest@0
   232
  print (global_area)
forrest@0
   233
  print (global_land)
forrest@0
   234
forrest@0
   235
  cc_space = esccr(data_mod_1d(good)*landfrac_1d(good),data_ob_1d(good)*landfrac_1d(good),0)
forrest@0
   236
forrest@0
   237
  delete (landmask_1d)
forrest@0
   238
  delete (landfrac_1d)
forrest@0
   239
; delete (area_1d)
forrest@0
   240
  delete (data_mod_1d)
forrest@0
   241
  delete (data_ob_1d)
forrest@0
   242
  delete (good)
forrest@0
   243
forrest@0
   244
;----------------------------------------------------
forrest@0
   245
; compute M_global
forrest@0
   246
forrest@0
   247
  score_max = 1.
forrest@0
   248
forrest@0
   249
  Mscore1 = cc_space * cc_space * score_max
forrest@0
   250
forrest@0
   251
  M_global = sprintf("%.2f", Mscore1)
forrest@0
   252
 
forrest@0
   253
;----------------------------------------------------
forrest@0
   254
; global res
forrest@0
   255
forrest@0
   256
  resg                      = True             ; Use plot options
forrest@0
   257
  resg@cnFillOn             = True             ; Turn on color fill
forrest@0
   258
  resg@gsnSpreadColors      = True             ; use full colormap
forrest@0
   259
  resg@cnLinesOn            = False            ; Turn off contourn lines
forrest@0
   260
  resg@mpFillOn             = False            ; Turn off map fill
forrest@0
   261
  resg@cnLevelSelectionMode = "ManualLevels"   ; Manual contour invtervals
forrest@0
   262
      
forrest@0
   263
;----------------------------------------------------
forrest@0
   264
; global contour: model vs ob
forrest@0
   265
forrest@0
   266
  plot_name = "global_model_vs_ob"
forrest@0
   267
forrest@0
   268
  wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   269
  gsn_define_colormap(wks,"gui_default")     
forrest@0
   270
forrest@0
   271
  plot=new(3,graphic)                        ; create graphic array
forrest@0
   272
forrest@0
   273
  resg@gsnFrame             = False          ; Do not draw plot 
forrest@0
   274
  resg@gsnDraw              = False          ; Do not advance frame
forrest@0
   275
forrest@0
   276
;----------------------
forrest@0
   277
; plot correlation coef
forrest@0
   278
forrest@0
   279
  gRes               = True
forrest@0
   280
  gRes@txFontHeightF = 0.02
forrest@0
   281
  gRes@txAngleF      = 90
forrest@0
   282
forrest@0
   283
  correlation_text = "(correlation coef = "+sprintf("%.2f", cc_space)+")"
forrest@0
   284
forrest@0
   285
  gsn_text_ndc(wks,correlation_text,0.20,0.50,gRes)
forrest@0
   286
forrest@0
   287
;-----------------------  
forrest@0
   288
; plot ob
forrest@0
   289
forrest@0
   290
  data_ob_m = where(landmask .gt. 0., data_ob_m, data_ob_m@_FillValue)
forrest@0
   291
forrest@0
   292
  title     = ob_name
forrest@0
   293
  resg@tiMainString  = title
forrest@0
   294
forrest@0
   295
  resg@cnMinLevelValF       = 1.             
forrest@0
   296
  resg@cnMaxLevelValF       = 10.             
forrest@0
   297
  resg@cnLevelSpacingF      = 1.
forrest@0
   298
forrest@0
   299
  plot(0) = gsn_csm_contour_map_ce(wks,data_ob_m,resg)       
forrest@0
   300
forrest@0
   301
;-----------------------
forrest@0
   302
; plot model
forrest@0
   303
forrest@0
   304
  data_mod_m = where(landmask .gt. 0., data_mod_m, data_mod_m@_FillValue)
forrest@0
   305
forrest@0
   306
  title     = "Model "+ model_name
forrest@0
   307
  resg@tiMainString  = title
forrest@0
   308
forrest@0
   309
  resg@cnMinLevelValF       = 1.             
forrest@0
   310
  resg@cnMaxLevelValF       = 10.             
forrest@0
   311
  resg@cnLevelSpacingF      = 1.
forrest@0
   312
forrest@0
   313
  plot(1) = gsn_csm_contour_map_ce(wks,data_mod_m,resg) 
forrest@0
   314
forrest@0
   315
;-----------------------
forrest@0
   316
; plot model-ob
forrest@0
   317
forrest@0
   318
  resg@cnMinLevelValF  = -8.           
forrest@0
   319
  resg@cnMaxLevelValF  =  2.            
forrest@0
   320
  resg@cnLevelSpacingF =  1.
forrest@0
   321
forrest@0
   322
  zz = data_ob_m
forrest@0
   323
  zz = data_mod_m - data_ob_m
forrest@0
   324
  title = "Model_"+model_name+" - Observed"
forrest@0
   325
  resg@tiMainString    = title
forrest@0
   326
forrest@0
   327
  plot(2) = gsn_csm_contour_map_ce(wks,zz,resg) 
forrest@0
   328
forrest@0
   329
; plot panel
forrest@0
   330
forrest@0
   331
  pres                            = True        ; panel plot mods desired
forrest@0
   332
  pres@gsnMaximize                = True        ; fill the page
forrest@0
   333
forrest@0
   334
  gsn_panel(wks,plot,(/3,1/),pres)              ; create panel plot
forrest@0
   335
forrest@0
   336
  system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   337
        "rm "+plot_name+"."+plot_type)
forrest@0
   338
forrest@0
   339
  clear (wks)
forrest@0
   340
  delete (plot)
forrest@0
   341
forrest@0
   342
  delete (data_ob_m)
forrest@0
   343
  delete (data_mod_m)
forrest@0
   344
  delete (zz)
forrest@0
   345
forrest@0
   346
  resg@gsnFrame             = True          ; Do advance frame 
forrest@0
   347
  resg@gsnDraw              = True          ; Do draw plot
forrest@0
   348
forrest@0
   349
;*******************************************************************
forrest@0
   350
; (B) Time series : per biome
forrest@0
   351
;*******************************************************************
forrest@0
   352
forrest@0
   353
 data_n = 2
forrest@0
   354
forrest@0
   355
 dsizes = dimsizes(data_mod)
forrest@0
   356
 nyear  = dsizes(0)
forrest@0
   357
 nmonth = dsizes(1)
forrest@0
   358
 ntime  = nyear * nmonth
forrest@0
   359
forrest@0
   360
 year_start = 1997
forrest@0
   361
 year_end   = 2004
forrest@0
   362
                
forrest@0
   363
;-------------------------------------------
forrest@0
   364
; Calculate "nice" bins for binning the data
forrest@0
   365
forrest@0
   366
; using ob biome class
forrest@0
   367
  nclass = nclass_ob
forrest@0
   368
; using model biome class
forrest@0
   369
; nclass = nclass_mod
forrest@0
   370
forrest@0
   371
  range  = fspan(0,nclass,nclass+1)
forrest@0
   372
forrest@0
   373
; Use this range information to grab all the values in a
forrest@0
   374
; particular range, and then take an average.
forrest@0
   375
forrest@0
   376
  nx = dimsizes(range) - 1
forrest@0
   377
forrest@0
   378
;-------------------------------------------
forrest@0
   379
; put data into bins
forrest@0
   380
forrest@0
   381
; using observed biome class
forrest@0
   382
  base  = ndtooned(classob)
forrest@0
   383
; using model biome class
forrest@0
   384
; base  = ndtooned(classmod)
forrest@0
   385
forrest@0
   386
; output
forrest@0
   387
forrest@0
   388
  area_bin = new((/nx/),float)
forrest@0
   389
  yvalues  = new((/ntime,data_n,nx/),float)
forrest@0
   390
forrest@0
   391
; Loop through each range, using base.
forrest@0
   392
forrest@0
   393
  do i=0,nx-1
forrest@0
   394
forrest@0
   395
     if (i.ne.(nx-1)) then
forrest@0
   396
        idx = ind((base.ge.range(i)).and.(base.lt.range(i+1)))
forrest@0
   397
     else
forrest@0
   398
        idx = ind(base.ge.range(i))
forrest@0
   399
     end if
forrest@0
   400
;---------------------
forrest@0
   401
;    for area 
forrest@0
   402
forrest@0
   403
     if (.not.any(ismissing(idx))) then 
forrest@0
   404
        area_bin(i) = sum(area_1d(idx))
forrest@0
   405
     else
forrest@0
   406
        area_bin(i) = area_bin@_FillValue
forrest@0
   407
     end if
forrest@0
   408
forrest@0
   409
;#############################################################
forrest@0
   410
;using observed biome class: 
forrest@0
   411
;     set the following 4 classes to _FillValue:
forrest@0
   412
;     Water Bodies(0), Urban and Build-Up(13),
forrest@0
   413
;     Permenant Snow and Ice(15), Unclassified(17)
forrest@0
   414
forrest@0
   415
      if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then
forrest@0
   416
         area_bin(i) = yvalues@_FillValue
forrest@0
   417
      end if
forrest@0
   418
;############################################################# 
forrest@0
   419
forrest@0
   420
forrest@0
   421
;#############################################################
forrest@0
   422
; using model biome class:
forrest@0
   423
;     set the following 4 classes to _FillValue:
forrest@0
   424
;     (3)Needleleaf Deciduous Boreal Tree,
forrest@0
   425
;     (8)Broadleaf Deciduous Boreal Tree,
forrest@0
   426
;     (9)Broadleaf Evergreen Shrub,
forrest@0
   427
;     (16)Wheat
forrest@0
   428
forrest@0
   429
;    if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
forrest@0
   430
;       area_bin(i) = area_bin@_FillValue
forrest@0
   431
;    end if
forrest@0
   432
;#############################################################  
forrest@0
   433
forrest@0
   434
;---------------------
forrest@0
   435
; for data_mod and data_ob
forrest@0
   436
forrest@0
   437
  do n = 0,data_n-1
forrest@0
   438
forrest@0
   439
     t = -1
forrest@0
   440
     do m = 0,nyear-1
forrest@0
   441
     do k = 0,nmonth-1
forrest@0
   442
    
forrest@0
   443
        t = t + 1 
forrest@0
   444
forrest@0
   445
        if (n.eq.0) then
forrest@0
   446
           data = ndtooned(data_ob(m,k,:,:))
forrest@0
   447
        end if
forrest@0
   448
forrest@0
   449
        if (n.eq.1) then
forrest@0
   450
           data = ndtooned(data_mod(m,k,:,:))
forrest@0
   451
        end if
forrest@0
   452
forrest@0
   453
;       Calculate average
forrest@0
   454
 
forrest@0
   455
        if (.not.any(ismissing(idx))) then
forrest@0
   456
           yvalues(t,n,i) = sum(data(idx)*area_1d(idx)) 
forrest@0
   457
        else
forrest@0
   458
           yvalues(t,n,i) = yvalues@_FillValue
forrest@0
   459
        end if
forrest@0
   460
forrest@0
   461
;#############################################################
forrest@0
   462
;using observed biome class: 
forrest@0
   463
;     set the following 4 classes to _FillValue:
forrest@0
   464
;     Water Bodies(0), Urban and Build-Up(13),
forrest@0
   465
;     Permenant Snow and Ice(15), Unclassified(17)
forrest@0
   466
forrest@0
   467
      if (i.eq.0 .or. i.eq.13 .or. i.eq.15 .or. i.eq.17) then
forrest@0
   468
         yvalues(t,n,i) = yvalues@_FillValue
forrest@0
   469
      end if
forrest@0
   470
;############################################################# 
forrest@0
   471
forrest@0
   472
forrest@0
   473
;#############################################################
forrest@0
   474
; using model biome class:
forrest@0
   475
;     set the following 4 classes to _FillValue:
forrest@0
   476
;     (3)Needleleaf Deciduous Boreal Tree,
forrest@0
   477
;     (8)Broadleaf Deciduous Boreal Tree,
forrest@0
   478
;     (9)Broadleaf Evergreen Shrub,
forrest@0
   479
;     (16)Wheat
forrest@0
   480
forrest@0
   481
;       if (i.eq.3 .or. i.eq.8 .or. i.eq.9 .or. i.eq.16) then
forrest@0
   482
;          yvalues(t,n,i) = yvalues@_FillValue
forrest@0
   483
;       end if
forrest@0
   484
;#############################################################  
forrest@0
   485
forrest@0
   486
     end do
forrest@0
   487
     end do
forrest@0
   488
forrest@0
   489
     delete(data)
forrest@0
   490
  end do 
forrest@0
   491
forrest@0
   492
    delete(idx)
forrest@0
   493
  end do
forrest@0
   494
forrest@0
   495
  delete (base)
forrest@0
   496
  delete (data_mod)
forrest@0
   497
  delete (data_ob)
forrest@0
   498
forrest@0
   499
  global_bin = sum(area_bin)
forrest@0
   500
  print (global_bin)
forrest@0
   501
forrest@0
   502
;----------------------------------------------------------------
forrest@0
   503
; get area_good
forrest@0
   504
forrest@0
   505
  good = ind(.not.ismissing(area_bin))
forrest@0
   506
forrest@0
   507
  area_g = area_bin(good)  
forrest@0
   508
forrest@0
   509
  n_biome = dimsizes(good)
forrest@0
   510
forrest@0
   511
  global_good = sum(area_g)
forrest@0
   512
  print (global_good)
forrest@0
   513
forrest@0
   514
;----------------------------------------------------------------
forrest@0
   515
; data for tseries plot
forrest@0
   516
forrest@0
   517
  yvalues_g = new((/ntime,data_n,n_biome/),float)
forrest@0
   518
forrest@0
   519
  yvalues_g@units = "TgC/month"
forrest@0
   520
forrest@0
   521
; change unit to Tg C/month
forrest@0
   522
; change unit from g to Tg (Tera gram)
forrest@0
   523
  factor_unit = 1.e-12
forrest@0
   524
forrest@0
   525
  yvalues_g = yvalues(:,:,good) * factor_unit
forrest@0
   526
forrest@0
   527
  delete (good)
forrest@0
   528
forrest@0
   529
;-------------------------------------------------------------------
forrest@0
   530
; general settings for line plot
forrest@0
   531
forrest@0
   532
  res                   = True               
forrest@0
   533
  res@xyDashPatterns    = (/0,0/)          ; make lines solid
forrest@0
   534
  res@xyLineThicknesses = (/2.0,2.0/)      ; make lines thicker
forrest@0
   535
  res@xyLineColors      = (/"blue","red"/) ; line color
forrest@0
   536
forrest@0
   537
  res@trXMinF   = year_start
forrest@0
   538
  res@trXMaxF   = year_end + 1
forrest@0
   539
forrest@0
   540
  res@vpHeightF = 0.4                 ; change aspect ratio of plot
forrest@0
   541
; res@vpWidthF  = 0.8
forrest@0
   542
  res@vpWidthF  = 0.75   
forrest@0
   543
forrest@0
   544
  res@tiMainFontHeightF = 0.025       ; size of title 
forrest@0
   545
forrest@0
   546
  res@tmXBFormat  = "f"               ; not to add trailing zeros
forrest@0
   547
forrest@0
   548
; res@gsnMaximize = True
forrest@0
   549
forrest@0
   550
;----------------------------------------------
forrest@0
   551
; Add a boxed legend using the simple method
forrest@0
   552
forrest@0
   553
  res@pmLegendDisplayMode    = "Always"
forrest@0
   554
; res@pmLegendWidthF         = 0.1
forrest@0
   555
  res@pmLegendWidthF         = 0.08
forrest@0
   556
  res@pmLegendHeightF        = 0.06
forrest@0
   557
  res@pmLegendOrthogonalPosF = -1.17
forrest@0
   558
; res@pmLegendOrthogonalPosF = -1.00  ;(downward)
forrest@0
   559
; res@pmLegendOrthogonalPosF = -0.30  ;(downward)
forrest@0
   560
forrest@0
   561
; res@pmLegendParallelPosF   =  0.18
forrest@0
   562
  res@pmLegendParallelPosF   =  0.23  ;(rightward)
forrest@0
   563
  res@pmLegendParallelPosF   =  0.73  ;(rightward)
forrest@0
   564
  res@pmLegendParallelPosF   =  0.83  ;(rightward)
forrest@0
   565
forrest@0
   566
; res@lgPerimOn             = False
forrest@0
   567
  res@lgLabelFontHeightF     = 0.015
forrest@0
   568
  res@xyExplicitLegendLabels = (/"observed",model_name/)
forrest@0
   569
forrest@0
   570
;*******************************************************************
forrest@0
   571
; (A) time series plot: monthly ( 2 lines per plot)
forrest@0
   572
;*******************************************************************
forrest@0
   573
forrest@0
   574
; x-axis in time series plot
forrest@0
   575
forrest@0
   576
  timeI = new((/ntime/),integer)
forrest@0
   577
  timeF = new((/ntime/),float)
forrest@0
   578
  timeI = ispan(1,ntime,1)
forrest@0
   579
  timeF = year_start + (timeI-1)/12.
forrest@0
   580
  timeF@long_name = "year" 
forrest@0
   581
forrest@0
   582
  plot_data = new((/2,ntime/),float)
forrest@0
   583
  plot_data@long_name = "TgC/month"
forrest@0
   584
forrest@0
   585
;----------------------------------------------
forrest@0
   586
; time series plot : per biome
forrest@0
   587
 
forrest@0
   588
  do m = 0, n_biome-1
forrest@0
   589
forrest@0
   590
     plot_name = "monthly_biome_"+ m
forrest@0
   591
forrest@0
   592
     wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   593
forrest@0
   594
     title = "Fire : "+ row_head(m)
forrest@0
   595
     res@tiMainString = title
forrest@0
   596
forrest@0
   597
     plot_data(0,:) = yvalues_g(:,0,m)
forrest@0
   598
     plot_data(1,:) = yvalues_g(:,1,m)
forrest@0
   599
                                  
forrest@0
   600
     plot = gsn_csm_xy(wks,timeF,plot_data,res)
forrest@0
   601
forrest@0
   602
     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   603
            "rm "+plot_name+"."+plot_type)
forrest@0
   604
forrest@0
   605
     clear (wks)  
forrest@0
   606
     delete (plot)
forrest@0
   607
   
forrest@0
   608
  end do
forrest@0
   609
forrest@0
   610
;------------------------------------------
forrest@0
   611
; data for table : per biome
forrest@0
   612
forrest@0
   613
; unit change from TgC/month to PgC/month
forrest@0
   614
  unit_factor = 1.e-3
forrest@0
   615
forrest@0
   616
  score_max = 1.
forrest@0
   617
forrest@0
   618
  tmp_ob    = new((/ntime/),float)
forrest@0
   619
  tmp_mod   = new((/ntime/),float)
forrest@0
   620
forrest@0
   621
  total_ob  = new((/n_biome/),float)
forrest@0
   622
  total_mod = new((/n_biome/),float)
forrest@0
   623
  Mscore2   = new((/n_biome/),float)
forrest@0
   624
forrest@0
   625
  do m = 0, n_biome-1
forrest@0
   626
forrest@0
   627
     tmp_ob  = yvalues_g(:,0,m) 
forrest@0
   628
     tmp_mod = yvalues_g(:,1,m) 
forrest@0
   629
forrest@0
   630
     total_ob(m)  = avg(month_to_annual(tmp_ob, 0)) * unit_factor 
forrest@0
   631
     total_mod(m) = avg(month_to_annual(tmp_mod,0)) * unit_factor
forrest@0
   632
     
forrest@0
   633
     cc_time = esccr(tmp_mod,tmp_ob,0)
forrest@0
   634
forrest@0
   635
     ratio = total_mod(m)/total_ob(m)
forrest@0
   636
forrest@0
   637
     good = ind(tmp_ob .ne. 0. .and. tmp_mod .ne. 0.)
forrest@0
   638
forrest@0
   639
     bias = sum( abs( tmp_mod(good)-tmp_ob(good) )/( abs(tmp_mod(good))+abs(tmp_ob(good)) ) )
forrest@0
   640
     Mscore2(m) = (1.- (bias/dimsizes(good)))*score_max
forrest@0
   641
forrest@0
   642
     delete (good)
forrest@0
   643
     
forrest@0
   644
     text(m,0) = sprintf("%.2f",total_ob(m))
forrest@0
   645
     text(m,1) = sprintf("%.2f",total_mod(m))
forrest@0
   646
     text(m,2) = sprintf("%.2f",cc_time)
forrest@0
   647
     text(m,3) = sprintf("%.2f",ratio)
forrest@0
   648
     text(m,4) = sprintf("%.2f",Mscore2(m))
forrest@0
   649
     text(m,5) = "<a href=./monthly_biome_"+m+".png>model_vs_ob</a>" 
forrest@0
   650
  end do
forrest@0
   651
 
forrest@0
   652
  delete (tmp_ob)
forrest@0
   653
  delete (tmp_mod)
forrest@0
   654
forrest@0
   655
;--------------------------------------------
forrest@0
   656
; time series plot: all biome
forrest@0
   657
forrest@0
   658
     plot_name = "monthly_global"
forrest@0
   659
forrest@0
   660
     wks = gsn_open_wks (plot_type,plot_name)   
forrest@0
   661
forrest@0
   662
     title = "Fire : "+ row_head(n_biome)
forrest@0
   663
     res@tiMainString = title
forrest@0
   664
forrest@0
   665
     do k = 0,ntime-1
forrest@0
   666
        plot_data(0,k) = sum(yvalues_g(k,0,:))
forrest@0
   667
        plot_data(1,k) = sum(yvalues_g(k,1,:))
forrest@0
   668
     end do
forrest@0
   669
                                  
forrest@0
   670
     plot = gsn_csm_xy(wks,timeF,plot_data,res)
forrest@0
   671
forrest@0
   672
     system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
forrest@0
   673
            "rm "+plot_name+"."+plot_type)
forrest@0
   674
forrest@0
   675
     clear (wks)  
forrest@0
   676
     delete (plot)
forrest@0
   677
forrest@0
   678
;------------------------------------------
forrest@0
   679
; data for table : global
forrest@0
   680
forrest@0
   681
  score_max = 1.
forrest@0
   682
forrest@0
   683
  tmp_ob  = ndtooned(yvalues_g(:,0,:))
forrest@0
   684
  tmp_mod = ndtooned(yvalues_g(:,1,:))
forrest@0
   685
forrest@0
   686
  cc_time = esccr(tmp_mod,tmp_ob,0)
forrest@0
   687
forrest@0
   688
  ratio = sum(total_mod)/sum(total_ob) 
forrest@0
   689
forrest@0
   690
  good = ind(tmp_ob .ne. 0. .and. tmp_mod .ne. 0.)
forrest@0
   691
forrest@0
   692
  bias = sum( abs( tmp_mod(good)-tmp_ob(good) )/( abs(tmp_mod(good))+abs(tmp_ob(good)) ) )
forrest@0
   693
  Mscore3 = (1.- (bias/dimsizes(good)))*score_max
forrest@0
   694
forrest@0
   695
  print (Mscore3)
forrest@0
   696
forrest@0
   697
  delete (good)
forrest@0
   698
forrest@0
   699
  text(nrow-1,0) = sprintf("%.2f",sum(total_ob))
forrest@0
   700
  text(nrow-1,1) = sprintf("%.2f",sum(total_mod))
forrest@0
   701
  text(nrow-1,2) = sprintf("%.2f",cc_time)
forrest@0
   702
  text(nrow-1,3) = sprintf("%.2f",ratio)
forrest@0
   703
; text(nrow-1,4) = sprintf("%.2f",avg(Mscore2))
forrest@0
   704
  text(nrow-1,4) = sprintf("%.2f",    Mscore3)
forrest@0
   705
  text(nrow-1,5) = "<a href=./monthly_global.png>model_vs_ob</a>"
forrest@0
   706
forrest@0
   707
;**************************************************
forrest@0
   708
; create html table
forrest@0
   709
;**************************************************
forrest@0
   710
forrest@0
   711
  header_text = "<H1>Fire Emission (1997-2004):  Model "+model_name+"</H1>" 
forrest@0
   712
forrest@0
   713
  header = (/"<HTML>" \
forrest@0
   714
            ,"<HEAD>" \
forrest@0
   715
            ,"<TITLE>CLAMP metrics</TITLE>" \
forrest@0
   716
            ,"</HEAD>" \
forrest@0
   717
            ,header_text \
forrest@0
   718
            /) 
forrest@0
   719
  footer = "</HTML>"
forrest@0
   720
forrest@0
   721
  table_header = (/ \
forrest@0
   722
        "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
forrest@0
   723
       ,"<tr>" \
forrest@0
   724
       ,"   <th bgcolor=DDDDDD >Biome Type</th>" \
forrest@0
   725
       ,"   <th bgcolor=DDDDDD >"+col_head(0)+"</th>" \
forrest@0
   726
       ,"   <th bgcolor=DDDDDD >"+col_head(1)+"</th>" \
forrest@0
   727
       ,"   <th bgcolor=DDDDDD >"+col_head(2)+"</th>" \
forrest@0
   728
       ,"   <th bgcolor=DDDDDD >"+col_head(3)+"</th>" \
forrest@0
   729
       ,"   <th bgcolor=DDDDDD >"+col_head(4)+"</th>" \
forrest@0
   730
       ,"   <th bgcolor=DDDDDD >"+col_head(5)+"</th>" \
forrest@0
   731
       ,"</tr>" \
forrest@0
   732
       /)
forrest@0
   733
  table_footer = "</table>"
forrest@0
   734
  row_header = "<tr>"
forrest@0
   735
  row_footer = "</tr>"
forrest@0
   736
forrest@0
   737
  lines = new(50000,string)
forrest@0
   738
  nline = 0
forrest@0
   739
forrest@0
   740
  set_line(lines,nline,header)
forrest@0
   741
  set_line(lines,nline,table_header)
forrest@0
   742
;-----------------------------------------------
forrest@0
   743
;row of table
forrest@0
   744
forrest@0
   745
  do n = 0,nrow-1
forrest@0
   746
     set_line(lines,nline,row_header)
forrest@0
   747
forrest@0
   748
     txt0  = row_head(n)
forrest@0
   749
     txt1  = text(n,0)
forrest@0
   750
     txt2  = text(n,1)
forrest@0
   751
     txt3  = text(n,2)
forrest@0
   752
     txt4  = text(n,3)
forrest@0
   753
     txt5  = text(n,4)
forrest@0
   754
     txt6  = text(n,5)
forrest@0
   755
forrest@0
   756
     set_line(lines,nline,"<th>"+txt0+"</th>")
forrest@0
   757
     set_line(lines,nline,"<th>"+txt1+"</th>")
forrest@0
   758
     set_line(lines,nline,"<th>"+txt2+"</th>")
forrest@0
   759
     set_line(lines,nline,"<th>"+txt3+"</th>")
forrest@0
   760
     set_line(lines,nline,"<th>"+txt4+"</th>")
forrest@0
   761
     set_line(lines,nline,"<th>"+txt5+"</th>")
forrest@0
   762
     set_line(lines,nline,"<th>"+txt6+"</th>")
forrest@0
   763
forrest@0
   764
     set_line(lines,nline,row_footer)
forrest@0
   765
  end do
forrest@0
   766
;-----------------------------------------------
forrest@0
   767
  set_line(lines,nline,table_footer)
forrest@0
   768
  set_line(lines,nline,footer) 
forrest@0
   769
forrest@0
   770
; Now write to an HTML file.
forrest@0
   771
forrest@0
   772
  output_html = "table_fire.html"
forrest@0
   773
forrest@0
   774
  idx = ind(.not.ismissing(lines))
forrest@0
   775
  if(.not.any(ismissing(idx))) then
forrest@0
   776
    asciiwrite(output_html,lines(idx))
forrest@0
   777
  else
forrest@0
   778
   print ("error?")
forrest@0
   779
  end if
forrest@0
   780
forrest@0
   781
  delete (idx)
forrest@0
   782
forrest@0
   783
end
forrest@0
   784