-
Notifications
You must be signed in to change notification settings - Fork 31
/
hco_config_mod.F90
5220 lines (4652 loc) · 179 KB
/
hco_config_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
!------------------------------------------------------------------------------
! Harmonized Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !MODULE: hco_config_mod.F90
!
! !DESCRIPTION: Module HCO\_Config\_Mod contains routines related
! to the HEMCO configuration file. It reads the content of the
! configuration file, checks which entires therein are actually used
! for this simulation run, and stores these information. This occurs
! in two calls: Config\_ReadFile and SetReadList. Config\_ReadFile
! writes the entire content of the configuration file into buffer
! except for the input data associated with a disabled extension.
! SetReadList does many more logical checks and adds all data used
! by HEMCO to ReadList. Scale factors not used by any of the base
! emissions and base emission fields (e.g. scale factors that won't
! be used ) are removed in this step.
!\\
!\\
! All data fields are saved in individual data containers, which are
! organized in the ConfigList. Hence, ConfigList is a collection of
! all HEMCO data containers, with every container representing an
! entry of the configuration file. Each data container has its unique
! container ID for identification. All HEMCO lists (ConfigList,
! ReadList, EmisList) access the same containers.
!\\
!\\
! The configuration file provides all source file information of the
! emission fields and scale factors to be used. It must be read at the
! beginning of a simulation run.
!\\
!\\
! As of HEMCO v2.0, the ConfigList linked list sits within the HEMCO
! configuration object (HcoConfig). HcoConfig must be passed to all
! routines. This allows the parallel usage of multiple invocations
! of HEMCO that use different input data. HcoConfig is initialized
! upon reading the HEMCO configuration file (within subroutine
! Config\_ReadFile).
!\\
!\\
! !INTERFACE:
!
MODULE HCO_Config_Mod
!
! !USES:
!
USE HCO_ERROR_MOD
USE HCO_DIAGN_MOD
USE HCO_CHARTOOLS_MOD
USE HCO_TYPES_MOD
USE HCO_STATE_MOD, ONLY : HCO_State
IMPLICIT NONE
PRIVATE
!
! !PUBLIC MEMBER FUNCTIONS:
!
PUBLIC :: SetReadList
PUBLIC :: Config_ReadFile
PUBLIC :: Config_GetnSpecies
PUBLIC :: Config_GetSpecNames
PUBLIC :: ConfigInit
!
! !PRIVATE MEMBER FUNCTIONS:
!
PRIVATE :: ReadSettings
PRIVATE :: ExtSwitch2Buffer
PRIVATE :: ConfigList_AddCont
PRIVATE :: Config_ReadCont
PRIVATE :: RegisterPrepare
PRIVATE :: Get_targetID
PRIVATE :: Calc_Coverage
PRIVATE :: Register_Base
PRIVATE :: Register_Scal
PRIVATE :: ReadAndSplit_Line
PRIVATE :: Config_GetSpecAttr
PRIVATE :: BracketCheck
PRIVATE :: AddZeroScal
PRIVATE :: AddShadowFields
PRIVATE :: ParseEmisL
PRIVATE :: CheckForDuplicateName
PRIVATE :: Hco_GetTagInfo
!
! !REVISION HISTORY:
! 18 Jun 2013 - C. Keller - Initialization
! See https://github.com/geoschem/hemco for complete history
!EOP
!------------------------------------------------------------------------------
!BOC
!
! !MODULE TYPES/ARGUMENTS:
!
!----------------------------------------------------------------
! MODULE ROUTINES follow below
!----------------------------------------------------------------
CONTAINS
!EOC
!------------------------------------------------------------------------------
! Harmonized Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !IROUTINE: Config_Readfile
!
! !DESCRIPTION: Subroutine CONFIG\_READFILE reads the HEMCO configuration file,
! archives all HEMCO options and settings (including traceback/error setup),
! and creates a data container for every (used) emission field in the config.
! file. All containers become linked through the ConfigList linked list.
! Note that lists EmisList and ReadList (created lateron) will point to the
! same containers, but will order the containers in a manner that is most
! efficient for the respective purpose.
! Argument HcoConfig represents the HEMCO configuration object. It contains
! pointers to the HEMCO traceback and error information as well as a pointer
! to ConfigList. If undefined, HcoConfig becomes initialized as part of this
! routine.
!\\
!\\
! !INTERFACE:
!
SUBROUTINE Config_ReadFile( am_I_Root, HcoConfig, ConfigFile, Phase, &
RC, IsNest, IsDryRun )
!
! !USES:
!
USE HCO_inquireMod, ONLY : findFreeLUN
USE HCO_CharPak_Mod, ONLY : STRREPL
USE HCO_EXTLIST_MOD, ONLY : AddExt, CoreNr, ExtNrInUse
!
! !INPUT PARAMETERS:
!
LOGICAL, INTENT(IN) :: am_I_Root ! root CPU?
TYPE(ConfigObj), POINTER :: HcoConfig ! HEMCO config obj
CHARACTER(LEN=*), INTENT(IN) :: ConfigFile ! Full file name
INTEGER, INTENT(IN) :: Phase ! 0: all
! 1: Settings and switches only
! 2: fields only
LOGICAL, INTENT(IN), OPTIONAL :: IsNest ! Nested call?
LOGICAL, INTENT(IN), OPTIONAL :: IsDryRun ! Dry-run?
!
! !INPUT/OUTPUT PARAMETERS:
!
INTEGER, INTENT(INOUT) :: RC ! Success?
!
! !REVISION HISTORY:
! 17 Sep 2012 - C. Keller - Initialization
! See https://github.com/geoschem/hemco for complete history
!EOP
!------------------------------------------------------------------------------
!BOC
!
! !LOCAL VARIABLES:
!
! Scalars
LOGICAL :: EOF
LOGICAL :: EXISTS, NEST, DoDryRUn
INTEGER :: NN
INTEGER :: IU_HCO, IOS
! Strings
CHARACTER(LEN=255) :: loc, fileMsg
CHARACTER(LEN=512) :: msg
CHARACTER(LEN=2047) :: CFDIR
CHARACTER(LEN=2047) :: LINE
!======================================================================
! Config_ReadFile begins here
!======================================================================
! Enter
RC = HCO_SUCCESS
msg = ''
loc = 'Config_ReadFile (hco_config_mod.F90)'
! Initialize config object if not already initialized
IF ( .NOT. ASSOCIATED(HcoConfig) ) THEN
CALL ConfigInit( HcoConfig, RC )
HcoConfig%ConfigFileName = TRIM(ConfigFile)
ENDIF
! Initialize
HcoConfig%amIRoot = am_I_Root
! Leave here if configuration file is already read
IF ( HcoConfig%ConfigFileRead ) THEN
RETURN
ENDIF
! Nested call?
IF ( PRESENT( IsNest ) ) THEN
NEST = IsNest
ELSE
NEST = .FALSE.
ENDIF
! Is this a dry-run simulation?
IF ( PRESENT( IsDryRun) ) THEN
DoDryRun= IsDryRun
ELSE
DoDryRun = .FALSE.
ENDIF
! Prompt to standard output (only on the root core
IF ( HcoConfig%amIRoot ) THEN
IF ( DoDryRun ) THEN
!-----------------------------------------------------------------
! For dry-run simulations: state if the configuration file
! is found on disk, or not. Only write this message once.
!-----------------------------------------------------------------
! Test if the file exists
INQUIRE( FILE=TRIM( ConfigFile ), EXIST=Exists )
! Test if the file exists and define an output string
IF ( Exists ) THEN
FileMsg = 'HEMCO (INIT): Opening '
ELSE
FileMsg = 'HEMCO (INIT): REQUIRED FILE NOT FOUND '
ENDIF
! Write message to stdout
WRITE( 6, 300 ) TRIM( FileMsg ), TRIM( ConfigFile )
300 FORMAT( a, ' ./', a )
ELSE
!-----------------------------------------------------------------
! For regular simulations, write a message containing
! the configuration file as well as the Phase value.
!-----------------------------------------------------------------
WRITE(6,*) ' '
IF ( Phase == 1 ) THEN
WRITE( 6, 310 ) TRIM(ConfigFile)
310 FORMAT( 'Reading settings & switches of HEMCO configuration file: ', a )
ELSEIF ( Phase == 2 ) THEN
WRITE( 6, 320 ) TRIM(ConfigFile)
320 FORMAT( 'Reading fields of HEMCO configuration file: ', a )
ELSE
WRITE( 6, 330 ) TRIM(ConfigFile)
330 FORMAT( 'Reading entire HEMCO configuration file: ', a )
ENDIF
ENDIF
ENDIF
! Extract configuration file directory. This is the directory containing
! the configuration file. Any tokens $CFDIR in the given configuration
! file will be replaced with the configuration file directory
CALL HCO_GetBase( ConfigFile, CFDIR, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Could not replace $CFDIR token in: ' // TRIM( ConfigFile )
CALL HCO_Error( msg, RC, thisLoc=LOC )
RETURN
ENDIF
! Find free LUN
IU_HCO = findFreeLUN()
INQUIRE( FILE=TRIM(ConfigFile), EXIST=EXISTS )
IF ( .NOT. EXISTS ) THEN
IF ( HcoConfig%amIRoot ) THEN
WRITE(*,*) 'Cannot read file - it does not exist: ', TRIM(ConfigFile)
ENDIF
RC = HCO_FAIL
RETURN
ENDIF
! Open configuration file
OPEN ( IU_HCO, FILE=TRIM( ConfigFile ), STATUS='OLD', IOSTAT=IOS )
IF ( IOS /= 0 ) THEN
IF ( HcoConfig%amIRoot ) THEN
WRITE(*,*) 'Error reading ', TRIM(ConfigFile)
ENDIF
RC = HCO_FAIL
RETURN
ENDIF
! Register HEMCO core as extension Nr. CoreNr (default). The
! core module is used by all HEMCO simulations, and the overall
! HEMCO settings are stored as options of this extension.
! Note: cannot use HCO_GetOpt('Wildcard') for species here because
! this is linked to the core extension...
IF ( .NOT. ExtNrInUse( HcoConfig%ExtList, CoreNr ) ) THEN
CALL AddExt( HcoConfig, 'CORE', CoreNr, .TRUE., 'all', RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error adding CORE extension'
CALL HCO_Error( msg, RC, thisLoc=loc )
RC = HCO_FAIL
RETURN
ENDIF
ENDIF
! NN counts how many sections have ben read already
NN = 0
! Loop until EOF
DO
! Read a line from the file, exit if EOF
CALL HCO_ReadLine ( IU_HCO, LINE, EOF, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error in HEMCO_Config.rc @ line: ' // TRIM( Line )
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
IF ( EOF ) EXIT
! Replace tab characters in LINE (if any) w/ spaces
CALL STRREPL( LINE, HCO_TAB, HCO_SPC )
! Read settings if this is beginning of settings section
! This reads all settings into buffer and initializes the
! HEMCO traceback/error options.
IF ( INDEX ( LINE, 'BEGIN SECTION SETTINGS' ) > 0 ) THEN
IF ( PHASE < 2 ) THEN
CALL ReadSettings( HcoConfig, IU_HCO, EOF, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error in HEMCO_Config.rc @ section ' // TRIM( line )
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
IF ( EOF ) EXIT
! Increase counter
NN = NN + 1
! Can we leave here?
IF ( PHASE == 1 .AND. NN == 2 ) EXIT
ENDIF
! Read extension switches. This registers all enabled extensions.
! This must include the core extension.
ELSEIF ( INDEX ( LINE, 'BEGIN SECTION EXTENSION SWITCHES' ) > 0 ) THEN
IF ( PHASE < 2 ) THEN
CALL ExtSwitch2Buffer( HcoConfig, IU_HCO, EOF, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error in HEMCO_Config,rc @ section: ' // TRIM( line )
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
IF ( EOF ) EXIT
! Increase counter
NN = NN + 1
! Can we leave here?
IF ( PHASE == 1 .AND. NN == 2 ) EXIT
ENDIF
! Read base emissions. This creates a new data container for each
! base emission field.
ELSEIF ( INDEX ( LINE, 'BEGIN SECTION BASE EMISSIONS' ) > 0 ) THEN
! Read data and write into container
! For dry-run simulations, print name of any
! nested configuration files (e.g. for standalone)
IF ( PHASE == 0 .OR. PHASE == 2 ) THEN
CALL Config_ReadCont( HcoConfig, IU_HCO, CFDIR, &
HCO_DCTTYPE_BASE, EOF, RC, &
IsDryRun=IsDryRun )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error in HEMCO_Config.rc @ section: ' // TRIM( Line )
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
IF ( EOF ) EXIT
! Increase counter
NN = NN + 1
ENDIF
! Read scale factors. This creates a new data container for each
! scale factor.
ELSE IF ( INDEX ( LINE, 'BEGIN SECTION SCALE FACTORS' ) > 0 ) THEN
CALL Config_ReadCont( HcoConfig, IU_HCO, CFDIR, &
HCO_DCTTYPE_SCAL, EOF, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg= 'Error in HEMCO_Config.rc @ section: ' // TRIM( Line )
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
IF ( EOF ) EXIT
! Read masks. This creates a new data container for each mask
ELSE IF ( INDEX ( LINE, 'BEGIN SECTION MASKS' ) > 0 ) THEN
IF ( PHASE == 0 .OR. PHASE == 2 ) THEN
CALL Config_ReadCont( HcoConfig, IU_HCO, CFDIR, &
HCO_DCTTYPE_MASK, EOF, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error in HEMCO_Config.rc @ line: ' // TRIM( Line )
CALL HCO_Error( msg, RC, thisLoc=LOC )
RETURN
ENDIF
IF ( EOF ) EXIT
! Increase counter
NN = NN + 1
ENDIF
ENDIF
ENDDO
! Check if we caught all sections. Do that only for phase 1.
! Sections SETTINGS and extension switches are needed.
IF ( PHASE == 1 .AND. NN /= 2 .AND. .NOT. NEST ) THEN
WRITE(*,*) 'Expected 2 sections, found/read ', NN
WRITE(*,*) 'Should read SETTINGS and EXTENSION SWITCHES'
RC = HCO_FAIL
RETURN
ENDIF
! Close file
CLOSE( UNIT=IU_HCO, IOSTAT=IOS )
IF ( IOS /= 0 ) THEN
msg = 'Error closing ' // TRIM(ConfigFile)
CALL HCO_Error( msg, RC, thisLoc=loc )
RC = HCO_FAIL
RETURN
ENDIF
! Configuration file is now read
IF ( .NOT. NEST ) THEN
IF ( PHASE == 0 .OR. PHASE == 2 ) THEN
HcoConfig%ConfigFileRead = .TRUE.
ENDIF
ENDIF
! Leave w/ success
RC = HCO_SUCCESS
END SUBROUTINE Config_ReadFile
!EOC
!------------------------------------------------------------------------------
! Harmonized Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !IROUTINE: SetReadList
!
! !DESCRIPTION: Subroutine SetReadList writes data to the data reading
! lists (ReadList). This routine assumes that the configuration file has
! been read beforehand (via Config\_ReadFile).
!\\
!\\
! !INTERFACE:
!
SUBROUTINE SetReadList( HcoState, RC )
!
! !USES:
!
USE HCO_DATACONT_Mod, ONLY : cIDList_Create
USE HCO_READLIST_Mod, ONLY : ReadList_Init, ReadList_Print
!
! !INPUT PARAMETERS:
!
TYPE(HCO_State), POINTER :: HcoState ! HEMCO state
!
! !INPUT/OUTPUT PARAMETERS:
!
INTEGER, INTENT(INOUT) :: RC ! Error stat
!
! !REVISION HISTORY:
! 18 Jun 2013 - C. Keller: Initialization
! See https://github.com/geoschem/hemco for complete history
!EOP
!------------------------------------------------------------------------------
!BOC
CHARACTER(LEN=255) :: loc
CHARACTER(LEN=512) :: msg
!======================================================================
! SetReadList begins here
!======================================================================
msg = ''
loc = 'SetReadList (HCO_CONFIG_MOD.F90)'
! Init
CALL HCO_ENTER ( HcoState%Config%Err, LOC, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error encountered in routine "HCO_Enter"!'
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
! Return w/ error if configuration file hasn't been read yet!
IF ( .NOT. ASSOCIATED(HcoState%Config) ) THEN
msg = 'HEMCO configuration object in HEMCO state is empty!'
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
IF ( .NOT. HcoState%Config%ConfigFileRead ) THEN
msg = 'HEMCO configuration file not read!'
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
! Only if not yet done so...
IF ( .NOT. HcoState%SetReadListCalled ) THEN
! Initialize ReadList
CALL ReadList_Init ( HcoState%ReadLists, RC )
IF ( RC /= HCO_SUCCESS ) THEN
PRINT *,'Error in ReadList_Init called from HEMCO SetReadList'
RETURN
ENDIF
! Prepare data in buffer. This call identifies all base fields
! that have to be read by this CPU. It also kicks out base
! fields for emissions with an invalid species ID (if any) or
! if there are other base fields with higher priority.
CALL RegisterPrepare ( HcoState, RC )
IF ( RC /= HCO_SUCCESS ) THEN
PRINT *,'Error in RegisterPrepare called from HEMCO SetReadList'
RETURN
ENDIF
! Register base emissions. In this step, we also redefine the
! list UnqScalIDs to make sure that only those scale factors
! will be registered that are effectively used in the next step.
CALL Register_Base( HcoState, RC )
IF ( RC /= HCO_SUCCESS ) THEN
PRINT *,'Error in Register_Base called from HEMCO SetReadList'
RETURN
ENDIF
! Register scale factors based upon UnqScalIDs.
CALL Register_Scal( HcoState, RC )
IF ( RC /= HCO_SUCCESS ) THEN
PRINT *,'Error in Register_Scal called from HEMCO SetReadList'
RETURN
ENDIF
! Create cIDList which allows quick access to all data containers
! based on their container IDs cID
CALL cIDList_Create ( HcoState, HcoState%Config%ConfigList, RC )
IF ( RC /= HCO_SUCCESS ) THEN
PRINT *,'Error in cIDList_Create called from HEMCO SetReadlist'
RETURN
ENDIF
! Don't need internal lists anymore.
CALL ScalID_Cleanup ( HcoState%Config%ScalIDList )
CALL SpecName_Cleanup( HcoState%Config%SpecNameList )
ENDIF ! SetReadListCalled
! SetReadList has now been called
HcoState%SetReadListCalled = .TRUE.
! Debug
IF ( HCO_IsVerb( HcoState%Config%Err ) ) THEN
CALL ReadList_Print( HcoState, HcoState%ReadLists )
ENDIF
! Leave w/ success
CALL HCO_LEAVE( HcoState%Config%Err, RC )
END SUBROUTINE SetReadList
!EOC
!------------------------------------------------------------------------------
! Harmonized Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !IROUTINE: Config_ReadCont
!
! !DESCRIPTION: Subroutine CONFIG\_READCONT reads the given line into a
! list container. Depending on the specified data type, the line is
! assumed to hold base emissions, scale factors, or mask information.
!\\
!\\
! !INTERFACE:
!
SUBROUTINE Config_ReadCont( HcoConfig, IU_HCO, CFDIR, &
DctType, EOF, RC, IsDryRun )
!
! !USES:
!
USE HCO_CHARPAK_MOD, ONLY : StrSplit
USE HCO_EXTLIST_MOD, ONLY : ExtNrInUse, HCO_GetOpt
USE HCO_TIDX_Mod, ONLY : HCO_ExtractTime
USE HCO_FILEDATA_Mod, ONLY : FileData_Init
USE HCO_DATACONT_Mod, ONLY : CatMax, ZeroScalID
!
! !INPUT PARAMETERS:
!
TYPE(ConfigObj), POINTER :: HcoConfig ! Config object
INTEGER, INTENT(IN ) :: IU_HCO ! Logfile LUN
CHARACTER(LEN=*), INTENT(IN ) :: CFDIR ! Configuration file directory
INTEGER, INTENT(IN ) :: DctType ! 1=base; 2=scale; 3=mask
LOGICAL, OPTIONAL :: IsDryRun ! Is this a HEMCO dry-run?
!
! !INPUT/OUTPUT PARAMETERS:
!
LOGICAL, INTENT(INOUT) :: EOF ! end of file encountered?
!
! !OUTPUT PARAMETERS:
!
INTEGER, INTENT( OUT) :: RC ! error code
!
! !REVISION HISTORY:
! 03 Jan 2014 - C. Keller - Initial version
! See https://github.com/geoschem/hemco for complete history
!EOP
!------------------------------------------------------------------------------
!BOC
!
! !LOCAL VARIABLES:
!
! Scalars
INTEGER :: I, N, nEdges
INTEGER :: nScl
INTEGER :: STAT
INTEGER :: Int1
INTEGER :: Int2
INTEGER :: Int3
INTEGER :: Int4
INTEGER :: nCat
INTEGER :: Cats(CatMax)
INTEGER :: STRLEN
INTEGER :: levScal1
INTEGER :: levScal2
INTEGER :: nTags
LOGICAL :: SKIP
LOGICAL :: Found
CHARACTER(LEN= 63) :: cName
CHARACTER(LEN=255) :: srcFile
CHARACTER(LEN= 50) :: srcVar
CHARACTER(LEN= 31) :: srcTime
CHARACTER(LEN= 31) :: TmCycle
CHARACTER(LEN= 1) :: WildCard
CHARACTER(LEN= 1) :: Separator
CHARACTER(LEN= 31) :: srcDim
CHARACTER(LEN= 31) :: srcUnit
CHARACTER(LEN= 31) :: SpcName
CHARACTER(LEN=255) :: Char1
CHARACTER(LEN=255) :: Char2
CHARACTER(LEN=255) :: loc
CHARACTER(LEN=255) :: LINE
CHARACTER(LEN=255) :: tagId
CHARACTER(LEN=255) :: tagName
CHARACTER(LEN=255) :: tagcName
CHARACTER(LEN=255) :: ItemPrefix
CHARACTER(LEN=512) :: msg
! Arrays
INTEGER :: SplitInts(255)
CHARACTER(LEN=255) :: SubStrs(255)
! Pointers
TYPE(ListCont), POINTER :: Lct
TYPE(ListCont), POINTER :: Tmp
TYPE(FileData), POINTER :: Dta
!=================================================================
! Config_ReadCont begins here!
!=================================================================
! Enter
loc = 'Config_ReadCont (hco_config_mod.F90)'
! Initialize
SKIP = .FALSE.
nCat = -1
Lct => NULL()
Tmp => NULL()
Dta => NULL()
! Get tokens
WildCard = HCO_GetOpt( HcoConfig%ExtList, 'Wildcard' )
Separator = HCO_GetOpt( HcoConfig%ExtList, 'Separator' )
! Repeat until end of the given section is found
DO
! Zero loop variables for safety's sake (bmy, 07 Mar 2022)
srcFile = ''
srcVar = ''
srcTime = ''
tmCycle = ''
srcDim = ''
srcUnit = ''
spcName = ''
char1 = ''
char2 = ''
int1 = 0
int2 = 0
int3 = 0
!==============================================================
! Read line and get desired character strings
! Since base emissions, scale factors and masks have different
! configuration file input parameter, need to use a different
! call for the three data types.
!==============================================================
IF ( DctType == HCO_DCTTYPE_BASE ) THEN
CALL ReadAndSplit_Line ( HcoConfig, IU_HCO, cName, 2, &
srcFile, 3, srcVar, 4, &
srcTime, 5, TmCycle, 6, &
srcDim, 7, srcUnit, 8, &
SpcName, 9, Char1, 10, &
Char2, 11, &
Int1, -1, Int2, 12, &
Int3, 1, STAT, &
OutLine=LINE )
ELSEIF ( DctType == HCO_DCTTYPE_SCAL ) THEN
CALL ReadAndSplit_Line ( HcoConfig, IU_HCO, cName, 2, &
srcFile, 3, srcVar, 4, &
srcTime, 5, TmCycle, 6, &
srcDim, 7, srcUnit, 8, &
SpcName, -1, Char1, -1, &
Char2, -1, &
Int1, 1, Int2, 9, &
Int3, 10, STAT, &
optcl=10 , OutLine=LINE )
ELSEIF ( DctType == HCO_DCTTYPE_MASK ) THEN
CALL ReadAndSplit_Line ( HcoConfig, IU_HCO, cName, 2, &
srcFile, 3, srcVar, 4, &
srcTime, 5, TmCycle, 6, &
srcDim, 7, srcUnit, 8, &
SpcName, -1, Char1, 10, &
Char2, 11, &
Int1, 1, Int2, 9, &
Int3, -1, STAT, &
optcl=11, OutLine=LINE )
ENDIF
!--------------------------------------------------------------
! Error checks
!--------------------------------------------------------------
! Check for end of file
IF ( STAT < 0 ) THEN
EOF = .TRUE.
EXIT
ENDIF
! Skip this entry if commented line
IF ( STAT == 1 ) THEN
CYCLE
ENDIF
! Leave routine here if end of section encountered
IF ( STAT == 10 ) THEN
EXIT
ENDIF
! Error if not enough entries found
IF ( STAT == 100 ) THEN
msg = 'STAT == 100; not enough entries found!'
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
! -------------------------------------------------------------
! Check for emission shortcuts. Fields can be bracketed into
! 'collections'.
! -------------------------------------------------------------
CALL BracketCheck( HcoConfig, STAT, LINE, SKIP, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Bracket error in HEMCO_Config.rc @ line ' // TRIM( line )
CALL HCO_ERROR( msg, RC, thisLoc=loc )
RETURN
ENDIF
! Skip if needed
IF ( SKIP ) CYCLE
! Can advance to next line if this was a bracket line: nothing
! else to do with this line.
IF ( STAT == 5 .OR. STAT == 6 ) CYCLE
! Read include file. Configuration files can be 'nested', e.g.
! configuration files can be included into the 'main' configuration
! file. These files must be listed as '>>>include FileName', where
! FileName is the actual path of the file.
IF ( STAT == 1000 ) THEN
! Call the parser. This is to make sure that any $ROOT statements
! will be evaluated properly. The configuration file must not
! contain any data tokens ($YR, $MM, etc.).
CALL HCO_CharParse ( HcoConfig, LINE, 0, 0, 0, 0, 0, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Parse error in HEMCO_Config.rc @ line: ' // TRIM( line )
CALL HCO_Error( msg, RC, thisLoc=LOC )
RETURN
ENDIF
CALL Config_ReadFile( HcoConfig%amIRoot, HcoConfig, LINE, 0, RC, &
IsNest=.TRUE., IsDryRun=IsDryRun )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error reading HEMCO_Config.rc @ line: ' // TRIM( line )
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
! All done with this line
CYCLE
ENDIF
! Output status should be 0 if none of the statuses above applies
IF ( STAT /= 0 ) THEN
msg = 'STAT /= 0; indicates I/O error!'
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
! For base fields, check if this extension number is indeed in
! use. Otherwise, we can ignore this line completely! The
! extension switches are read and evaluated prior to the
! extension data!
IF ( DctType == HCO_DCTTYPE_BASE .AND. &
.NOT. ExtNrInUse( HcoConfig%ExtList, Int3 ) ) CYCLE
!==============================================================
! Create and fill list container and add to ConfigList
!==============================================================
! Test if wildcard is present
IF ( INDEX( srcVar, '?' ) > 0 ) THEN
! Split the name to get wildcard and string prior to wildcard
CALL StrSplit( srcVar, '?', SubStrs, N )
tagId = SubStrs(N-1)
ItemPrefix = SubStrs(1)
! Get number of tags for this wildcard
CALL Hco_GetTagInfo( tagId, HcoConfig, Found, RC, nTags=nTags )
! Add each tagged name as a separate item in the collection
DO N = 1, nTags
! Construct the item name
tagcName = ''
! Get tag, if any
CALL Hco_GetTagInfo( tagId, HcoConfig, Found, RC, N=N, &
tagName=tagName )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Error retrieving tag name for' // &
' wildcard ' // TRIM(tagId)
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
! Append the tag name to the output name
srcVar = TRIM( ItemPrefix ) // TRIM( tagName )
tagcName = TRIM( cName ) // TRIM( tagName )
! Do not overwrite species name for now. Use HEMCO wildcard
! to read all external model species.
!spcName = TRIM( tagName )
! -------------------------------------------------------------
! Fill data container
! -------------------------------------------------------------
! Add blank list container (ListCont object) to ConfigList.
! The container is placed at the beginning of the list.
CALL ConfigList_AddCont ( Lct, HcoConfig%ConfigList )
! Check if name exists already
CALL CheckForDuplicateName( HcoConfig, tagcName, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Duplicate container name: ' // TRIM( tagcName )
CALL HCO_ERROR( msg, RC, thisLoc=loc )
RETURN
ENDIF
! Attributes used by all data types: data type number and
! container name.
Lct%Dct%DctType = DctType
Lct%Dct%cName = ADJUSTL( tagcName )
! Set species name, extension number, emission category,
! hierarchy
Lct%Dct%SpcName = ADJUSTL( SpcName )
Lct%Dct%Hier = Int2
Lct%Dct%ExtNr = Int3
! Extract category from character 2. This can be up to
! CatMax integers, or empty.
CALL HCO_CharSplit( Char2, Separator, Wildcard, Cats, nCat, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Could not extract category at line: ' // TRIM( char2 )
CALL HCO_Error( msg, RC, thisLoc=LOC )
RETURN
ENDIF
IF ( nCat == 0 ) THEN
Lct%Dct%Cat = -999
ELSE
Lct%Dct%Cat = Cats(1)
ENDIF
! Set scale factor IDs into Scal_cID. These values will be
! replaced lateron with the container IDs (in register_base)!
CALL HCO_CharSplit( Char1, Separator, Wildcard, &
SplitInts, nScl, RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Could not get scale factor ID''s in line: ' // &
TRIM( char1 )
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
IF ( nScl > 0 ) THEN
ALLOCATE ( Lct%Dct%Scal_cID(nScl) )
Lct%Dct%Scal_cID(1:nScl) = SplitInts(1:nScl)
Lct%Dct%nScalID = nScl
ENDIF
! Register species name. A list of all species names can be
! returned to the atmospheric model to match HEMCO species
! with model species (see Config\_GetSpecNames).
CALL SpecName_Register ( HcoConfig, ADJUSTL(SpcName), RC )
IF ( RC /= HCO_SUCCESS ) THEN
msg = 'Could not register species name: ' // TRIM(SpcName)
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
! -------------------------------------------------------------
! Create and fill file data object. Use previous file data
! object if filename is undefined. Do not yet update the
! DoShare and DtaHome flags of the data file object and data
! container, respectively, since we still don't know which
! data containers will be effectively used for emission
! calculation (containers may be dropped lateron because the
! emission category / hierarchy is too low, species is not
! used, etc.). The DoShare and DtaHome flags will be set the
! first time that data is read (in hco_readlist_mod.F90).
! Here, we only set the DtaHome flag to -1000 instead of the
! default value of -999 to be able to identify data objects
! used by multiple containers.
! -------------------------------------------------------------
IF ( TRIM( srcFile ) == '-' ) THEN
! The current entry of the configuration file specifies that
! we will get data from the file listed immediately above it.
! Thus we have to reuse a previously-defined FileData object
! (aka Dta). Stop if Dta is not initialized.
IF ( .not. ASSOCIATED( Dta ) ) THEN
MSG = 'Cannot use previous data container: '//TRIM(tagcName)
CALL HCO_Error( msg, RC, thisLoc=loc )
RETURN
ENDIF
! Reuse the file metadata specified in PrevDta for
! this entry of the HEMCO configuration file.
Lct%Dct%DtaHome = Lct%Dct%DtaHome - 1
ELSE
! The current entry of the configuration file specifies that
! we will read data from a file. We thus need to initialize
! a new FileData object to keep track of the file metadata.
!
! >>> NOTE: This seems to cause a memory leak! <<<
! >>> We will look into this at a later date. <<<
CALL FileData_Init( Dta )
! Set source file name. Check if the read file name starts
! with the configuration file token '$CFDIR', in which case
! we replace this value with the passed CFDIR value.
STRLEN = LEN( srcFile )
IF ( STRLEN > 6 ) THEN
IF ( srcFile(1:6) == '$CFDIR' ) THEN
srcFile = TRIM( CFDIR ) // TRIM( srcFile(7:STRLEN) )
ENDIF
ENDIF
Dta%ncFile = srcFile
! Set source variable and original data unit.
Dta%ncPara = ADJUSTL( srcVar )
Dta%OrigUnit = ADJUSTL( srcUnit )
! If the parameter ncPara is not defined, attempt to read data
! directly from configuration file instead of netCDF.
! These data are always assumed to be in local time. Gridded
! data read from netCDF is always in UTC, except for weekdaily
! data that is treated in local time. The corresponding
! IsLocTime flag is updated when reading the data (see
! hcoio_dataread_mod.F90).
IF ( TRIM( Dta%ncPara ) == '-' ) THEN
Dta%ncRead = .FALSE.
Dta%IsLocTime = .TRUE.
ENDIF
! Extract information from time stamp character and pass values
! to the corresponding container variables. If no time string is
! defined, keep default values (-1 for all of them)