-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmo_errormeasures.f90
3076 lines (2500 loc) · 101 KB
/
mo_errormeasures.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
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
MODULE mo_errormeasures
! This module contains routines for the masked calculation of
! error measures like MSE, RMSE, BIAS, SSE, NSE, KGE, ...
! Note: all except variance and standard deviation are population and not sample moments,
! i.e. they are normally divided by n and not (n-1)
! Written Aug 2012, Matthias Zink
! Modified 2012-2018, Juliane Mai, Stephan Thober, Matthias Cuntz
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2012 Matthias Zink, Juliane Mai, Stephan Thober, Matthias Cuntz - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
USE mo_kind, ONLY: i4, sp, dp
IMPLICIT NONE
PUBLIC :: BIAS ! Bias
PUBLIC :: KGE ! Kling-Gupta efficiency measure
PUBLIC :: LNNSE ! Logarithmic Nash Sutcliffe efficiency
PUBLIC :: MAE ! Mean of absolute errors
PUBLIC :: MAE_PROB_ONE ! Mean absolute error of occurence probability with ONE cdf for obs and mod
! comment until mo_empcdf is commited
! PUBLIC :: MAE_PROB_TWO ! Mean absolute error of occurence probability with TWO separate cdfs for obs and mod
PUBLIC :: MSE ! Mean of squared errors
PUBLIC :: NSE ! Nash Sutcliffe efficiency
PUBLIC :: SSE ! Sum of squared errors
PUBLIC :: SAE ! Sum of absolute errors
PUBLIC :: RMSE ! Root mean squared error
! ------------------------------------------------------------------
! NAME
! BIAS
! PURPOSE
! Calculates the bias
! BIAS = mean(y) - mean(x)
!
! If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! x and y can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = BIAS(dat, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
! real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
! real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: BIAS bias
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
!
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! vec1 = (/ 1., 2, 3., -999., 5., 6. /)
! vec2 = (/ 1., 2, 3., -999., 5., 6. /)
! m = BIAS(vec1, vec2, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
! Written, Matthias Zink, Sept 2012
INTERFACE BIAS
MODULE PROCEDURE BIAS_1d_sp, BIAS_1d_dp, BIAS_2d_sp, BIAS_2d_dp, BIAS_3d_sp, BIAS_3d_dp
END INTERFACE BIAS
! ------------------------------------------------------------------
! NAME
! KGE
!> \brief Kling-Gupta-Efficiency measure.
!> \details The Kling-Gupta model efficiency coefficient \f$ KGE \f$ is
!> \f[ KGE = 1 - \sqrt{( (1-r)^2 + (1-\alpha)^2 + (1-\beta)^2 )} \f]
!> where \n
!> \f$ r \f$ = Pearson product-moment correlation coefficient \n
!> \f$ \alpha \f$ = ratio of simulated mean to observed mean \n
!> \f$ \beta \f$ = ratio of simulated standard deviation to
!> observed standard deviation \n
!> This three measures are calculated between two arrays (1d, 2d, or 3d).
!> Usually, one is an observation and the second is a modelled variable.\n
!>
!> The higher the KGE the better the observation and simulation are matching.
!> The upper limit of KGE is 1.\n
!>
!> Therefore, if you apply a minimization algorithm to calibrate regarding
!> KGE you have to use the objective function
!> \f[ obj\_value = 1.0 - KGE \f]
!> which has then the optimum at 0.0.
!> (Like for the NSE where you always optimize 1-NSE.)\n
!>
! INTENT(IN)
!> real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
!> real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
!> real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
! INTENT(IN), OPTIONAL
!> logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
!> logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
!> logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
!> \return kge — Kling-Gupta-Efficiency (value less equal 1.0)
! RESTRICTIONS
!> \note Input values must be floating points. \n
! EXAMPLE
! para = (/ 1., 2, 3., -999., 5., 6. /)
! kge = kge(x,y,mask=mask)
! LITERATURE
!> Gupta, Hoshin V., et al.
!> "Decomposition of the mean squared error and NSE performance criteria:
!> Implications for improving hydrological modelling."
!> Journal of Hydrology 377.1 (2009): 80-91.
! HISTORY
!> \author Rohini Kumar
!> \date August 2014
! Modified, R. Kumar & O. Rakovec - Sep. 2014
! J. Mai - remove double packing of input data (bug)
! - KGE instead of 1.0-KGE
! - 1d, 2d, 3d, version in sp and dp
INTERFACE KGE
MODULE PROCEDURE KGE_1d_dp, KGE_2d_dp, KGE_3d_dp, KGE_1d_sp, KGE_2d_sp, KGE_3d_sp
END INTERFACE KGE
! ------------------------------------------------------------------
! NAME
! LNNSE
! PURPOSE
! Calculates the Logarithmic Nash Sutcliffe Efficiency
! LNNSE = sum((ln(y) - ln(x))**2) / sum( (ln(x) - ln(mean(x)))**2 )
! where x is the observation and y is the modelled data.
!
! If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! Note that the mask is intent inout, since values which are less or equal zero will be masked additionally.
! x and y can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = LNNSE(dat, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
! real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
! real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: LNNSE Logarithmic Nash Sutcliffe Efficiency
! INTENT(IN), OPTIONAL
! None
! INTENT(INOUT), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
!
! If present, only those locations in vec corresponding to the true values in mask are used.
! The mask will be updated if non-masked values are less equal zero.
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! vec1 = (/ 1., 2, 3., -999., 5., 6. /)
! vec2 = (/ 1., 2, 3., -999., 5., 6. /)
! m = LNNSE(vec1, vec2, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
! Written, Juliane Mai, May 2013
! updated, Rohin Kumar, May 2013 ! for mean of logQ
INTERFACE LNNSE
MODULE PROCEDURE LNNSE_1d_sp, LNNSE_1d_dp, LNNSE_2d_dp, LNNSE_2d_sp, LNNSE_3d_sp, LNNSE_3d_dp
END INTERFACE LNNSE
! ------------------------------------------------------------------
! NAME
! MAE
! PURPOSE
! Calculates the mean absolute error
! MAE = sum(abs(y - x)) / count(mask)
!
! If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! x and y can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = MAE(dat, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
! real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
! real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: MAE Mean Absolute Error
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
!
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! vec1 = (/ 1., 2, 3., -999., 5., 6. /)
! vec2 = (/ 1., 2, 3., -999., 5., 6. /)
! m = MAE(vec1, vec2, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
! Written, Matthias Zink, Sept 2012
INTERFACE MAE
MODULE PROCEDURE MAE_1d_sp, MAE_1d_dp, MAE_2d_sp, MAE_2d_dp, MAE_3d_sp, MAE_3d_dp
END INTERFACE MAE
! ! ------------------------------------------------------------------
! ! NAME
! ! MAE_PROB_TWO
! ! PURPOSE
! ! Calculate mean absolute error of occurence probabilities
! ! MAE_PROB_TWO = mean( |P_sim(Q_obs(t)) - P_obs(Q_obs(t))| )
! !
! ! If an optional mask is given, the calculations are over those locations that correspond to true values in the mask.
! ! x and y have to be double precision. The result will have the same numerical precision.
! ! CALLING SEQUENCE
! ! out = MAE_PROB_TWO(dat, mask=mask)
! ! INTENT(IN)
! ! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! ! INTENT(INOUT)
! ! None
! ! INTENT(OUT)
! ! real(sp/dp) :: BIAS bias
! ! INTENT(IN), OPTIONAL
! ! logical :: mask(:) 1D-array of logical values with size(x/y).
! !
! ! If present, only those locations in vec corresponding to the true values in mask are used.
! ! INTENT(INOUT), OPTIONAL
! ! None
! ! INTENT(OUT), OPTIONAL
! ! None
! ! RESTRICTIONS
! ! Input values must be floating points.
! ! EXAMPLE
! ! vec1 = (/ 1., 2, 3., -9999., 5., 6. /)
! ! vec2 = (/ 1., 2, 3., -9999., 5., 6. /)
! ! m = MAE_PROB_TWO(vec1, vec2, )
! ! -> see also example in test directory
! ! LITERATURE
! ! None
! ! HISTORY
! ! Written, Stephan Thober, May 2016
! INTERFACE MAE_PROB_TWO
! MODULE PROCEDURE MAE_PROB_TWO_1D_DP
! END INTERFACE MAE_PROB_TWO
! ------------------------------------------------------------------
! NAME
! MAE_PROB_ONE
! PURPOSE
! Calculate mean absolute error of occurence probabilities
! MAE_PROB_ONE = mean( |P_obs(Q_obs(t)) - P_obs(Q_sim(t))| )
!
! If an optional mask is given, the calculations are over those locations that correspond to true values in the mask.
! x and y have to be double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = MAE_PROB_ONE(dat, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: BIAS bias
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! If present, only those locations in vec corresponding to
! the true values in mask are used.
! real(sp/dp), dimension(:) :: cdfx 1D-array with kernel_cumdensity of x
! real(sp/dp) :: h Silverman estimate of kernel_density bandwidth for x
!
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! m = MAE_PROB(x, y, mask=mask)
! same as
! h = kernel_density_h(x, silverman=.true., mask=mask)
! cdfx = kernel_cumdensity(x, xout=x, h=h, mask=mask)
! m = MAE_PROB(vec1, vec2, mask=vec1, cdfx=cdf, h=h)
! but cumdensity of x is calculated outside and h is reused.
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
! Written, Stephan Thober, May 2016
! Modified, Matthias Cuntz, Jun 2016 - rm dummy=pack(x), cdfx, h
INTERFACE MAE_PROB_ONE
MODULE PROCEDURE MAE_PROB_ONE_1D_DP
END INTERFACE MAE_PROB_ONE
! ------------------------------------------------------------------
! NAME
! MSE
! PURPOSE
! Calculates the mean squared error
! MSE = sum((y - x)**2) / count(mask)
!
! If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! x and y can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = MSE(dat, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
! real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
! real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: MSE Mean squared error
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
!
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! vec1 = (/ 1., 2, 3., -999., 5., 6. /)
! vec2 = (/ 1., 2, 3., -999., 5., 6. /)
! m = MSE(vec1, vec2, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
! Written, Matthias Zink, Sept 2012
INTERFACE MSE
MODULE PROCEDURE MSE_1d_sp, MSE_1d_dp, MSE_2d_sp, MSE_2d_dp, MSE_3d_sp, MSE_3d_dp
END INTERFACE MSE
! ------------------------------------------------------------------
! NAME
! NSE
! PURPOSE
! Calculates the Nash Sutcliffe Efficiency
! NSE = sum((y - x)**2) / sum( (x - mean(x))**2)
! where x is the observation and y is the modelled data.
!
! If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! x and y can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = NSE(dat, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
! real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
! real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: NSE Nash Sutcliffe Efficiency
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
!
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! vec1 = (/ 1., 2, 3., -999., 5., 6. /)
! vec2 = (/ 1., 2, 3., -999., 5., 6. /)
! m = NSE(vec1, vec2, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! NASH, J., & SUTCLIFFE, J. (1970). River flow forecasting through conceptual models part I: A discussion of
! principles. Journal of Hydrology, 10(3), 282-290. doi:10.1016/0022-1694(70)90255-6
! HISTORY
! Written, Matthias Zink, Sept 2012
INTERFACE NSE
MODULE PROCEDURE NSE_1d_sp, NSE_1d_dp, NSE_2d_dp, NSE_2d_sp, NSE_3d_sp, NSE_3d_dp
END INTERFACE NSE
! ------------------------------------------------------------------
! NAME
! SAE
! PURPOSE
! Calculates the sum of absolute errors
! SAE = sum(abs(y - x))
!
! If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! x and y can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = SAE(x, y, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
! real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
! real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: SAE sum of absolute errors
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
!
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! vec1 = (/ 1., 2, 3., -999., 5., 6. /)
! vec2 = (/ 1., 2, 3., -999., 5., 6. /)
! m = SAE(vec1, vec2, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! none
! HISTORY
! Written, Matthias Zink, Sept 2012
INTERFACE SAE
MODULE PROCEDURE SAE_1d_sp, SAE_1d_dp, SAE_2d_sp, SAE_2d_dp, SAE_3d_sp, SAE_3d_dp
END INTERFACE SAE
! ------------------------------------------------------------------
! NAME
! SSE
! PURPOSE
! Calculates the sum of squared errors
! SSE = sum((y - x)**2)
!
! If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! x and y can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = SSE(x, y, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
! real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
! real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: SSE sum of squared errors
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
!
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! vec1 = (/ 1., 2, 3., -999., 5., 6. /)
! vec2 = (/ 1., 2, 3., -999., 5., 6. /)
! m = SSE(vec1, vec2, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! none
! HISTORY
! Written, Matthias Zink, Sept 2012
INTERFACE SSE
MODULE PROCEDURE SSE_1d_sp, SSE_1d_dp, SSE_2d_sp, SSE_2d_dp, SSE_3d_sp, SSE_3d_dp
END INTERFACE SSE
! ------------------------------------------------------------------
! NAME
! RMSE
! PURPOSE
! Calculates the root-mean-square error
! RMSE = sqrt(sum((y - x)**2) / count(mask))
!
! If an optinal mask is given, the calculations are over those locations that correspond to true values in the mask.
! x and y can be single or double precision. The result will have the same numerical precision.
! CALLING SEQUENCE
! out = RMSE(dat, mask=mask)
! INTENT(IN)
! real(sp/dp), dimension(:) :: x, y 1D-array with input numbers
! OR
! real(sp/dp), dimension(:,:) :: x, y 2D-array with input numbers
! OR
! real(sp/dp), dimension(:,:,:) :: x, y 3D-array with input numbers
! INTENT(INOUT)
! None
! INTENT(OUT)
! real(sp/dp) :: RMSE Root-mean-square error
! INTENT(IN), OPTIONAL
! logical :: mask(:) 1D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:) 2D-array of logical values with size(x/y).
! OR
! logical :: mask(:,:,:) 3D-array of logical values with size(x/y).
!
! If present, only those locations in vec corresponding to the true values in mask are used.
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RESTRICTIONS
! Input values must be floating points.
! EXAMPLE
! vec1 = (/ 1., 2, 3., -999., 5., 6. /)
! vec2 = (/ 1., 2, 3., -999., 5., 6. /)
! m = RMSE(vec1, vec2, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! None
! HISTORY
! Written, Matthias Zink, Sept 2012
INTERFACE RMSE
MODULE PROCEDURE RMSE_1d_sp, RMSE_1d_dp, RMSE_2d_sp, RMSE_2d_dp, RMSE_3d_sp, RMSE_3d_dp
END INTERFACE RMSE
! ------------------------------------------------------------------
PRIVATE
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
FUNCTION BIAS_1d_sp(x, y, mask)
USE mo_moment, ONLY: average
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: x, y
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(sp) :: BIAS_1d_sp
INTEGER(i4) :: n
INTEGER(i4), DIMENSION(size(shape(x)) ) :: shapemask
LOGICAL, DIMENSION(size(x)) :: maske
if (present(mask)) then
shapemask = shape(mask)
else
shapemask = shape(x)
end if
!
if ( (any(shape(x) .NE. shape(y))) .OR. (any(shape(x) .NE. shapemask)) ) &
stop 'BIAS_1d_sp: shapes of inputs(x,y) or mask are not matching'
!
if (present(mask)) then
maske = mask
n = count(maske)
else
maske = .true.
n = size(x)
endif
!
if (n .LE. 1_i4) stop 'BIAS_1d_sp: number of arguments must be at least 2'
!
BIAS_1d_sp = average(y, mask=maske) - average(x, mask=maske)
END FUNCTION BIAS_1d_sp
FUNCTION BIAS_1d_dp(x, y, mask)
USE mo_moment, ONLY: average
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: x, y
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(dp) :: BIAS_1d_dp
INTEGER(i4) :: n
INTEGER(i4), DIMENSION(size(shape(x)) ) :: shapemask
LOGICAL, DIMENSION(size(x)) :: maske
if (present(mask)) then
shapemask = shape(mask)
else
shapemask = shape(x)
end if
!
if ( (any(shape(x) .NE. shape(y))) .OR. (any(shape(x) .NE. shapemask)) ) &
stop 'BIAS_1d_dp: shapes of inputs(x,y) or mask are not matching'
!
if (present(mask)) then
maske = mask
n = count(maske)
else
maske = .true.
n = size(x)
endif
if (n .LE. 1_i4) stop 'BIAS_1d_dp: number of arguments must be at least 2'
!
BIAS_1d_dp = average(y, mask=maske) - average(x, mask=maske)
END FUNCTION BIAS_1d_dp
FUNCTION BIAS_2d_sp(x, y, mask)
USE mo_moment, ONLY: average
IMPLICIT NONE
REAL(sp), DIMENSION(:,:), INTENT(IN) :: x, y
LOGICAL, DIMENSION(:,:), OPTIONAL, INTENT(IN) :: mask
REAL(sp) :: BIAS_2d_sp
INTEGER(i4) :: n
INTEGER(i4), DIMENSION(size(shape(x)) ) :: shapemask
LOGICAL, DIMENSION(size(x, dim=1), size(x, dim=2)):: maske
if (present(mask)) then
shapemask = shape(mask)
else
shapemask = shape(x)
end if
!
if ( (any(shape(x) .NE. shape(y))) .OR. (any(shape(x) .NE. shapemask)) ) &
stop 'BIAS_2d_sp: shapes of inputs(x,y) or mask are not matching'
!
if (present(mask)) then
maske = mask
n = count(maske)
else
maske = .true.
n = size(x, dim=1) * size(x, dim=2)
endif
!
if (n .LE. 1_i4) stop 'BIAS_2d_sp: number of arguments must be at least 2'
!
BIAS_2d_sp = average(reshape(y, (/size(y,dim=1) * size(y,dim=2)/)), &
mask=reshape(maske,(/size(y,dim=1) * size(y,dim=2)/))) - &
average(reshape(x, (/size(x,dim=1) * size(x,dim=2)/)), &
mask=reshape(maske,(/size(x,dim=1) * size(x,dim=2)/)))
!
END FUNCTION BIAS_2d_sp
FUNCTION BIAS_2d_dp(x, y, mask)
USE mo_moment, ONLY: average
IMPLICIT NONE
REAL(dp), DIMENSION(:,:), INTENT(IN) :: x, y
LOGICAL, DIMENSION(:,:), OPTIONAL, INTENT(IN) :: mask
REAL(dp) :: BIAS_2d_dp
INTEGER(i4) :: n
INTEGER(i4), DIMENSION(size(shape(x)) ) :: shapemask
LOGICAL, DIMENSION(size(x, dim=1), size(x, dim=2)):: maske
if (present(mask)) then
shapemask = shape(mask)
else
shapemask = shape(x)
end if
!
if ( (any(shape(x) .NE. shape(y))) .OR. (any(shape(x) .NE. shapemask)) ) &
stop 'BIAS_2d_dp: shapes of inputs(x,y) or mask are not matching'
!
if (present(mask)) then
maske = mask
n = count(maske)
else
maske = .true.
n = size(x, dim=1) * size(x, dim=2)
endif
!
if (n .LE. 1_i4) stop 'BIAS_2d_dp: number of arguments must be at least 2'
!
BIAS_2d_dp = average(reshape(y, (/size(y,dim=1) * size(y,dim=2)/)), &
mask=reshape(maske,(/size(y,dim=1) * size(y,dim=2)/))) - &
average(reshape(x, (/size(x,dim=1) * size(x,dim=2)/)), &
mask=reshape(maske,(/size(x,dim=1) * size(x,dim=2)/)))
!
END FUNCTION BIAS_2d_dp
FUNCTION BIAS_3d_sp(x, y, mask)
USE mo_moment, ONLY: average
IMPLICIT NONE
REAL(sp), DIMENSION(:,:,:), INTENT(IN) :: x, y
LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(IN) :: mask
REAL(sp) :: BIAS_3d_sp
INTEGER(i4) :: n
INTEGER(i4), DIMENSION(size(shape(x)) ) :: shapemask
LOGICAL, DIMENSION(size(x, dim=1), &
size(x, dim=2), size(x, dim=3)) :: maske
if (present(mask)) then
shapemask = shape(mask)
else
shapemask = shape(x)
end if
!
if ( (any(shape(x) .NE. shape(y))) .OR. (any(shape(x) .NE. shapemask)) ) &
stop 'BIAS_3d_sp: shapes of inputs(x,y) or mask are not matching'
!
if (present(mask)) then
maske = mask
n = count(maske)
else
maske = .true.
n = size(x, dim=1) * size(x, dim=2) * size(x, dim=3)
endif
!
! not really sopisticated, it has to be checked if the 3 numbers of x and y are matching in arry position
if (n .LE. 1_i4) stop 'BIAS_3d_sp: number of arguments must be at least 2'
!
BIAS_3d_sp = average(reshape(y, (/size(y,dim=1) * size(y,dim=2) * size(y,dim=3)/)), &
mask=reshape(maske,(/size(y,dim=1) * size(y,dim=2) * size(y,dim=3)/))) - &
average(reshape(x, (/size(x,dim=1) * size(x,dim=2) * size(x,dim=3)/)), &
mask=reshape(maske,(/size(x,dim=1) * size(x,dim=2) * size(x,dim=3)/)))
!
END FUNCTION BIAS_3d_sp
FUNCTION BIAS_3d_dp(x, y, mask)
USE mo_moment, ONLY: average
IMPLICIT NONE
REAL(dp), DIMENSION(:,:,:), INTENT(IN) :: x, y
LOGICAL, DIMENSION(:,:,:), OPTIONAL, INTENT(IN) :: mask
REAL(dp) :: BIAS_3d_dp
INTEGER(i4) :: n
INTEGER(i4), DIMENSION(size(shape(x)) ) :: shapemask
LOGICAL, DIMENSION(size(x, dim=1), &
size(x, dim=2), size(x, dim=3)) :: maske
if (present(mask)) then
shapemask = shape(mask)
else
shapemask = shape(x)
end if
!
if ( (any(shape(x) .NE. shape(y))) .OR. (any(shape(x) .NE. shapemask)) ) &
stop 'BIAS_3d_dp: shapes of inputs(x,y) or mask are not matching'
!
if (present(mask)) then
maske = mask
n = count(maske)
else
maske = .true.
n = size(x, dim=1) * size(x, dim=2) * size(x, dim=3)
endif
!
! not really sopisticated, it has to be checked if the 3 numbers of x and y are matching in arry position
if (n .LE. 1_i4) stop 'BIAS_3d_dp: number of arguments must be at least 2'
!
BIAS_3d_dp = average(reshape(y, (/size(y,dim=1) * size(y,dim=2) * size(y,dim=3)/)), &
mask=reshape(maske,(/size(y,dim=1) * size(y,dim=2) * size(y,dim=3)/))) - &
average(reshape(x, (/size(x,dim=1) * size(x,dim=2) * size(x,dim=3)/)), &
mask=reshape(maske,(/size(x,dim=1) * size(x,dim=2) * size(x,dim=3)/)))
!
END FUNCTION BIAS_3d_dp
! ------------------------------------------------------------------
FUNCTION KGE_1d_sp(x, y, mask)
USE mo_moment, ONLY: average, stddev, correlation
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: x, y
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(sp) :: KGE_1d_sp