-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLSTR_Preprocessor.f90
1745 lines (1575 loc) · 54.4 KB
/
SLSTR_Preprocessor.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
!------------------------------------------------------------------------------
!M+
! NAME:
! SLSTR_Preprocessor
!
! PURPOSE:
!> Regrid Vis and NIR observations at 1km using nearest neighbourhood and
!> including orphan pixels
!
! CATEGORY:
! Data I/O
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! USE SLSTR_Preprocessor
!
! PUBLIC DATA:
! None
!
! MODULES:
! GbcsPath
!
! CONTAINS:
! handle_err
! safe_open
! safe_get_real_data
! safe_get_real_attribute
! safe_get_int_data
! safe_get_dimlen
! insert_neighbour
! find_neighbours
! build_neighbourhood_map
! apply_function
! apply_neighbours
! apply_simple_aggregation
! load_radiance_data
! process_scene_band
! align_cosmetics
! merge_neighbourhoods
! compute_scene_neighbourhood
!
! DERIVED TYPES:
! NEIGHBOURHOOD_ENTRY
! NEIGHBOURHOOD_MAP
! Locations
!
! NOTES:
!
! CREATION HISTORY:
! Written by: Niall McCarroll 12/01/21
! University of Reading
!
! Copyright 2021 Owen Embury, and Claire Bulgin
! Department of Meteorology, University of Reading, UK
!
! This file is part of the GBCS software package.
!
! The GBCS software package 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.
!
! The GBCS software package 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.
!
! You should have received a copy of the GNU General Public License
! along with the GBCS software package.
! If not, see <http://www.gnu.org/licenses/>.
!
!M-
!--------------------------------------------------------------------------------------------
MODULE SLSTR_Preprocessor
IMPLICIT NONE
PRIVATE
PUBLIC :: NEIGHBOURHOOD_MAP
PUBLIC :: compute_scene_neighbourhood, merge_neighbourhoods, process_scene_band
PUBLIC :: INVALID_PIXEL, MISSING_I, MISSING_R
PUBLIC :: MAIN_PIXEL_SOURCE_A, MAIN_PIXEL_SOURCE_B, ORPHAN_PIXEL_SOURCE_A, ORPHAN_PIXEL_SOURCE_B
PUBLIC :: MAX_K_NEAREST_NEIGHBOURS, MAX_NEIGHBOUR_DISTANCE
PUBLIC :: FUNCTION_MEAN, FUNCTION_SD, FUNCTION_MAX, FUNCTION_MIN_MAX_DIFF
! ----------------
! Module variables
! ----------------
!> Real number to represent missing values in I/O.
REAL :: MISSING_R = -1.0e+30
!> Integer value to represent missing values in I/O.
INTEGER :: MISSING_I = -32768
!> Exclude neighbours further away than this (metres).
REAL :: MAX_NEIGHBOUR_DISTANCE = 10000
!> Define a search window around the area (2*x,2*y) in which to look for closest visible pixels
INTEGER :: search_width = 8, search_height = 6 ! Search window size, in pixels
! -----------------
! Module parameters
! -----------------
!> Whether to perform extra computation to process cosmetically filled IR pixels the same as the original pixels
LOGICAL :: align_cosmetic_pixels = .false.
!> Maximum neighbourhood size.
INTEGER, PARAMETER :: MAX_K_NEAREST_NEIGHBOURS = 15
!> Mask to use to check if a pixel is cosmetically filled, in flags_ao.nc/flags_an.nc
INTEGER, PARAMETER :: COSMETIC_PIXEL_MASK = 256 ! Identify COSMETIC pixels with these bits flagged
!> internal: code for an invalid pixel index
INTEGER, PARAMETER :: INVALID_PIXEL = -1
!> internal: code for the source of a neighbourhood pixel from main dataset (stripe A)
INTEGER, PARAMETER :: MAIN_PIXEL_SOURCE_A = 0
!> internal: code for the source of a neighbourhood pixel from orphan dataset (stripe A)
INTEGER, PARAMETER :: ORPHAN_PIXEL_SOURCE_A = 1
!> internal: code for the source of a neighbourhood pixel from main dataset (stripe B)
INTEGER, PARAMETER :: MAIN_PIXEL_SOURCE_B = 2
!> internal: code for the source of a neighbourhood pixel from orphan dataset (stripe B)
INTEGER, PARAMETER :: ORPHAN_PIXEL_SOURCE_B = 3
!> Allowed confidence code for visible pixels
INTEGER, PARAMETER :: EXCEPTION_SATURATION = 16
!> Constant representing the mean function
INTEGER, PARAMETER :: FUNCTION_MEAN = 1
!> Constant representing the std deviation function
INTEGER, PARAMETER :: FUNCTION_SD = 2
!> Constant representing the max function
INTEGER, PARAMETER :: FUNCTION_MAX = 3
!> Constant representing the max-min function
INTEGER, PARAMETER :: FUNCTION_MIN_MAX_DIFF = 4
! -------------
! Derived Types
! -------------
!> Define a structure grouping three arrays, organised by K into a neighbourhood
TYPE NEIGHBOURHOOD_ENTRY
INTEGER, DIMENSION(MAX_K_NEAREST_NEIGHBOURS) :: x = INVALID_PIXEL !< Image coordinate
INTEGER, DIMENSION(MAX_K_NEAREST_NEIGHBOURS) :: y = INVALID_PIXEL !< Image coordinate
INTEGER, DIMENSION(MAX_K_NEAREST_NEIGHBOURS) :: source = INVALID_PIXEL !< Pixel source (a, b, orphan etc.)
REAL, DIMENSION(MAX_K_NEAREST_NEIGHBOURS) :: d = 0 !< Distance
INTEGER :: n = 0 !< Number of pixels in neighbourhood
END TYPE NEIGHBOURHOOD_ENTRY
!> Define a neighbourhood as an array of entries and metadata on which stripe(s) the neighbourhood is built on
TYPE NEIGHBOURHOOD_MAP
INTEGER :: width = 0
INTEGER :: height = 0
LOGICAL :: include_a_stripe = .False.
LOGICAL :: include_b_stripe = .False.
TYPE(NEIGHBOURHOOD_ENTRY), ALLOCATABLE, DIMENSION(:,:) :: entries
END TYPE NEIGHBOURHOOD_MAP
!> Define a structure to hold the pixel across (x) and along (y) track distances for each (col,row)
TYPE Locations
INTEGER :: width = 0
INTEGER :: height = 0
REAL, ALLOCATABLE, DIMENSION(:,:) :: x !< Across track pixel coordinate
REAL, ALLOCATABLE, DIMENSION(:,:) :: y !< Along track pixel coordinate
INTEGER, ALLOCATABLE, DIMENSION(:,:) :: conf !< Confidence flags
END TYPE Locations
!> Structure to hold input radiances, both image and orphan grids
TYPE Radiances
INTEGER :: width = 0
INTEGER :: height = 0
REAL, DIMENSION(:,:), ALLOCATABLE :: image !< Image pixels
REAL, DIMENSION(:,:), ALLOCATABLE :: orphan !< Orphan pixels
INTEGER, DIMENSION(:,:), ALLOCATABLE :: status !< Status flags
END TYPE Radiances
CONTAINS
!------------------------------------------------------------------------------
!S+
! NAME:
! handle_err
!
! PURPOSE:
!> Passed a status code returned from a netcdf module operation, if the code
!> indicates an error, print more information and stop the program
!
! CATEGORY:
! netCDF
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! CALL handle_err(code)
!
! ARGUMENTS:
!>@ARG{status, in, INTEGER} netCDF return status
!
! CALLS:
! nf90_strerror
!
! SIDE EFFECTS:
! Stops program if code indicates a netcdf module operation has failed
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 11/11/20 NM Creation
!S-
!------------------------------------------------------------------------------
SUBROUTINE handle_err(status, msg)
USE netcdf
IMPLICIT NONE
! -----------
! Arguments
! -----------
INTEGER, INTENT(in) :: status
CHARACTER(LEN=*), INTENT(in) :: msg
IF (status /= nf90_noerr) THEN
WRITE(*,*) nf90_strerror(status), msg
STOP 1
END IF
END SUBROUTINE handle_err
!------------------------------------------------------------------------------
!F+
! NAME:
! safe_open
!
! PURPOSE:
!> Open a netcdf4 file and return its fileid
!
! CATEGORY:
! netCDF
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! ncid = safe_open('/path/to/file.nc')
!
! ARGUMENTS:
!>@ARG{path, in, CHARACTER(LEN=*)} path of the file to be opened
!
! FUNCTION RESULT:
!>@RES{id, INTEGER} ID of opened file
!
! CALLS:
! nf90_open
!
! SIDE EFFECTS:
! Stops program if code indicates a netcdf module operation has failed
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 11/11/20 NM Creation
!F-
!------------------------------------------------------------------------------
FUNCTION safe_open(path)
USE netcdf
IMPLICIT NONE
! -----------
! Arguments
! -----------
CHARACTER(LEN=*), INTENT(IN) :: path
! ----------------
! Function Result
! ----------------
INTEGER :: safe_open
! ---------------
! Local Variables
! ---------------
INTEGER :: ncid
INTEGER :: status
status = nf90_open(path, NF90_NOWRITE, ncid)
CALL handle_err(status, path)
safe_open = ncid
END FUNCTION safe_open
!------------------------------------------------------------------------------
!S+
! NAME:
! safe_get_real_data
!
! PURPOSE:
!> Reads real values from an opened netcdf4 file into an allocated array
!
! CATEGORY:
! netCDF
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! CALL safe_get_real_data(ncid, name, values, fill_value)
!
! ARGUMENTS:
!>@ARG{ncid, in, INTEGER} ID of the opened file (returned from safe_open)
!>@ARG{name, in, CHARACTER (LEN = *)} name of the variable
!>@ARG{values, out, REAL\, DIMENSION(:\,:)} array of real values which will be read, organised by (COL,ROW)
!>@ARG{fill_value, in, REAL} a real value to represent missing values in the array
!
! CALLS:
! nf90_inq_varid
! nf90_get_var
! nf90_get_att
!
! SIDE EFFECTS:
! Stops program if code indicates a netcdf module operation has failed
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 11/11/20 NM Creation
!S-
!------------------------------------------------------------------------------
SUBROUTINE safe_get_real_data(ncid, name, values, fill_value)
USE netcdf
IMPLICIT NONE
! -----------
! Arguments
! -----------
INTEGER, INTENT(in) :: ncid
CHARACTER(LEN=*), INTENT(in) :: name
REAL, DIMENSION(:,:), INTENT(out) :: values
REAL, INTENT(in) :: fill_value
! ---------------
! Local Variables
! ---------------
REAL :: file_fill_value
INTEGER :: varid
INTEGER :: status
REAL :: scale_factor
status = nf90_inq_varid(ncid,name,varid)
CALL handle_err(status, name)
status = nf90_get_var(ncid,varid,values)
CALL handle_err(status, name)
status = nf90_get_att(ncid,varid,"scale_factor",scale_factor)
CALL handle_err(status, name)
status = nf90_get_att(ncid,varid,"_FillValue",file_fill_value)
CALL handle_err(status, name)
WHERE (values /= file_fill_value)
values = values * scale_factor
ELSE WHERE
values = fill_value
END WHERE
END SUBROUTINE safe_get_real_data
!------------------------------------------------------------------------------
!F+
! NAME:
! safe_get_real_attribute
!
! PURPOSE:
!> Reads real value for named attribute from field in an opened netcdf4 file
!
! CATEGORY:
! netCDF
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! value = safe_get_real_attribute(ncid, field_name, attr_name)
!
! ARGUMENTS:
!>@ARG{ncid, in, INTEGER} ID of the opened file (returned from safe_open)
!>@ARG{field_name, in, CHARACTER(LEN=*)} name of the variable
!>@ARG{attr_name, in, CHARACTER(LEN=*)} name of the attribute
!
! RETURNS:
!@RES{value, REAL} value of attribute
!
! CALLS:
! nf90_inq_varid
! nf90_get_att
!
! SIDE EFFECTS:
! Stops program if code indicates a netcdf module operation has failed
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 11/11/20 NM Creation
!F-
!------------------------------------------------------------------------------
FUNCTION safe_get_real_attribute(ncid, field_name, attr_name)
USE netcdf
IMPLICIT NONE
! -----------
! Arguments
! -----------
INTEGER, INTENT(in) :: ncid
CHARACTER(LEN=*), INTENT(in) :: field_name
CHARACTER(LEN=*), INTENT(in) :: attr_name
! ----------------
! Function Result
! ----------------
REAL :: safe_get_real_attribute
! ---------------
! Local Variables
! ---------------
REAL :: value
INTEGER :: varid
INTEGER :: status
status = nf90_inq_varid(ncid, field_name, varid)
CALL handle_err(status, field_name)
status = nf90_get_att(ncid, varid, attr_name, value)
CALL handle_err(status, field_name)
safe_get_real_attribute = value
END FUNCTION safe_get_real_attribute
!------------------------------------------------------------------------------
!S+
! NAME:
! safe_get_int_data
!
! PURPOSE:
!> Reads integer values from an opened netcdf4 file into an allocated array
!
! CATEGORY:
! netCDF
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! CALL safe_get_int_data(ncid, name, values, fill_value)
!
! ARGUMENTS:
!>@ARG{ncid, in, INTEGER} ID of the opened file (returned from safe_open)
!>@ARG{name, in, CHARACTER(LEN=*)} name of the variable
!>@ARG{values, out, INTEGER\, DIMENSION(:\,:)} array of integer values which will be read, organised by (COL,ROW)
!>@ARG{fill_value, in, INTEGER} an integer value to represent missing values in the array
!
! CALLS:
! nf90_inq_varid
! nf90_get_var
! nf90_get_att
!
! SIDE EFFECTS:
! Stops program if code indicates a netcdf module operation has failed
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 11/11/20 NM Creation
!S-
!------------------------------------------------------------------------------
SUBROUTINE safe_get_int_data(ncid, name, values, fill_value)
USE netcdf
IMPLICIT NONE
! -----------
! Arguments
! -----------
INTEGER, INTENT(in) :: ncid
CHARACTER(LEN=*), INTENT(in) :: name
INTEGER, DIMENSION(:,:), INTENT(out) :: values
INTEGER, INTENT(in) :: fill_value
! ---------------
! Local Variables
! ---------------
INTEGER :: varid
INTEGER :: status
INTEGER :: file_fill_value
status = nf90_inq_varid(ncid,name,varid)
CALL handle_err(status, name)
status = nf90_get_var(ncid,varid,values)
CALL handle_err(status, name)
status = nf90_get_att(ncid, varid, "_FillValue", file_fill_value)
IF (status == nf90_noerr) THEN
WHERE (values == file_fill_value)
values = fill_value
END WHERE
END IF
END SUBROUTINE safe_get_int_data
!------------------------------------------------------------------------------
!F+
! NAME:
! safe_get_dimlen
!
! PURPOSE:
!> Get the length of the specified dimension
! CATEGORY:
! netCDF
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! dimlen = safe_get_dimlen(ncid, name)
!
! ARGUMENTS:
!>@ARG{ncid, in, INTEGER} ID of the opened file (returned from safe_open)
!>@ARG{name, in, CHARACTER(LEN=*)} name of the variable
!
! CALLS:
! nf90_inq_dimid
! nf90_inquire_dimension
!F-
!------------------------------------------------------------------------------
FUNCTION safe_get_dimlen(ncid, name)
USE netcdf
IMPLICIT NONE
! ---------
! Arguments
! ---------
INTEGER, INTENT(in) :: ncid
CHARACTER(LEN=*), INTENT(in) :: name
! ---------
! Function result
! ---------
INTEGER :: safe_get_dimlen
! ---------------
! Local variables
! ---------------
INTEGER :: status, dimid
safe_get_dimlen = -1
status = nf90_inq_dimid(ncid, name, dimid)
IF (status /= nf90_noerr) RETURN
status = nf90_inquire_dimension(ncid, dimid, LEN=safe_get_dimlen)
END FUNCTION safe_get_dimlen
!------------------------------------------------------------------------------
!S+
! NAME:
! insert_neighbour
!
! PURPOSE:
!> Insert a neighbour (squared-distance, source, x_index, y_index) into a
!> neighbourhood that is already sorted by increasing distance. The
!> neighbourhood may be modified by adding a neighbour and potentially
!> shuffling/removing existing ones the assigned may be increased by 1 if
!> the number of neighbours stored in the neighbourhood increases
!
! CATEGORY:
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! CALL insert_neighbour(neighbourhood,distance,source,x_index,y_index)
!
! ARGUMENTS:
!>@ARG{neighbourhood, inout, NEIGHBOURHOOD_ENTRY} the neighbourhood into which the neigbour is to be inserted
!>@ARG{distance, in, REAL} the squared distance of the neighbour
!>@ARG{source, in, INTEGER} the source of the neighbour (orphan|main, a|b)
!>@ARG{x_index, in, INTEGER} the x-index (column) of the pixel on the IR grid
!>@ARG{y_index, in, INTEGER} the y-index (row) of the pixel on the IR grid
!
! CALLS:
! None
! SIDE EFFECTS:
! None
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 29/01/21 NM Creation
!S-
!------------------------------------------------------------------------------
PURE SUBROUTINE insert_neighbour(neighbourhood,distance,source,x_index,y_index)
IMPLICIT NONE
! ---------
! Arguments
! ---------
TYPE(NEIGHBOURHOOD_ENTRY), INTENT(inout) :: neighbourhood
REAL, INTENT(in) :: distance
INTEGER, INTENT(in) :: source
INTEGER, INTENT(in) :: x_index
INTEGER, INTENT(in) :: y_index
! ---------------
! Local Variables
! ---------------
INTEGER :: insert_after_index
! work out the position to insert the neighbour. zero means insert at the first position.
insert_after_index = 0
IF (neighbourhood%n > 0) THEN
! Some neighbours already assigned, so work out where to insert the neighbour (if anywhere)
DO insert_after_index = neighbourhood%n, 0, -1
IF (insert_after_index == 0) EXIT
IF (distance >= neighbourhood%d(insert_after_index)) EXIT
IF (insert_after_index < MAX_K_NEAREST_NEIGHBOURS) THEN
! Shuffle neighbours up to make space for the neighbour to be added
neighbourhood%d(insert_after_index+1) = neighbourhood%d(insert_after_index)
neighbourhood%source(insert_after_index+1) = neighbourhood%source(insert_after_index)
neighbourhood%x(insert_after_index+1) = neighbourhood%x(insert_after_index)
neighbourhood%y(insert_after_index+1) = neighbourhood%y(insert_after_index)
END IF
END DO
END IF
! insert the neighbour if it belongs in the neighbourhood
IF (insert_after_index < MAX_K_NEAREST_NEIGHBOURS) THEN
neighbourhood%d(insert_after_index+1) = distance
neighbourhood%source(insert_after_index+1) = source
neighbourhood%x(insert_after_index+1) = x_index
neighbourhood%y(insert_after_index+1) = y_index
IF (neighbourhood%n < MAX_K_NEAREST_NEIGHBOURS) THEN
neighbourhood%n = neighbourhood%n + 1
END IF
END IF
END SUBROUTINE insert_neighbour
!------------------------------------------------------------------------------
!S+
! NAME:
! find_neighbours
!
! PURPOSE:
!> Find the closest radiance band pixels to a specific IR band pixel
!
! CATEGORY:
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! CALL find_neighbours(ir_x_index, ir_y_index, ir_x, ir_y, &
! main_cartesian, orphan_cartesian, neighborhood)
!
! ARGUMENTS:
!>@ARG{ir_x_index, in, INTEGER} the column of the IR pixel
!>@ARG{ir_y_index, in, INTEGER} the row of the IR pixel
!>@ARG{ir_x, in, REAL} across track distance of the IR pixel
!>@ARG{ir_y, in, REAL} along track distance of the IR pixel
!>@ARG{main_cartesian, in, Locations} visible main pixel locations and confidences
!>@ARG{orphan_cartesian, in, Locations} viisble orphan pixel locations
!>@ARG{stripe, in, CHARACTER(1)} the stripe which the cartesian data represents, 'a' or 'b'
!>@ARG{neighbourhood, inout, NEIGHBOURHOOD_ENTRY} the neighbourhood map that is being constructed
!
! CALLS:
! insert_neighbour
!
! SIDE EFFECTS:
! None
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 23/10/20 NM Creation
! 06/11/20 NM Refactor after code review with CB,AW,OE
! 13/05/21 OE Rewrite to avoid array temporaries, improve performace
!S-
!------------------------------------------------------------------------------
PURE SUBROUTINE find_neighbours(ir_x_index, ir_y_index, ir_x, ir_y, &
main_cartesian, orphan_cartesian, stripe, neighbourhood)
IMPLICIT NONE
! ---------
! Arguments
! ---------
INTEGER, INTENT(in) :: ir_x_index
INTEGER, INTENT(in) :: ir_y_index
REAL, INTENT(in) :: ir_x
REAL, INTENT(in) :: ir_y
TYPE(Locations), INTENT(in) :: main_cartesian
TYPE(Locations), INTENT(in) :: orphan_cartesian
CHARACTER(1), INTENT(in) :: stripe
TYPE(NEIGHBOURHOOD_ENTRY), INTENT(out) :: neighbourhood
! ---------------
! Local Variables
! ---------------
INTEGER :: vis_x_min_index, vis_x_max_index, vis_y_min_index, vis_y_max_index
REAL :: max_dist_sq
INTEGER :: visible_width, visible_height
INTEGER :: main_source, orphan_source
INTEGER :: elem, line
REAL :: v
IF (stripe == 'a') THEN
main_source = MAIN_PIXEL_SOURCE_A
orphan_source = ORPHAN_PIXEL_SOURCE_A
ELSE
main_source = MAIN_PIXEL_SOURCE_B
orphan_source = ORPHAN_PIXEL_SOURCE_B
END IF
visible_width = main_cartesian%width
visible_height = main_cartesian%height
! if the across or along track distances of the IR pixel are missing, set all neighbours to missing and
! return immediately
IF (ir_x == MISSING_R .or. ir_y == MISSING_R) THEN
RETURN
ENDIF
! First work out the search window in terms of radiance indices
vis_x_min_index = MAX(2*ir_x_index - search_width, 1)
vis_x_max_index = MIN(2*ir_x_index + search_width, visible_width)
vis_y_min_index = MAX(2*ir_y_index - search_height,1)
vis_y_max_index = MIN(2*ir_y_index + search_height, visible_height)
! Compute the threshold for squared distances - neighbours must be closer than this
max_dist_sq = MAX_NEIGHBOUR_DISTANCE**2
! iterate over the search window and build an ordered list of the closest K
! values in the neighborhood structure
DO line = vis_y_min_index, vis_y_max_index
! main pixel array
DO elem = vis_x_min_index, vis_x_max_index
! ignore missing and cosmetically filled pixels
IF (main_cartesian%x(elem, line) == MISSING_R) CYCLE
IF (IAND(main_cartesian%conf(elem, line), COSMETIC_PIXEL_MASK) /= 0) CYCLE
v = (main_cartesian%x(elem, line) - ir_x) ** 2 + (main_cartesian%y(elem, line) - ir_y) ** 2
IF (v >= max_dist_sq) CYCLE ! ignore if the squared distance is too far away
CALL insert_neighbour(neighbourhood, v, main_source, elem, line)
END DO
! orphan pixel array
DO elem = 1, orphan_cartesian%width
IF (orphan_cartesian%x(elem, line) == MISSING_R) EXIT
v = (orphan_cartesian%x(elem, line) - ir_x) ** 2 + (orphan_cartesian%y(elem, line) - ir_y) ** 2
IF (v >= max_dist_sq) CYCLE
CALL insert_neighbour(neighbourhood, v, orphan_source, elem, line)
END DO
END DO
END SUBROUTINE find_neighbours
!------------------------------------------------------------------------------
!S+
! NAME:
! build_neighbourhood_map
!
! PURPOSE:
!> Find the K closest radiance band pixels to a every IR band pixel and store
!> the resulting mapping in a neighbourhood map. Considers main and orphan
!> band pixels.
!
! CATEGORY:
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! CALL build_neighbourhood_map(ir, visnir, orphan, neighbourhood)
!
! ARGUMENTS:
!>@ARG{ir, in, Locations} ir main pixel locations
!>@ARG{visnir, in, Locations} visible main pixel locations and confidences
!>@ARG{orphan, in, Locations} visible orphan pixel locations
!>@ARG{neighbourhood, inout, NEIGHBOURHOOD_ENTRY} the neighbourhood map that is being constructed
!
! CALLS:
! find_neighbours
!
! SIDE EFFECTS:
! None
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 23/10/20 NM Creation
! 06/11/20 NM Refactor after code review with CB,AW,OE
!S-
!------------------------------------------------------------------------------
PURE SUBROUTINE build_neighbourhood_map(ir, visnir, orphan, &
neighbourhood)
IMPLICIT NONE
! ---------
! Arguments
! ---------
TYPE(Locations), INTENT(in) :: ir
TYPE(Locations), INTENT(in) :: visnir
TYPE(Locations), INTENT(in) :: orphan
TYPE(NEIGHBOURHOOD_MAP), INTENT(inout) :: neighbourhood
! ---------------
! Local Variables
! ---------------
INTEGER :: elem, line
REAL :: x, y
CHARACTER(1) :: stripe
IF (neighbourhood%include_a_stripe) THEN
stripe = 'a'
ELSE
stripe = 'b'
END IF
! Now loop over each IR pixel location and find its neighbours
DO line = 1,ir%height
DO elem = 1,ir%width
x = ir%x(elem,line)
y = ir%y(elem,line)
CALL find_neighbours(elem, line, x, y, visnir, orphan, &
stripe, neighbourhood%entries(elem, line))
END DO
END DO
END SUBROUTINE build_neighbourhood_map
!------------------------------------------------------------------------------
!F+
! NAME:
! apply_function
!
! PURPOSE:
!> Aggregate a neighbourhood of radiance pixel values using the specified function.
!> Customise according to taste to add new functions.
!
! CATEGORY:
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! aggregated_value = apply_function(function_code, pixel_values, n)
!
! ARGUMENTS:
!>@ARG{function_code, in, INTEGER} the code of the function to apply to neighbourhood, see FUNCTION_* parameters
!>@ARG{values, in, REAL\, DIMENSION(:)} array containing values to aggregate
!>@ARG{n, in, INTEGER} the number of valid values in the values array
!
! FUNCTION RESULT:
!>@RES{aggregated_value, REAL} result of applying the function to the array of pixel values
!
! CALLS:
! None
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 17/02/21 NM Creation
!F-
!------------------------------------------------------------------------------
PURE FUNCTION apply_function(function_code, values, n)
IMPLICIT NONE
! ---------
! Arguments
! ---------
INTEGER, INTENT(in) :: function_code
REAL, DIMENSION(:), INTENT(in) :: values
INTEGER, INTENT(in) :: n
! ----------------
! Function Result
! ----------------
REAL :: apply_function
! ---------------
! Local Variables
! ---------------
REAL :: vsum, vsumsq
apply_function = MISSING_R
IF (n <= 0) RETURN
vsum = 0.0
vsumsq = 0.0
SELECT CASE (function_code)
CASE (FUNCTION_MEAN)
vsum = SUM(values(1:n))
apply_function = vsum/n
CASE (FUNCTION_SD)
IF (n > 1) THEN
vsum = SUM(values(1:n))
vsumsq = SUM(values(1:n)**2)
apply_function = SQRT((vsumsq/n) - (vsum/n)**2)
ELSE
apply_function = 0
END IF
CASE (FUNCTION_MAX)
apply_function = MAXVAL(values(1:n))
CASE (FUNCTION_MIN_MAX_DIFF)
apply_function = MAXVAL(values(1:n)) - MINVAL(values(1:n))
END SELECT
END FUNCTION apply_function
!------------------------------------------------------------------------------
!S+
! NAME:
! apply_neighbours
!
! PURPOSE:
!> Aggregate radiance pixels to regrid onto the IR band. Currently, this
!> computes the mean value of the neighbours. Customise according to taste.
!
! CATEGORY:
!
! LANGUAGE:
! Fortran-95
!
! CALLING SEQUENCE:
! CALL apply_neighbours(rads_a, rads_b, neighbourhood, effective_k
! rads_i, function_code, std)
!
! ARGUMENTS:
!>@ARG{rads_a, in, Radiances} Input radiances, a-stripe
!>@ARG{rads_b, in, Radiances} Input radiances, b-stripe
!>@ARG{neighbourhood, in, NEIGHBOURHOOD_ENTRY\, DIMENSION(:\,:)} the neighbourhood map that was populated by build_neighbourhood_map
!>@ARG{effective_k, in, INTEGER} use the closest n neighbours, must be less than or equal to MAX_K_NEAREST_NEIGHBOURS
!>@ARG{rads_i, out, REAL\, DIMENSION(:\,:)} Regridded output radiances, i-stripe
!>@ARG{function_code, in, INTEGER} Aggregation function to apply to neighbourhood, see FUNCTION_* parameters
!>@ARG{std, out, REAL\, DIMENSION(:\,:)\, OPTIONAL} Standard deviation of aggregated pixels
!
! CALLS:
! apply_function
!
! SIDE EFFECTS:
! None
!
! RESTRICTIONS:
!
! PROCEDURE:
!
! CREATION HISTORY:
! 23/10/20 NM Creation
! 06/11/20 NM Refactor after code review with CB,AW,OE
! 17/02/21 NM Refactor out apply_function
!S-
!------------------------------------------------------------------------------
PURE SUBROUTINE apply_neighbours(rads_a, rads_b, neighbourhood, effective_k, &
rads_i, function_code, std)
IMPLICIT NONE
! ---------
! Arguments
! ---------
TYPE(Radiances), INTENT(in) :: rads_a
TYPE(Radiances), INTENT(in) :: rads_b
TYPE(NEIGHBOURHOOD_MAP), INTENT(in) :: neighbourhood
INTEGER, INTENT(in) :: effective_k
REAL, DIMENSION(:,:), INTENT(out) :: rads_i
INTEGER, INTENT(in) :: function_code
REAL, DIMENSION(:,:), OPTIONAL, INTENT(out) :: std
! ---------------