-
Notifications
You must be signed in to change notification settings - Fork 145
/
obs_sequence_mod.f90
3020 lines (2299 loc) · 89.5 KB
/
obs_sequence_mod.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
! DART software - Copyright UCAR. This open source software is provided
! by UCAR, "as is", without charge, subject to all terms of use at
! http://www.image.ucar.edu/DAReS/DART/DART_download
!> @{
!> @brief Manage lists of observations
!>
!> Time-ordered sequences of observations.
!> get expected obs is in here
!> @}
module obs_sequence_mod
! WARNING OPERATOR OVERLOAD FOR EQUIVALENCE???
! FURTHER WARNING: Compiler problems exist with the use of assignment(=) in
! use only statement. First, can only use it at the level above if the internals
! of the type are not private. Second, if I inherit assignment(=) from obs_def
! and also define one in obs_sequence, I get an error if I try to make it public
! to a module that uses obs_sequence but not obs_def with the intel compiler. No
! obvious workaround exists. For now, make modules at higher levels use explicit
! copy subroutines. USERS MUST BE VERY CAREFUL TO NOT DO DEFAULT ASSIGNMENT
! FOR THESE TYPES THAT HAVE COPY SUBROUTINES.
use types_mod, only : r8, i8, MISSING_R8, metadatalength
use location_mod, only : location_type, is_location_in_region
use obs_def_mod, only : obs_def_type, get_obs_def_time, read_obs_def, &
write_obs_def, destroy_obs_def, copy_obs_def, &
interactive_obs_def, get_obs_def_location, &
get_obs_def_type_of_obs, get_obs_def_key, &
operator(==), operator(/=), print_obs_def
use obs_kind_mod, only : write_type_of_obs_table, &
read_type_of_obs_table, &
max_defined_types_of_obs, &
get_index_for_type_of_obs, &
get_name_for_type_of_obs
use time_manager_mod, only : time_type, set_time, print_time, print_date, &
operator(-), operator(+), &
operator(>), operator(<), &
operator(>=), operator(/=), operator(==)
use utilities_mod, only : get_unit, error_handler, &
find_namelist_in_file, check_namelist_read, &
E_ERR, E_MSG, nmlfileunit, do_nml_file, do_nml_term, &
open_file, close_file
implicit none
private
interface assignment(=)
module procedure copy_obs
end interface
interface operator(==)
module procedure eq_obs
end interface
interface operator(/=)
module procedure ne_obs
end interface
! Public interfaces for obs sequences
public :: obs_sequence_type, init_obs_sequence, interactive_obs_sequence, &
get_num_copies, get_num_qc, get_num_obs, get_max_num_obs, &
get_copy_meta_data, get_qc_meta_data, get_next_obs, get_prev_obs, &
insert_obs_in_seq, delete_obs_from_seq, set_copy_meta_data, &
set_qc_meta_data, get_first_obs, get_last_obs, add_copies, add_qc, &
write_obs_seq, read_obs_seq, set_obs, append_obs_to_seq, &
get_obs_from_key, get_obs_time_range, get_time_range_keys, &
get_num_times, get_num_key_range, operator(==), operator(/=), &
static_init_obs_sequence, destroy_obs_sequence, read_obs_seq_header, &
delete_seq_head, delete_seq_tail, &
get_next_obs_from_key, get_prev_obs_from_key, delete_obs_by_typelist, &
select_obs_by_location, delete_obs_by_qc, delete_obs_by_copy, &
print_obs_seq_summary, validate_obs_seq_time
! Public interfaces for obs
public :: obs_type, init_obs, destroy_obs, get_obs_def, set_obs_def, &
get_obs_values, set_obs_values, replace_obs_values, get_qc, set_qc, &
read_obs, write_obs, replace_qc, interactive_obs, copy_obs, assignment(=), &
get_obs_key, copy_partial_obs, print_obs
! Public interfaces for obs covariance modeling
public :: obs_cov_type
character(len=*), parameter :: source = 'obs_sequence_mod.f90'
type obs_sequence_type
private
integer :: num_copies
integer :: num_qc
integer :: num_obs
integer :: max_num_obs
! F95 allows pointers to be initialized to a known value.
! However, if you get an error on the following lines from your
! compiler, remove the => NULL() from the end of the 5 lines below.
character(len=metadatalength), pointer :: copy_meta_data(:) => NULL()
character(len=metadatalength), pointer :: qc_meta_data(:) => NULL()
integer :: first_time
integer :: last_time
! integer :: first_avail_time, last_avail_time
type(obs_type), pointer :: obs(:) => NULL()
! What to do about groups
end type obs_sequence_type
type obs_type
private
! The key is needed to indicate the element number in the storage for the obs_sequence
! Do I want to enforce the identity of the particular obs_sequence?
integer :: key
type(obs_def_type) :: def
real(r8), pointer :: values(:) => NULL()
real(r8), pointer :: qc(:) => NULL()
! Put sort indices directly into the data structure
integer :: prev_time, next_time
integer :: cov_group
end type obs_type
type obs_cov_type
private
integer :: num_cov_groups
end type obs_cov_type
! for errors
character(len=512) :: string1, string2, string3
!-------------------------------------------------------------
! Namelist with default values
! if .true., use unformatted files which are full precision,
! faster, smaller but not necessarily portable between machines.
logical :: write_binary_obs_sequence = .false.
! try reading in binary obs_seq files with a different byte order.
! valid values are: native, little_endian, big_endian
character(len=32) :: read_binary_file_format = 'native'
namelist /obs_sequence_nml/ write_binary_obs_sequence, read_binary_file_format
!--------------------------------------------------------------
contains
!--------------------------------------------------------------
subroutine static_init_obs_sequence
! reads namelist and registers module
! Read the namelist input
integer :: iunit, io
! Read the namelist entry
call find_namelist_in_file("input.nml", "obs_sequence_nml", iunit)
read(iunit, nml = obs_sequence_nml, iostat = io)
call check_namelist_read(iunit, io, "obs_sequence_nml")
if (do_nml_file()) write(nmlfileunit,nml=obs_sequence_nml)
if (do_nml_term()) write( * ,nml=obs_sequence_nml)
end subroutine static_init_obs_sequence
!--------------------------------------------------------------
!WHAT ABOUT PASS THROUGHS TO THE OBS_DEF???
! WHAT ABOUT copy_obs_sequence similar to read.
!-------------------------------------------------
subroutine init_obs_sequence(seq, num_copies, num_qc, &
expected_max_num_obs)
! Constructor for an obs_sequence
type(obs_sequence_type), intent(out) :: seq
integer, intent(in) :: num_copies, num_qc, expected_max_num_obs
integer :: i
seq%num_copies = num_copies
seq%num_qc = num_qc
seq%num_obs = 0
seq%max_num_obs = expected_max_num_obs
allocate(seq%copy_meta_data(seq%num_copies), &
seq%qc_meta_data(seq%num_qc), &
seq%obs(seq%max_num_obs) )
do i = 1, seq%num_copies
seq%copy_meta_data(i) = 'Copy metadata not initialized'
end do
do i = 1, seq%num_qc
seq%qc_meta_data(i) = 'QC metadata not initialized'
end do
! Initialize the pointers to allocated and initialize to something benign
! (Go ahead and allocated even in the case the counts are 0.)
do i = 1, seq%max_num_obs
allocate(seq%obs(i)%values(num_copies))
if (num_copies > 0) seq%obs(i)%values = MISSING_R8
allocate(seq%obs(i)%qc(num_qc))
if (num_qc > 0) seq%obs(i)%qc = 0.0_r8
end do
seq%first_time = -1
seq%last_time = -1
!seq%first_avail_time = -1
!seq%last_avail_time = -1
end subroutine init_obs_sequence
!--------------------------------------------------------------
subroutine destroy_obs_sequence(seq)
! Destructor for an obs_sequence
type(obs_sequence_type), intent(inout) :: seq
integer :: i
if ( seq%max_num_obs > 0 ) then
if (associated(seq%copy_meta_data)) then
deallocate(seq%copy_meta_data)
nullify(seq%copy_meta_data)
endif
if (associated(seq%qc_meta_data)) then
deallocate(seq%qc_meta_data)
nullify(seq%qc_meta_data)
endif
do i = 1, seq%max_num_obs
! seq%obs is a derived type, not a pointer.
! if (associated(seq%obs(i))) call destroy_obs( seq%obs(i) )
call destroy_obs( seq%obs(i) )
end do
! Also free up the obs storage in the sequence
if(associated(seq%obs)) then
deallocate(seq%obs)
nullify(seq%obs)
else
print *, 'destroy_obs_sequence called but seq%obs not associated'
endif
seq%first_time = -1
seq%last_time = -1
seq%num_copies = -1
seq%num_qc = -1
seq%num_obs = -1
seq%max_num_obs = -1
endif
end subroutine destroy_obs_sequence
!--------------------------------------------------------------
function interactive_obs_sequence()
! Interactive creation of an observation sequence
type(obs_sequence_type) :: interactive_obs_sequence
type(obs_type) :: obs, prev_obs
type(obs_def_type) :: obs_def
type(time_type) :: obs_time, prev_time
integer :: max_num_obs, num_copies, num_qc, i, end_it_all
write(*, *) 'Input upper bound on number of observations in sequence'
read(*, *) max_num_obs
write(*, *) 'Input number of copies of data (0 for just a definition)'
read(*, *) num_copies
write(*, *) 'Input number of quality control values per field (0 or greater)'
read(*, *) num_qc
! Initialize an obs_sequence structure
call init_obs_sequence(interactive_obs_sequence, num_copies, num_qc, max_num_obs)
do i = 1, num_copies
write(*, *) 'input meta data for data copy ', i
read(*, *) interactive_obs_sequence%copy_meta_data(i)
end do
do i = 1, num_qc
write(*, *) 'input meta data for qc field ', i
read(*, *) interactive_obs_sequence%qc_meta_data(i)
end do
! Initialize the obs variable
call init_obs(obs, num_copies, num_qc)
call init_obs(prev_obs, num_copies, num_qc)
! Loop to initialize each observation in turn; terminate by -1
do i = 1, max_num_obs
write(*, *) 'input a -1 if there are no more obs'
read(*, *) end_it_all
if(end_it_all == -1) exit
! Need to have key available for specialized observation modules
call interactive_obs(num_copies, num_qc, obs, i)
if(i == 1) then
call insert_obs_in_seq(interactive_obs_sequence, obs)
else
! if this is not the first obs, make sure the time is larger
! than the previous observation. if so, we can start the
! linked list search at the location of the previous obs.
! otherwise, we have to start at the beginning of the entire
! sequence to be sure the obs are ordered correctly in
! monotonically increasing times.
call get_obs_def(obs, obs_def)
obs_time = get_obs_def_time(obs_def)
call get_obs_def(prev_obs, obs_def)
prev_time = get_obs_def_time(obs_def)
if(prev_time > obs_time) then
call insert_obs_in_seq(interactive_obs_sequence, obs)
else
call insert_obs_in_seq(interactive_obs_sequence, obs, prev_obs)
endif
endif
prev_obs = obs
end do
call destroy_obs(obs)
call destroy_obs(prev_obs)
end function interactive_obs_sequence
!---------------------------------------------------------
!---------------------------------------------------------
function get_num_copies(seq)
type(obs_sequence_type), intent(in) :: seq
integer :: get_num_copies
get_num_copies = seq%num_copies
end function get_num_copies
!-------------------------------------------------
function get_num_qc(seq)
type(obs_sequence_type), intent(in) :: seq
integer :: get_num_qc
get_num_qc= seq%num_qc
end function get_num_qc
!-------------------------------------------------
function get_num_obs(seq)
type(obs_sequence_type), intent(in) :: seq
integer :: get_num_obs
get_num_obs = seq%num_obs
end function get_num_obs
!-------------------------------------------------
function get_max_num_obs(seq)
type(obs_sequence_type), intent(in) :: seq
integer :: get_max_num_obs
get_max_num_obs = seq%max_num_obs
end function get_max_num_obs
!-------------------------------------------------
function get_copy_meta_data(seq, copy_num)
type(obs_sequence_type), intent(in) :: seq
integer, intent(in) :: copy_num
character(len=metadatalength) :: get_copy_meta_data
! Should have an error check for copy_num range
get_copy_meta_data = seq%copy_meta_data(copy_num)
end function get_copy_meta_data
!-------------------------------------------------
function get_qc_meta_data(seq, qc_num)
type(obs_sequence_type), intent(in) :: seq
integer, intent(in) :: qc_num
character(len=metadatalength) :: get_qc_meta_data
! Should have an error check for qc_num range
get_qc_meta_data = seq%qc_meta_data(qc_num)
end function get_qc_meta_data
!-------------------------------------------------
subroutine get_next_obs(seq, obs, next_obs, is_this_last)
type(obs_sequence_type), intent(in) :: seq
type(obs_type), intent(in) :: obs
type(obs_type), intent(out) :: next_obs
logical, intent(out) :: is_this_last
integer :: next_index
! Get index of the next observation
next_index = obs%next_time
if(next_index == -1) then
is_this_last = .true.
return
else
is_this_last = .false.
next_obs = seq%obs(next_index)
endif
!print *, 'next index = ', next_index
end subroutine get_next_obs
!-------------------------------------------------
subroutine get_prev_obs(seq, obs, prev_obs, is_this_first)
type(obs_sequence_type), intent(in) :: seq
type(obs_type), intent(in) :: obs
type(obs_type), intent(out) :: prev_obs
logical, intent(out) :: is_this_first
integer :: prev_index
! Get index of the next observation
prev_index = obs%prev_time
if(prev_index == -1) then
is_this_first= .true.
return
else
is_this_first= .false.
prev_obs = seq%obs(prev_index)
endif
end subroutine get_prev_obs
!-------------------------------------------------------------
subroutine get_obs_from_key(seq, key, obs)
type(obs_sequence_type), intent(in) :: seq
integer, intent(in) :: key
type(obs_type) :: obs
obs = seq%obs(key)
end subroutine get_obs_from_key
!-------------------------------------------------
subroutine get_next_obs_from_key(seq, last_key_used, next_obs, is_this_last)
type(obs_sequence_type), intent(in) :: seq
integer, intent(in) :: last_key_used
type(obs_type), intent(out) :: next_obs
logical, intent(out) :: is_this_last
integer :: next_index
! Get index of the next observation
next_index = seq%obs(last_key_used)%next_time
if(next_index == -1) then
is_this_last = .true.
return
else
is_this_last = .false.
next_obs = seq%obs(next_index)
endif
end subroutine get_next_obs_from_key
!-------------------------------------------------
subroutine get_prev_obs_from_key(seq, last_key_used, prev_obs, is_this_first)
type(obs_sequence_type), intent(in) :: seq
integer, intent(in) :: last_key_used
type(obs_type), intent(out) :: prev_obs
logical, intent(out) :: is_this_first
integer :: prev_index
! Get index of the next observation
prev_index = seq%obs(last_key_used)%prev_time
if(prev_index == -1) then
is_this_first= .true.
return
else
is_this_first= .false.
prev_obs = seq%obs(prev_index)
endif
end subroutine get_prev_obs_from_key
!-----------------------------------------------------------------
subroutine set_obs(seq, obs, key_in)
! Copies the obs into the key element of sequence where key is the key field
! in obs. If the integer argument key is present, the obs is copied into
! the key-th element of the sequence.
type(obs_sequence_type), intent(inout) :: seq
type(obs_type), intent(in) :: obs
integer, intent(in), optional :: key_in
integer :: key
! Get the key to copy into
if(present(key_in)) then
key = key_in
else
key = obs%key
endif
seq%obs(key) = obs
! Make sure the key in sequence is set properly
seq%obs(key)%key = key
end subroutine set_obs
!-------------------------------------------------------------------
subroutine get_obs_time_range(seq, time1, time2, key_bounds, num_keys, out_of_range, obs)
! Add other options for getting the first time to minimize search
type(obs_sequence_type), intent(in) :: seq
type(time_type), intent(in) :: time1, time2
integer, intent(out) :: key_bounds(2)
integer, intent(out) :: num_keys
logical, intent(out) :: out_of_range
type(obs_type), intent(in), optional :: obs
type(time_type) :: cur_time
type(obs_def_type) :: obs_def
integer :: current, last_key
! Returns the first key and last key of sequence of obs between time1 and
! time2 along with the total number.
! A complete list of the keys can be obtained by call to get_time_range_keys
! Logical out_of_range is true if the time range is all past the end of sequence times
num_keys = 0
out_of_range = .false.
! The optional argument obs says the search can be started at this observation
! Figure out where to begin search
if(present(obs)) then
current = obs%key
else
current = seq%first_time
endif
! Check for all observations after the last time in the window
call get_obs_def(seq%obs(current), obs_def)
cur_time = get_obs_def_time(obs_def)
if(cur_time > time2) then
out_of_range = .true.
return
endif
! Find the first element in the time window
do while(current /= -1)
call get_obs_def(seq%obs(current), obs_def)
cur_time = get_obs_def_time(obs_def)
if(cur_time >= time1) goto 10
current = seq%obs(current)%next_time
end do
! Falling off the end means there are no times greater than time1
out_of_range = .true.
return
10 continue
! current is pointer to first
! First pass, count the keys for storage requirements
key_bounds(1) = current
last_key = current
do while(current /= -1)
call get_obs_def(seq%obs(current), obs_def)
cur_time = get_obs_def_time(obs_def)
if(cur_time > time2) goto 20
! Found a time in the range
num_keys = num_keys + 1
last_key = current
current = seq%obs(current)%next_time
end do
20 continue
key_bounds(2) = last_key
end subroutine get_obs_time_range
!---------------------------------------------------------------
subroutine get_time_range_keys(seq, key_bounds, num_keys, keys)
! Given bounds from get_obs_time_range and an array keys big enough to hold
! all the keys in the range, returns the keys in the range
type(obs_sequence_type), intent(in) :: seq
integer, intent(in) :: key_bounds(2), num_keys
integer, intent(out) :: keys(num_keys)
integer :: current, i
! Now loop through again to get these keys
current = key_bounds(1)
do i = 1, num_keys
keys(i) = seq%obs(current)%key
current = seq%obs(current)%next_time
end do
end subroutine get_time_range_keys
!-------------------------------------------------
subroutine insert_obs_in_seq(seq, obs, prev_obs)
type(obs_sequence_type), intent(inout) :: seq
type(obs_type), intent(inout) :: obs
type(obs_type), intent(in), optional :: prev_obs
type(time_type) :: obs_time, current_time
integer :: prev, next, current
! Inserts an observation into a sequence, optional argument
! prev_obs says that this was the predecessor in time.
! This avoids time search in cases where one is building
! a sequence from scratch.
! Make sure there is room, fail for now if not
if(seq%num_obs >= seq%max_num_obs) then
! Later do an increase of space and copy
write(string1,*) 'ran out of room, num_obs (',seq%num_obs, &
') > max_num_obs (',seq%max_num_obs,')'
call error_handler(E_ERR,'insert_obs_in_seq',string1, source)
endif
! Set the key for the observation
obs%key = seq%num_obs + 1
seq%num_obs = seq%num_obs + 1
! Get the time for the observation
obs_time = get_obs_def_time(obs%def)
! Assume we're starting at the beginning.
! If we make this smarter eventually, here is where
! we'd set the initial key number for a search.
! If given an existing obs, be sure the new obs time is
! consistent - later or equal to the given previous obs.
if(present(prev_obs)) then
prev = prev_obs%key
current = prev
next = prev_obs%next_time
! it is an error to try to insert an observation after an
! existing obs which has a smaller timestamp.
if (prev /= -1) then
current_time = get_obs_def_time(seq%obs(prev)%def)
if (obs_time < current_time) then
!! or, do the insert searching from the start
!prev = -1
!current = -1
!next = seq%first_time
! error out
write(string1,*) 'time of prev_obs cannot be > time of new obs'
call error_handler(E_ERR,'insert_obs_in_seq',string1, source)
endif
endif
! the insert code will search forward starting at the
! given obs, so it is not an error to give an obs which
! has a larger time than the next obs.
else
! Start search at beginning
prev = -1
current = -1
next = seq%first_time
endif
! Have to search through the linked list to find last member
! already in with a time less than or equal to obs time
do while(next /= -1)
prev = current
current = next
next = seq%obs(current)%next_time
current_time = get_obs_def_time(seq%obs(current)%def)
! If the time of the observation in the sequence is >, stop
if(current_time > obs_time) then
! The observation that will follow the one being inserted is current
next = current
goto 10
endif
end do
! Falling off the end means that next is -1, so current should be previous for insertion
prev = current
! If the time check occured, previous is already pointing to previous
10 continue
! prev now holds the key of the previous observation, next holds the one after
! Link into the foward moving pointer chain
! If prev is -1, new observation goes at the start
if(prev == -1) then
obs%next_time = seq%first_time
obs%prev_time = -1
seq%first_time = obs%key
else
obs%prev_time = prev
obs%next_time = next
seq%obs(prev)%next_time = obs%key
endif
! Link into the backward moving pointer chain
if(next == -1) then
obs%prev_time = seq%last_time
obs%next_time = -1
seq%last_time = obs%key
else
seq%obs(next)%prev_time = obs%key
endif
! Finally, copy this obs structure into the sequence
seq%obs(obs%key) = obs
end subroutine insert_obs_in_seq
!----------------------------------------------------------------------
subroutine append_obs_to_seq(seq, obs)
! Appends an observation to an existing sequence; Error if new obs is
! not later than time of last obs already in seq
type(obs_sequence_type), intent(inout) :: seq
type(obs_type), intent(inout) :: obs
type(obs_type) :: last_obs
type(time_type) :: obs_time, last_time
! Initialize obs_type before using
call init_obs(last_obs, 0, 0)
! If this is first, just put it in
if(.not. get_last_obs(seq, last_obs)) then
call insert_obs_in_seq(seq, obs)
else
! Otherwise, get last obs from sequence and do insert with it as
! the previous after checking times
! Get the time for the observation
obs_time = get_obs_def_time(obs%def)
last_time = get_obs_def_time(last_obs%def)
if(obs_time < last_time) then
write(string1, *) 'time of appended obs cannot be < time of last obs in sequence'
call error_handler(E_ERR,'append_obs_to_seq',string1, source)
endif
!!! call insert_obs_in_seq(seq, obs)
!!! if(1 == 1) return
! Make sure there is room, fail for now if not
if(seq%num_obs >= seq%max_num_obs) then
! Later do an increase of space and copy
write(string1,*) 'ran out of room, max_num_obs = ',seq%max_num_obs
call error_handler(E_ERR,'append_obs_to_seq',string1, source)
endif
! Set the key for the observation
obs%key = seq%num_obs + 1
seq%num_obs = seq%num_obs + 1
! Link into the pointer chains
! Previous last points to this one, this one points back to previous last
obs%prev_time = seq%last_time
seq%obs(seq%last_time)%next_time = obs%key
seq%last_time = obs%key
! Appended is at end, put a -1 for the next
obs%next_time = -1
! Put this obs into the sequence's last slot
seq%obs(seq%num_obs) = obs
endif
! free any space allocated at init time.
call destroy_obs(last_obs)
end subroutine append_obs_to_seq
!---------------------------------------------------------------
!subroutine insert_obs_group_in_seq(seq, obs_grp, prev_obs)
! Insert a group of observations from the same time into a sequence
!type(obs_sequence_type), intent(inout) :: seq
!type(obs_type), intent(inout) :: obs
!type(obs_type), intent(in), optional :: prev_obs
!
!end subroutine insert_obs_group_in_seq
!-------------------------------------------------
subroutine delete_obs_from_seq(seq, obs)
! Removes this observation from the sequence, does not free storage in this implementation
type(obs_sequence_type), intent(inout) :: seq
type(obs_type), intent(inout) :: obs
integer :: prev, next
prev = obs%prev_time
next = obs%next_time
!print *, 'del key, initial prev,next=', obs%key, prev, next
! update obs count?? i think this should be done, but other code
! is not prepared to deal with it.
!seq%num_obs = seq%num_obs - 1
! If only one obs, seq first_time and last_time to -1
if(prev == -1 .and. next == -1) then
seq%first_time = -1
seq%last_time = -1
return
endif
! Previous should now point to next; if deleted was first update sequence first_time
if(prev /= -1) then
seq%obs(prev)%next_time = next
else
seq%obs(next)%prev_time = -1
seq%first_time = next
endif
! Next should point to previous; if deleted is last, set previous next_time to -1
if(next /= -1) then
seq%obs(next)%prev_time = prev
else
seq%obs(prev)%next_time = -1
seq%last_time = prev
endif
!print *, 'prev key, next = ', prev, seq%obs(prev)%next_time
!print *, 'next key, prev = ', next, seq%obs(next)%prev_time
!print *, 'seq entire first/last = ', seq%first_time, seq%last_time
end subroutine delete_obs_from_seq
!-------------------------------------------------
subroutine set_copy_meta_data(seq, copy_num, meta_data)
! Need all sorts of error checking to avoid silly stuff eventually
type(obs_sequence_type), intent(inout) :: seq
integer, intent(in) :: copy_num
character(len=*), intent(in) :: meta_data
character(len=len(meta_data)) :: lj_meta_data ! left justified version
lj_meta_data = adjustl(meta_data)
if (len_trim(lj_meta_data) > metadatalength) then
write(string1,*) 'metadata string [', trim(lj_meta_data),']'
write(string2,*) 'must be shorter than ',metadatalength
call error_handler(E_ERR, 'set_copy_meta_data', string1, source, text2=string2)
endif
if (copy_num > seq%num_copies) then
write(string1,*) 'trying to set copy (', copy_num, &
') which is larger than num_copies (', seq%num_copies, ')'
call error_handler(E_ERR,'set_copy_meta_data',string1, source)
endif
seq%copy_meta_data(copy_num) = trim(lj_meta_data)
end subroutine set_copy_meta_data
!-------------------------------------------------
subroutine set_qc_meta_data(seq, qc_num, meta_data)
! Need error checks
type(obs_sequence_type), intent(inout) :: seq
integer, intent(in) :: qc_num
character(len=*), intent(in) :: meta_data
character(len=len(meta_data)) :: lj_meta_data ! left justified version
lj_meta_data = adjustl(meta_data)
if (len_trim(lj_meta_data) > metadatalength) then
write(string1,*) 'metadata string [', trim(lj_meta_data),']'
write(string2,*) 'must be shorter than ',metadatalength
call error_handler(E_ERR, 'set_qc_meta_data', string1, source, text2=string2)
endif
if (qc_num > seq%num_qc) then
write(string1,*) 'trying to set qc (', qc_num, &
') which is larger than num_qc (', seq%num_qc, ')'
call error_handler(E_ERR,'set_qc_meta_data',string1, source)
endif
seq%qc_meta_data(qc_num) = trim(lj_meta_data)
end subroutine set_qc_meta_data
!-------------------------------------------------
function get_first_obs(seq, obs)
type(obs_sequence_type), intent(in) :: seq
type(obs_type), intent(out) :: obs
logical :: get_first_obs
if(seq%num_obs == 0 .or. seq%first_time <= 0) then
get_first_obs = .false.
else
get_first_obs = .true.
obs = seq%obs(seq%first_time)
endif
end function get_first_obs
!-------------------------------------------------
function get_last_obs(seq, obs)
type(obs_sequence_type), intent(in) :: seq
type(obs_type), intent(out) :: obs
logical :: get_last_obs
if(seq%num_obs == 0 .or. seq%last_time <=0) then
get_last_obs = .false.
return
else
get_last_obs = .true.
obs = seq%obs(seq%last_time)
endif
end function get_last_obs
!-------------------------------------------------
subroutine add_copies(seq, num_to_add)
! This requires a complete recreation of the entire obs sequence
! Add additional copies to an observation sequence. This increases
! the space for copy meta_data and goes through the whole string of
! observations deallocating and allocating (yuck), to add space.
! In the long run, may want a smoother way to do this globally.
type(obs_sequence_type), intent(inout) :: seq
integer, intent(in) :: num_to_add
character(len=metadatalength) :: meta_temp(seq%num_copies)
real(r8) :: values_temp(seq%num_copies)
integer :: i, old_num
old_num = seq%num_copies
seq%num_copies = old_num + num_to_add
! Copy the old copy metadata to temp storage, reallocate and copy