-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclsm_ensupd_read_obs.F90
More file actions
10547 lines (6755 loc) · 349 KB
/
clsm_ensupd_read_obs.F90
File metadata and controls
10547 lines (6755 loc) · 349 KB
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
!
! this file contains subroutines for reading and processing observations
! for the GEOS5 land EnKF update algorithm
!
! reichle, 27 Jan 2005
! reichle, 10 Jan 2011 - replaced "UVA" with "LPRM"
! reichle, 1 Jul 2015 - clarified definition of obs time stamp
! (J2000 seconds w/ 'TT12' epoch)
! lcandre2, 10 Jul 2021 - confirmed and cleaned up MODIS obs
! added work_path to inputs of many subroutines so that "tmpfname"
! (needed several times for reading AMSR-E hdf files) is distinct for each job
! reichle, 27 Aug 2005
! *********************************************************************
module clsm_ensupd_read_obs
use MAPL_BaseMod, ONLY: &
MAPL_UNDEF
use MAPL_ConstantsMod, ONLY: &
MAPL_TICE
use io_hdf5, ONLY: &
hdf5read
use EASE_conv, ONLY: &
ease_convert, &
ease_extent
use LDAS_ensdrv_globals, ONLY: &
logit, &
logunit, &
nodata_tolfrac_generic
use clsm_ensupd_glob_param, ONLY: &
unitnum_obslog
use LDAS_DateTimeMod, ONLY: &
date_time_type, &
augment_date_time, &
get_dofyr_pentad, &
datetime_le_refdatetime, &
datetime_lt_refdatetime, &
date_time2string, &
datetime_to_J2000seconds
use enkf_types, ONLY: &
obs_type, &
obs_param_type
use LDAS_TilecoordType, ONLY: &
tile_coord_type, &
grid_def_type
use clsm_ensdrv_drv_routines, ONLY: &
f2l_real, &
f2l_real8, &
f2l_logical
use LDAS_TilecoordRoutines, ONLY: &
get_tile_num_from_latlon
use LDAS_ensdrv_mpi, ONLY: &
root_proc, &
numprocs, &
mpicomm, &
MPI_obs_type, &
mpierr
use LDAS_exceptionsMod, ONLY: &
ldas_abort, &
ldas_warn, &
LDAS_GENERIC_ERROR, &
LDAS_GENERIC_WARNING
use clsm_ensupd_upd_routines, ONLY: &
dist_km2deg
implicit none
include 'mpif.h'
private
public :: collect_obs
contains
! *****************************************************************
subroutine read_ae_l2_sm_hdf( &
N_files, fnames, N_data, lon, lat, ae_l2_sm, ease_col, ease_row )
! read soil moisture data from one or more AMSR-E Land hdf files
!
! return ONLY valid data points (ie. excluding no-data-values)
! that also pass initial QC (based on "Surface Type" QC flag and on
! Heterogeneity_Index )
!
! reichle, 20 Sep 2005 - added "Surface Type" QC flag
! reichle, 17 Nov 2005 - replace hdp with call to hdf library
! reichle, 8 Feb 2006 - optionally read EASE row- and column index
! - added Heterogeneity_Index QC
! reichle, 10 Jan 2011 - revised "Surface Type" QC flag
implicit none
integer, intent(in) :: N_files
character(*), dimension(N_files), intent(in) :: fnames
integer, intent(out) :: N_data
real, dimension(:), pointer :: lon, lat, ae_l2_sm ! output
integer, dimension(:), pointer, optional :: ease_col, ease_row ! output
! local parameters
integer, parameter :: N_fields = 7
character(19), dimension(N_fields), parameter :: field_names = (/ &
'Longitude ', & ! 1
'Latitude ', & ! 2
'Surface_Type ', & ! 3
'Soil_Moisture ', & ! 4
'Row_Index ', & ! 5
'Column_Index ', & ! 6
'Heterogeneity_Index' /) ! 7
real, parameter :: scale_fac_Soil_Moisture = 1000.0;
integer, parameter :: nodata = -9999 ! NOTE: integer
! Initial QC:
!
! "Surface_Type": Only "low vegetation" or "moderate vegetation" (and no precip,
! frozen ground, etc) passes.
!
! "Heterogeneity_Index": Discard all pixels with
! Heterogeneity_Index>max_Heterogeneity_Index
! because they are likely mixed land/water pixels. See matlab code
! "detect_coast_in_AMSRE_Land.m"
! in land01:/home/reichle/NSIPP/AMSR/AMSR_E_Land/matlab/
! In this subroutine, Heterogeneity_Index is used on its raw form and
! never scaled into units of Kelvin.
!
! Further info:
! http://nsidc.org/data/docs/daac/ae_land_l2b_soil_moisture.gd.html
! http://nsidc.org/data/amsre/versions.html
! -reichle, 20 Sep 2005
! -reichle, 8 Feb 2006
integer, parameter :: max_Heterogeneity_Index = 500 ! = 5 Kelvin
! declarations of hdf functions
integer :: hopen, vfstart, vsfatch, vsqfnelt, vsfseek, vsfsfld, vsfread
integer :: vsfdtch, vfend, hclose
! declarations of hdf-related parameters and variables
integer, dimension(N_files) :: file_id, vdata_id
integer :: status, n_read, n_records, record_pos
integer, parameter :: num_dds_block = 0 ! only important for writing hdf
integer, parameter :: vdata_ref = 7 ! works for AMSRE_L2_Land
integer, parameter :: DFACC_READ = 1 ! from hdf.inc
integer, parameter :: FULL_INTERLACE = 0 ! from hdf.inc
! local variables
logical :: must_stop
integer, dimension(N_files) :: N_data_tmp
integer :: i, j, k, k_off
integer, dimension(:), allocatable :: surface_type_qc_flag
integer, dimension(:), allocatable :: Heterogeneity_Index
integer*2, dimension(:), allocatable :: tmpint2vec
real, dimension(:), allocatable :: tmprealvec
character(len=*), parameter :: Iam = 'read_ae_l2_sm_hdf'
character(len=400) :: err_msg
! -------------------------------------------------------------
! determine number of data to be read from each file
do j=1,N_files
! open and "start" hdf file
file_id(j) = hopen( fnames(j), DFACC_READ, num_dds_block )
status = vfstart(file_id(j))
! select vdata block that contains fields of interest
vdata_id(j) = vsfatch(file_id(j), vdata_ref, 'r')
! determine number of records in vdata
status = vsqfnelt(vdata_id(j), n_records)
N_data_tmp(j) = n_records
end do
! allocate pointers (must be deallocated outside this subroutine)
must_stop = .false.
if ( associated(lon) .or. associated(lat) .or. associated(ae_l2_sm) ) then
must_stop = .true.
end if
if ( present(ease_col) ) then
if (associated(ease_col)) must_stop = .true.
end if
if ( present(ease_row) ) then
if (associated(ease_row)) must_stop = .true.
end if
if (must_stop) then
err_msg = 'output pointers must not be associated/allocated on input.'
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end if
N_data = sum(N_data_tmp)
allocate(lon(N_data))
allocate(lat(N_data))
if (present(ease_col)) allocate(ease_col(N_data))
if (present(ease_row)) allocate(ease_row(N_data))
allocate(ae_l2_sm(N_data))
allocate(surface_type_qc_flag(N_data))
allocate(Heterogeneity_Index(N_data))
! read hdf data into arrays, concatenate data from N_files files
k_off = 0
do j=1,N_files
allocate(tmprealvec(N_data_tmp(j)))
allocate(tmpint2vec(N_data_tmp(j)))
do i=1,N_fields
! go to start of record (zero-based count)
record_pos = vsfseek(vdata_id(j), 0)
! pick the field to be read
status = vsfsfld(vdata_id(j), field_names(i))
! read data
select case (i)
case (1)
n_read = vsfread( vdata_id(j), tmprealvec, &
N_data_tmp(j), FULL_INTERLACE)
lon(k_off+1:k_off+N_data_tmp(j)) = tmprealvec
case (2)
n_read = vsfread( vdata_id(j), tmprealvec, &
N_data_tmp(j), FULL_INTERLACE)
lat(k_off+1:k_off+N_data_tmp(j)) = tmprealvec
case (3)
n_read = vsfread( vdata_id(j) ,tmpint2vec, &
N_data_tmp(j), FULL_INTERLACE)
surface_type_qc_flag(k_off+1:k_off+N_data_tmp(j)) = tmpint2vec
! overwrite surface_type_qc_flag with common pass/fail
! (negative number -1 means fail)
do k=1,N_data_tmp(j)
! keep only data with
!
! surface_type_qc_flag=128 ("moderate veg", and no other bits set)
! surface_type_qc_flag=256 ("low veg", and no other bits set)
!
! http://nsidc.org/data/docs/daac/ae_land_l2b_soil_moisture.gd.html
if (.not. ( &
(surface_type_qc_flag(k+k_off)==128) .or. &
(surface_type_qc_flag(k+k_off)==256) ) ) &
surface_type_qc_flag(k+k_off) = -1
end do
case (4)
n_read = vsfread(vdata_id(j), tmpint2vec, &
N_data_tmp(j), FULL_INTERLACE)
do k=1,N_data_tmp(j)
if (tmpint2vec(k)/=nodata) then
ae_l2_sm(k+k_off) = real(tmpint2vec(k)) / &
scale_fac_Soil_Moisture
else
ae_l2_sm(k+k_off) = real(tmpint2vec(k))
end if
end do
case (5)
if (present(ease_row)) then
n_read = vsfread( vdata_id(j) ,tmpint2vec, &
N_data_tmp(j), FULL_INTERLACE)
ease_row(k_off+1:k_off+N_data_tmp(j)) = tmpint2vec
end if
case (6)
if (present(ease_col)) then
n_read = vsfread( vdata_id(j) ,tmpint2vec, &
N_data_tmp(j), FULL_INTERLACE)
ease_col(k_off+1:k_off+N_data_tmp(j)) = tmpint2vec
end if
case (7)
n_read = vsfread( vdata_id(j) ,tmpint2vec, &
N_data_tmp(j), FULL_INTERLACE)
Heterogeneity_Index(k_off+1:k_off+N_data_tmp(j)) = tmpint2vec
case default
call ldas_abort(LDAS_GENERIC_ERROR, Iam, 'unknown case')
end select
if (n_read/=N_data_tmp(j)) then
call ldas_abort(LDAS_GENERIC_ERROR, Iam, 'ERROR reading hdf')
end if
end do
! clean up
deallocate(tmprealvec)
deallocate(tmpint2vec)
! close hdf files
status = vsfdtch(vdata_id(j))
status = vfend(file_id(j))
status = hclose(file_id(j))
! prepare next j
k_off = k_off + N_data_tmp(j)
end do
! -------------------------------------
!
! eliminate no-data-values and data that fail initial QC
j = 0
do i=1,N_data
if ( (ae_l2_sm(i)>0.) .and. & ! any neg is nodata
(surface_type_qc_flag(i)>=0) .and. & ! any neg is fail
(Heterogeneity_Index(i)<=max_Heterogeneity_Index) &
) then
j=j+1
ae_l2_sm(j) = ae_l2_sm(i)
lon(j) = lon(i)
lat(j) = lat(i)
if (present(ease_col)) ease_col(j) = ease_col(i)
if (present(ease_row)) ease_row(j) = ease_row(i)
end if
end do
N_data = j
deallocate(surface_type_qc_flag)
deallocate(Heterogeneity_Index)
end subroutine read_ae_l2_sm_hdf
! *****************************************************************
subroutine read_obs_ae_l2_sm( &
work_path, exp_id, &
date_time, dtstep_assim, N_catd, tile_coord, &
tile_grid_d, N_tile_in_cell_ij, tile_num_in_cell_ij, &
this_obs_param, &
found_obs, ae_l2_sm, std_ae_l2_sm )
! Read observations of surface soil moisture from AMSR-E L2 files
! Set flag "found_obs" to true if observations are available
! for assimilation.
!
! If there are N > 1 observations in a given tile,
! a "super-observation" is computed by averaging the N observations
!
! inputs to this subroutine:
! date_time = current model date and time
! N_catd = number of catchments in domain
!
! reichle, 25 Jul 2005
!
! --------------------------------------------------------------------
implicit none
! inputs:
character(*), intent(in) :: work_path
character(*), intent(in) :: exp_id
type(date_time_type), intent(in) :: date_time
integer, intent(in) :: dtstep_assim, N_catd
type(tile_coord_type), dimension(:), pointer :: tile_coord ! input
type(grid_def_type), intent(in) :: tile_grid_d
integer, dimension(tile_grid_d%N_lon,tile_grid_d%N_lat), intent(in) :: &
N_tile_in_cell_ij
integer, dimension(:,:,:), pointer :: tile_num_in_cell_ij ! input
type(obs_param_type), intent(in) :: this_obs_param
! outputs:
real, intent(out), dimension(N_catd) :: ae_l2_sm
real, intent(out), dimension(N_catd) :: std_ae_l2_sm
logical, intent(out) :: found_obs
! ---------------
! locals
! AMSR_E_L2_Land files are available for approximately 50min periods
! covering one ascending or descending swath. The filename indicates
! the start time of the swath
! "ae_time_offset" is used to find the mean time of the the interval
! which is approximately the time of the equator overpass.
! This time is assigned to all observations of the swath.
integer, parameter :: ae_time_offset = 1500 ! 25 minutes in seconds
character(4) :: DDHH
character(6) :: YYYYMM
character(8) :: date_string
character(10) :: time_string
character(300) :: tmpfname, tmpfname2
character(400) :: cmd
type(date_time_type) :: date_time_tmp
integer :: i, ind, N_tmp, N_files
character(300), dimension(:), allocatable :: fnames
real, dimension(:), pointer :: tmp_obs, tmp_lat, tmp_lon
integer, dimension(:), pointer :: tmp_tile_num
integer, dimension(N_catd) :: N_obs_in_tile
character(len=*), parameter :: Iam = 'read_obs_ae_l2_sm'
! -------------------------------------------------------------------
nullify( tmp_obs, tmp_lat, tmp_lon, tmp_tile_num )
! ---------------
! initialize
found_obs = .false.
! find files that are within half-open interval
! [date_time-dtstep_assim/2,date_time+dtstep_assim/2)
date_time_tmp = date_time
call augment_date_time( -(dtstep_assim/2 + ae_time_offset), date_time_tmp )
! get tmp file name and remove file if it exists
call date_and_time(date_string, time_string) ! f90 intrinsic function
tmpfname = trim(work_path) // '/' // 'tmp.' // trim(exp_id) &
// '.' // date_string // time_string
cmd = '/bin/rm -f ' // tmpfname
call Execute_command_line(trim(cmd))
! identify all files within current assimilation interval
! (list all files within hourly intervals)
do i=1,(dtstep_assim/3600)
write (YYYYMM,'(i6.6)') date_time_tmp%year*100 + date_time_tmp%month
write (DDHH, '(i4.4)') date_time_tmp%day *100 + date_time_tmp%hour
cmd = 'ls ' // trim(this_obs_param%path) // '/' // YYYYMM(1:4) // &
'/M' // YYYYMM(5:6) // '/' // trim(this_obs_param%name) &
// '*' // YYYYMM // DDHH // '*'
if (trim(this_obs_param%descr)=='ae_l2_sm_a') then
cmd = trim(cmd) // '_A.hdf'
elseif (trim(this_obs_param%descr)=='ae_l2_sm_d') then
cmd = trim(cmd) // '_D.hdf'
else
call ldas_abort(LDAS_GENERIC_ERROR, Iam, 'unknown descr')
end if
cmd = trim(cmd) // ' >> ' // trim(tmpfname)
call Execute_command_line(trim(cmd))
call augment_date_time( 3600, date_time_tmp )
end do
! find out how many need to be read
tmpfname2 = trim(tmpfname) // '.wc'
cmd = 'wc -w ' // trim(tmpfname) // ' > ' // trim(tmpfname2)
call Execute_command_line(trim(cmd))
open(10, file=tmpfname2, form='formatted', action='read')
read(10,*) N_files
close(10,status='delete')
! load file names into "fnames"
open(10, file=tmpfname, form='formatted', action='read')
if (N_files>0) then
allocate(fnames(N_files))
do i=1,N_files
read(10,'(a)') fnames(i)
end do
end if
close(10,status='delete')
! read observations:
!
! 1.) read N_tmp observations and their lat/lon info from file
! 2.) for each observation
! a) determine grid cell that contains lat/lon
! b) determine tile within grid cell that contains lat/lon
! 3.) compute super-obs for each tile from all obs w/in that tile
!
! ----------------------------------------------------------------
!
! 1.) read N_tmp observations and their lat/lon info from file
if (N_files>0) then
call read_ae_l2_sm_hdf( &
N_files, fnames, &
N_tmp, tmp_lon, tmp_lat, tmp_obs )
if (logit) then
write (logunit,*) 'read_obs_ae_l2_sm: read ', N_tmp, &
' at date_time = ', date_time, ' from '
do i=1,N_files
write (logunit,*) trim(fnames(i))
end do
write (logunit,*) '----------'
end if
deallocate(fnames)
else
N_tmp = 0
end if
! ------------------------------------------------------------------
! note QC and no-data-value block in subroutine read_ae_l2_sm_hdf()
! ------------------------------------------------------------------
!
! 2.) for each observation
! a) determine grid cell that contains lat/lon
! b) determine tile within grid cell that contains lat/lon
if (N_tmp>0) then
allocate(tmp_tile_num(N_tmp))
call get_tile_num_for_obs(N_catd, tile_coord, &
tile_grid_d, N_tile_in_cell_ij, tile_num_in_cell_ij, &
N_tmp, tmp_lat, tmp_lon, &
this_obs_param, &
tmp_tile_num )
! ----------------------------------------------------------------
!
! 3.) compute super-obs for each tile from all obs w/in that tile
! (also eliminate observations that are not in domain)
ae_l2_sm = 0.
N_obs_in_tile = 0
do i=1,N_tmp
ind = tmp_tile_num(i) ! 1<=tmp_tile_num<=N_catd (unless nodata)
if (ind>0) then ! this step eliminates obs outside domain
ae_l2_sm(ind) = ae_l2_sm(ind) + tmp_obs(i)
N_obs_in_tile(ind) = N_obs_in_tile(ind) + 1
end if
end do
! normalize
do i=1,N_catd
if (N_obs_in_tile(i)>1) then
ae_l2_sm(i) = ae_l2_sm(i)/real(N_obs_in_tile(i))
elseif (N_obs_in_tile(i)==0) then
ae_l2_sm(i) = this_obs_param%nodata
end if
end do
! clean up
if (associated(tmp_tile_num)) deallocate(tmp_tile_num)
! --------------------------------
! set observation error standard deviation
do i=1,N_catd
std_ae_l2_sm(i) = this_obs_param%errstd
end do
! --------------------------------
if (any(N_obs_in_tile>0)) then
found_obs = .true.
else
found_obs = .false.
end if
end if
! clean up
if (associated(tmp_obs)) deallocate(tmp_obs)
if (associated(tmp_lon)) deallocate(tmp_lon)
if (associated(tmp_lat)) deallocate(tmp_lat)
end subroutine read_obs_ae_l2_sm
! ***************************************************************************
subroutine read_ae_sm_LPRM_bin( &
this_obs_param, N_files, fnames, N_data, lon, lat, ae_sm_LPRM, ease_col, ease_row )
! read soil moisture data from one or more AMSR-E LPRM bin files
!
! return ONLY valid data points (ie. excluding no-data-values)
!
! no QC in addition to what was done in matlab-preprocessing
!
! reichle, 20 Feb 2009
implicit none
type(obs_param_type), intent(in) :: this_obs_param
integer, intent(in) :: N_files
character(*), dimension(N_files), intent(in) :: fnames
integer, intent(out) :: N_data
real, dimension(:), pointer :: lon, lat, ae_sm_LPRM ! output
integer, dimension(:), pointer, optional :: ease_col, ease_row ! output
! local variables
logical :: must_stop
integer, dimension(N_files) :: N_data_tmp
integer :: i, j, k_off
integer, dimension(:), allocatable :: tmpintvec
real, dimension(:), allocatable :: tmprealvec
character(len=*), parameter :: Iam = 'read_ae_sm_LPRM_bin'
character(len=400) :: err_msg
! -------------------------------------------------------------
! make sure pointers are not allocated or associated
must_stop = .false.
if ( associated(lon) .or. associated(lat) .or. associated(ae_sm_LPRM) ) then
must_stop = .true.
end if
if ( present(ease_col) ) then
if (associated(ease_col)) must_stop = .true.
end if
if ( present(ease_row) ) then
if (associated(ease_row)) must_stop = .true.
end if
if (must_stop) then
err_msg = 'output pointers must not be associated/allocated on input'
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end if
! determine number of data to be read from each file
N_data = 0
do j=1,N_files
! open file
open( 10, file=trim(fnames(j)), form='unformatted',convert='big_endian', action='read' )
read( 10) N_data_tmp(j)
close(10, status='keep')
end do
! allocate pointers (must be deallocated outside this subroutine!)
N_data = sum(N_data_tmp)
allocate(lon(N_data))
allocate(lat(N_data))
if (present(ease_col)) allocate(ease_col(N_data))
if (present(ease_row)) allocate(ease_row(N_data))
allocate(ae_sm_LPRM(N_data))
! read data into arrays, concatenate data from N_files files
! format of AMSR_sm_LPRM_EASE_bin files:
!
! record 1 -- N_data int*4
! record 2 -- lon( 1:N_data) real*4
! record 3 -- lat( 1:N_data) real*4
! record 4 -- sm_C( 1:N_data) real*4
! record 5 -- sm_X( 1:N_data) real*4
! record 6 -- od_C( 1:N_data) real*4
! record 7 -- od_X( 1:N_data) real*4
! record 8 -- res_C(1:N_data) real*4
! record 9 -- res_X(1:N_data) real*4
! record 10 -- ts_C( 1:N_data) real*4
! record 11 -- ts_X( 1:N_data) real*4
! record 12 -- rfi_C(1:N_data) int*4
! record 13 -- rfi_X(1:N_data) int*4
! record 14 -- ind_i(1:N_data) int*4 zero-based (!) EASE row index
! record 15 -- ind_j(1:N_data) int*4 zero-based (!) EASE col index
! record 16 -- time( 1:N_data) real*4 minutes since beginning of half-orbit
k_off = 0
do j=1,N_files
allocate(tmprealvec(N_data_tmp(j)))
if (present(ease_col)) allocate(tmpintvec(N_data_tmp(j)))
open (10, file=trim(fnames(j)), form='unformatted',convert='big_endian', action='read' )
! re-read N_data
read (10) N_data_tmp(j)
! read data as needed
read (10) tmprealvec; lon(k_off+1:k_off+N_data_tmp(j)) = tmprealvec
read (10) tmprealvec; lat(k_off+1:k_off+N_data_tmp(j)) = tmprealvec
if (this_obs_param%descr(13:14)=='_C') then
read (10) tmprealvec; ae_sm_LPRM(k_off+1:k_off+N_data_tmp(j)) = tmprealvec
read (10) ! skip sm_X
else if (this_obs_param%descr(13:14)=='_X') then
read (10) ! skip sm_C
read (10) tmprealvec; ae_sm_LPRM(k_off+1:k_off+N_data_tmp(j)) = tmprealvec
else
err_msg = 'unknown descr, ' // this_obs_param%descr
call ldas_abort(LDAS_GENERIC_ERROR, Iam, err_msg)
end if
if (present(ease_col) .and. present(ease_row)) then
read (10) ! skip od_C
read (10) ! skip od_X
read (10) ! skip res_C
read (10) ! skip res_X
read (10) ! skip ts_C
read (10) ! skip ts_X
read (10) ! skip rfi_C
read (10) ! skip rfi_X
read (10) tmpintvec; ease_col(k_off+1:k_off+N_data_tmp(j)) = tmpintvec
read (10) tmpintvec; ease_row(k_off+1:k_off+N_data_tmp(j)) = tmpintvec
! read (10) ! skip time
end if
! clean up
close(10, status='keep')
deallocate(tmprealvec)
if (allocated(tmpintvec)) deallocate(tmpintvec)
! prepare next j
k_off = k_off + N_data_tmp(j)
end do
! -------------------------------------
!
! eliminate no-data-values
j = 0
do i=1,N_data
if (ae_sm_LPRM(i)>0.) then ! any neg is nodata
j=j+1
ae_sm_LPRM(j) = ae_sm_LPRM(i)
lon(j) = lon(i)
lat(j) = lat(i)
if (present(ease_col)) ease_col(j) = ease_col(i)
if (present(ease_row)) ease_row(j) = ease_row(i)
end if
end do
N_data = j
end subroutine read_ae_sm_LPRM_bin
! *****************************************************************
subroutine read_obs_ae_sm_LPRM( &
work_path, exp_id, &
date_time, dtstep_assim, N_catd, tile_coord, &
tile_grid_d, N_tile_in_cell_ij, tile_num_in_cell_ij, &
this_obs_param, &
found_obs, ae_sm_LPRM, std_ae_sm_LPRM )
! Read observations of surface soil moisture from AMSR-E sm LPRM files
! (Richard de Jeu, Vrije Universiteit Amsterdam)
! Set flag "found_obs" to true if observations are available
! for assimilation.
!
! If there are N > 1 observations in a given tile,
! a "super-observation" is computed by averaging the N observations
!
! inputs to this subroutine:
! date_time = current model date and time
! N_catd = number of catchments in domain
!
! reichle, 20 Feb 2009
!
! --------------------------------------------------------------------
implicit none
! inputs:
character(*), intent(in) :: work_path
character(*), intent(in) :: exp_id
type(date_time_type), intent(in) :: date_time
integer, intent(in) :: dtstep_assim, N_catd
type(tile_coord_type), dimension(:), pointer :: tile_coord ! input
type(grid_def_type), intent(in) :: tile_grid_d
integer, dimension(tile_grid_d%N_lon,tile_grid_d%N_lat), intent(in) :: &
N_tile_in_cell_ij
integer, dimension(:,:,:), pointer :: tile_num_in_cell_ij ! input
type(obs_param_type), intent(in) :: this_obs_param
! outputs:
real, intent(out), dimension(N_catd) :: ae_sm_LPRM
real, intent(out), dimension(N_catd) :: std_ae_sm_LPRM
logical, intent(out) :: found_obs
! ---------------
! locals
! AMSR_E_L2_Land files are available for approximately 50min periods
! covering one ascending or descending swath. The filename indicates
! the start time of the swath
! "ae_time_offset" is used to find the mean time of the the interval
! which is approximately the time of the equator overpass.
! This time is assigned to all observations of the swath.
integer, parameter :: ae_time_offset = 1500 ! 25 minutes in seconds
character(4) :: DDHH
character(6) :: YYYYMM
character(8) :: date_string
character(10) :: time_string
character(300) :: tmpfname, tmpfname2