-
Notifications
You must be signed in to change notification settings - Fork 2
/
fao56-crop.ncl
1644 lines (1425 loc) · 58.6 KB
/
fao56-crop.ncl
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Reference:
;
; Richard G. Allen, Luis S. Pereira, Dirk Raes & Martin Smith (1998)
; Crop Evapotranspiration – Guidelines for Computing Crop Water Requirements
; FAO Irrigation and drainage paper 56. Rome, Italy: Food and Agriculture Organization of the United Nations.
; ISBN 92-5-104219-5.
; http://www.fao.org/docrep/X0490E/x0490e00.htm#Contents
; In particular: Chapters 3 & 4
; http://www.fao.org/docrep/X0490E/x0490e07.htm#chapter 3 meteorological data
; http://www.fao.org/docrep/X0490E/x0490e08.htm#chapter 4 determination of eto
; ----------------------------------------------------------------------------------
;ET is the loss of water by evaporation from both the soil and plant (evaporation + transpiration)
;
;- ET depends on several factors: solar radiation, air temperature, air vapour pressure, wind speed and surface area.
;- Potential ET (PET) is the maximum rate of ET given the current conditions. PET is not a constant value but
; varies with field conditions: it is the rate of water loss if water supply is not limiting. But often
; water supply is limited, so water loss is often smaller than PET. The rate at which water is being lost
; is known as actual ET (AET).
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("getMidMonDay")
function getMidMonDay(time:numeric)
;
; utility function
; The input 'time' (seconds/hours/days since ...") has the middle of the month
; This returns the 'middle day' of the current month as an integer
local date, yyyy, mm, dd, jday
begin
date = cd_calendar(time, 0)
yyyy = toint(date(:,0))
mm = toint(date(:,1))
dd = toint(date(:,2))
jday = day_of_year(yyyy, mm, dd)
copy_VarCoords(time, jday)
jday@long_name = "mid-month day of current year"
return(jday)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("satvpr_mean_fao56")
function satvpr_mean_fao56(t:numeric, tunit[1]:integer)
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; Estimate 'mean' saturation vapor pressure (kPa): EQN 11; Chapter 3
;
; Note_1: If t=t_dew then this calculates actual vapor pressure EQN 14 (ea)
; Note_2: if any (t.le.0) svp is undefined ==> _FillValue
; Do *not* return 0.0 because other eqns take log(satvpr)
; t could have no _FillValue BUT values <0 hence svp@_FillValue
local svp, tc, tFill
begin
if (tunit.lt.0 .or. tunit.gt.2) then ; tunit error check
print("satvpr_mean_fao56: unrecognized tunit argument: tunit="+tunit)
exit
end if
if (isatt(t,"_FillValue")) then
tFill = t@_FillValue
else
tFill = 1e20 ; t has no _FillValue BUT there could b t.le.0
end if
if (tunit.eq.0) then ; degC
svp = where(t.gt.0, 0.6108*exp((17.27*t)/(t+237.3)), tFill )
end if
if (tunit.eq.1) then ; degK
tc = t-273.16
svp = where(tc.gt.0, 0.6108*exp((17.27*tc)/(tc+237.3)), tFill )
end if
if (tunit.eq.2) then ; F
tc = (t-32)*0.5555556
svp = where(tc.gt.0, 0.6108*exp((17.27*tc)/(tc+237.3)), tFill )
end if
svp@long_name = "mean saturation vapor pressure via Tmean"
svp@units = "kPa"
svp@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
svp@info = "FAO 56; EQN 11; satvpr_mean_fao56"
if (any(t.le.0) .and. .not.isatt(svp,"_FillValue") ) then
svp@_FillValue = tFill
end if
copy_VarCoords(t, svp)
return(svp)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("satvpr_tdew_fao56")
function satvpr_tdew_fao56(tdew:numeric, tunit[1]:integer)
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; Compute actual saturation vapor pressure (kPa): EQN 14; Chapter 3
; This is the same code as 'satvpr_mean_fao56'
; Just different 'long_name' and 'info' attributes
local actsvp
begin
actsvp = satvpr_mean_fao56(tdew, tunit)
actsvp@long_name = "actual saturation vapor pressure via Tdew"
actsvp@units = "kPa"
actsvp@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
actsvp@info = "FAO 56; EQN 14; satvpr_tdew_fao56"
return(actsvp)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("actvpr_tmin_fao56")
function actvpr_tmin_fao56(tmin:numeric, tunit[1]:integer)
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; Estimate actual vapor pressure (kPa) using Tmin: EQN 48; Chapter 3
; This uses the same code as 'satvpr_mean_fao56'
; Just different 'long_name' and 'info' attributes
;
; This assumes that the dew point is near the minimum temperature.
;
; NOTE:
; (a) In well watered areas Tdew >> Tmin
; (b) In arid areas Tmin >> Tdew.
; In these areas (Tmin-2) or (Tmin-3) might be better.
;
local act
begin
act = satvpr_mean_fao56(tmin, tunit)
act@long_name = "actual vapor pressure"
act@units = "kPa"
act@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
act@info = "FAO 56; EQN 48; actvpr_tmin_fao56"
return(act)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("tdew_actvpr_fao56")
function tdew_actvpr_fao56(actvpr:numeric, tunit[1]:integer)
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; Compute dew point temperature from actual vapor pressure via EQN 3-11; Annex 3
; actvpr - actual vapor pressure (kPa)
local tdew, units
begin
tdew = (116.91+237.3*log(actvpr))/(16.78-log(actvpr))
units = "degC"
if (tunit.eq.1) then
tdew = tdew+273.16
units = "degK"
end if
if (tunit.eq.2) then
tdew = 1.8*tdew+32
units = "deg farenheit"
end if
tdew@long_name = "Estimated dew point temperature"
tdew@units = units
tdew@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
tdew@info = "FAO 56; Annex: EQN 3-11; tdew_actvpr_fao56"
copy_VarCoords(actvpr, tdew)
return(tdew)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("prsatm_tz_fao56")
function prsatm_tz_fao56(t:numeric, z:numeric, P0[1]:numeric, z0[1]:numeric, tunit[1]:integer)
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; Compute atmospheric pressure via via EQN 3-2; Annex 3
local R, g, a, con, tk0, prs
begin
if (tunit.lt.0 .or. tunit.gt.2) then ; tunit error check
print("prsatm_tz_fao56: unrecognized tunit argument: tunit="+tunit)
exit
end if
R = 287.04 ; J/(kg-degK) <==> J/(kg-degC)
g = 9.807 ; m/2
a = 0.0065 ; degK/m <==> degC/m
con = g/(a*R) ; exponent
if (tunit.eq.0) then ; degC
tk0 = 273.16 + t
prs = P0*( (tk0-a*(z-z0))/tk0 )^con
else if (tunit.eq.1) then ; degK
prs = P0*( (t-a*(z-z0))/t )^con
else if (tunit.eq.2) then ; degF
tk0 = 273.16 + (t-32)*0.555556
prs = P0*( (tk0-a*(z-z0))/tk0 )^con
end if
end if
end if
prs@long_name = "atmospheric pressure"
prs@units = "kPa"
prs@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
prs@info = "FAO 56; Annex 3: EQN 3-2; prsatm_tz_fao56"
copy_VarCoords(t, prs)
return(prs)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("u2_fao56")
function u2_fao56(uz:numeric, z:numeric, wunit[1]:integer)
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; Compute 2-meter height wind
local u2, con
begin
con = 1.0
if (wunit.eq.1) then
con = 0.277778 ; km/hr => m/s
else if (wunit.eq.2) then
con = 0.44704 ; mph => m/s
end if
end if
u2 = (uz*con)*(4.87/(log(67.8*z-5.42)))
u2@long_name = "Estimated 2-meter wind speed"
u2@units = "m/s"
u2@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
u2@info = "FAO 56; EQN 47; u2_fao56"
copy_VarCoords(uz, u2)
return(u2)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("rhum_fao56")
function rhum_fao56(actvpr:numeric, satvpr:numeric,rhunit [1]:integer)
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; Compute relative humidity
local rhum, rhum_units
begin
if (any(actvpr.lt.0.0) .or. any(satvpr.le.0.0)) then
print("rhum_fao56: actvpr<0 or satvpr<=0")
exit
end if
rhum = ( actvpr/satvpr)
rhum@long_name = "Relative Humidity"
if (rhunit.eq.0) then
rhum = rhum*100
rhum@units = "%"
else
rhum@units = "fraction"
end if
rhum@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
rhum@info = "FAO 56; EQN 10; rhum_fao56"
copy_VarCoords(actvpr, rhum)
return(rhum)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("satvpr_slope_fao56")
function satvpr_slope_fao56(t:numeric, tunit[1]:integer)
; Compute slope (kPa/C) of saturation vapor pressure (svp) curve; EQN 13; Chapter 3
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; See: http://www.fao.org/docrep/x0490e/x0490e0j.htm#annex 2. meteorological tables
;
local satvpr_slope, tc, tFill
begin
if (tunit.lt.0 .or. tunit.gt.2) then ; tunit error check
print("satvpr_slope_fao56: unrecognized tunit argument: tunit="+tunit)
exit
end if
if (isatt(t,"_FillValue")) then
tFill = t@_FillValue
else
tFill = 1e20
end if
if (tunit.eq.0) then ; degC
satvpr_slope = where(t.gt.0, 4098*satvpr_mean_fao56(t,0)/(t+237.3)^2, tFill)
else
if (tunit.eq.1) then ; degK
tc = t-273.16
else ; F
tc = (t-32)*0.5555556
end if
satvpr_slope = where(tc.gt.0, 4098*satvpr_mean_fao56(tc,0)/(tc+237.3)^2, tFill)
end if
satvpr_slope@long_name = "slope saturation vapor pressure curve"
satvpr_slope@units = "kPa/C"
satvpr_slope@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
satvpr_slope@info = "FAO 56; EQN 13; satvpr_slope_fao56"
if (.not.isatt(satvpr_slope,"_FillValue") .and. any(satvpr_slope.eq.tFill) ) then
satvpr_slope@_FillValue = tFill
end if
copy_VarCoords(t, satvpr_slope)
return( satvpr_slope )
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("actvpr_rhmean_fao56")
function actvpr_rhmean_fao56(tmin:numeric, tmax:numeric, rhmean:numeric, tunit[1]:integer)
;
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; http://www.fao.org/docrep/x0490e/x0490e0j.htm#annex 2. meteorological tables
; Compute 'actual' saturation vapor pressure with mean relative humidity (%): EQN 19
;
; tmin, tmax - min/max temperature (C or K)
; rmean - mean relative humidity (%)
; tunit - 0 (degC), 1 (degK)
; opt - False ... Both tmin and tmax > 0C
; - True ... use tmax *only*.
; Effectively, this sets 'satvpr_mean_fao56(tmin,0)=0.0'
; If tmin> 0 this has no effect.
;
local conmean, avp_rhmean, tc, tcx, tcn, tFill, tCrit \
, dim_tmin, dim_tmax, dim_rhmn, rnk_tmin, rnk_tmax, rnk_rhmn
begin
; check input
if (tunit.lt.0 .or. tunit.gt.2) then ; tunit error check
print("actvpr_rhmean_fao56: unrecognized tunit argument: tunit="+tunit)
exit
end if
dim_tmin = dimsizes(tmin) ; size(s)
dim_tmax = dimsizes(tmax)
dim_rhmn = dimsizes(rhmean)
rnk_tmin = dimsizes(dim_tmin) ; rank
rnk_tmax = dimsizes(dim_tmax)
rnk_rhmn = dimsizes(dim_rhmn)
if (.not.(rnk_tmin.eq.rnk_tmax .and. rnk_tmin.eq.rnk_rhmn)) then
print("actvpr_rhmean_fao56: rank mismatch on input arguments")
print(" rnk_tmin="+rnk_tmin)
print(" rnk_tmax="+rnk_tmax)
print(" rnk_rhmn="+rnk_rhmn)
exit
end if
if (.not.(all(dim_tmin.eq.dim_tmax) .and. all(dim_tmin.eq.dim_rhmn))) then
print("actvpr_rhmean_fao56: dimension size mismatch on input arguments")
print(" dim_tmin="+dim_tmin+" dim_tmax="+dim_tmax+" dim_rhmn="+dim_rhmn)
exit
end if
if (isatt(tmin,"_FillValue")) then
tFill = tmin@_FillValue
else
tFill = 1e20
end if
if (all(rhmean.le.1)) then
conmean = rhmean/2.0 ; must be fractional and not %
else
conmean = rhmean/200.0
end if
tCrit = 0
if (tunit.eq.0) then ; degC; use input array
avp_rhmean = where(tmin.gt.tCrit \
,conmean*(satvpr_mean_fao56(tmax,0)+satvpr_mean_fao56(tmin,0)) \
,tFill )
avp_rhmean = where(tmin.le.tCrit .and. tmax.gt.tCrit \ ; Should this be done?
,conmean*(satvpr_mean_fao56(tmax,0) ) \ ; satvpr_mean_fao56(0,0)=0.0
,avp_rhmean )
avp_rhmean = where(avp_rhmean.lt.0.0, tFill, avp_rhmean)
else ; create and use temporary arrays
if (tunit.eq.1) then ; degK
tcn = tmin-273.16 ; convert to degC
tcx = tmax-273.16
else ; Farenheit
tcn = (tmin-32)*0.5555556 ; convert to degC
tcx = (tmax-32)*0.5555556
end if
avp_rhmean = where(tcn.gt.tCrit \
,conmean*(satvpr_mean_fao56(tcx,0)+satvpr_mean_fao56(tcn,0)) \
,tFill )
avp_rhmean = where(tcn.le.tCrit .and. tcx.gt.tCrit \ ; Should this be done?
,conmean*(satvpr_mean_fao56(tcx,0) ) \ ; satvpr_mean_fao56(0,0)=0.0
,avp_rhmean )
avp_rhmean = where(avp_rhmean.lt.0.0, tFill, avp_rhmean)
end if
avp_rhmean@long_rhmean_name = "actual saturation vapor pressure"
avp_rhmean@units = "kPa"
avp_rhmean@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
avp_rhmean@info = "FAO 56; EQN 19: min/max t; mean rh; actvpr_rhmean_fao56"
if (.not.isatt(avp_rhmean,"_FillValue") .and. any(avp_rhmean.eq.tFill) ) then
avp_rhmean@_FillValue = tFill
end if
copy_VarCoords(tmin, avp_rhmean)
return( avp_rhmean )
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("ACTVPR_RHMEAN_FAO56")
function ACTVPR_RHMEAN_FAO56(tmin:numeric, tmax:numeric, rhmean:numeric, tunit[1]:integer, opt[1]:logical)
;
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; http://www.fao.org/docrep/x0490e/x0490e0j.htm#annex 2. meteorological tables
; Compute 'actual' saturation vapor pressure with mean relative humidity (%): EQN 19
;
; tmin, tmax - min/max temperature (C or K)
; rmean - mean relative humidity (%)
; tunit - 0 (degC), 1 (degK)
; opt - False ... Both tmin and tmax > 0C
; - True ... use tmax *only*.
; Effectively, this sets 'satvpr_mean_fao56(tmin,0)=0.0'
; If tmin> 0 this has no effect.
;
local conmean, avp_rhmean, tc, tcx, tcn, tFill, tCrit \
, dim_tmin, dim_tmax, dim_rhmn, rnk_tmin, rnk_tmax, rnk_rhmn
begin
; check input
if (tunit.lt.0 .or. tunit.gt.2) then ; tunit error check
print("ACTVPR_RHMEAN_FAO56: unrecognized tunit argument: tunit="+tunit)
exit
end if
dim_tmin = dimsizes(tmin) ; size(s)
dim_tmax = dimsizes(tmax)
dim_rhmn = dimsizes(rhmean)
rnk_tmin = dimsizes(dim_tmin) ; rank
rnk_tmax = dimsizes(dim_tmax)
rnk_rhmn = dimsizes(dim_rhmn)
if (.not.(rnk_tmin.eq.rnk_tmax .and. rnk_tmin.eq.rnk_rhmn)) then
print("ACTVPR_RHMEAN_FAO56: rank mismatch on input arguments")
print(" rnk_tmin="+rnk_tmin)
print(" rnk_tmax="+rnk_tmax)
print(" rnk_rhmn="+rnk_rhmn)
exit
end if
if (.not.(all(dim_tmin.eq.dim_tmax) .and. all(dim_tmin.eq.dim_rhmn))) then
print("ACTVPR_RHMEAN_FAO56: dimension size mismatch on input arguments")
print(" dim_tmin="+dim_tmin+" dim_tmax="+dim_tmax+" dim_rhmn="+dim_rhmn)
exit
end if
if (isatt(tmin,"_FillValue")) then
tFill = tmin@_FillValue
else
tFill = 1e20
end if
if (all(rhmean.le.1)) then
conmean = rhmean/2.0 ; must be fractional and not %
else
conmean = rhmean/200.0
end if
tCrit = 0
if (tunit.eq.0) then ; degC
avp_rhmean = where(tmin.gt.tCrit \
,conmean*(satvpr_mean_fao56(tmax,0)+satvpr_mean_fao56(tmin,0)) \
,tFill )
if (opt) then
avp_rhmean = where(tmin.le.tCrit .and. tmax.gt.tCrit \ ; Should this be done?
,conmean*(satvpr_mean_fao56(tmax,0) ) \ ; satvpr_mean_fao56(0,0)=0.0
,avp_rhmean )
end if
else
if (tunit.eq.1) then ; degK
tcn = tmin-273.16 ; convert to degC
tcx = tmax-273.16
else ; Farenheit
tcn = (tmin-32)*0.5555556 ; convert to degC
tcx = (tmax-32)*0.5555556
end if
avp_rhmean = where(tcn.gt.tCrit \
,conmean*(satvpr_mean_fao56(tcx,0)+satvpr_mean_fao56(tcn,0)) \
,tFill )
if (opt) then
avp_rhmean = where(tcn.le.tCrit .and. tcx.gt.tCrit \ ; Should this be done?
,conmean*(satvpr_mean_fao56(tcx,0) ) \ ; satvpr_mean_fao56(0,0)=0.0
,avp_rhmean )
end if
end if
avp_rhmean@long_rhmean_name = "actual saturation vapor pressure"
avp_rhmean@units = "kPa"
avp_rhmean@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
avp_rhmean@info = "FAO 56; EQN 19: min/max t; mean rh; ACTVPR_RHMEAN_FAO56"
if (.not.isatt(avp_rhmean,"_FillValue") .and. any(avp_rhmean.eq.tFill) ) then
avp_rhmean@_FillValue = tFill
end if
copy_VarCoords(tmin, avp_rhmean)
return( avp_rhmean )
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("actvpr_mnmx_fao56")
function actvpr_mnmx_fao56(tmin:numeric, tmax:numeric, rhmin:numeric, rhmax:numeric, tunit[1]:integer)
; http://www.fao.org/docrep/X0490E/x0490e07.htm
; http://www.fao.org/docrep/x0490e/x0490e0j.htm#annex 2. meteorological tables
; Compute 'actual' saturation vapor pressure with min/max relative humidity (%): EQN 17
; tmin, tmax - min/max temperature (C)
; rhmin, rhmax - min/max relative humidity (%)
local conmin, conmax, avp_mnmx, tc, tcx, tcn, tFill \
, dim_tmin, dim_tmax, dim_rhmn, dim_rhmx, rnk_tmin, rnk_tmax, rnk_rhmn, rnk_rhmx
begin
; check input
if (tunit.lt.0 .or. tunit.gt.2) then ; tunit error check
print("actvpr_mnmx_fao56: unrecognized tunit argument: tunit="+tunit)
exit
end if
dim_tmin = dimsizes(tmin) ; size(s)
dim_tmax = dimsizes(tmax)
dim_rhmn = dimsizes(rhmin)
dim_rhmx = dimsizes(rhmax)
rnk_tmin = dimsizes(dim_tmin) ; rank
rnk_tmax = dimsizes(dim_tmax)
rnk_rhmn = dimsizes(dim_rhmn)
rnk_rhmx = dimsizes(dim_rhmx)
if (.not.(rnk_tmin.eq.rnk_tmax .and. rnk_tmin.eq.rnk_rhmn)) then
print("actvpr_rhmean_fao56: rank mismatch on input arguments")
print(" rnk_tmin="+rnk_tmin)
print(" rnk_tmax="+rnk_tmax)
print(" rnk_rhmn="+rnk_rhmn)
print(" rnk_rhmx="+rnk_rhmx)
exit
end if
if (.not.(all(dim_tmin.eq.dim_tmax) .and. \
all(dim_tmin.eq.dim_rhmn) .and. \
all(dim_tmin.eq.dim_rhmx))) then
print("actvpr_mnmx_fao56: dimension size mismatch on input arguments")
print(" dim_tmin="+dim_tmin+" dim_tmax="+dim_tmax)
print(" dim_rhmn="+dim_rhmn+" dim_rhmx="+dim_rhmx)
exit
end if
if (isatt(tmin,"_FillValue")) then
tFill = tmin@_FillValue
else
tFill = 1e20
end if
if (all(rhmin.le.1)) then
conmin = rhmin/2.0
else
conmin = rhmin/200.0
end if
if (all(rhmax.le.1)) then
conmax = rhmax/2.0
else
conmax = rhmax/200.0
end if
; note the (min*max) and (max*min) 'cross-product'
if (tunit.eq.0) then
avp_mnmx = where(tmin.gt.0 \
,conmin*satvpr_mean_fao56(tmax,0) + conmax*satvpr_mean_fao56(tmin,0) \
,tFill)
else
if (tunit.eq.1) then ; degK
tcn = tmin-273.16 ; convert to degC
tcx = tmax-273.16
else ; Farenheit
tcn = (tmin-32)*0.5555556 ; convert to degC
tcx = (tmax-32)*0.5555556
end if
avp_mnmx = where(tcn.gt.0 \
,conmin*satvpr_mean_fao56(tcx,0) + conmax*satvpr_mean_fao56(tcn,0) \
,tFill)
end if
avp_mnmx@long_name = "actual saturation vapor pressure"
avp_mnmx@units = "kPa"
avp_mnmx@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
avp_mnmx@info = "FAO 56; EQN 17: min/max t & rh; actvpr_mnmx_fao56"
if (.not.isatt(avp_mnmx,"_FillValue") .and. any(avp_mnmx.eq.tFill) ) then
avp_mnmx@_FillValue = tFill
end if
copy_VarCoords(tmin, avp_mnmx)
return( avp_mnmx )
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("daylight_fao56")
function daylight_fao56(jday[*]:numeric, lat:numeric)
; Compute maximum day light hours; EQN 34
; http://www.fao.org/docrep/X0490E/x0490e07.htm
;
; **For abs(lat) > 55 the eqns have limited validity**
; NOTE:
; arg := -tan(latrad(nl))*tan(sdec) ; can be > 1
; arg = where(arg.lt.-1, -0.99999, arg)
; arg = where(arg.gt. 1, 0.99999, arg)
; ws = acos(arg) ; acos(-tan(latrad(nl))*tan(sdec)) ; eq 25 [ntim]
local pi, pi2, con, rad, sdec, sdec, ws, pi2yr, latrad, ntim,nlat, mlon, arg
begin
pi = 4*atan(1)
pi2 = 2*pi
rad = pi/180
pi2yr = pi2/365
latrad = lat*rad
con = (24/pi)
; dimensional stuff
ntim = dimsizes(jday) ; # time time steps
dimlat = dimsizes(lat)
ranklat= dimsizes(dimlat)
if (ranklat.ge.3) then
print("daylight_fao56: latitude rank exceeds 2: FATAL: ranklat="+ranklat)
exit
end if
nlat = dimlat(0)
if (ranklat.eq.2) then
mlon = dimlat(1)
end if
sdec = 0.409*sin(pi2yr*jday - 1.39) ; eq 24 [ntim]
if (ranklat.eq.1) then
daylightmax = new( (/ntim,nlat/), "float", "No_FillValue")
do nl=0,nlat-1
arg := -tan(latrad(nl))*tan(sdec)
arg = where(arg.lt.-1, -0.99999, arg)
arg = where(arg.gt. 1, 0.99999, arg)
ws = acos(arg) ; acos(-tan(latrad(nl))*tan(sdec)) ; eq 25 [ntim]
daylightmax(:,nl) = con*ws ; eq 34
end do
; handle meta data
copy_VarCoords(jday, daylightmax(:,0))
copy_VarCoords( lat, daylightmax(0,:))
else if (ranklat.eq.2) then
daylightmax = new( (/ntim,nlat,mlon/), "float", "No_FillValue")
do nl=0,nlat-1
do ml=0,mlon-1
arg := -tan(latrad(nl,ml))*tan(sdec)
arg = where(arg.lt.-1, -0.99999, arg)
arg = where(arg.gt. 1, 0.99999, arg)
ws = acos(arg) ; acos(-tan(latrad(nl,ml))*tan(sdec)) ; eq 25 [ntim]
daylightmax(:,nl) = con*ws ; eq 34
end do
end do
; handle meta data
copy_VarCoords(jday, daylightmax(:,0,0))
copy_VarCoords( lat, daylightmax(0,:,:))
end if
end if
daylightmax@long_name = "maximum day light hours: FAO_56"
daylightmax@units = "hours/day"
daylightmax@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
daylightmax@info = "FAO 56; EQN 34; daylight_fao56"
return(daylightmax)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef ("radext_fao56")
function radext_fao56(jday[*]:integer, lat:numeric, ounit[1]:integer)
; Compute extra terrestrial radiation; EQN 21
; http://www.fao.org/docrep/X0490E/x0490e07.htm
;
; jday - day of year
; lat - degrees_{north/south}; can be a scalar, [*], or [*][*]
; ***For abs(lat) > 55 the eqns have limited validity ***
;
local pi, pi2, con, mnday, rad, gsc, rdist, ntim, dimlat, ranklat \
, sdec, s, pi2yr, latrad, radextday, nlat, mlon, arg
begin
; constants
pi = 4.0*atan(1.0)
pi2 = 2*pi
rad = pi/180
mnday = 1440 ; minutes/day (24*60)
gsc = 0.0820 ; solar constant: MJ/(m2-min)
; dimensional stuff
ntim = dimsizes(jday) ; # time time steps
dimlat = dimsizes(lat)
ranklat= dimsizes(dimlat)
if (ranklat.ge.3) then
print("radext_fao56: latitude rank exceeds 2: FATAL: ranklat="+ranklat)
exit
end if
nlat = dimlat(0)
if (ranklat.eq.2) then
mlon = dimlat(1)
end if
con = (mnday/pi) ; units conversation
pi2yr = pi2/365
if (typeof(lat).eq."float") then
latrad = lat*rad ; [nlat] or [nlat,mlon]
else
latrad = tofloat(lat)*rad ; [nlat] or [nlat,mlon]
end if
;latrad@_FillValue = 1e20 ; avoid any 90S, 90N
;latrad = where(abs(lat).ne.90, latrad, latrad@_FillValue)
rdist = 1 + 0.033*cos(pi2yr*jday) ; eq 23 [ntim]
sdec = 0.409*sin(pi2yr*jday - 1.39) ; eq 24 [ntim]
if (nlat.eq.1 .and. ntim.eq.1) then ; both input are scalar
arg := -tan(latrad(nl))*tan(sdec)
arg = where(arg.lt.-1, -0.99999, arg)
arg = where(arg.gt. 1, 0.99999, arg)
ws = acos(arg) ; acos(-tan(latrad(nl))*tan(sdec)) ; eq 25 [ntim]
radextday = con*gsc*rdist*(ws*sin(latrad)*sin(sdec) \
+cos( latrad)*cos(sdec)*sin(ws) )
else if (ranklat.eq.1) then
radextday = new( (/ntim,nlat/), "float", "No_FillValue")
do nl=0,nlat-1
arg = -tan(latrad(nl))*tan(sdec)
arg = where(arg.lt.-1, -0.99999, arg)
arg = where(arg.gt. 1, 0.99999, arg)
ws = acos(arg) ; acos(-tan(latrad(nl))*tan(sdec)) ; eq 25 [ntim]
radextday(:,nl) = con*gsc*rdist*(ws*sin(latrad(nl))*sin(sdec) \
+cos( latrad(nl))*cos(sdec)*sin(ws) )
end do
; handle meta data
copy_VarCoords(jday, radextday(:,0))
copy_VarCoords( lat, radextday(0,:)) ; this does not seem to work!!!
if (.not.isdimnamed(radextday,1) .and. isdimnamed(lat,0)) then
radextday!1 = lat!0 ; fix above issue
end if
else if (ranklat.eq.2) then
radextday = new( (/ntim,nlat,mlon/), "float", "No_FillValue")
do nl=0,nlat-1
do ml=0,mlon-1
arg := -tan(latrad(nl,ml))*tan(sdec)
arg = where(arg.lt.-1, -0.99999, arg)
arg = where(arg.gt. 1, 0.99999, arg)
ws = acos(arg) ; acos(-tan(latrad(nl,ml))*tan(sdec)) ; eq 25 [ntim]
radextday(:,nl,ml) = con*gsc*rdist*(ws*sin(latrad(nl,ml))*sin(sdec) \
+cos( latrad(nl,ml))*cos(sdec)*sin(ws) )
end do
end do
; handle meta data
copy_VarCoords(jday, radextday(:,0,0))
copy_VarCoords( lat, radextday(0,:,:))
end if ; ranklat.eq.2
end if ; ranklat.eq.1
end if ; scalar; nlat.eq.1 .and. ntim.eq.1
ra_unit = "MJ/(m2-day)" ; ounit=1
if (ounit.eq.0) then
radextday = (/ radextday*0.408 /) ; MJ/(m2-day) ==> mm/day
ra_unit = "mm/day"
end if
if (ounit.eq.2) then
radextday = (/ radextday*11.574/) ; MJ/(m2-day) ==> W/m2
ra_unit = "W/m2"
end if
radextday@long_name = "extra terrestrial radiation: FAO_56"
radextday@units = ra_unit
radextday@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
radextday@info = "FAO 56; EQN 21; radext_fao56"
;;if (isatt(radextday,"jday")) then
;; delete(radextday@jday)
;;end if
;;if (isatt(radextday,"time")) then
;; delete(radextday@time)
;;end if
return(radextday)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef ("radsol_fao56")
function radsol_fao56(radext:numeric, sunhrx:numeric, sunhr:numeric, iounit[2]:integer, opt[1]:logical)
;
; Similar to 'radsol2_fao56 except that the arguments 'radext' and 'sunhrx' are input
; Avoids repetitive computations
;
; Compute solar radiation; EQN 35
; http://www.fao.org/docrep/X0490E/x0490e07.htm
;=======
; This is the 'Angstrom Formula' which relates solar radiation to extraterrestrial
; radiation and relative sunshine duration.
;
local as, bs, units, radsol, cunits, conrad
begin
as = 0.25 ; default; Chap 3, pg 23
bs = 0.50
if (opt .and. isatt(opt, "as")) then
as := opt@as
end if
if (opt .and. isatt(opt, "bs")) then
bs := opt@bs
end if
if (opt .and. isatt(opt, "as_bs") .and. dimsizes(opt@as_bs).eq.2) then
as := opt@as_bs(0)
bs := opt@as_bs(1)
end if
if (iounit(0).eq.0) then
conrad = 2.45 ; mm/day => MJ/(m2-day)
end if
if (iounit(0).eq.1) then
conrad = 1.00 ; MJ/(m2-day)
end if
if (iounit(0).eq.2) then
conrad = 0.864 ; W/m2 => MJ/(m2-day)
end if
; EQN 35; Rs
if (all(sunhrx.gt.0)) then
radsol = (as+bs*(sunhr/sunhrx))*(radext*conrad)
else ; avoid division by zero
sunx = where(sunhrx.le.0, 1e20, sunhrx)
sunx@_FillValue = 1e20
radsol = (as+bs*(sunhr/sunx))*(radext*conrad)
radsol = where(ismissing(radsol), 0, radsol)
end if
; EQN 35; Rs
;;;if (iounit(0).eq.0) then
;;; radsol = (as+bs*(sunhr/sunhrx))*(radext*2.45) ; mm/day => MJ/(m2-day)
;;;else if (iounit(0).eq.1) then
;;; radsol = (as+bs*(sunhr/sunhrx))*radext ; MJ/(m2-day)
;;;else if (iounit(0).eq.2) then
;;; radsol = (as+bs*(sunhr/sunhrx))*(radext*0.0864) ; W/m2 => MJ/(m2-day)
;;; end if
;;; end if
;;;end if
if (iounit(1).eq.0) then
radsol = (/ radsol*0.408 /) ; MJ/(m2-day) => mm/day
units = "mm/day"
end if
if (iounit(1).eq.1) then
units = "MJ/(m2-day)" ; ounit=1
end if
if (iounit(1).eq.2) then
radsol = (/ radsol*11.574 /) ; MJ/(m2-day) ==> W/m2
units = "W/m2"
end if
radsol@long_name = "solar radiation: FAO_56"
radsol@units = units
radsol@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
radsol@info = "FAO 56; EQN 35; radsol_fao56"
radsol@parameters = "as="+as+" bs="+bs
copy_VarCoords(radext, radsol)
return(radsol)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef ("radsol2_fao56")
function radsol2_fao56(jday[*]:integer, lat:numeric, sunhr:numeric, ounit[1]:integer, opt[1]:logical)
; Compute solar radiation; EQN 35
; http://www.fao.org/docrep/X0490E/x0490e07.htm
;=======
; This is the 'Angstrom Formula' which relates solar radiation to extraterrestrial
; radiation and relative sunshine duration.
;=======
; For abs(lat) > 55 the eqns have limited validity
;=======
;
; julian day of current year
; lat can be a scalar, [*], or [*][*]
;
local as, bs, sunhrx, raext, radsol2, units
begin
sunhrx = daylight_fao56(jday, lat) ; max daylight/sun; hr per day
radext = radext_fao56(jday, lat, 1) ; radiation MJ/(m2-day)
as = 0.25 ; default; Chap 3, pg 23
bs = 0.50
if (opt .and. isatt(opt, "as")) then
as := opt@as
end if
if (opt .and. isatt(opt, "bs")) then
bs := opt@bs
end if
if (opt .and. isatt(opt, "as_bs") .and. dimsizes(opt@as_bs).eq.2) then
as := opt@as_bs(0)
bs := opt@as_bs(1)
end if
radsol2 = (as+bs*(sunhr/sunhrx))*radext ; MJ/(m2-day) ; EQN 35; Rs
if (ounit.eq.0) then
radsol2 = (/ radsol2*0.408 /) ; MJ/(m2-day) ==? mm/day
units = "mm/day"
end if
if (ounit.eq.1) then
units = "MJ/(m2-day)" ; ounit=1
end if
if (ounit.eq.2) then
radsol2 = (/ radsol2*11.574 /) ; MJ/(m2-day) ==> W/m2
units = "W/m2"
end if
radsol2@long_name = "solar radiation: FAO_56"
radsol2@units = units
radsol2@url = "http://www.fao.org/docrep/X0490E/x0490e07.htm"
radsol2@info = "FAO 56; EQN 35; radsol2_fao56"
radsol2@parameters = "as="+as+" bs="+bs
copy_VarCoords(radext, radsol2)
return(radsol2)
end
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
undef("radsol3_hargreaves_fao56")
function radsol3_hargreaves_fao56(tmin:numeric, tmax:numeric, radext:numeric, krs:numeric, iounit[3]:integer)
;
; Solar radiation derived from air temp differences
; FAO 56: EQN 50
;
; tmin, tmax - degC
; radsol - MJ/(m2-day)
;
; iounit - option for input [iounit(0)] and returned units [iounit(1)]
; iounit(0) =0: degC ; <b>input</b> units: <em>tmin/tmax</em>
; =1: degK
; =2: deg farenheit
;
; iounit(1) =0: mm/day ; <b>input</b>: <em>rex</em>
; =1: MJ/(M2-day)
; =2: W/m2
;
; iounit(2) =0: mm/day ; <b>output</b> (returned) units
; =1: MJ/(M2-day)
; =2: W/m2
local conext, consol, tmn, tmx, radsol_hg, units
begin
if (any(iounit.lt.0) .or. any(iounit.gt.2)) then ; iounit error check
if (iounit(0).lt.0 .or. iounit(0).gt.2) then
print("radsol3_hargreaves_fao56: unrecognized iounit(0) argument: iounit(0)="+iounit(0))
end if
if (iounit(1).lt.0 .or. iounit(1).gt.2) then
print("radsol3_hargreaves_fao56: unrecognized iounit(1) argument: iounit(1)="+iounit(1))
end if
if (iounit(2).lt.0 .or. iounit(2).gt.2) then
print("radsol3_hargreaves_fao56: unrecognized iounit(2) argument: iounit(2)="+iounit(2))
end if
exit
end if
; constant used to change input radext units to MJ/(m2=day)
if (iounit(1).eq.0) then
conext = 2.45 ; mm/day ==> MJ/(m2=day)
else if (iounit(1).eq.1) then
conext = 1.0 ; no units change
else if (iounit(1).eq.2) then
conext = 0.0865 ; W/m2 ==> MJ/(m2=day)
end if
end if
end if
if (iounit(0).eq.0 .or. iounit(0).eq.1) then ; degC or degK
radsol3 = sqrt(tmax-tmin)
end if