-
Notifications
You must be signed in to change notification settings - Fork 5
/
wrfgc_history_mod.F
1643 lines (1465 loc) · 67.7 KB
/
wrfgc_history_mod.F
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
! DESCRIPTION: Implement history.rc defined diagnostics output
! for wrf-gc like history_mod.F90 in GEOS-Chem
MODULE WRFGC_History_Mod
USE Precision_Mod
USE HistContainer_Mod, ONLY : HistContainer, HistContainer_Create
USE MetaHistContainer_Mod, ONLY : MetaHistContainer
USE Input_Opt_Mod, ONLY : OptInput
USE MetaHistItem_Mod
USE State_Chm_Mod , ONLY : ChmState
USE State_Diag_Mod, ONLY : DgnState
USE State_Grid_Mod, ONLY : GrdState
USE State_Met_Mod, ONLY : MetState
USE HistItem_Mod
USE GC_Stateful_Mod, ONLY : GIGC_States
IMPLICIT NONE
type MetaHistContainerptr
type(MetaHistContainer), pointer :: p;
end type MetaHistContainerptr
type MetaHistItemptr
type(MetaHistItem), pointer :: p;
end type MetaHistItemptr
! !LOCAL VARIABLES:
!
public :: wrfgc_history_init
TYPE(MetaHistContainerptr), dimension(1:8), PUBLIC :: CollectionList
TYPE(MetaHistItemptr), dimension(1:8), PUBLIC :: IndexVariables
! Scalars
INTEGER, PUBLIC :: CollectionCount
! Strings
CHARACTER(LEN=255), ALLOCATABLE :: CollectionName (: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionFileName (: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionTemplate (: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionFormat (: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionFrequency (: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionAccInterval(: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionDuration (: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionMode (: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionLonRange (: )
CHARACTER(LEN=255), ALLOCATABLE :: CollectionLatRange (: )
INTEGER, ALLOCATABLE :: CollectionSubsetInd (:,:)
CHARACTER(LEN=255), ALLOCATABLE :: CollectionLevels (: )
INTEGER, ALLOCATABLE :: CollectionLevelInd (:,:)
CHARACTER(LEN=255), ALLOCATABLE :: CollectionHrRange (: )
! Objects
!
! !DEFINED PARAMETERS:
!
! Maximum number of collections (set to a ridiculously big number)
INTEGER, PARAMETER :: MAX_COLLECTIONS = 500
INTEGER :: N
INTEGER, PARAMETER, PUBLIC :: UNDEFINED_INT = -999
REAL(f4), PARAMETER, PUBLIC :: UNDEFINED = -1.0e+31_f4
REAL(f8), PARAMETER, PUBLIC :: UNDEFINED_DBL = -1.0e+31_f8
CHARACTER(LEN=9), PARAMETER, PUBLIC :: UNDEFINED_STR = 'not found'
INTEGER, PUBLIC, PARAMETER :: GC_SUCCESS = 0 ! Routine returns success
INTEGER, PUBLIC, PARAMETER :: GC_FAILURE = -1 ! Routine returns failure
CONTAINS
subroutine wrfgc_history_init(Input_Opt, State_Chm, State_Diag, State_Met, State_Grid, comm)
USE InquireMod, ONLY : FindFreeLun
! USE ErrorMod, ONLY : GC_Error
USE Charpak_Mod
USE History_Util_Mod
USE MetaHistContainer_Mod
USE state_diag_mod
USe DiagList_Mod, ONLY : CollList, ColItem, Search_CollList
USE HCO_TYPES_MOD, ONLY : DiagnCollection, DiagnCont, DiagnBundle, ConfigObj
USE HCO_State_Mod, ONLY : HCO_State
USE Grid_Registry_Mod, ONLY : Lookup_Grid, Init_Grid_Registry
IMPLICIT NONE
type(OptInput), intent(in) :: Input_Opt
TYPE(ChmState), intent(in) :: State_Chm ! GEOS-Chem chemical state
TYPE(DgnState), intent(in) :: State_Diag ! GEOS-Chem diagnostic state
TYPE(MetState), intent(in) :: State_Met ! GEOS-Chem meteorological state
TYPE(GrdState), intent(in) :: State_Grid ! GEOS-Chem grid state
integer, intent(in) :: comm
TYPE(HistItem), POINTER :: Item ! HISTORY ITEM object
type(HistContainer), POINTER :: Container ! HISTORY CONTAINER object
TYPE(ColItem), POINTER :: Current
INTEGER :: RC
!
! !LOCAL VARIABLES:
!
! Scalars
LOGICAL :: EOF, Found
LOGICAL :: FileExists
INTEGER :: yyyymmdd, hhmmss
INTEGER :: yyyymmdd_end, hhmmss_end
INTEGER :: DeltaYMD, DeltaHMS
INTEGER :: X, Y
INTEGER :: C, N, W
INTEGER :: nX, nY, nZ
INTEGER :: fId, IOS, LineNum
INTEGER :: nSubs1, nSubs2
INTEGER :: Ind1, Ind2
INTEGER :: UpdateYmd, UpdateHms
INTEGER :: FileCloseYmd, FileCloseHms
INTEGER :: FileWriteYmd, FileWriteHms
INTEGER :: ItemCount, SpaceDim, Operation
INTEGER :: Ind_All, Ind_Adv, Ind_Aer
INTEGER :: Ind_Dry, Ind_Fix, Ind_Gas
INTEGER :: Ind_Kpp, Ind_Pho, Ind_Rst
INTEGER :: Ind_Var, Ind_Wet, Ind
INTEGER :: HbHrs, HbMin, HbSec
INTEGER :: HeartBeatHms, nTags
integer :: charsize, totalsize, offset
REAL(f8) :: UpdateAlarm, HeartBeatDtSec
REAL(f8) :: FileWriteAlarm, FileCloseAlarm
REAL(f8) :: JulianDate, JulianDateEnd
REAL(f8) :: UpdateCheck, FileWriteCheck
REAL(f8) :: SimLengthSec
! Strings
CHARACTER(LEN=6 ) :: TStr
CHARACTER(LEN=8 ) :: DStr
CHARACTER(LEN=20 ) :: StartTimeStamp, EndTimeStamp
CHARACTER(LEN=63 ) :: CName
CHARACTER(LEN=80 ) :: ErrorLine
CHARACTER(LEN=255) :: FileExpId
CHARACTER(LEN=255) :: Line, FileName
CHARACTER(LEN=255) :: OutputName, ThisLoc
CHARACTER(LEN=255) :: MetaData, Reference
CHARACTER(LEN=255) :: Title, Units
CHARACTER(LEN=255) :: ItemTemplate, ItemTemplateUC
CHARACTER(LEN=255) :: ItemName, Description
CHARACTER(LEN=255) :: TmpMode, Contact
CHARACTER(LEN=255) :: Pattern, ItemPrefix
CHARACTER(LEN=255) :: tagId, tagName
CHARACTER(LEN=512) :: ErrMsg, FileMsg
REAL(f8) :: Subset(2)
INTEGER :: Levels(200)
CHARACTER(LEN=255) :: Subs1(255)
CHARACTER(LEN=255) :: Subs2(255)
CHARACTER(LEN=255) :: SubStrs(255)
! Strings
CHARACTER(LEN=20) :: ItemDimNames(13)
CHARACTER(LEN=20) :: ItemNames(13)
CHARACTER(LEN=20) :: RegistryNames(13)
! Pointer arrays
REAL(f8), POINTER :: Ptr0d_8
REAL(f8), POINTER :: Ptr1d_8(: )
REAL(f8), POINTER :: Ptr2d_8(:,:)
REAL(f4), POINTER :: Ptr2d_4(:,:)
LOGICAL :: OnLevelEdges
INTEGER :: Output_KindVal
INTEGER :: Source_KindVal
INTEGER :: Rank
INTEGER :: nILev
INTEGER :: nLev
INTEGER :: Dimensions(3)
INTEGER :: Subset_X(2), Subset_Xc(2)
INTEGER :: Subset_Y(2), Subset_Yc(2)
INTEGER :: Subset_Z(2), Subset_Zc(2), Subset_Ze(2)
! initialize variables
EOF = .FALSE.
offset = 0
IOS = 0
UpdateYmd = 0
UpdateHms = 0
FileCloseYmd = 0
FileCloseHms = 0
FileWriteYmd = 0
FileWriteHms = 0
LineNum = 0
SpaceDim = 0
HeartBeatDtSec = DBLE( Input_Opt%TS_DYN )
yyyymmdd = Input_Opt%NymdB
hhmmss = Input_Opt%NhmsB
yyyymmdd_end = Input_Opt%NymdE
hhmmss_end = Input_Opt%NhmsE
Container => NULL()
Item => NULL()
C = 0
Ptr0d_8 => NULL()
Ptr1d_8 => NULL()
Ptr2d_8 => NULL()
Ptr2d_4 => NULL()
! do N = 1, 8
! allocate(IndexVariables(State_Grid%ID));
! allocate(CollectionList(State_Grid%ID));
! enddo
ThisLoc = " -> at wrfgc_history_init (in module wfrcg_history_mod.F)"
if(State_Grid%id == 1)then
call Init_Grid_Registry(Input_Opt, State_Grid, RC)
call checkgc(RC, 'Error initializing grid registry'//ThisLoc )
endif
CALL Compute_DeltaYmdHms_For_End( yyyymmdd, hhmmss, &
yyyymmdd_end, hhmmss_end, &
deltaYMD, deltaHMS )
! Initialize the collection list
! read collection : 1. collection name,
! 1). template, 2). frequency, 3). duration, 4). mode, 5). fields
call get_collection_name()
fId = FindFreeLun()
OPEN( fId, FILE=TRIM(Input_Opt%HistoryInputFile), STATUS='OLD', IOSTAT=RC )
ErrMsg = 'Error opening "' //TRIM(Input_Opt%HistoryInputFile) // '"!'
CALL checkgc( RC, ThisLoc//ErrMsg)
do
500 continue
Line = ReadOneLine(fId, EOF, IOS, Squeeze = .TRUE.)
LineNum = LineNum + 1
if (EOF) exit
IF ( IOS > 0 ) THEN
ErrMsg = 'Unexpected end-of-file in "' // &
TRIM( Input_Opt%HistoryInputFile )
WRITE( ErrorLine, 250 ) LineNum
250 FORMAT( ' -> ERROR occurred at (or near) line ', i6, &
' of the HISTORY.rc file' )
CALL checkgc( RC, ErrMsg//ThisLoc//ErrorLine )
RETURN
ENDIF
! skip commented line
if(Line(1:1) == '#') cycle
!====================================================================
! Get the EXPID string. This is the "front part" of the netCDF
! file path for each collection. In other words, if EXPID is
! "OutputDir/GEOSChem", then the default SpeciesConc collection file
! names will be "OutputDir/GEOSChem.SpeciesConc_YYYYMMDD_hhmmz.nc4"
!====================================================================
IF ( INDEX( Line, 'EXPID' ) > 0 ) THEN
! Split the line on the colon
CALL StrSplit( Line, ":", Subs1, nSubs1 )
! Stop with error if there are more than 2 substrings
IF ( nSubs1 /= 2 ) THEN
ErrMsg = 'Error in extracting the EXPID value from the ' // &
'HISTORY.rc file. This forms the start of the ' // &
'netCDF file name for each collection. Please ' // &
'check the HISTORY.rc file for typos.'
WRITE( ErrorLine, 250 ) LineNum
CALL checkgc( RC, ThisLoc//ErrorLine //ErrMsg)
ENDIF
! Save the EXPID parameter
FileExpId = Subs1(2)
CALL CStrip( FileExpId )
ENDIF
! "template": Specifies the year/month/day/hr/min/sec in filenames
! Can be omitted if "filename" is specified
Pattern = 'template'
IF ( INDEX( TRIM( Line ), TRIM( Pattern ) ) > 0 ) THEN
CALL GetCollectionMetaData( Input_Opt, Line, Pattern, MetaData, C )
IF ( C > 0 ) then
! write(*,*)"get template of collection: ", TRIM(CollectionName(C))
CollectionTemplate(C) = Metadata
endif
ENDIF
! "format": Specifies the file output format (e.g. netCDF-4, CFIO)
Pattern = 'format'
IF ( INDEX( TRIM( Line ), TRIM( Pattern ) ) > 0 ) THEN
CALL GetCollectionMetaData( Input_Opt, Line, Pattern, MetaData, C )
IF ( C > 0 )then
! write(*,*)'get format of collection: ', TRIM(CollectionName(C))
CollectionFormat(C) = Metadata
endif
ENDIF
! "frequency": Specifies how often diagnostics are updated,
! Must be either in "YYYYMMDD hhmmss" or "hhmmss" format.
Pattern = 'frequency'
IF ( INDEX( TRIM( Line ), TRIM( Pattern ) ) > 0 ) THEN
CALL GetCollectionMetaData( Input_Opt, Line, Pattern, MetaData, C )
IF ( C > 0 ) THEN
IF ( LEN_TRIM( MetaData ) == 6 .or. &
LEN_TRIM( MetaData ) == 14 .or. &
TRIM( MetaData ) == 'End' .or. &
TRIM( MetaData ) == 'end' ) THEN
CollectionFrequency(C) = Metadata
! write(*,*)'get frequency of collection: ', TRIM(CollectionName(C))
ELSE
ErrMsg = 'Error in defining "frequency" for collection "' // &
TRIM( CollectionName(C) ) // '"! This field ' // &
'must either be of the format "YYYYMMDD ' // &
'hhmmss", "hhmmss", or "End". Please check the '// &
'"frequency" setting in the HISTORY.rc file.'
WRITE( ErrorLine, 250 ) LineNum
CALL checkgc( RC, ThisLoc//ErrorLine //ErrMsg)
RETURN
ENDIF
ENDIF
ENDIF
Pattern = 'duration'
IF ( INDEX( TRIM( Line ), TRIM( Pattern ) ) > 0 ) THEN
CALL GetCollectionMetaData( Input_Opt, Line, Pattern, MetaData, C )
IF ( C > 0 ) THEN
IF ( LEN_TRIM( MetaData ) == 6 .or. &
LEN_TRIM( MetaData ) == 14 .or. &
TRIM( MetaData ) == 'End' .or. &
TRIM( MetaData ) == 'end' ) THEN
CollectionDuration(C) = Metadata
! write(*,*)'get duration of collection: ', TRIM(CollectionName(C))
ELSE
ErrMsg = 'Error in defining "duration" for collection "' // &
TRIM( CollectionName(C) ) // '"! This field ' // &
'must either be of the format "YYYYMMDD ' // &
'hhmmss", "hhmmss", or "End". Please check the '// &
'"duration" setting in the HISTORY.rc file.'
WRITE( ErrorLine, 250 ) LineNum
CALL checkgc( RC, ThisLoc//ErrorLine //ErrMsg)
RETURN
ENDIF
ENDIF
ENDIF
Pattern = 'mode'
IF ( INDEX( TRIM( Line ), TRIM( Pattern ) ) > 0 ) THEN
CALL GetCollectionMetaData( Input_Opt, Line, Pattern, MetaData, C )
IF ( C > 0 ) THEN
TmpMode = Metadata
! write(*,*)'get mode of collection: ', TRIM(CollectionName(C))
CALL TranUc( TmpMode )
SELECT CASE( TmpMode )
CASE( 'INSTANTANEOUS', 'TIME-AVERAGED', 'TIMEAVERAGED' )
CollectionMode(C) = Metadata
CASE DEFAULT
ErrMsg = 'Error in defining "mode" for collection "' // &
TRIM( CollectionName(C) ) // '"! The mode ' // &
'value can either be "instantaneous" or ' // &
'"time-averaged". Please check the "mode" ' // &
'setting in the HISTORY.rc file.'
WRITE( ErrorLine, 250 ) LineNum
CALL checkgc( RC, ThisLoc//ErrorLine //ErrMsg)
RETURN
END SELECT
ENDIF
ENDIF
if (INDEX(trim(Line), 'fields') > 0) then
if (C>0)then
! write(*,*)'get fields of collection: ', TRIM(CollectionName(C))
! write(*,*)'Line: ', trim(Line), &
! 'CollectionName: ', TRIM(CollectionName(C)), &
! 'CollectionFileName: ', TRIM(CollectionFileName(C)), &
! 'CollectionTemplate: ', TRIM(CollectionTemplate(C)), &
! 'CollectionFormat: ', TRIM(CollectionFormat(C)), &
! 'CollectionFrequency: ', TRIM(CollectionFrequency(C)), &
! 'CollectionAccInterval: ', TRIM(CollectionAccInterval(C)), &
! 'CollectionDuration: ', TRIM(CollectionDuration(C)), &
! 'CollectionMode: ', TRIM(CollectionMode(C))
! Frequecy indicates how often the data will be writen to file
IF ( LEN_TRIM( CollectionFrequency(C) ) == 6 ) THEN
READ( CollectionFrequency(C), '(i6.6)' ) FileWriteHms
ELSE IF ( LEN_TRIM( CollectionFrequency(C) ) == 14 ) THEN
READ( CollectionFrequency(C), '(i8,i6)' ) FileWriteYmd, &
FileWriteHms
ELSE IF ( TRIM( CollectionFrequency(C) ) == 'End' .or. &
TRIM( CollectionFrequency(C) ) == 'end' ) THEN
FileWriteYmd = DeltaYMD
FileWriteHms = DeltaHMS
ENDIF
IF ( FileWriteHms == 240000 ) THEN
FileWriteYmd = 00000001
FileWriteHms = 000000
ENDIF
UpdateYmd = FileWriteYmd
UpdateHms = FileWriteHms
! Duration indicates how often the new file will be created
IF ( TRIM( CollectionDuration(C) ) == UNDEFINED_STR ) THEN
FileCloseYmd = FileWriteYmd
FileCloseHms = FileWriteHms
ELSE IF ( LEN_TRIM( CollectionDuration(C) ) == 6 ) THEN
READ( CollectionDuration(C), '(i6.6)' ) FileCloseHms
ELSE IF ( LEN_TRIM( CollectionDuration(C) ) == 14 ) THEN
READ( CollectionDuration(C), '(i8,i6)' ) FileCloseYmd, &
FileCloseHms
ELSE IF ( TRIM( CollectionDuration(C) ) == 'End' .or. &
TRIM( CollectionDuration(C) ) == 'end' ) THEN
FileCloseYmd = DeltaYMD
FileCloseHms = DeltaHMS
ENDIF
call HistContainer_Create( Input_Opt = Input_Opt, &
Container = Container, &
id = C, &
name = CollectionName(C), &
CurrentYmd = yyyymmdd, &
CurrentHms = hhmmss, &
UpdateMode = CollectionMode(C), &
UpdateYmd = UpdateYmd, &
UpdateHms = UpdateHms, &
FileWriteYmd = FileWriteYmd, &
FileWriteHms = FileWriteHms, &
FileCloseYmd = FileCloseYmd, &
FileCloseHms = FileCloseHms, &
FileName = CollectionFileName(C),&
FileTemplate = CollectionTemplate(C),&
FileExpId = FileExpId, &
RC = RC )
endif
! get collection name
N = INDEX( Trim(Line), '.' )
CName = Line(1:N-1)
! skip all fileds if collection name is not in the list
CALL Search_CollList( Input_Opt%amIRoot, CollList, CName, Found, RC )
IF ( .not. Found ) then
do
Line = ReadOneLine(fId, EOF, IOS, Squeeze = .TRUE.)
LineNum = LineNum + 1
call StrSqueeze(Line)
if (TRIM(Line) == '::') GOTO 500
end do
END IF
! Zero the counter of items
ItemCount = 0
! Create title string for collection
Title = 'GEOS-Chem diagnostic collection: ' // &
TRIM( CollectionName(C) )
! read item name
do
if (ItemCount == 0)then
call GetCollectionMetaData(Input_Opt, Line, 'fields', MetaData, C)
call StrSplit(MetaData, " ", Subs1, nSubs1)
ItemName = Subs1(1)
else
Line = ReadOneLine(fId, EOF, IOS, Squeeze = .TRUE.)
LineNum = LineNum + 1
Line = CleanText(Line)
if(Line(1:1) == "#") cycle
if (Line(1:2)== '::') exit
if (EOF) exit
if (IOS > 0) then
ErrMsg = 'Unexpected end-of-file in "' // &
TRIM( Input_Opt%HistoryInputFile )
WRITE( ErrorLine, 250 ) LineNum
CALL checkgc( RC, ErrMsg//ThisLoc//ErrorLine )
RETURN
ENDIF
call StrSplit(Line, " ", Subs1, nSubs1)
ItemName = Subs1(1)
end if
ItemTemplate = ItemName
ItemTemplateUC = To_UpperCase(ItemTemplate)
if (index(ItemTemplate, "?")>0) then
call StrSplit(ItemTemplate, '?', SubStrs, N)
tagId = SubStrs(N-1)
ItemPrefix = SubStrs(1)
! Get number of tags for this wildcard
CALL Get_TagInfo( Input_Opt, tagId, State_Chm, Found, RC, &
nTags=nTags )
ErrMsg = 'Error retrieving # of tags for' // &
' wildcard ' // TRIM(tagId)
call checkgc(RC, ErrMsg)
! add tag to the item
do N = 1, nTags
call Get_TagInfo(Input_Opt, tagId, State_Chm, Found, RC, &
N=N, tagName=tagName)
ErrMsg = 'Error retreving tag name for wildcard '//trim(tagId)
call checkgc(RC, ErrMsg)
ItemName = trim(ItemPrefix)//trim(tagName)
call Get_NameInfo(Input_Opt, ItemName, OutputName, RC)
ItemCount = ItemCount + 1
call History_AddItemToCollection(Input_Opt = Input_Opt, &
State_Chm = State_Chm, &
State_Diag = State_Diag, &
State_Met = State_Met, &
ItemName = OutputName, &
ItemCount = ItemCount, &
Collection = Container, &
CollectionId = C, &
RC=RC &
)
ErrMsg = 'Could not add diagnostic "' // &
TRIM( OutputName ) // '" to collection: ' // &
TRIM( CollectionName(C) )
enddo
else
! the item has no wildcard
call Get_NameInfo(Input_Opt, ItemName, OutputName, RC)
ItemCount = ItemCount + 1
call History_AddItemToCollection(Input_Opt = Input_Opt, &
State_Chm = State_Chm, &
State_Diag = State_Diag, &
State_Met = State_Met, &
ItemName = OutputName, &
ItemCount = ItemCount, &
Collection = Container, &
CollectionId = C, &
RC=RC &
)
ErrMsg = 'Could not add diagnostic "' // &
TRIM( OutputName ) // '" to collection: ' // &
TRIM( CollectionName(C) )
endif
enddo
call MetaHistContainer_AddNew( Input_Opt = Input_Opt, &
Node = CollectionList(State_Grid%ID)%p, &
Container = Container, &
RC = RC )
ErrMsg = 'Could not add Container' // &
TRIM( CollectionName(C) ) // &
' to the list of collections!'
call checkgc(RC, ErrMsg)
C = 0
endif
enddo
! add HISTORY ITEM for each netCDF index variable (e.g.
! lon, lat, lev, time, area, hyam, hybm, p0)
! Fields saved in the Registry object in GeosUtil/grid_registry_mod.F90
! The registry object current only works for domain 1
if (State_Grid%ID == 1) then
RegistryNames(1 ) = 'GRID_AREA'
RegistryNames(2 ) = 'GRID_P0'
RegistryNames(3 ) = 'GRID_HYBI'
RegistryNames(4 ) = 'GRID_HYAI'
RegistryNames(5 ) = 'GRID_HYBM'
RegistryNames(6 ) = 'GRID_HYAM'
RegistryNames(7 ) = 'GRID_LON'
RegistryNames(8 ) = 'GRID_LONBND'
RegistryNames(9 ) = 'GRID_LAT'
RegistryNames(10) = 'GRID_LATBND'
RegistryNames(11) = 'GRID_ILEV'
RegistryNames(12) = 'GRID_LEV'
RegistryNames(13) = 'GRID_TIME'
! Name for each HISTORY ITEM
ItemNames(1 ) = 'AREA'
ItemNames(2 ) = 'P0'
ItemNames(3 ) = 'hybi'
ItemNames(4 ) = 'hyai'
ItemNames(5 ) = 'hybm'
ItemNames(6 ) = 'hyam'
ItemNames(7 ) = 'lon'
ItemNames(8 ) = 'lon_bnds'
ItemNames(9 ) = 'lat'
ItemNames(10) = 'lat_bnds'
ItemNames(11) = 'ilev'
ItemNames(12) = 'lev'
ItemNames(13) = 'time'
! Dimensions for each HISTORY ITEM
ItemDimNames(1 ) = 'xy'
ItemDimNames(2 ) = '-'
ItemDimNames(3 ) = 'z'
ItemDimNames(4 ) = 'z'
ItemDimNames(5 ) = 'z'
ItemDimNames(6 ) = 'z'
ItemDimNames(7 ) = 'x'
ItemDimNames(8 ) = 'bx'
ItemDimNames(9 ) = 'y'
ItemDimNames(10) = 'by'
ItemDimNames(11) = 'z'
ItemDimNames(12) = 'z'
ItemDimNames(13) = 't'
DO N = 2, SIZE(RegistryNames)
!---------------------------------------------------------------------
! Look up one of the index fields from grid_registry_mod.F90
!---------------------------------------------------------------------
CALL Lookup_Grid( Input_Opt = Input_Opt, &
Variable = RegistryNames(N), &
Description = Description, &
Dimensions = Dimensions, &
Source_KindVal = Source_KindVal, &
Output_KindVal = Output_KindVal, &
Rank = Rank, &
Units = Units, &
OnLevelEdges = OnLevelEdges, &
Ptr0d_8 = Ptr0d_8, &
Ptr1d_8 = Ptr1d_8, &
Ptr2d_8 = Ptr2d_8, &
Ptr2d_4 = Ptr2d_4, &
RC = RC )
ErrMsg = 'Error looking up grid variable "' // TRIM( RegistryNames(N) ) // '"'
CALL checkgc( RC, ErrMsg )
CALL Get_Number_Of_Levels( Container, nLev, nIlev )
! Subset indices
Subset_Xc = (/CollectionList(State_Grid%ID)%p%Container%X0,CollectionList(State_Grid%ID)%p%Container%X1/)
Subset_Yc = (/ CollectionList(State_Grid%ID)%p%Container%Y0,CollectionList(State_Grid%ID)%p%Container%Y1 /)
Subset_Zc = (/ CollectionList(State_Grid%ID)%p%Container%Z0, nLev /)
Subset_Ze = (/ CollectionList(State_Grid%ID)%p%Container%Z0, nILev /)
SELECT CASE( N )
CASE( 2 ) ! P0
Subset_X = (/ 0, 0 /)
Subset_Y = (/ 0, 0 /)
Subset_Z = (/ 0, 0 /)
CASE( 3, 4, 11 ) ! hybi, hyai, ilev
Subset_X = Subset_Xc
Subset_Y = Subset_Yc
Subset_Z = Subset_Ze
CASE( 8 ) ! lon_bnds
Subset_X = (/ 1, 2 /)
Subset_Y = Subset_Xc
Subset_Z = (/ 0, 0 /)
CASE( 10 ) ! lat_bnds
Subset_X = (/ 1, 2 /)
Subset_Y = Subset_Yc
Subset_Z = (/ 0, 0 /)
CASE DEFAULT ! everything else
Subset_X = Subset_Xc
Subset_Y = Subset_Yc
Subset_Z = Subset_Zc
END SELECT
write(*,*)'dingao debug: ', ItemNames(N)
!---------------------------------------------------------------------
! Create a HISTORY ITEM for this index field
!---------------------------------------------------------------------
if(N==7)then
write(*,*)N
write(*,*)Ptr1d_8
endif
CALL HistItem_Create( Input_Opt = Input_Opt, &
Item = Item, &
Id = N, &
ContainerId = 0, &
Name = ItemNames(N), &
LongName = Description, &
Units = Units, &
SpaceDim = Rank, &
OnLevelEdges = OnLevelEdges, &
DimNames = ItemDimNames(N), &
Operation = 0, &
Subset_X = Subset_X, &
Subset_Y = Subset_Y, &
Subset_Z = Subset_Z, &
Source_KindVal = Source_KindVal, &
Output_KindVal = Output_KindVal, &
Source_0d_8 = Ptr0d_8, &
Source_1d_8 = Ptr1d_8, &
Source_2d_4 = Ptr2d_4, &
Source_2d_8 = Ptr2d_8, &
RC = RC )
ErrMsg = 'Error creating HISTORY ITEM for grid variable "' // &
TRIM( RegistryNames(N) ) // '"'
CALL checkgc( RC, ErrMsg )
call MetaHistItem_AddNew( Input_Opt = Input_Opt, &
Item = Item, &
Node = IndexVariables(State_Grid%ID)%p, &
RC = RC )
ErrMsg = 'Could not add HISTORY ITEM for grid variable "' // &
TRIM( RegistryNames(N) ) // '" to the list of HISTORY ITEMS!'
call checkgc(RC, ErrMsg)
enddo
endif
!============================
! clean up and quit
!============================
close(fId)
! Write output
IF ( Input_Opt%amIRoot ) THEN
WRITE( 6, '(/,a)' ) REPEAT( '=', 79 )
WRITE( 6, '(a )' ) 'DEFINED DIAGNOSTIC COLLECTIONS:'
WRITE( 6, '(a )' ) REPEAT( '=', 79 )
DO C = 1, CollectionCount
print*, 'Collection ', TRIM( CollectionName (C) )
print*, ' -> FileName ', TRIM( CollectionFileName (C) )
print*, ' -> Format ', TRIM( CollectionFormat (C) )
print*, ' -> Frequency ', TRIM( CollectionFrequency (C) )
IF ( CollectionAccInterval(C) /= UNDEFINED_STR ) THEN
print*, ' -> Acc_Interval ', TRIM( CollectionAccInterval(C) )
ENDIF
IF ( CollectionDuration(C) /= UNDEFINED_STR ) THEN
print*, ' -> Duration ', TRIM( CollectionDuration (C) )
ENDIF
print*, ' -> Mode ', TRIM( CollectionMode (C) )
IF ( CollectionLonRange(C) /= UNDEFINED_STR ) THEN
print*, ' -> LON_RANGE ', &
TRIM(ADJUSTL(ADJUSTR( CollectionLonRange(C) )))
print*, ' -> X0 X1 ', ((CollectionSubsetInd(N,C)), N=1,2)
ENDIF
IF ( CollectionLatRange(C) /= UNDEFINED_STR ) THEN
print*, ' -> LAT_RANGE ', &
TRIM(ADJUSTL(ADJUSTR( CollectionLatRange(C) )))
print*, ' -> Y0 Y1 ', ((CollectionSubsetInd(N,C)), N=3,4)
ENDIF
IF ( CollectionLevels(C) /= UNDEFINED_STR ) THEN
print*, ' -> Levels ' , TRIM( CollectionLevels(C) )
print*, ' -> Z0 Z1 ', ((CollectionLevelInd(N,C)), N=1,2)
ENDIF
IF ( CollectionHrRange(C) /= UNDEFINED_STR ) THEN
print*, ' -> hrrange ' , TRIM( CollectionHrRange(C) )
ENDIF
! Trap error if the collection frequency is undefined
! This indicates an error in parsing the file
IF ( TRIM( CollectionFrequency(C) ) == UNDEFINED_STR ) THEN
ErrMsg = 'Collection: ' // TRIM( CollectionName(C) ) // &
' is undefined!'
WRITE( ErrorLine, 250 ) LineNum
call checkgc( RC, ThisLoc//ErrorLine //ErrMsg)
RETURN
ENDIF
ENDDO
ENDIF
IF (Input_Opt%amIRoot ) THEN
! Print information about each diagnostic collection
CALL MetaHistContainer_Print( Input_Opt, CollectionList(State_Grid%ID)%p, RC )
ENDIF
! Write spacer
WRITE( 6, '(a,/)' ) REPEAT( '=', 79 )
end subroutine wrfgc_history_init
! !IROUTINE: GetCollectionMetaData
!
! !DESCRIPTION: Parses a line of the HISTORY.rc file and returns metadata
! for a given attribute (e.g. "frequency", "template", etc.)
!\\
!\\
! !INTERFACE:
!
SUBROUTINE GetCollectionMetaData( Input_Opt, Line, Pattern, MetaData, &
nCollection )
!
! !USES:
!
USE Charpak_Mod, ONLY : CleanText, StrSplit
USE DiagList_Mod, ONLY : CollList, Search_CollList
USE History_Util_Mod
USE Input_Opt_Mod, ONLY : OptInput
!
! !INPUT PARAMETERS:
!
TYPE(OptInput), INTENT(IN) :: Input_Opt ! Input Options object
CHARACTER(LEN=*), INTENT(IN) :: Line ! Line to be searched
CHARACTER(LEN=*), INTENT(IN) :: Pattern ! Search pattern
!
! !OUTPUT PARAMETERS:
!
CHARACTER(LEN=255), INTENT(OUT) :: MetaData ! Metadata value
INTEGER, INTENT(OUT) :: nCollection ! Collection Id
!
! !REVISION HISTORY:
! 16 Aug 2017 - R. Yantosca - Initial version
! See https://github.com/geoschem/geos-chem for complete history
!EOP
!------------------------------------------------------------------------------
!BOC
!
! !LOCAL VARIABLES:
!
! Scalars
LOGICAL :: Found
INTEGER :: C, Ind, nSubStr, N, P, RC
! Strings
CHARACTER(LEN=255) :: Name
CHARACTER(LEN=255) :: SubStr(255)
!=======================================================================
! Initialize
!=======================================================================
nCollection = UNDEFINED_INT
MetaData = UNDEFINED_STR
!=======================================================================
! Find the metadata for the given collection
!=======================================================================
! The collection name is between column 1 and the first "." character
Ind = INDEX( TRIM( Line ), '.' )
Name = Line(1:Ind-1)
! Exit if the collection name is not in the list of active collections
CALL Search_CollList( Input_Opt%amIRoot, CollList, Name, Found, RC )
IF ( .not. Found ) RETURN
! Non-white-space lengths of the collection cname and search pattern
N = LEN_TRIM( Name )
P = LEN_TRIM( Pattern )
! Loop over all collection names
! NOTE: This algorithm may not be the most efficient, as it will
! not skip collections that we have already encountered. But it
! only gets done during the init phase, so it might not be a huge
! expenditure of time anyway. Worry about this later. (bmy, 1/18/18)
DO C = 1, CollectionCount
! Check to see if the current line matches the collection name
! Then check to see which collection this is in
IF ( Name(1:30) == CollectionName(C)(1:30) ) THEN
Ind = 1
ELSE
Ind = 0
ENDIF
! If the we match the current collection, then ...
IF ( Ind > 0 ) THEN
! Split the line on the the colon
CALL StrSplit( Line, ':', SubStr, nSubStr )
! If there are 2 substrings ...
IF ( nSubStr == 2 ) THEN
! Make sure the first substring matches the name
! of the metadata field we would like to obtain.
! if it does, then we have found a match, and so return
IF ( SubStr(1)(N+2:P+N+1) == Pattern(1:P) ) THEN
nCollection = C
MetaData = CleanText( SubStr(2) )
EXIT
ENDIF
ENDIF
ENDIF
ENDDO
END SUBROUTINE GetCollectionMetaData
! read collection name into CollectionName and CollectionCount
subroutine get_collection_name()
USe DiagList_Mod, ONLY : CollList, ColItem
TYPE(ColItem), POINTER :: Current
CHARACTER(LEN=255) :: TmpCollectionName(MAX_COLLECTIONS)
CHARACTER(LEN=255) :: ErrMsg, ThisLoc, Line, Line2
INTEGER :: RC
! Zero local variables
RC = GC_SUCCESS
ErrMsg = 'No error'
ThisLoc = &
' -> at get_collection_name (in module wfrcg_history_mod.F)'
CollectionCount = 0
Current => CollList%head
do while(associated(Current))
CollectionCount = CollectionCount + 1
TmpCollectionName(CollectionCount) = Current%cname
Current => Current%next
end do
Current => NULL()
if ( .not. allocated(CollectionName) ) then
allocate(CollectionName(CollectionCount), STAT=RC)
call checkgc(RC, 'Could not allocate CollectionName!'//ThisLoc)
end if
! Copy the collection names from the temporary array
! NOTE: The linked-list stors in reverse order, so revert here
DO N = 1, CollectionCount
CollectionName(N) = TmpCollectionName(CollectionCount-N+1)
ENDDO
write(*,*)'collection name:', ThisLoc
DO N = 1, CollectionCount
write(*,*)CollectionName(N)
enddo
! Allocate CollectionFileName
IF ( .not. ALLOCATED( CollectionFileName ) ) THEN
ALLOCATE( CollectionFileName( CollectionCount ), STAT=RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Could not allocate CollectionFileName!'
CALL checkgc( RC, ErrMsg//ThisLoc )
RETURN
ENDIF
CollectionFileName = UNDEFINED_STR
ENDIF
! Allocate CollectionTemplate
IF ( .not. ALLOCATED( CollectionTemplate ) ) THEN
ALLOCATE( CollectionTemplate( CollectionCount ), STAT=RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Could not allocate CollectionTemplate!'
CALL checkgc( RC, ErrMsg//ThisLoc )
RETURN
ENDIF
CollectionTemplate = UNDEFINED_STR
ENDIF
! Allocate CollectionFormat
IF ( .not. ALLOCATED( CollectionFormat ) ) THEN
ALLOCATE( CollectionFormat( CollectionCount ), STAT=RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Could not allocate CollectionFormat!'
CALL checkgc( RC, ErrMsg//ThisLoc )
RETURN
ENDIF
CollectionFormat = UNDEFINED_STR
ENDIF
! Allocate CollectionFrequency
IF ( .not. ALLOCATED( CollectionFrequency ) ) THEN
ALLOCATE( CollectionFrequency( CollectionCount ), STAT=RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Could not allocate CollectionFrequency!'
CALL checkgc( RC, ErrMsg//ThisLoc )
RETURN
ENDIF
CollectionFrequency = UNDEFINED_STR
ENDIF
! Allocate CollectionAccInterval
IF ( .not. ALLOCATED( CollectionAccInterval ) ) THEN
ALLOCATE( CollectionAccInterval( CollectionCount ), STAT=RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Could not allocate CollectionAccInterval!'
CALL checkgc( RC, ErrMsg//ThisLoc )
RETURN
ENDIF
CollectionAccInterval = UNDEFINED_STR
ENDIF
! Allocate CollectionDuration
IF ( .not. ALLOCATED( CollectionDuration ) ) THEN
ALLOCATE( CollectionDuration( CollectionCount ), STAT=RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Could not allocate CollectionDuration!'
CALL checkgc( RC, ErrMsg//ThisLoc )
RETURN
ENDIF
CollectionDuration = UNDEFINED_STR
ENDIF
! Allocate CollectionMode
IF ( .not. ALLOCATED( CollectionMode ) ) THEN
ALLOCATE( CollectionMode( CollectionCount ), STAT=RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Could not allocate CollectionMode!'
CALL checkgc( RC, ErrMsg//ThisLoc )
RETURN
ENDIF
CollectionMode = UNDEFINED_STR
ENDIF
! Allocate CollectionLonRange
IF ( .not. ALLOCATED( CollectionLonRange ) ) THEN
ALLOCATE( CollectionLonRange( CollectionCount ), STAT=RC )
IF ( RC /= GC_SUCCESS ) THEN
ErrMsg = 'Could not allocate CollectionLonRange!'
CALL checkgc( RC, ErrMsg//ThisLoc )
RETURN
ENDIF