-
Notifications
You must be signed in to change notification settings - Fork 3
/
fiduceo_uncertainties.f90
executable file
·5263 lines (4679 loc) · 182 KB
/
fiduceo_uncertainties.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
! * Copyright (C) 2017 J.Mittaz University of Reading
! * This code was developed for the EC project Fidelity and Uncertainty in
! * Climate Data Records from Earth Observations (FIDUCEO).
! * Grant Agreement: 638822
! *
! * This program is free software; you can redistribute it and/or modify it
! * under the terms of the GNU General Public License as published by the Free
! * Software Foundation; either version 3 of the License, or (at your option)
! * any later version.
! * This program is distributed in the hope that it will be useful, but WITHOUT
! * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
! * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
! * more details.
! *
! * A copy of the GNU General Public License should have been supplied along
! * with this program; if not, see http://www.gnu.org/licenses/
!
!
! This code is a simple rewrite of Marines original code to split out
! the FIDUCEO uncertainties and sensitivities into a different structure
! and then write it out
!
! Only science changes are using the new readers filtering/Allan deviation
! calculations plus adding in a different systematic component to the
! visible channels
!
! Written by J,Mittaz University of Reading
! Under the FIDUCEO project (www.fiduceo.eu)
!
! Original 21-07-2017 (v0.1pre)
!
! MT: 11-11-2017: Define temp variables us1,us2,us3a to store structured
! uncertainties on the reflectance channels
! MT: 11-11-2017: fix problem of value not filling array with 0.03 for
! u_structured_Ch1
! MT: 11-11-2017: fix problem of value not filling array with 0.05 for
! u_structured_Ch2
! MT: 11-11-2017: fix problem of value not filling array with 0.05 for
! u_structured_Ch3a
! MT: 13-11-2017: allocated nsmoothBB3,4,5 and nsmoothSp3,4,5 to AVHRRout data
! structure in combine_orbits.f90 so that the calculations don't fail
! MT: 19-12-2017: v0.3pre
! MT: 09-03-2018: v0.5beta
!
! JM: 14-05-2018: Added GBCS CCI L1C output routine
! JM: 12-07-2018: Added in FIDUCEO measurement equations
!
! Note: Coefs data from CCI are ordered as
!
! 1 = a0 (bias term)
! 2 = a1 (offset to emissivity)
! 3 = (ICT bias term for IAsI calibration
! 4 = a2 (non-linear term)
! 5 = nuc (SRF)
! 6 = aval "
! 7 = bval "
! 8 = a4 (Tinstrument term)
MODULE fiduceo_uncertainties
! From Marines code base
USE GbcsTypes
USE GbcsConstants
USE GbcsErrorHandler
USE NOAA_LoadAVHRRLevel1B
USE NETCDF
USE GbcsDateTime
USE FIDUCEO_Calibration
IMPLICIT NONE
TYPE FIDUCEO_Data
REAL, ALLOCATABLE :: Rict_c3(:)
REAL, ALLOCATABLE :: Rict_c4(:)
REAL, ALLOCATABLE :: Rict_c5(:)
REAL, ALLOCATABLE :: dtstar_over_dT3(:)
REAL, ALLOCATABLE :: dtstar_over_dT4(:)
REAL, ALLOCATABLE :: dtstar_over_dT5(:)
REAL, ALLOCATABLE :: dtstar_over_daval3(:)
REAL, ALLOCATABLE :: dtstar_over_daval4(:)
REAL, ALLOCATABLE :: dtstar_over_daval5(:)
REAL, ALLOCATABLE :: dtstar_over_dbval3(:)
REAL, ALLOCATABLE :: dtstar_over_dbval4(:)
REAL, ALLOCATABLE :: dtstar_over_dbval5(:)
REAL, ALLOCATABLE :: dtstar_over_dnuc3(:)
REAL, ALLOCATABLE :: dtstar_over_dnuc4(:)
REAL, ALLOCATABLE :: dtstar_over_dnuc5(:)
REAL, ALLOCATABLE :: drict_over_dnuc3(:)
REAL, ALLOCATABLE :: drict_over_dnuc4(:)
REAL, ALLOCATABLE :: drict_over_dnuc5(:)
REAL, ALLOCATABLE :: drict_over_dtstar3(:)
REAL, ALLOCATABLE :: drict_over_dtstar4(:)
REAL, ALLOCATABLE :: drict_over_dtstar5(:)
REAL, ALLOCATABLE :: dre_over_da03(:,:)
REAL, ALLOCATABLE :: dre_over_da04(:,:)
REAL, ALLOCATABLE :: dre_over_da05(:,:)
REAL, ALLOCATABLE :: dre_over_da13(:,:)
REAL, ALLOCATABLE :: dre_over_da14(:,:)
REAL, ALLOCATABLE :: dre_over_da15(:,:)
REAL, ALLOCATABLE :: dre_over_da23(:,:)
REAL, ALLOCATABLE :: dre_over_da24(:,:)
REAL, ALLOCATABLE :: dre_over_da25(:,:)
REAL, ALLOCATABLE :: dre_over_da33(:,:)
REAL, ALLOCATABLE :: dre_over_da34(:,:)
REAL, ALLOCATABLE :: dre_over_da35(:,:)
REAL, ALLOCATABLE :: dre_over_dtinstr3(:,:)
REAL, ALLOCATABLE :: dre_over_dtinstr4(:,:)
REAL, ALLOCATABLE :: dre_over_dtinstr5(:,:)
REAL, ALLOCATABLE :: dre_over_dce3(:,:)
REAL, ALLOCATABLE :: dre_over_dce4(:,:)
REAL, ALLOCATABLE :: dre_over_dce5(:,:)
REAL, ALLOCATABLE :: dre_over_dcict3(:,:)
REAL, ALLOCATABLE :: dre_over_dcict4(:,:)
REAL, ALLOCATABLE :: dre_over_dcict5(:,:)
REAL, ALLOCATABLE :: dre_over_drict3(:,:)
REAL, ALLOCATABLE :: dre_over_drict4(:,:)
REAL, ALLOCATABLE :: dre_over_drict5(:,:)
REAL, ALLOCATABLE :: dre_over_dcs1(:,:)
REAL, ALLOCATABLE :: dre_over_dcs2(:,:)
REAL, ALLOCATABLE :: dre_over_dcs3a(:,:)
REAL, ALLOCATABLE :: dre_over_dcs3(:,:)
REAL, ALLOCATABLE :: dre_over_dcs4(:,:)
REAL, ALLOCATABLE :: dre_over_dcs5(:,:)
REAL, ALLOCATABLE :: uc3(:,:)
REAL, ALLOCATABLE :: uc4(:,:)
REAL, ALLOCATABLE :: uc5(:,:)
REAL, ALLOCATABLE :: ur3(:,:)
REAL, ALLOCATABLE :: ur4(:,:)
REAL, ALLOCATABLE :: ur5(:,:)
REAL, ALLOCATABLE :: us1(:,:)
REAL, ALLOCATABLE :: us2(:,:)
REAL, ALLOCATABLE :: us3a(:,:)
REAL, ALLOCATABLE :: us3(:,:)
REAL, ALLOCATABLE :: us4(:,:)
REAL, ALLOCATABLE :: us5(:,:)
REAL, ALLOCATABLE :: btf3(:,:)
REAL, ALLOCATABLE :: btf4(:,:)
REAL, ALLOCATABLE :: btf5(:,:)
REAL, ALLOCATABLE :: uce3(:,:)
REAL, ALLOCATABLE :: uce4(:,:)
REAL, ALLOCATABLE :: uce5(:,:)
REAL, ALLOCATABLE :: urict3_r(:)
REAL, ALLOCATABLE :: urict4_r(:)
REAL, ALLOCATABLE :: urict5_r(:)
REAL, ALLOCATABLE :: urict3_s(:)
REAL, ALLOCATABLE :: urict4_s(:)
REAL, ALLOCATABLE :: urict5_s(:)
REAL, ALLOCATABLE :: ucict3(:)
REAL, ALLOCATABLE :: ucict4(:)
REAL, ALLOCATABLE :: ucict5(:)
REAL, ALLOCATABLE :: ucs3(:)
REAL, ALLOCATABLE :: ucs4(:)
REAL, ALLOCATABLE :: ucs5(:)
!
! extra CURUC variables
REAL, ALLOCATABLE :: dBT_over_dT3(:,:)
REAL, ALLOCATABLE :: dBT_over_dT4(:,:)
REAL, ALLOCATABLE :: dBT_over_dT5(:,:)
REAL, ALLOCATABLE :: dBT_over_dcs3(:,:)
REAL, ALLOCATABLE :: dBT_over_dcs4(:,:)
REAL, ALLOCATABLE :: dBT_over_dcs5(:,:)
REAL, ALLOCATABLE :: dBT_over_dcict3(:,:)
REAL, ALLOCATABLE :: dBT_over_dcict4(:,:)
REAL, ALLOCATABLE :: dBT_over_dcict5(:,:)
REAL, ALLOCATABLE :: hu3(:,:)
REAL, ALLOCATABLE :: hu4(:,:)
REAL, ALLOCATABLE :: hu5(:,:)
!
! Harmonisation covar
!
REAL, ALLOCATABLE :: Cmatrix3(:)
REAL, ALLOCATABLE :: Omatrix3(:)
REAL, ALLOCATABLE :: Cmatrix(:)
REAL, ALLOCATABLE :: Omatrix(:)
REAL :: nuc(3)
REAL :: aval(3)
REAL :: bval(3)
INTEGER, ALLOCATABLE :: flag_no_detection(:,:)
INTEGER(GbcsInt1), ALLOCATABLE :: quality_channel_bitmask(:,:)
INTEGER(GbcsInt1), ALLOCATABLE :: quality_scanline_bitmask(:)
END TYPE FIDUCEO_Data
!
! Structure to hold radiance/bt deltas from MC runs
!
TYPE mc_delta_str
INTEGER :: nscan
INTEGER :: nelem
INTEGER :: nmc
REAL, ALLOCATABLE :: ch1(:,:,:)
REAL, ALLOCATABLE :: ch2(:,:,:)
REAL, ALLOCATABLE :: ch3a(:,:,:)
REAL, ALLOCATABLE :: ch3(:,:,:)
REAL, ALLOCATABLE :: ch4(:,:,:)
REAL, ALLOCATABLE :: ch5(:,:,:)
END type mc_delta_str
! Constants
REAL, PARAMETER :: c1 = 1.1910427E-5
REAL, PARAMETER :: c2 = 1.4387752
REAL, PARAMETER :: eta_ict = 0.985140
REAL, PARAMETER :: prt_accuracy=0.1, prt_noise=0.
!
! This is where the FIDUCEO software version number is defined
!
! MT: 19-12-2017: v0.3pre
! MT: 09-03-2018: v0.5beta
! JM: 12/04/2019: v0.2Bet (Beta)
CHARACTER(LEN=6) :: software_version = '0.3Bet'
! MT: 11-11-2017: Define temp variables to store structured uncertainties on the reflectance channels
REAL, ALLOCATABLE :: us1(:,:)
REAL, ALLOCATABLE :: us2(:,:)
REAL, ALLOCATABLE :: us3a(:,:)
PRIVATE
PUBLIC :: FIDUCEO_Data
PUBLIC :: Add_FIDUCEO_Uncert
PUBLIC :: mc_delta_str
PUBLIC :: prt_accuracy
CONTAINS
SUBROUTINE Add_FIDUCEO_Uncert(IMG,AVHRR,uuid_in,filename_nc,&
use_iasi_calibration,gbcs_l1c_output,&
gbcs_l1c_cal,use_walton,keep_temp,write_fcdr,monte_carlo,&
delta_radiance,seedval,ocean_only)
TYPE(Imagery), INTENT(IN) :: IMG
TYPE(AVHRR_Data), INTENT(INOUT) :: AVHRR
CHARACTER(LEN=*), INTENT(IN) :: uuid_in
CHARACTER(LEN=*), INTENT(IN) :: filename_nc
LOGICAL, OPTIONAL :: use_iasi_calibration
LOGICAL, OPTIONAL :: gbcs_l1c_output
LOGICAL, OPTIONAL :: gbcs_l1c_cal
LOGICAL, OPTIONAL :: use_walton
LOGICAL, OPTIONAL :: keep_temp
LOGICAL, OPTIONAL :: write_fcdr
LOGICAL, OPTIONAL :: monte_carlo
TYPE(mc_delta_str), OPTIONAL, INTENT(INOUT) :: delta_radiance
INTEGER, OPTIONAL :: seedval
LOGICAL, OPTIONAL :: ocean_only
! Local variables
INTEGER :: I
INTEGER :: ncoefs
REAL :: coefs1(8,2)
REAL :: coefs2(8,2)
REAL :: coefs3(8,2)
REAL :: coefs_frac1
REAL :: coefs_frac2
REAL :: cal_coef_overlap
CHARACTER(LEN=10) :: height
CHARACTER(LEN=512) :: command_fcdr
CHARACTER(LEN=256) :: temp_file
LOGICAL :: use_iasi_cal
LOGICAL :: twelve_micron_there
LOGICAL :: l1c_output
LOGICAL :: l1c_output_cal
TYPE(FIDUCEO_Data) :: FCDR
LOGICAL :: usewalton
LOGICAL :: keeptemp
LOGICAL :: writefcdr
REAL, ALLOCATABLE :: covar_tot(:,:,:)
REAL, ALLOCATABLE :: covar3(:,:)
REAL, ALLOCATABLE :: covar4(:,:)
REAL, ALLOCATABLE :: covar5(:,:)
INTEGER :: nparam
INTEGER :: nparam3
INTEGER :: stat
LOGICAL :: montecarlo
TYPE(mc_delta_str) :: delta_bts
LOGICAL :: oceanonly
IF( .not. AVHRR%valid_data_there )THEN
RETURN
ENDIF
IF( PRESENT(use_iasi_calibration) )THEN
use_iasi_cal = use_iasi_calibration
ELSE
use_iasi_cal = .FALSE.
ENDIF
IF( PRESENT(gbcs_l1c_output) )THEN
l1c_output = gbcs_l1c_output
ELSE
l1c_output = .FALSE.
ENDIF
IF( PRESENT(gbcs_l1c_cal) )THEN
l1c_output_cal = gbcs_l1c_cal
ELSE
l1c_output_cal = .FALSE.
ENDIF
IF( PRESENT(use_walton) )THEN
usewalton = use_walton
ELSE
usewalton = .FALSE.
ENDIF
IF( PRESENT(keep_temp) )THEN
keeptemp = keep_temp
ELSE
keeptemp = .FALSE.
ENDIF
IF( PRESENT(write_fcdr) )THEN
writefcdr = write_fcdr
ELSE
writefcdr = .TRUE.
ENDIF
IF( PRESENT(monte_carlo) )THEN
montecarlo = monte_carlo
ELSE
montecarlo = .FALSE.
ENDIF
IF( PRESENT(ocean_only) )THEN
oceanonly = ocean_only
ELSE
oceanonly = .FALSE.
ENDIF
IF( montecarlo .and. .not. PRESENT(delta_radiance) .and. &
.not. PRESENT(seedval) )THEN
CALL Gbcs_Critical(.TRUE.,'delta_radiance needs to be set for montecarlo',&
'Add_FIDUCEO_Uncert','fiduceo_uncertainties.f90')
ENDIF
call Get_Calib_Coefficients_FIDUCEO( IMG, AVHRR%time(AVHRR%start_valid), &
AVHRR%AVHRR_No, coefs1, coefs2, coefs3, ncoefs, coefs_frac1, &
coefs_frac2, twelve_micron_there, covar=covar_tot, nparam=nparam, &
nparam3=nparam3)
ALLOCATE(covar3(nparam3,nparam3),covar4(nparam,nparam),&
covar5(nparam,nparam),stat=stat)
IF( 0 .ne. stat )THEN
CALL Gbcs_Critical(.TRUE.,'Cannot allocate covar3/4/5',&
'Add_FIDUCEO_Uncert','fiduceo_uncertainties.f90')
ENDIF
covar3 = covar_tot(1:nparam3,1:nparam3,1)
covar4 = covar_tot(1:nparam,1:nparam,2)
covar5 = covar_tot(1:nparam,1:nparam,3)
! FCDR%nuc = (/coefs1(5,1),coefs2(5,1),coefs3(5,1)/)
! FCDR%aval = (/coefs1(6,1),coefs2(6,1),coefs3(6,1)/)
! FCDR%bval = (/coefs1(7,1),coefs2(7,1),coefs3(7,1)/)
IF( usewalton )THEN
coefs1(5,1) = AVHRR%nuc(1)
coefs1(6,1) = AVHRR%aval(1)
coefs1(7,1) = AVHRR%bval(1)
coefs2(5,1) = AVHRR%nuc(2)
coefs2(6,1) = AVHRR%aval(2)
coefs2(7,1) = AVHRR%bval(2)
coefs3(5,1) = AVHRR%nuc(3)
coefs3(6,1) = AVHRR%aval(3)
coefs3(7,1) = AVHRR%bval(3)
ENDIF
!
! make sure coefs are correct for the form of Rad->Temp and Temp-Rad
!
IF( coefs1(6,1) .gt. 0 )THEN
coefs1(6,1) = -coefs1(6,1)/coefs1(7,1)
coefs1(7,1) = 1./coefs1(7,1)
coefs2(6,1) = -coefs2(6,1)/coefs2(7,1)
coefs2(7,1) = 1./coefs2(7,1)
coefs3(6,1) = -coefs3(6,1)/coefs3(7,1)
coefs3(7,1) = 1./coefs3(7,1)
ENDIF
FCDR%nuc = AVHRR%nuc
FCDR%aval = AVHRR%aval
FCDR%bval = AVHRR%bval
!
! Recalculate radiances using potentially more complex models than
! available in the Level 1B reader
!
!
! Do FIDUCEO Uncertainties
!
DO I=1,AVHRR%arraySize
CALL Get_Sensitivities(I,AVHRR,coefs1,coefs2,coefs3,FCDR,&
twelve_micron_there)
CALL calculate_urict(I,AVHRR,coefs1,coefs2,coefs3,FCDR,&
twelve_micron_there)
CALL radiance_uncertainties(I,AVHRR,coefs1,coefs2,coefs3,FCDR,&
twelve_micron_there,nparam3,nparam,covar3,covar4,covar5)
END DO
!
! Get Quality flags
!
CALL Get_Quality_Flags(AVHRR,FCDR)
!
! If monte-carlo data, then calculate delta BTs
! Forces use of same coefficients
!
IF( montecarlo )THEN
CALL Get_Delta_BTs(AVHRR,delta_radiance,coefs1,coefs2,coefs3,&
delta_bts,twelve_micron_there)
CALL Deallocate_delta(delta_radiance)
ENDIF
!
! If GBCS output - write out
!
IF( l1c_output )THEN
CALL Write_GBCS_L1C(AVHRR,FCDR,uuid_in,twelve_micron_there,&
l1c_output_cal)
ELSE
!
! write to NetCDF
!
temp_file=TRIM(uuid_in)//'.nc'
CALL Write_Temp_NETCDF(temp_file,AVHRR,FCDR,twelve_micron_there,&
montecarlo,delta_bts,seedval)
! CALL Rescale(AVHRR,FCDR)
IF( writefcdr )THEN
!
! Because of Gerrit's CURUC routines needing Python 3 but pyGAC
! needed Python 2 we have to run a script here that runs the
! write easy python code after switching environments
!
IF( 'None' .eq. filename_nc )THEN
! command_fcdr ='python2.7 write_easy_fcdr_from_netcdf.py '//TRIM(temp_file)
IF( oceanonly )THEN
command_fcdr = './write_easy_fcdr.sh '//TRIM(temp_file)//' --ocean'
ELSE
command_fcdr = './write_easy_fcdr.sh '//TRIM(temp_file)
ENDIF
ELSE
! command_fcdr ='python2.7 write_easy_fcdr_from_netcdf.py '//TRIM(temp_file)//' '//TRIM(filename_nc)
IF( oceanonly )THEN
command_fcdr = './write_easy_fcdr.sh '//TRIM(temp_file)//' --output '//TRIM(filename_nc)//' --ocean'
ELSE
command_fcdr = './write_easy_fcdr.sh '//TRIM(temp_file)//' --output '//TRIM(filename_nc)
ENDIF
ENDIF
call SYSTEM(TRIM(command_fcdr))
ENDIF
IF( .not. keeptemp )THEN
command_fcdr = 'rm -f '//TRIM(temp_file) !MT: 05-11-2017: comment to keep temp netcdf files
call SYSTEM(TRIM(command_fcdr))
ENDIF
! print*, "remplissage"
! Which is French for "filling"
! call fill_netcdf(filename_nc,AVHRR,FCDR)
ENDIF
DEALLOCATE(covar3,covar4,covar5)
END SUBROUTINE Add_FIDUCEO_Uncert
!
! Deallocate delta arrays
!
SUBROUTINE Deallocate_delta(delta)
TYPE(mc_delta_str), INTENT(INOUT) :: delta
! Local variables
INTEGER :: STAT
DEALLOCATE(delta%ch1,delta%ch2,delta%ch3a,delta%ch3,delta%ch4,delta%ch5,&
STAT=STAT)
IF( 0 .ne. STAT )THEN
CALL Gbcs_Critical(.TRUE.,'Cannot DEALLOCATE delta',&
'Deallocate_delta','fiduceo_uncertainties.f90')
ENDIF
END SUBROUTINE Deallocate_delta
!
! Get deltaBTs from Monte-Carlo runs
! Note we have delta radiances so use the derivative of the Planck
! function to get delta B
!
SUBROUTINE Get_Delta_BTs(AVHRR,delta_radiance,coefs1,coefs2,coefs3,&
delta_bts,twelve_micron_there)
TYPE(AVHRR_Data), INTENT(IN) :: AVHRR
TYPE(mc_delta_str), INTENT(IN) :: delta_radiance
REAL, INTENT(IN) :: coefs1(8,2)
REAL, INTENT(IN) :: coefs2(8,2)
REAL, INTENT(IN) :: coefs3(8,2)
TYPE(mc_delta_str), INTENT(OUT) :: delta_bts
LOGICAL, INTENT(IN) :: twelve_micron_there
! Local variables
INTEGER :: I,J,K
INTEGER :: STAT
REAL :: nrmin
REAL :: nrmax
REAL :: trmin
REAL :: trmax
REAL :: newBT37
REAL :: newBT11
REAL :: newBT12
!
! Check AVHRR/delta_radiance inputs
!
IF( AVHRR%arraySize .ne. delta_radiance%nscan .or. &
AVHRR%nelem .ne. delta_radiance%nelem )THEN
print *,AVHRR%nelem,delta_radiance%nelem
print *,AVHRR%arraySize,delta_radiance%nscan
CALL Gbcs_Critical(.TRUE.,'Mismatch in size',&
'Get_Delta_BTs','fiduceo_uncertainties.f90')
ENDIF
!
! Allocate BT arrays
!
delta_bts%nelem = delta_radiance%nelem
delta_bts%nscan = delta_radiance%nscan
delta_bts%nmc = delta_radiance%nmc
ALLOCATE(delta_bts%ch1(delta_bts%nelem,delta_bts%nscan,delta_bts%nmc),&
delta_bts%ch2(delta_bts%nelem,delta_bts%nscan,delta_bts%nmc),&
delta_bts%ch3a(delta_bts%nelem,delta_bts%nscan,delta_bts%nmc),&
delta_bts%ch3(delta_bts%nelem,delta_bts%nscan,delta_bts%nmc),&
delta_bts%ch4(delta_bts%nelem,delta_bts%nscan,delta_bts%nmc),&
delta_bts%ch5(delta_bts%nelem,delta_bts%nscan,delta_bts%nmc),&
STAT=STAT)
IF( 0 .ne. STAT )THEN
CALL Gbcs_Critical(.TRUE.,'Allocation error delta_bts',&
'Get_Delta_BTs','fiduceo_uncertainties.f90')
ENDIF
!
! Copy over vis chan deltas (needed in radiance space)
!
delta_bts%ch1 = delta_radiance%ch1
delta_bts%ch2 = delta_radiance%ch2
delta_bts%ch3a = delta_radiance%ch3a
!
! Get delta BTs
!
DO J=1,delta_bts%nscan
DO K=1,delta_bts%nelem
!
! As this is constant over all MC runs calculate here
!
IF( AVHRR%new_array3B(k,j) .gt. 0 )THEN
newBT37 = convertBT(AVHRR%new_array3B(k,j),DBLE(coefs1(5,1)), &
DBLE(coefs1(6,1)), DBLE(coefs1(7,1)))
ENDIF
IF( AVHRR%new_array4(k,j) .gt. 0 )THEN
newBT11 = convertBT(AVHRR%new_array4(k,j),DBLE(coefs2(5,1)), &
DBLE(coefs2(6,1)), DBLE(coefs2(7,1)))
ENDIF
IF( AVHRR%new_array5(k,j) .gt. 0 )THEN
newBT12 = convertBT(AVHRR%new_array5(k,j),DBLE(coefs3(5,1)), &
DBLE(coefs3(6,1)), DBLE(coefs3(7,1)))
ENDIF
!
! Loop round MC runs
!
DO I=1,delta_bts%nmc
IF( -1e20 .lt. delta_radiance%ch3(k,j,i) )THEN
nrmax=AVHRR%new_array3B(k,j)+delta_radiance%ch3(k,j,i)
IF( nrmax .gt. 0 )THEN
delta_bts%ch3(k,j,i)=convertBT(nrmax,DBLE(coefs1(5,1)), &
DBLE(coefs1(6,1)), DBLE(coefs1(7,1)))-&
newBT37
ELSE
delta_bts%ch3(k,j,i) = NAN_R
ENDIF
ELSE
delta_bts%ch3(k,j,i) = NAN_R
ENDIF
IF( -1e20 .lt. delta_radiance%ch4(k,j,i) )THEN
nrmax=AVHRR%new_array4(k,j)+delta_radiance%ch4(k,j,i)
IF( nrmax .gt. 0 )THEN
delta_bts%ch4(k,j,i)=convertBT(nrmax,DBLE(coefs2(5,1)), &
DBLE(coefs2(6,1)), DBLE(coefs2(7,1)))-&
newBT11
ELSE
delta_bts%ch4(k,j,i) = NAN_R
ENDIF
ELSE
delta_bts%ch4(k,j,i) = NAN_R
ENDIF
IF( twelve_micron_there )THEN
IF( -1e20 .lt. delta_radiance%ch5(k,j,i) )THEN
nrmax=AVHRR%new_array5(k,j)+delta_radiance%ch5(k,j,i)
IF( nrmax .gt. 0 )THEN
delta_bts%ch5(k,j,i)=convertBT(nrmax,DBLE(coefs3(5,1)), &
DBLE(coefs3(6,1)), DBLE(coefs3(7,1)))-&
newBT12
ELSE
delta_bts%ch5(k,j,i) = NAN_R
ENDIF
ELSE
delta_bts%ch5(k,j,i) = NAN_R
ENDIF
ELSE
delta_bts%ch5(k,j,i) = NAN_R
ENDIF
END DO
END DO
END DO
END SUBROUTINE Get_Delta_BTs
!
! Is sun in earth view?
!
LOGICAL FUNCTION check_for_solar_earthview(AVHRR,y)RESULT(solar_there)
TYPE(AVHRR_Data), INTENT(IN) :: AVHRR
INTEGER, INTENT(IN) :: y
! Local variables
INTEGER :: x
solar_there = .FALSE.
DO x=1,AVHRR%nelem
IF( AVHRR%solza(x,y) .ge. 102 .and. AVHRR%array1(x,y) > 0.05 )THEN
solar_there = .TRUE.
ENDIF
END DO
END FUNCTION check_for_solar_earthview
!
! Work out quality flags from data in AVHRR_Data structure
!
SUBROUTINE Get_Quality_Flags(AVHRR,FCDR)
TYPE(AVHRR_Data), INTENT(IN) :: AVHRR
TYPE(FIDUCEO_Data), INTENT(INOUT) :: FCDR
! Local variables
INTEGER :: I,J
INTEGER :: STAT
!
! Allocate and set quality bitmasks
!
ALLOCATE(FCDR%quality_channel_bitmask(6,AVHRR%arraySize),&
FCDR%quality_scanline_bitmask(AVHRR%arraySize),&
STAT=STAT)
FCDR%quality_channel_bitmask = 0
FCDR%quality_scanline_bitmask = 0
!
! Scanline level flags
!
DO I=1,AVHRR%arraySize
IF( AVHRR%badTop(I) )THEN
FCDR%quality_scanline_bitmask(I) = &
IBSET(FCDR%quality_scanline_bitmask(I),0)
ENDIF
IF( AVHRR%badTime(I) )THEN
FCDR%quality_scanline_bitmask(I) = &
IBSET(FCDR%quality_scanline_bitmask(I),1)
ENDIF
IF( AVHRR%badNavigation(I) )THEN
FCDR%quality_scanline_bitmask(I) = &
IBSET(FCDR%quality_scanline_bitmask(I),2)
ENDIF
IF( AVHRR%badCalibration(I) )THEN
FCDR%quality_scanline_bitmask(I) = &
IBSET(FCDR%quality_scanline_bitmask(I),3)
ENDIF
!
! Channel 3A present
!
IF( ANY(AVHRR%array3A(:,I) .ge. 0) )THEN
FCDR%quality_scanline_bitmask(I) = &
IBSET(FCDR%quality_scanline_bitmask(I),4)
ENDIF
! !
! ! Solar contamination failure
! !
! IF( AVHRR%solar_contamination_failure(I) )THEN
! FCDR%quality_scanline_bitmask(I) = &
! IBSET(FCDR%quality_scanline_bitmask(I),5)
! ENDIF
!
! Solar contamination
!
IF( AVHRR%solar_contamination_3B(I) )THEN
FCDR%quality_scanline_bitmask(I) = &
IBSET(FCDR%quality_scanline_bitmask(I),5)
ENDIF
!
! Sun in earth view
!
IF( check_for_solar_earthview(AVHRR,I) )THEN
FCDR%quality_scanline_bitmask(I) = &
IBSET(FCDR%quality_scanline_bitmask(I),6)
ENDIF
END DO
!
! Channel level quality flags
!
DO I=1,AVHRR%arraySize
IF( ALL(AVHRR%array1(:,I) .lt. 0) )THEN
FCDR%quality_channel_bitmask(1,I) = &
IBSET(FCDR%quality_channel_bitmask(1,I),0)
ENDIF
IF( ALL(AVHRR%array2(:,I) .lt. 0) )THEN
FCDR%quality_channel_bitmask(2,I) = &
IBSET(FCDR%quality_channel_bitmask(2,I),0)
ENDIF
IF( ALL(AVHRR%array3A(:,I) .lt. 0) )THEN
FCDR%quality_channel_bitmask(3,I) = &
IBSET(FCDR%quality_channel_bitmask(3,I),0)
ENDIF
IF( ALL(AVHRR%array3B(:,I) .lt. 0) )THEN
FCDR%quality_channel_bitmask(4,I) = &
IBSET(FCDR%quality_channel_bitmask(4,I),0)
ENDIF
IF( ALL(AVHRR%array4(:,I) .lt. 0) )THEN
FCDR%quality_channel_bitmask(5,I) = &
IBSET(FCDR%quality_channel_bitmask(5,I),0)
ENDIF
IF( ALL(AVHRR%array5(:,I) .lt. 0) )THEN
FCDR%quality_channel_bitmask(6,I) = &
IBSET(FCDR%quality_channel_bitmask(6,I),0)
ENDIF
DO J=4,6
IF( 1 .eq. FCDR%flag_no_detection(J-3,I) )THEN
FCDR%quality_channel_bitmask(J,I) = &
IBSET(FCDR%quality_channel_bitmask(J,I),1)
ENDIF
END DO
END DO
END SUBROUTINE Get_Quality_Flags
!
! Return a string for sensor
!
SUBROUTINE noaa_name(AVHRR,noaa_string,noaa)
TYPE(AVHRR_Data), INTENT(IN) :: AVHRR
CHARACTER(LEN=*), INTENT(OUT) :: noaa_string
LOGICAL, OPTIONAL :: noaa
! Local variables
LOGICAL :: noaa_type
IF( PRESENT(noaa) )THEN
noaa_type = noaa
ELSE
noaa_type = .TRUE.
ENDIF
IF( noaa_type )THEN
IF( AVHRR%AVHRR_No .eq. 1 )THEN
noaa_string = 'TIROSN'
ELSE IF( AVHRR%AVHRR_No .eq. 6 )THEN
noaa_string = 'NOAA06'
ELSE IF( AVHRR%AVHRR_No .eq. 7 )THEN
noaa_string = 'NOAA07'
ELSE IF( AVHRR%AVHRR_No .eq. 8 )THEN
noaa_string = 'NOAA08'
ELSE IF( AVHRR%AVHRR_No .eq. 9 )THEN
noaa_string = 'NOAA09'
ELSE IF( AVHRR%AVHRR_No .eq. 10 )THEN
noaa_string = 'NOAA10'
ELSE IF( AVHRR%AVHRR_No .eq. 11 )THEN
noaa_string = 'NOAA11'
ELSE IF( AVHRR%AVHRR_No .eq. 12 )THEN
noaa_string = 'NOAA12'
ELSE IF( AVHRR%AVHRR_No .eq. 14 )THEN
noaa_string = 'NOAA14'
ELSE IF( AVHRR%AVHRR_No .eq. 15 )THEN
noaa_string = 'NOAA15'
ELSE IF( AVHRR%AVHRR_No .eq. 16 )THEN
noaa_string = 'NOAA16'
ELSE IF( AVHRR%AVHRR_No .eq. 17 )THEN
noaa_string = 'NOAA17'
ELSE IF( AVHRR%AVHRR_No .eq. 18 )THEN
noaa_string = 'NOAA18'
ELSE IF( AVHRR%AVHRR_No .eq. 19 )THEN
noaa_string = 'NOAA19'
ELSE IF( AVHRR%AVHRR_No .eq. -1 )THEN
noaa_string = 'METOPA'
ELSE IF( AVHRR%AVHRR_No .eq. -2 )THEN
noaa_string = 'METOPB'
ELSE IF( AVHRR%AVHRR_No .eq. -3 )THEN
noaa_string = 'METOPC'
ELSE
CALL Gbcs_Critical(.TRUE.,'Cannot match AVHRR_No','noaa_name',&
'fiduceo_uncertainties.f90')
ENDIF
ELSE
IF( AVHRR%AVHRR_No .eq. 1 )THEN
noaa_string = 'AVHRRTN_G'
ELSE IF( AVHRR%AVHRR_No .eq. 6 )THEN
noaa_string = 'AVHRR06_G'
ELSE IF( AVHRR%AVHRR_No .eq. 7 )THEN
noaa_string = 'AVHRR07_G'
ELSE IF( AVHRR%AVHRR_No .eq. 8 )THEN
noaa_string = 'AVHRR08_G'
ELSE IF( AVHRR%AVHRR_No .eq. 9 )THEN
noaa_string = 'AVHRR09_G'
ELSE IF( AVHRR%AVHRR_No .eq. 10 )THEN
noaa_string = 'AVHRR10_G'
ELSE IF( AVHRR%AVHRR_No .eq. 11 )THEN
noaa_string = 'AVHRR11_G'
ELSE IF( AVHRR%AVHRR_No .eq. 12 )THEN
noaa_string = 'AVHRR12_G'
ELSE IF( AVHRR%AVHRR_No .eq. 14 )THEN
noaa_string = 'AVHRR14_G'
ELSE IF( AVHRR%AVHRR_No .eq. 15 )THEN
noaa_string = 'AVHRR15_G'
ELSE IF( AVHRR%AVHRR_No .eq. 16 )THEN
noaa_string = 'AVHRR16_G'
ELSE IF( AVHRR%AVHRR_No .eq. 17 )THEN
noaa_string = 'AVHRR17_G'
ELSE IF( AVHRR%AVHRR_No .eq. 18 )THEN
noaa_string = 'AVHRR18_G'
ELSE IF( AVHRR%AVHRR_No .eq. 19 )THEN
noaa_string = 'AVHRR19_G'
ELSE IF( AVHRR%AVHRR_No .eq. -1 )THEN
noaa_string = 'AVHRRMTA_G'
ELSE IF( AVHRR%AVHRR_No .eq. -2 )THEN
noaa_string = 'AVHRRMTB_G'
ELSE IF( AVHRR%AVHRR_No .eq. -3 )THEN
noaa_string = 'AVHRRMTC_G'
ELSE
CALL Gbcs_Critical(.TRUE.,'Cannot match AVHRR_No','noaa_name',&
'fiduceo_uncertainties.f90')
ENDIF
ENDIF
END SUBROUTINE noaa_name
!
! Write a tempory netcdf file for python to then convert
! Different from Marine's original version
!
SUBROUTINE Write_Temp_NETCDF(temp_file,AVHRR,FCDR,twelve_micron_there,&
monte_carlo,delta_bts,seedval)
CHARACTER(LEN=*), INTENT(IN) :: temp_file
TYPE(AVHRR_Data), INTENT(IN) :: AVHRR
TYPE(FIDUCEO_Data), INTENT(IN) :: FCDR
LOGICAL, INTENT(IN) :: twelve_micron_there
LOGICAL, INTENT(IN) :: monte_carlo
TYPE(mc_delta_str), INTENT(INOUT) :: delta_bts
INTEGER, INTENT(IN) :: seedval
! Local variables
INTEGER :: I
INTEGER :: ncid
INTEGER :: latitude_varid
INTEGER :: longitude_varid
INTEGER :: time_varid
INTEGER :: year_varid
INTEGER :: month_varid
INTEGER :: day_varid
INTEGER :: hours_varid
INTEGER :: satza_varid
INTEGER :: solza_varid
INTEGER :: relaz_varid
INTEGER :: ch1_varid
INTEGER :: ch2_varid
INTEGER :: ch3a_varid
INTEGER :: ch3b_varid
INTEGER :: ch4_varid
INTEGER :: ch5_varid
INTEGER :: ch1_random_varid
INTEGER :: ch2_random_varid
INTEGER :: ch3a_random_varid
INTEGER :: ch3b_random_varid
INTEGER :: ch4_random_varid
INTEGER :: ch5_random_varid
INTEGER :: ch1_non_random_varid
INTEGER :: ch2_non_random_varid
INTEGER :: ch3a_non_random_varid
INTEGER :: ch3b_non_random_varid
INTEGER :: ch4_non_random_varid
INTEGER :: ch5_non_random_varid
INTEGER :: ch1_common_varid
INTEGER :: ch2_common_varid
INTEGER :: ch3a_common_varid
INTEGER :: ch3b_common_varid
INTEGER :: ch4_common_varid
INTEGER :: ch5_common_varid
INTEGER :: scan_qual_varid
INTEGER :: chan_qual_varid
INTEGER :: dBT3_over_dT_varid
INTEGER :: dBT4_over_dT_varid
INTEGER :: dBT5_over_dT_varid
INTEGER :: dRe1_over_dCS_varid
INTEGER :: dRe2_over_dCS_varid
INTEGER :: dRe3a_over_dCS_varid
INTEGER :: dBT3_over_dCS_varid
INTEGER :: dBT4_over_dCS_varid
INTEGER :: dBT5_over_dCS_varid
INTEGER :: dBT3_over_dCICT_varid
INTEGER :: dBT4_over_dCICT_varid
INTEGER :: dBT5_over_dCICT_varid
INTEGER :: cal_cnts_varid
INTEGER :: earth_cnts_varid
INTEGER :: nuc_varid
INTEGER :: aval_varid
INTEGER :: bval_varid
INTEGER :: scanline_varid
INTEGER :: oscanline_varid
INTEGER :: smoothprt_varid
INTEGER :: badNavigation_varid
INTEGER :: badCalibration_varid
INTEGER :: badTime_varid
INTEGER :: missingLines_varid
INTEGER :: solar3_varid
INTEGER :: solar4_varid
INTEGER :: solar5_varid
INTEGER :: ch3a_there_varid
INTEGER :: ch3b_harm_varid
INTEGER :: ch4_harm_varid
INTEGER :: ch5_harm_varid
INTEGER :: ch1_MC_varid
INTEGER :: ch2_MC_varid
INTEGER :: ch3a_MC_varid
INTEGER :: ch3_MC_varid
INTEGER :: ch4_MC_varid
INTEGER :: ch5_MC_varid
INTEGER :: stat
INTEGER :: dimid_nx
INTEGER :: dimid_ny
INTEGER :: dimid_ir
INTEGER :: dimid_band
INTEGER :: dimid_mc
INTEGER :: dims1(1)
INTEGER :: dims2(2)
INTEGER :: dims3(3)
INTEGER :: dims_band(1)
INTEGER(GbcsInt1), ALLOCATABLE :: badNav(:)
INTEGER(GbcsInt1), ALLOCATABLE :: badCal(:)
INTEGER(GbcsInt1), ALLOCATABLE :: badTime(:)
INTEGER(GbcsInt1), ALLOCATABLE :: missingLines(:)
INTEGER(GbcsInt1), ALLOCATABLE :: solar3(:)
INTEGER(GbcsInt1), ALLOCATABLE :: solar4(:)
INTEGER(GbcsInt1), ALLOCATABLE :: solar5(:)
REAL :: noise_cnts(6)
REAL :: earth_noise_cnts(6)
REAL :: min_ict
REAL :: max_ict
iNTEGER :: nsolar
INTEGER :: compress_level=5
CHARACTER(LEN=512) :: noaa_string
stat = NF90_CREATE(temp_file,IOR(NF90_HDF5,NF90_CLOBBER),ncid)
call check(stat,1)
stat = NF90_DEF_DIM(ncid,'nx',AVHRR%nelem,dimid_nx)
call check(stat,2)
stat = NF90_DEF_DIM(ncid,'ny',AVHRR%arraySize,dimid_ny)
call check(stat,3)
stat = NF90_DEF_DIM(ncid,'nir',6,dimid_ir)
call check(stat,4)
stat = NF90_DEF_DIM(ncid,'nband_coef',3,dimid_band)
call check(stat,5)
if( monte_carlo )THEN
stat = NF90_DEF_DIM(ncid,'n_montecarlo',delta_bts%nmc,dimid_mc)
call check(stat,6)
ENDIF
dims1(1) = dimid_ny
dims2(1) = dimid_nx
dims2(2) = dimid_ny
stat = NF90_DEF_VAR(ncid,'latitude',NF90_FLOAT,dims2,latitude_varid)
call check(stat,7)
stat = NF90_DEF_VAR_DEFLATE(ncid, latitude_varid, 1, 1, compress_level)
call check(stat,8)
stat = NF90_DEF_VAR_FILL(ncid, latitude_varid, 0, NAN_R)
call check(stat,9)
!
! Write variables including define - ensures valid min/max held
!
stat = NF90_DEF_VAR(ncid,'longitude',NF90_FLOAT,dims2,longitude_varid)
call check(stat,10)
stat = NF90_DEF_VAR_DEFLATE(ncid, longitude_varid, 1, 1, compress_level)
call check(stat,11)
stat = NF90_DEF_VAR_FILL(ncid, longitude_varid, 0, NAN_R)
call check(stat,12)
stat = NF90_DEF_VAR(ncid,'time',NF90_DOUBLE,dims1,time_varid)
call check(stat,13)
stat = NF90_DEF_VAR_DEFLATE(ncid, time_varid, 1, 1, compress_level)
call check(stat,14)
stat = NF90_DEF_VAR_FILL(ncid, time_varid, 0, NAN_R)