-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbiome4driver.f90
649 lines (459 loc) · 16.8 KB
/
biome4driver.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
program biome4main
! compile with makefile
use iso_fortran_env
use netcdf
use coordsmod
use netcdfmod, only : handle_err,genoutfile
use parametersmod
implicit none
real(sp), parameter :: p0 = 101325. ! sea level standard atmospheric pressure (Pa)
real(sp), parameter :: cp = 1004.68506 ! constant-pressure specific heat (J kg-1 K-1)
real(sp), parameter :: T0 = 288.16 ! sea level standard temperature (K)
real(sp), parameter :: g = 9.80665 ! earth surface gravitational acceleration (m s-1)
real(sp), parameter :: M = 0.02896968 ! molar mass of dry air (kg mo-1)
real(sp), parameter :: R0 = 8.314462618 ! universal gas constant (J mol-1 K-1)
real(dp), allocatable, dimension(:) :: lon
real(dp), allocatable, dimension(:) :: lat
real(sp), allocatable, dimension(:) :: dz
real(sp), allocatable, dimension(:,:) :: elv
real(sp), allocatable, dimension(:,:) :: tcm
real(sp), allocatable, dimension(:,:) :: tmin
integer(i2), allocatable, dimension(:,:,:) :: ivar
real(sp), allocatable, dimension(:,:,:) :: temp
real(sp), allocatable, dimension(:,:,:) :: prec
real(sp), allocatable, dimension(:,:,:) :: cldp
real(sp), allocatable, dimension(:,:,:) :: whc
real(sp), allocatable, dimension(:,:,:) :: ksat
integer(i2), allocatable, dimension(:,:) :: biome
integer(i2), allocatable, dimension(:,:) :: wdom
integer(i2), allocatable, dimension(:,:) :: gdom
real(sp), allocatable, dimension(:,:,:) :: NPP
real(sp), allocatable, dimension(:,:,:) :: LAI
real(sp) :: co2
real(sp) :: p
real(sp) :: iopt
! I/O variables
integer :: status
integer :: ncid
integer :: dimid
integer :: varid
integer :: xlen
integer :: ylen
integer :: tlen
integer :: llen
integer :: srtx
integer :: srty
integer :: cntx
integer :: cnty
integer :: endx
integer :: endy
real(sp) :: scale_factor
real(sp) :: add_offset
integer(i2) :: missval_i2
real(sp), parameter :: missval_sp = -9999.
character(200) :: jobfile
character(200) :: climatefile
character(200) :: soilfile
character(200) :: outfile
character(45) :: coordstring
real(dp), dimension(4) :: boundingbox
integer :: x,y
integer :: i,j,l
! biome4 arguments; for catalog, see bottom of this code
real(sp), dimension(50) :: input
real(sp), dimension(500) :: output
! integer :: ompchunk
real(dp) :: xres
real(dp) :: yres
real(dp) :: minlon
real(dp) :: maxlon
real(dp) :: minlat
real(dp) :: maxlat
real(dp) :: gridres
real(dp) :: halfres
real(dp), dimension(2) :: lonrange
real(dp), dimension(2) :: latrange
logical :: sun = .false.
logical :: diag = .false.
character(60) :: status_line
namelist / joboptions / climatefile,soilfile,co2,diag
! ------------------------------------------------------------------------------------------------------------
call getarg(1,jobfile)
open(10,file=jobfile,status='old')
read(10,nml=joboptions)
close(10)
! -------------------------------------------------------
! input data file size
status = nf90_open(climatefile,nf90_nowrite,ncid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'lon',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=xlen)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'lat',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=ylen)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'time',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=tlen)
if (status /= nf90_noerr) call handle_err(status)
allocate(lon(xlen))
allocate(lat(ylen))
status = nf90_inq_varid(ncid,'lon',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,lon)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_varid(ncid,'lat',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,lat)
if (status /= nf90_noerr) call handle_err(status)
! -------------------------------------------------------
call getarg(2,coordstring)
if (coordstring == 'alldata') then
srtx = 1
srty = 1
cntx = xlen
cnty = ylen
else
call parsecoords(coordstring,boundingbox)
call calcpixels(lon,lat,boundingbox,srtx,srty,cntx,cnty)
end if
endx = srtx + cntx - 1
endy = srty + cnty - 1
write(*,'(i0,a,i0,a,i0,a)')cntx,' x ',cnty,' = ',cntx*cnty,' pixels'
write(*,'(a,i0,a,i0,a,f0.6,a,f0.6)')'starting at: ',srtx,', ',srty,' ',lon(srtx),' ',lat(srty)
gridres = lon(2) - lon(1)
halfres = gridres / 2._dp
lonrange = [lon(srtx) - halfres,lon(endx) + halfres]
write(*,*)'lon resolution:',gridres
write(*,*)'output lon range:',lonrange
gridres = lat(2) - lat(1)
halfres = gridres / 2._dp
latrange = [lat(srty) - halfres,lat(endy) + halfres]
write(*,*)'lat resolution:',gridres
write(*,*)'output lat range:',latrange
allocate(elv(cntx,cnty))
allocate(tmin(cntx,cnty))
allocate(ivar(cntx,cnty,tlen))
allocate(temp(cntx,cnty,tlen))
allocate(prec(cntx,cnty,tlen))
allocate(cldp(cntx,cnty,tlen))
! -------------------------------------------------------
! elevation
write(*,*)'reading climate: ',climatefile
status = nf90_inq_varid(ncid,'elv',varid)
if (status == nf90_noerr) then
status = nf90_get_var(ncid,varid,elv,start=[srtx,srty],count=[cntx,cnty])
if (status /= nf90_noerr) call handle_err(status)
else
elv = 0.
end if
! -------------------------------------------------------
! temperature
temp = missval_sp
status = nf90_inq_varid(ncid,'tmp',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,ivar,start=[srtx,srty,1],count=[cntx,cnty,tlen])
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'scale_factor',scale_factor)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'add_offset',add_offset)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'missing_value',missval_i2)
if (status /= nf90_noerr) call handle_err(status)
where (ivar /= missval_i2)
temp = real(ivar) * scale_factor + add_offset
end where
write(*,*)'temp range: ',minval(temp,mask=temp/=missval_sp),maxval(temp,mask=temp/=missval_sp)
! -------------------------------------------------------
! precipitation
prec = missval_sp
status = nf90_inq_varid(ncid,'pre',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,ivar,start=[srtx,srty,1],count=[cntx,cnty,tlen])
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'scale_factor',scale_factor)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'add_offset',add_offset)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'missing_value',missval_i2)
if (status /= nf90_noerr) call handle_err(status)
where (ivar /= missval_i2)
prec = max(real(ivar) * scale_factor + add_offset,0.)
end where
write(*,*)'prec range: ',minval(prec,mask=prec/=missval_sp),maxval(prec,mask=prec/=missval_sp)
! -------------------------------------------------------
! cloud percent
cldp = missval_sp
status = nf90_inq_varid(ncid,'cld',varid)
if (status /= nf90_noerr) then
status = nf90_inq_varid(ncid,'sun',varid)
if (status /= nf90_noerr) call handle_err(status)
sun = .true.
end if
status = nf90_get_var(ncid,varid,ivar,start=[srtx,srty,1],count=[cntx,cnty,tlen])
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'scale_factor',scale_factor)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'add_offset',add_offset)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'missing_value',missval_i2)
if (status /= nf90_noerr) call handle_err(status)
if (sun) then
where (ivar /= missval_i2)
cldp = real(ivar) * scale_factor + add_offset
end where
else
where (ivar /= missval_i2)
cldp = 100. - real(ivar) * scale_factor + add_offset
end where
end if
write(*,*)'cldp range: ',minval(cldp,mask=cldp/=missval_sp),maxval(cldp,mask=cldp/=missval_sp)
! -------------------------------------------------------
! absolute minimum temperature
tmin = missval_sp
status = nf90_inq_varid(ncid,'tmin',varid)
if (status == nf90_noerr) then ! tmin is present, we will read it from the file
status = nf90_get_var(ncid,varid,ivar(:,:,1),start=[srtx,srty,1],count=[cntx,cnty])
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'scale_factor',scale_factor)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'add_offset',add_offset)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_att(ncid,varid,'missing_value',missval_i2)
if (status /= nf90_noerr) call handle_err(status)
where (ivar(:,:,1) /= missval_i2)
tmin = real(ivar(:,:,1)) * scale_factor + add_offset
end where
else
! if tmin is not present in the input climate files, we will estimate it based on the temperature of the coldest month
! following Prentice et al. (Eqn. 1, J. Biogeog., 1992)
write(*,*)'NB: Using calculated Tmin'
allocate(tcm(cntx,cnty))
tcm = minval(temp,dim=3)
where (tcm /= missval_sp)
tmin = 0.006 * tcm**2 + 1.316 * tcm - 21.9
end where
deallocate(tcm)
end if
! -------------------------------------------------------
status = nf90_close(ncid)
! -------------------------------------------------------
write(*,*)'reading soils, layer info:'
status = nf90_open(soilfile,nf90_nowrite,ncid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_dimid(ncid,'depth',dimid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inquire_dimension(ncid,dimid,len=llen)
if (status /= nf90_noerr) call handle_err(status)
allocate(dz(llen))
allocate(whc(cntx,cnty,llen))
allocate(ksat(cntx,cnty,llen))
status = nf90_inq_varid(ncid,'dz',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,dz)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_varid(ncid,'whc',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,whc,start=[srtx,srty,1],count=[cntx,cnty,llen])
if (status /= nf90_noerr) call handle_err(status)
status = nf90_inq_varid(ncid,'Ksat',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_get_var(ncid,varid,ksat,start=[srtx,srty,1],count=[cntx,cnty,llen])
if (status /= nf90_noerr) call handle_err(status)
status = nf90_close(ncid)
if (status /= nf90_noerr) call handle_err(status)
do l = 1,llen
write(*,*)l,dz(l)
end do
! -------------------------------------------------------
call getarg(3,outfile)
call genoutfile(jobfile,outfile,cntx,cnty,ncid)
! -------------------------------------------------------
allocate(biome(cntx,cnty))
allocate(wdom(cntx,cnty))
allocate(NPP(cntx,cnty,13))
allocate(LAI(cntx,cnty,13))
biome = missval_i2
wdom = missval_i2
npp = missval_sp
lai = missval_sp
! lai = missval_sp
do y = 1,cnty
write(status_line,'(a,i0,a,i0)')' working on row ',y,' out of ',cnty
call overprint(status_line)
!$OMP PARALLEL DEFAULT(SHARED) PRIVATE(p,input,output)
!$OMP DO SCHEDULE(GUIDED)
do x = 1,cntx
if (temp(x,y,1) == missval_sp .or. whc(x,y,1) == missval_sp) cycle
p = p0 * (1. - (g * elv(x,y)) / (cp * T0))**(cp * M / R0)
input(1) = lat(y+srty-1)
input(2) = co2
input(3) = p
input(4) = tmin(x,y)
input(5:16) = temp(x,y,:)
input(17:28) = prec(x,y,:)
input(29:40) = cldp(x,y,:)
input(41) = sum(Ksat(x,y,1:3) * dz(1:3)) / sum(dz(1:3))
input(42) = sum(Ksat(x,y,4:6) * dz(4:6)) / sum(dz(4:6))
input(43) = sum(whc(x,y,1:3) * dz(1:3)) ! input whc are in mm/cm
input(44) = sum(whc(x,y,4:6) * dz(4:6))
input(49) = lon(x+srtx-1)
if (diag) then
input(46) = 1. ! diagnostic mode on
else
input(46) = 0. ! diagnostic mode off
end if
call biome4(input,output)
biome(x,y) = nint(output(1))
wdom(x,y) = nint(output(12))
NPP(x,y,:) = output(301:313)
LAI(x,y,:) = output(314:326)
end do
!$OMP END DO NOWAIT
!$OMP END PARALLEL
end do
write(0,*)
! -------------------------------------------------------
write(*,*)'writing'
! ----------
! coordinate variables
status = nf90_inq_varid(ncid,'lon',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,lon(srtx:endx))
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_att(ncid,varid,'actual_range',lonrange)
if (status /= nf90_noerr) call handle_err(status)
! ---
status = nf90_inq_varid(ncid,'lat',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,lat(srty:endy))
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_att(ncid,varid,'actual_range',latrange)
if (status /= nf90_noerr) call handle_err(status)
! ---
status = nf90_inq_varid(ncid,'pft',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,[(i,i=1,13)])
if (status /= nf90_noerr) call handle_err(status)
! ----------
! regular variables
! ---
! biome
status = nf90_inq_varid(ncid,'biome',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,biome)
if (status /= nf90_noerr) call handle_err(status)
! ---
! woody dominant PFT
status = nf90_inq_varid(ncid,'wdom',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,wdom)
if (status /= nf90_noerr) call handle_err(status)
! ---
! annual NPP by PFT
status = nf90_inq_varid(ncid,'NPP',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,NPP)
if (status /= nf90_noerr) call handle_err(status)
! ---
! maximum LAI by PFT
status = nf90_inq_varid(ncid,'LAI',varid)
if (status /= nf90_noerr) call handle_err(status)
status = nf90_put_var(ncid,varid,LAI)
if (status /= nf90_noerr) call handle_err(status)
! ---
status = nf90_close(ncid)
if (status /= nf90_noerr) call handle_err(status)
! ------------------------------------------------------------------------------------------------------------
! catalog of arguments
! ---------------------------------------
! input: real, dimension(50) :: vars_in(50)
! ---------------------------------------
! 1 latitude
! 2 co2
! 3 air pressure
! 4 tmin
! 5-16 temperature (degC)
! 17-27 precip (mm)
! 28-40 cloud (percent)
! 41-44 soil: 1-2 = Ksat (mm/h); 3-4 = volumetric whc (mm); NB k(6) does not appear to be actually used
! 45 not used
! 46 iopt (diagnostic mode: 1 = on, anything else = off)
! 47-48 not used
! 49 longitude
! 50 not used
!
! ---------------------------------------
! output: real, dimension(500) :: output
! ---------------------------------------
! 1 biome
! 2 lai
! 3 npp
! 4 woody LAI
! 5 woody NPP
! 6 grass LAI
! 7 grass NPP
! 8 APAR
! 9 autotrophic resp
! 10 soil moisture (not used)
! 11 runoff (dominant PFT)
! 12 dominant woody PFT
! 13 annual precip
! 14 PAR (calculated)
! 15 LAI ratio
! 16 npp difference (nppdiff)
! 17 FVC half of LAI (foliar vegetative cover)
! 18 root distribuion
! 19-24 not used
! 25-36 monthly FPAR
! 37-48 monthly NPP for dominant PFT
! 48 longitude (NB will be overwritten by above)
! 49 latitude
! 50 deltaA for dominant PFT (13C)
! 51 mean deltaA
! 52 phi for C4 plants
! 53-59 not used
! 60-72 NPP by PFT
! 73-79 not used
! 80-91 monthly Delta13C dominant PFT
! 92-96 not used
! 97 mean annual hetresp scalar
! 98 percent of NPP that is C4
! 99 annual het resp
! 100 not used
! 101-112 monthly DeltaE, dominant PFT
! 113-124 monthly het resp, dominant PFT
! 125-136 monthly isoresp (product)
! 137-148 monhtly net C flux, dominant PFT (npp-resp)
! 149 annual NEP
! 150 annual mean A/g
! 151-159 not used
! 160-172 monthly mean gc, dominant PFT
! 173-184 monthly LAI, dominant PFT
! 185-196 monthly runoff
! 197-198 not used
! 199 fire days
! 200 green days
! 201-241 ten day LAI, dominant PFT
! 242-300 not used
! 301-314 npp by pft
! 315-328 lai by pft
! 329-388 not used
! 389-400 mean soil moisture, dominant PFT
! 401-413 not used
! 413-424 monthly soil moisture, top layer, dominant PFT
! 425-436 monthly soil moisture, bottom layer, dominant PFT
! 425 soil moisture top layer, dominant PFT (NB will overwrite above)
! 426 soil moisture bottom layer, dominant PFT
! 427 soil moisture ratio, dominant (PFT)
! 437-449 not used
! 450 mean Klit
! 451 mean Ksoil
! 452 temperature of the coldest month
! 453 gdd0
! 454 gdd5
! 455-500 not used
! ------------------------------------------------------------------------------------------------------------
end program biome4main