1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/energy/99.new.ncl Mon Jan 26 22:08:20 2009 -0500
1.3 @@ -0,0 +1,528 @@
1.4 +;************************************************************
1.5 +; required command line input parameters:
1.6 +; ncl 'model_name="10cn" model_grid="T42" dirm="/.../ film="..."' 01.npp.ncl
1.7 +;
1.8 +; using gsn_table for all
1.9 +; output: line plot for each site (4 fields)
1.10 +; table for M_score
1.11 +;************************************************************
1.12 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
1.13 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
1.14 +load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
1.15 +;************************************************************
1.16 +procedure set_line(lines:string,nline:integer,newlines:string)
1.17 +begin
1.18 +; add line to ascci/html file
1.19 +
1.20 + nnewlines = dimsizes(newlines)
1.21 + if(nline+nnewlines-1.ge.dimsizes(lines))
1.22 + print("set_line: bad index, not setting anything.")
1.23 + return
1.24 + end if
1.25 + lines(nline:nline+nnewlines-1) = newlines
1.26 +; print ("lines = " + lines(nline:nline+nnewlines-1))
1.27 + nline = nline + nnewlines
1.28 + return
1.29 +end
1.30 +;*************************************************************
1.31 +begin
1.32 +
1.33 + plot_type = "ps"
1.34 + plot_type_new = "png"
1.35 +
1.36 +;************************************************
1.37 +; read model data
1.38 +;************************************************
1.39 +
1.40 +; model_name = "i01.10cn"
1.41 + model_name = "i01.10casa"
1.42 +
1.43 + dirm = "/fis/cgd/cseg/people/jeff/surface_data/"
1.44 + film = "lnd_T42.nc"
1.45 + fm = addfile(dirm+film,"r")
1.46 +
1.47 + xm = fm->lon
1.48 + ym = fm->lat
1.49 +;------------------------------------------------
1.50 + nlat = dimsizes(ym)
1.51 + nlon = dimsizes(xm)
1.52 +
1.53 +; for 4 fields, 12-monthly
1.54 + nmon = 12
1.55 + nfield = 4
1.56 +
1.57 + data_mod0 = new ((/nfield,nmon,nlat,nlon/),float)
1.58 +
1.59 +; change to unit of observed (u mol/m2/s)
1.60 +; Model_units [=] gC/m2/s
1.61 +; 12. = molecular weight of C
1.62 +; u mol = 1e-6 mol
1.63 + factor = 1e6 /12.
1.64 +
1.65 + ENERGY ="new"
1.66 +
1.67 +;************************************************
1.68 +; read data: observed
1.69 +;************************************************
1.70 +
1.71 + station = (/"BOREAS_NSA_OBS" \
1.72 + ,"CastelPorziano" \
1.73 + ,"Hyytiala" \
1.74 + ,"Kaamanen" \
1.75 + ,"LBA_Tapajos_KM67" \
1.76 + ,"Lethbridge" \
1.77 + ,"Tharandt" \
1.78 + ,"Vielsalm" \
1.79 + /)
1.80 +
1.81 + year_ob = (/"1994-2004" \
1.82 + ,"1997-2003" \
1.83 + ,"1996-2003" \
1.84 + ,"2000-2003" \
1.85 + ,"2002-2005" \
1.86 + ,"1999-2004" \
1.87 + ,"1996-2003" \
1.88 + ,"1998-2003" \
1.89 + /)
1.90 +
1.91 + field = (/"CO2 Flux" \
1.92 + ,"Net Radiation" \
1.93 + ,"Latent Heat" \
1.94 + ,"Sensible Heat" \
1.95 + /)
1.96 +
1.97 + nstation = dimsizes(station)
1.98 + nmon = 12
1.99 + nfield = dimsizes(field)
1.100 +
1.101 + data_mod = new ((/nstation, nfield, nmon/),float)
1.102 + data_ob = new ((/nstation, nfield, nmon/),float)
1.103 + lat_ob = new ((/nstation/),float)
1.104 + lon_ob = new ((/nstation/),float)
1.105 +
1.106 + diro_root = "/fis/cgd/cseg/people/jeff/clamp_data/fluxnet/"
1.107 + dirm_root = "/fis/cgd/cseg/people/jeff/clamp_data/model/"
1.108 +
1.109 + do n = 0,nstation-1
1.110 +
1.111 +;-------------------------------------------------
1.112 +; get ob data
1.113 +
1.114 + diro = diro_root + station(n)+"/"
1.115 + filo = station(n)+"_"+year_ob(n)+"_monthly.nc"
1.116 + fo = addfile (diro+filo,"r")
1.117 +
1.118 + print (filo)
1.119 +
1.120 + lon_ob(n) = fo->lon
1.121 + lat_ob(n) = fo->lat
1.122 +
1.123 + data = fo->CO2_FLUX
1.124 + data_ob(n,0,:) = dim_avg(data(month|:,year|:))
1.125 + delete (data)
1.126 +
1.127 + data = fo->RAD_FLUX
1.128 + data_ob(n,1,:) = dim_avg(data(month|:,year|:))
1.129 + delete (data)
1.130 +
1.131 + data = fo->LH_FLUX
1.132 + data_ob(n,2,:) = dim_avg(data(month|:,year|:))
1.133 + delete (data)
1.134 +
1.135 + data = fo->SH_FLUX
1.136 + data_ob(n,3,:) = dim_avg(data(month|:,year|:))
1.137 + delete (data)
1.138 +
1.139 + delete (fo)
1.140 +;---------------------------------------------------
1.141 +; get model data
1.142 +
1.143 + film = model_name+"_"+year_ob(n)+"_MONS_climo.nc"
1.144 + fm = addfile (dirm_root+film,"r")
1.145 +
1.146 + print (film)
1.147 +
1.148 +if (ENERGY .eq. "old") then
1.149 +
1.150 + data = fm->NEE
1.151 + data_mod0(0,:,:,:) = data(:,:,:) * factor
1.152 + delete (data)
1.153 +
1.154 +; data = fm->LATENT
1.155 + data1 = fm->FCEV
1.156 + data2 = fm->FCTR
1.157 + data3 = fm->FGEV
1.158 + data_mod0(2,:,:,:) = data1(:,:,:)+data2(:,:,:)+data3(:,:,:)
1.159 + delete (data1)
1.160 + delete (data2)
1.161 + delete (data3)
1.162 +
1.163 +; data = fm->SENSIBLE
1.164 + data = fm->FSH
1.165 + data_mod0(3,:,:,:) = data(:,:,:)
1.166 + delete (data)
1.167 +
1.168 +; data = fm->NETRAD
1.169 + data1 = fm->FSA
1.170 + data2 = fm->FIRA
1.171 + data_mod0(1,:,:,:) = data1(:,:,:)-data2(:,:,:)-data_mod0(2,:,:,:)-data_mod0(3,:,:,:)
1.172 + delete (data1)
1.173 + delete (data2)
1.174 +
1.175 +else
1.176 +
1.177 + data = fm->NEE
1.178 + data_mod0(0,:,:,:) = data(:,:,:) * factor
1.179 + delete (data)
1.180 +
1.181 + data = fm->NETRAD
1.182 + data_mod0(1,:,:,:) = data(:,:,:)
1.183 + delete (data)
1.184 +
1.185 + data = fm->LATENT
1.186 + data_mod0(2,:,:,:) = data(:,:,:)
1.187 + delete (data)
1.188 +
1.189 +; data = fm->SENSIBLE
1.190 + data = fm->FSH
1.191 + data_mod0(3,:,:,:) = data(:,:,:)
1.192 + delete (data)
1.193 +end if
1.194 +
1.195 + delete (fm)
1.196 +
1.197 +;************************************************************
1.198 +; interpolate model data into observed station
1.199 +; note: model is 0-360E, 90S-90N
1.200 +;************************************************************
1.201 +
1.202 +; to be able to handle observation at (-89.98,-24.80)
1.203 + ym(0) = -90.
1.204 +
1.205 + yy = linint2_points_Wrap(xm,ym,data_mod0,True,lon_ob(n),lat_ob(n),0)
1.206 +
1.207 +; print (yy)
1.208 +
1.209 + data_mod(n,:,:) = yy(:,:,0)
1.210 +
1.211 + delete (yy)
1.212 +
1.213 + end do
1.214 +;************************************************************
1.215 +; compute correlation coef and M score
1.216 +;************************************************************
1.217 +
1.218 + score_max = 5.
1.219 +
1.220 + ccr = new ((/nstation, nfield/),float)
1.221 + M_score = new ((/nstation, nfield/),float)
1.222 +
1.223 + do n=0,nstation-1
1.224 + do m=0,nfield-1
1.225 + ccr(n,m) = esccr(data_ob(n,m,:),data_mod(n,m,:),0)
1.226 + bias = sum(abs(data_mod(n,m,:)-data_ob(n,m,:))/(abs(data_mod(n,m,:))+abs(data_ob(n,m,:))))
1.227 + M_score(n,m) = (1. -(bias/nmon)) * score_max
1.228 + end do
1.229 + end do
1.230 +
1.231 + M_co2 = avg(M_score(:,0))
1.232 + M_rad = avg(M_score(:,1))
1.233 + M_lh = avg(M_score(:,2))
1.234 + M_sh = avg(M_score(:,3))
1.235 + M_all = M_co2+ M_rad +M_lh + M_sh
1.236 +
1.237 + M_energy_co2 = sprintf("%.2f", M_co2)
1.238 + M_energy_rad = sprintf("%.2f", M_rad)
1.239 + M_energy_lh = sprintf("%.2f", M_lh )
1.240 + M_energy_sh = sprintf("%.2f", M_sh )
1.241 + M_energy_all = sprintf("%.2f", M_all)
1.242 +
1.243 +;*******************************************************************
1.244 +; for station line plot
1.245 +;*******************************************************************
1.246 +
1.247 +; for x-axis in xyplot
1.248 + mon = ispan(1,12,1)
1.249 + mon@long_name = "month"
1.250 +
1.251 + res = True ; plot mods desired
1.252 + res@xyLineThicknesses = (/2.0,2.0/) ; make 2nd lines thicker
1.253 + res@xyLineColors = (/"blue","red"/) ; line color (ob,model)
1.254 +;-------------------------------------------------------------------------
1.255 +; Add a boxed legend using the more simple method
1.256 +
1.257 + res@pmLegendDisplayMode = "Always"
1.258 +; res@pmLegendWidthF = 0.1
1.259 + res@pmLegendWidthF = 0.08
1.260 + res@pmLegendHeightF = 0.06
1.261 +; res@pmLegendOrthogonalPosF = -1.17
1.262 +; res@pmLegendOrthogonalPosF = -1.00 ;(downward)
1.263 + res@pmLegendOrthogonalPosF = -0.30 ;(downward)
1.264 +
1.265 +; res@pmLegendParallelPosF = 0.18
1.266 + res@pmLegendParallelPosF = 0.23 ;(rightward)
1.267 +
1.268 +; res@lgPerimOn = False
1.269 + res@lgLabelFontHeightF = 0.015
1.270 + res@xyExplicitLegendLabels = (/"observed",model_name/)
1.271 +;-------------------------------------------------------------------
1.272 +; for panel plot
1.273 + res@gsnFrame = False ; Do not draw plot
1.274 + res@gsnDraw = False ; Do not advance frame
1.275 +
1.276 + pres = True ; panel plot mods desired
1.277 + pres@gsnPanelYWhiteSpacePercent = 5 ; increase white space around
1.278 + ; indiv. plots in panel
1.279 + pres@gsnMaximize = True ; fill the page
1.280 +;-------------------------------------------------------------------
1.281 +
1.282 + plot_data = new((/2,12/),float)
1.283 + plot_data!0 = "case"
1.284 + plot_data!1 = "month"
1.285 +
1.286 + do n = 0,nstation-1
1.287 +;----------------------------
1.288 +; for observed
1.289 +
1.290 + plot_name = station(n)+"_ob"
1.291 + title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"
1.292 + res@tiMainString = title
1.293 +
1.294 + wks = gsn_open_wks (plot_type,plot_name)
1.295 + plot=new(4,graphic) ; create graphic array
1.296 +
1.297 + plot_data(0,:) = (/data_ob (n,0,:)/)
1.298 + plot_data@long_name = field(0)
1.299 + plot(0)=gsn_csm_xy(wks,mon,plot_data(0,:),res) ; create plot 1
1.300 +
1.301 + plot_data(0,:) = (/data_ob (n,1,:)/)
1.302 + plot_data@long_name = field(1)
1.303 + plot(1)=gsn_csm_xy(wks,mon,plot_data(0,:),res) ; create plot 2
1.304 +
1.305 + plot_data(0,:) = (/data_ob (n,2,:)/)
1.306 + plot_data@long_name = field(2)
1.307 + plot(2)=gsn_csm_xy(wks,mon,plot_data(0,:),res) ; create plot 3
1.308 +
1.309 + plot_data(0,:) = (/data_ob (n,3,:)/)
1.310 + plot_data@long_name = field(3)
1.311 + plot(3)=gsn_csm_xy(wks,mon,plot_data(0,:),res) ; create plot 4
1.312 +
1.313 + gsn_panel(wks,plot,(/2,2/),pres) ; create panel plot
1.314 +
1.315 + system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
1.316 + "rm "+plot_name+"."+plot_type)
1.317 +
1.318 + clear (wks)
1.319 + delete (plot)
1.320 +;----------------------------
1.321 +; for model_vs_ob
1.322 +
1.323 + plot_name = station(n)+"_model_vs_ob"
1.324 + title = station(n)+"("+sprintf("%5.2f",lat_ob(n))+","+sprintf("%5.2f",lon_ob(n))+")"
1.325 + res@tiMainString = title
1.326 +
1.327 + wks = gsn_open_wks (plot_type,plot_name)
1.328 + plot=new(4,graphic) ; create graphic array
1.329 +
1.330 + plot_data(0,:) = (/data_ob (n,0,:)/)
1.331 + plot_data(1,:) = (/data_mod(n,0,:)/)
1.332 + plot_data@long_name = field(0)
1.333 + plot(0)=gsn_csm_xy(wks,mon,plot_data,res) ; create plot 1
1.334 +
1.335 + plot_data(0,:) = (/data_ob (n,1,:)/)
1.336 + plot_data(1,:) = (/data_mod(n,1,:)/)
1.337 + plot_data@long_name = field(1)
1.338 + plot(1)=gsn_csm_xy(wks,mon,plot_data,res) ; create plot 2
1.339 +
1.340 + plot_data(0,:) = (/data_ob (n,2,:)/)
1.341 + plot_data(1,:) = (/data_mod(n,2,:)/)
1.342 + plot_data@long_name = field(2)
1.343 + plot(2)=gsn_csm_xy(wks,mon,plot_data,res) ; create plot 3
1.344 +
1.345 + plot_data(0,:) = (/data_ob (n,3,:)/)
1.346 + plot_data(1,:) = (/data_mod(n,3,:)/)
1.347 + plot_data@long_name = field(3)
1.348 + plot(3)=gsn_csm_xy(wks,mon,plot_data,res) ; create plot 4
1.349 +
1.350 + gsn_panel(wks,plot,(/2,2/),pres) ; create panel plot
1.351 +
1.352 + system("convert "+plot_name+"."+plot_type+" "+plot_name+"."+plot_type_new+";"+ \
1.353 + "rm "+plot_name+"."+plot_type)
1.354 +
1.355 + clear (wks)
1.356 + delete (plot)
1.357 + end do
1.358 +
1.359 +;*******************************************************************
1.360 +; html table of site: observed
1.361 +;*******************************************************************
1.362 + output_html = "line_ob.html"
1.363 +
1.364 + header = (/"<HTML>" \
1.365 + ,"<HEAD>" \
1.366 + ,"<TITLE>CLAMP metrics</TITLE>" \
1.367 + ,"</HEAD>" \
1.368 + ,"<H1>Energy at Site: Observation</H1>" \
1.369 + /)
1.370 + footer = "</HTML>"
1.371 +
1.372 + table_header = (/ \
1.373 + "<table border=1 cellspacing=0 cellpadding=3 width=60%>" \
1.374 + ,"<tr>" \
1.375 + ," <th bgcolor=DDDDDD >Site Name</th>" \
1.376 + ," <th bgcolor=DDDDDD >Latitude</th>" \
1.377 + ," <th bgcolor=DDDDDD >Longitude</th>" \
1.378 + ," <th bgcolor=DDDDDD >Observed</th>" \
1.379 + ,"</tr>" \
1.380 + /)
1.381 + table_footer = "</table>"
1.382 + row_header = "<tr>"
1.383 + row_footer = "</tr>"
1.384 +
1.385 + lines = new(50000,string)
1.386 + nline = 0
1.387 +
1.388 + set_line(lines,nline,header)
1.389 + set_line(lines,nline,table_header)
1.390 +;-----------------------------------------------
1.391 +; row of table
1.392 +
1.393 + do n = 0,nstation-1
1.394 + set_line(lines,nline,row_header)
1.395 +
1.396 + txt0 = station(n)
1.397 + txt1 = sprintf("%5.2f", lat_ob(n))
1.398 + txt2 = sprintf("%5.2f", lon_ob(n))
1.399 + txt3 = year_ob(n)
1.400 +
1.401 + set_line(lines,nline,"<th><a href="+txt0+"_ob.png>"+txt0+"</a></th>")
1.402 + set_line(lines,nline,"<th>"+txt1+"</th>")
1.403 + set_line(lines,nline,"<th>"+txt2+"</th>")
1.404 + set_line(lines,nline,"<th>"+txt3+"</th>")
1.405 +
1.406 + set_line(lines,nline,row_footer)
1.407 + end do
1.408 +;-----------------------------------------------
1.409 + set_line(lines,nline,table_footer)
1.410 + set_line(lines,nline,footer)
1.411 +
1.412 +; Now write to an HTML file.
1.413 + idx = ind(.not.ismissing(lines))
1.414 + if(.not.any(ismissing(idx))) then
1.415 + asciiwrite(output_html,lines(idx))
1.416 + else
1.417 + print ("error?")
1.418 + end if
1.419 + delete (idx)
1.420 +
1.421 +;*******************************************************************
1.422 +; score and line table : model vs observed
1.423 +;*******************************************************************
1.424 + output_html = "score+line_vs_ob.html"
1.425 +
1.426 + header = (/"<HTML>" \
1.427 + ,"<HEAD>" \
1.428 + ,"<TITLE>CLAMP metrics</TITLE>" \
1.429 + ,"</HEAD>" \
1.430 + ,"<H1>Energy at Site: Model "+model_name+"</H1>" \
1.431 + /)
1.432 + footer = "</HTML>"
1.433 +
1.434 + delete (table_header)
1.435 + table_header = (/ \
1.436 + "<table border=1 cellspacing=0 cellpadding=3 width=100%>" \
1.437 + ,"<tr>" \
1.438 + ," <th bgcolor=DDDDDD >Site Name</th>" \
1.439 + ," <th bgcolor=DDDDDD >Latitude</th>" \
1.440 + ," <th bgcolor=DDDDDD >Longitude</th>" \
1.441 + ," <th bgcolor=DDDDDD >Observed</th>" \
1.442 + ," <th bgcolor=DDDDDD >CO2 Flux</th>" \
1.443 + ," <th bgcolor=DDDDDD >Net Radiation</th>" \
1.444 + ," <th bgcolor=DDDDDD >Latent Heat</th>" \
1.445 + ," <th bgcolor=DDDDDD >Sensible Heat</th>" \
1.446 + ," <th bgcolor=DDDDDD >Average</th>" \
1.447 + ,"</tr>" \
1.448 + /)
1.449 + table_footer = "</table>"
1.450 + row_header = "<tr>"
1.451 + row_footer = "</tr>"
1.452 +
1.453 + lines = new(50000,string)
1.454 + nline = 0
1.455 +
1.456 + set_line(lines,nline,header)
1.457 + set_line(lines,nline,table_header)
1.458 +;-----------------------------------------------
1.459 +; row of table
1.460 +
1.461 + do n = 0,nstation-1
1.462 + set_line(lines,nline,row_header)
1.463 +
1.464 + txt0 = station(n)
1.465 + txt1 = sprintf("%5.2f", lat_ob(n))
1.466 + txt2 = sprintf("%5.2f", lon_ob(n))
1.467 + txt3 = year_ob(n)
1.468 + txt4 = sprintf("%5.2f", M_score(n,0))
1.469 + txt5 = sprintf("%5.2f", M_score(n,1))
1.470 + txt6 = sprintf("%5.2f", M_score(n,2))
1.471 + txt7 = sprintf("%5.2f", M_score(n,3))
1.472 + txt8 = sprintf("%5.2f", avg(M_score(n,:)))
1.473 +
1.474 + set_line(lines,nline,"<th><a href="+txt0+"_model_vs_ob.png>"+txt0+"</a></th>")
1.475 + set_line(lines,nline,"<th>"+txt1+"</th>")
1.476 + set_line(lines,nline,"<th>"+txt2+"</th>")
1.477 + set_line(lines,nline,"<th>"+txt3+"</th>")
1.478 + set_line(lines,nline,"<th>"+txt4+"</th>")
1.479 + set_line(lines,nline,"<th>"+txt5+"</th>")
1.480 + set_line(lines,nline,"<th>"+txt6+"</th>")
1.481 + set_line(lines,nline,"<th>"+txt7+"</th>")
1.482 + set_line(lines,nline,"<th>"+txt8+"</th>")
1.483 +
1.484 + set_line(lines,nline,row_footer)
1.485 + end do
1.486 +
1.487 +; last row, summary
1.488 + set_line(lines,nline,row_header)
1.489 +
1.490 + txt0 = "All_"+sprintf("%.0f", nstation)
1.491 + txt1 = "-"
1.492 + txt2 = "-"
1.493 + txt3 = "-"
1.494 + txt4 = M_energy_co2
1.495 + txt5 = M_energy_rad
1.496 + txt6 = M_energy_lh
1.497 + txt7 = M_energy_sh
1.498 + txt8 = M_energy_all
1.499 +
1.500 + set_line(lines,nline,"<th>"+txt0+"</th>")
1.501 + set_line(lines,nline,"<th>"+txt1+"</th>")
1.502 + set_line(lines,nline,"<th>"+txt2+"</th>")
1.503 + set_line(lines,nline,"<th>"+txt3+"</th>")
1.504 + set_line(lines,nline,"<th>"+txt4+"</th>")
1.505 + set_line(lines,nline,"<th>"+txt5+"</th>")
1.506 + set_line(lines,nline,"<th>"+txt6+"</th>")
1.507 + set_line(lines,nline,"<th>"+txt7+"</th>")
1.508 + set_line(lines,nline,"<th>"+txt8+"</th>")
1.509 +
1.510 + set_line(lines,nline,row_footer)
1.511 +;-----------------------------------------------
1.512 + set_line(lines,nline,table_footer)
1.513 + set_line(lines,nline,footer)
1.514 +
1.515 +; Now write to an HTML file.
1.516 + idx = ind(.not.ismissing(lines))
1.517 + if(.not.any(ismissing(idx))) then
1.518 + asciiwrite(output_html,lines(idx))
1.519 + else
1.520 + print ("error?")
1.521 + end if
1.522 + delete (idx)
1.523 +
1.524 +;***************************************************************************
1.525 +; output plots
1.526 +;***************************************************************************
1.527 + output_dir = model_name+"/energy"
1.528 +
1.529 + system("mv *.png *.html " + output_dir)
1.530 +;***************************************************************************
1.531 +end