-
Notifications
You must be signed in to change notification settings - Fork 8
/
nexus_methods_mod.F90
3680 lines (3230 loc) · 134 KB
/
nexus_methods_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
module NEXUS_Methods_Mod
use ESMF
use HCO_Error_Mod
use HCO_Diagn_Mod
use HCO_CharTools_Mod
use HCO_Types_Mod
use HCOX_State_Mod, only : Ext_State
use HCO_State_Mod, only : HCO_State
use NEXUS_Error_Mod
implicit none
type(ESMF_Grid) :: HCO_Grid
type(ESMF_Grid) :: NXS_Grid
type(ESMF_State) :: NXS_Diag_State
type(ESMF_State) :: NXS_Expt_State
type(ESMF_RouteHandle) :: NXS_RouteHandle
logical :: do_Regrid = .false.
logical :: do_Debug = .false.
logical :: do_NEXUS = .false.
! Default values for HEMCO input files: contain definitions of
! species, grid, and time settings, etc.
character(len=255) :: GridFile = 'HEMCO_sa_Grid'
character(len=255) :: SpecFile = 'HEMCO_sa_Spec'
character(len=255) :: TimeFile = 'HEMCO_sa_Time'
character(len=255) :: DiagFile = 'NEXUS_Diag.nc'
character(len=255) :: ExptFile = 'NEXUS_Expt.nc'
! HEMCO state
type(HCO_State), pointer :: HcoState => NULL()
! HEMCO extensions state
type(Ext_State), pointer :: ExtState => NULL()
! HEMCO config object
type(ConfigObj), pointer :: HcoConfig => NULL()
! Pointers used during initialization (for species matching)
integer :: nHcoSpec
character(len= 31), pointer :: HcoSpecNames (:) => NULL()
integer :: nModelSpec
character(len= 31), pointer :: ModelSpecNames (:) => NULL()
integer, pointer :: ModelSpecIDs (:) => NULL()
real(hp), pointer :: ModelSpecMW (:) => NULL()
real(hp), pointer :: ModelSpecK0 (:) => NULL()
real(hp), pointer :: ModelSpecCR (:) => NULL()
real(hp), pointer :: ModelSpecPKA (:) => NULL()
integer, pointer :: matchidx (:) => NULL()
! Start and end time of simulation
integer :: YRS(2), MTS(2), DYS(2)
integer :: HRS(2), MNS(2), SCS(2)
! Grid
real(hp), allocatable, target :: XMID (:,:,:)
real(hp), allocatable, target :: YMID (:,:,:)
real(hp), allocatable, target :: XEDGE (:,:,:)
real(hp), allocatable, target :: YEDGE (:,:,:)
real(hp), allocatable, target :: YSIN (:,:,:)
real(hp), allocatable, target :: AREA_M2(:,:,:)
real(hp), allocatable, target :: PBL_M (:,:)
! MAXIT is the maximum number of run calls allowed
integer, parameter :: MAXIT = 100000
integer, parameter :: rootPet = 0
type(ESMF_StaggerLoc), parameter :: staggerList(2) = &
(/ ESMF_STAGGERLOC_CENTER, ESMF_STAGGERLOC_CORNER /)
interface NEXUS_Initialize
module procedure Init
end interface
interface NEXUS_Run
module procedure Run
end interface
interface NEXUS_Finalize
module procedure Finalize
end interface
private
public :: rootPet
public :: NEXUS_Initialize
public :: NEXUS_Run
public :: NEXUS_Finalize
contains
subroutine Init( ConfigFile, ReGridFile, OutputFile, debugLevel, rc )
use HCO_Config_Mod, only : Config_ReadFile
use HCO_State_Mod, only : HcoState_Init
use HCO_Driver_Mod, only : HCO_Init
use HCOX_Driver_Mod, only : HCOX_Init
use HCO_EXTLIST_Mod, only : GetExtOpt, CoreNr
character(len=*), intent(in) :: ConfigFile
character(len=*), intent(in) :: ReGridFile
character(len=*), intent(in) :: OutputFile
integer, intent(in) :: debugLevel
integer, optional, intent(out) :: rc
! -- local variables
integer :: localrc
integer :: localPet
integer :: nnMatch
logical :: am_I_Root
logical :: Dum, Found
character(len=ESMF_MAXSTR) :: msgString
type(ESMF_VM) :: vm
! -- begin
if (present(rc)) rc = ESMF_SUCCESS
! -- determine whether I am root
call ESMF_VMGetCurrent(vm, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
call ESMF_VMGet(vm, localPet=localPet, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
am_I_Root = (localPet == rootPet)
do_Regrid = (len_trim(ReGridFile) > 0)
do_Debug = (debugLevel > 0)
do_NEXUS = (do_Debug .or. do_Regrid)
if (len_trim(OutputFile) > 0) ExptFile = OutputFile
if ( am_I_Root ) then
if ( do_Debug ) print *,'Writing debug emissions to: '//trim(DiagFile)
if ( do_Regrid ) print *,'Writing regridded emissions to: '//trim(ExptFile)
end if
!=======================================================================
! Read HEMCO configuration file and save into buffer. This also
! sets the HEMCO error properties (verbose mode? log file name,
! etc.) based upon the specifications in the configuration file.
!=======================================================================
call Config_ReadFile( am_I_Root, HcoConfig, ConfigFile, &
0, localrc, IsDryRun=.false. )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "Config_Readfile!"', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=======================================================================
! Open logfile
!======================================================================
if ( am_I_Root ) then
call HCO_LogFile_Open( HcoConfig%Err, RC=localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCO_Logfile_Open_Readfile!"', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
end if
!=======================================================================
! Initialize HEMCO state object and populate it
!=======================================================================
!-----------------------------------------------------------------------
! Extract species to use in HEMCO
call Get_nnMatch( HcoConfig, nnMatch, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "Get_nnMatch"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!-----------------------------------------------------------------------
! Initialize HCO state. Use only species that are used
! in HEMCO_sa_Spec.rc and are also found in the HEMCO config. file.
call HcoState_Init( HcoState, HcoConfig, nnMatch, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HcoState_Init"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!-----------------------------------------------------------------------
! Set grid
call Set_Grid ( HcoState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "Set_Grid"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!-----------------------------------------------------------------------
! Create NEXUS grid and reset HEMCO grid as distributed
if (do_NEXUS) then
HCO_Grid = HCO_GridCreate( HcoState, rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
!-----------------------------------------------------------------------
! Register species
call Register_Species( HcoState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "Register_Species"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!-----------------------------------------------------------------------
! Read time information, incl. timesteps and simulation time(s)
call Read_Time( HcoState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "Read_Time"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=======================================================================
! Set misc. parameter
!=======================================================================
! Set ESMF flag
HcoState%Options%isESMF = .FALSE.
! Let HEMCO schedule the diagnostics output
HcoState%Options%HcoWritesDiagn = .not.do_NEXUS
! If not explicitly set, make sure that option Field2Diagn is true
call GetExtOpt ( HcoState%Config, CoreNr, &
'ConfigField to diagnostics', &
OptValBool=Dum, Found=Found, RC=localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "GetExtOpt"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=======================================================================
if ( .NOT. Found ) HcoState%Options%Field2Diagn = .TRUE.
!=======================================================================
! Are we running the HEMCO standalone in a dry-run mode?
! This is dictated by the HEMCO environment. If HEMCO is in a
! dry-run mode, no compute is performed and files are only "checked".
! Simulations will NOT stop on missing files. This is intended to be a
! quick sanity check to make sure that GEOS-Chem IO are all correctly
! set up, which is why most of the runs fail to complete successfully.
! (hplin, 11/2/19)
!
! Dry-run simulations now send output to a log file that is separate
! from the HEMCO log files. (bmy, 11/11/19)
!
! NOTE: The dry-run option is not invoked when we use HEMCO
! in external ESMs. (bmy, 11/13/19)
!=======================================================================
!=======================================================================
! Initialize HEMCO internal lists and variables. All data
! information is written into internal lists (ReadList) and
! the HEMCO configuration file is removed from buffer in this
! step. Also initializes the HEMCO clock
!=======================================================================
call HCO_Init( HcoState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCO_Init"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=======================================================================
! Initialize extensions.
! This initializes all (enabled) extensions and selects all met.
! fields needed by them.
!=======================================================================
call HCOX_Init( HcoState, ExtState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCOX_Init"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=======================================================================
! Define diagnostics
!=======================================================================
!--------------------------------------------------------------------
! For regular simulations, read diagnostics configuration file
! and define diagnostic variables for output
!--------------------------------------------------------------------
call Define_Diagnostics( HcoState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "Define_Diagnostics"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=======================================================================
! Leave HEMCO Init
!=======================================================================
call HCOI_SA_InitCleanup( localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCOI_SA_InitCleanup"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=======================================================================
! Start NEXUS Init
!=======================================================================
if (do_NEXUS) then
NXS_Diag_State = ESMF_StateCreate( rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
call NXS_DiagState_Init( HCO_Grid, HcoState, NXS_Diag_State, rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
! if (do_Debug) then
! call GridWrite( HCO_Grid, DiagFile, rc=localrc )
! if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
! line=__LINE__, &
! file=__FILE__, &
! rcToReturn=rc)) return ! bail out
! end if
if (do_Regrid) then
NXS_Grid = GridCreate_GridSpec( ReGridFile, rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
NXS_Expt_State = ESMF_StateCreate( rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
call NXS_ExptState_Init( NXS_Grid, NXS_Diag_State, NXS_Expt_State, rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
! call GridWrite( NXS_Grid, ExptFile, rc=localrc )
! if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
! line=__LINE__, &
! file=__FILE__, &
! rcToReturn=rc)) return ! bail out
end if
end subroutine Init
subroutine Run( RC )
use HCO_FluxArr_Mod, only : HCO_FluxarrReset
use HCO_Clock_Mod, only : HcoClock_Set
use HCO_Clock_Mod, only : HcoClock_Get
use HCO_Clock_Mod, only : HcoClock_Increase
use HCO_Driver_Mod, only : HCO_RUN
use HCOX_Driver_Mod, only : HCOX_RUN
integer, optional, intent(out) :: RC ! Failure or success
! -- local variables
integer :: localrc
integer :: CNT
integer :: YR, MT, DY, HR, MN, SC
character(len=255) :: Msg
! -- begin
if (present(rc)) rc = ESMF_SUCCESS
! Time step counter
CNT = 0
! Do until end of simulation
do while ( CNT < MAXIT )
! Increase counter by one
CNT = CNT + 1
!====================================================================
! Set HcoClock. On first call, use specified start date.
! Increase clock by one emission time step otherwise.
!====================================================================
if ( CNT == 1 ) then
call HcoClock_Set ( HcoState, YRS(1), MTS(1), &
DYS(1), HRS(1), MNS(1), SCS(1), &
IsEmisTime=.TRUE., RC=localrc)
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HcoClock_Set"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
else
call HcoClock_Increase ( HcoState, HcoState%TS_EMIS, .TRUE., RC=localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HcoClock_Increase"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
end if
! Get current time
call HcoClock_Get ( HcoState%Clock, cYYYY=YR, &
cMM=MT, cDD=DY, cH=HR, cM=MN, cS=SC, RC=localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HcoClock_Get"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
! Leave loop if this is the end of the simulation
if ( IsEndOfSimulation(YR,MT,DY,HR,MN,SC) ) EXIT
! Write to logfile and standard output (skip for dry-run)
write( Msg, 100 ) YR, MT, DY, HR, MN, SC
100 FORMAT( 'Calculate emissions at ', i4, '-', i2.2 ,'-', i2.2,' ', &
i2.2,':', i2.2, ':', i2.2 )
call ESMF_LogWrite( Msg )
write(*,*) trim( MSG )
! ================================================================
! Reset all emission and deposition values
! ================================================================
call HCO_FluxArrReset( HcoState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCO_FluxArrReset"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
! ================================================================
! Set HCO options and define all arrays needed by core module
! and the extensions
! ================================================================
! Range of tracers and emission categories.
! Set Extension number ExtNr to 0, indicating that the core
! module shall be executed.
HcoState%Options%SpcMin = 1
HcoState%Options%SpcMax = nModelSpec
HcoState%Options%CatMin = 1
HcoState%Options%CatMax = -1
HcoState%Options%ExtNr = 0
! Use temporary array?
HcoState%Options%FillBuffer = .FALSE.
! ================================================================
! Run HCO core module
! Emissions will be written into the corresponding flux arrays
! in HcoState.
!
! NOTE: Call HCO_Run explicitly twice, once for phase 1 and
! once for phase 2. This will ensure emissions get computed.
! (bmy, 1/29/18)
! ================================================================
! Phase 1: Update reading data fields etc.
call HCO_Run( HcoState, 1, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "Hco_Run", phase 1!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
! Phase 2: Compute emissions (skip for dry-run)
call HCO_Run( HcoState, 2, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "Hco_Run", phase 2!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
! ================================================================
! Run HCO extensions
! ================================================================
! Set ExtState fields (skip for dry-run)
call ExtState_SetFields ( HcoState, ExtState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "ExtState_SetFields"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
! Update ExtState fields (skip for dry-run)
call ExtState_UpdateFields( HcoState, ExtState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "ExtState_Update_Fields"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
! Execute all enabled emission extensions. Emissions will be
! added to corresponding flux arrays in HcoState.
call HCOX_Run ( HcoState, ExtState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCOX_Run"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=================================================================
! Update all autofill diagnostics (skip for dry-run)
!=================================================================
call HcoDiagn_AutoUpdate ( HcoState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCOX_AutoUpdate"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
!=================================================================
! Update NEXUS Diagnostic state
!=================================================================
if (do_NEXUS) then
call NXS_DiagState_Update( HcoState, NXS_Diag_State, rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
!=================================================================
! Write NEXUS Diagnostic state
!=================================================================
if (do_Debug) then
call StateWrite( NXS_Diag_State, DiagFile, timeSlice=CNT, rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
if (do_Regrid) then
!=================================================================
! Update NEXUS Export state
!=================================================================
call NXS_ExptState_Update( NXS_Diag_State, NXS_Expt_State, rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
!=================================================================
! Write NEXUS Export state
!=================================================================
call StateWrite( NXS_Expt_State, ExptFile, timeSlice=CNT, rc=localrc )
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
end do
! Set iteration limit to avoid infinite runs
if ( CNT == MAXIT ) then
call ESMF_LogSetError(ESMF_RC_TIMEOUT, &
msg='Counter limit reached - Increase MAXIT if you don`t like that!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)
return
end if
end subroutine Run
subroutine Finalize( rc )
use HCO_Driver_Mod, only : HCO_Final
use HCOX_Driver_Mod, only : HCOX_Final
use HCO_State_Mod, only : HcoState_Final
integer, optional, intent(out) :: rc
! -- local variables
integer :: localrc
logical :: isCreated
! -- begin
if (present(rc)) rc = ESMF_SUCCESS
! Cleanup HCO core
call HCO_FINAL( HcoState, .FALSE., localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCO_Final"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
! Cleanup extensions and ExtState object
! This will also nullify all pointer to the met fields.
call HCOX_FINAL( HcoState, ExtState, localrc )
if (NEXUS_Error_Log(localrc, msg='Error encountered in routine "HCOX_Final"!', &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return
! Cleanup diagnostics (skip if dry-run)
call DiagnBundle_Cleanup( HcoState%Diagn )
! Deallocate module arrays/pointers
if ( allocated( XMID ) ) deallocate ( XMID )
if ( allocated( YMID ) ) deallocate ( YMID )
if ( allocated( XEDGE ) ) deallocate ( XEDGE )
if ( allocated( YEDGE ) ) deallocate ( YEDGE )
if ( allocated( YSIN ) ) deallocate ( YSIN )
if ( allocated( AREA_M2 ) ) deallocate ( AREA_M2 )
if ( allocated( PBL_M ) ) deallocate ( PBL_M )
! Cleanup HcoState object
call HcoState_Final( HcoState )
! Cleanup NEXUS
isCreated = ESMF_GridIsCreated(HCO_Grid, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
if (isCreated) then
call ESMF_GridDestroy(HCO_Grid, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
isCreated = ESMF_GridIsCreated(NXS_Grid, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
if (isCreated) then
call ESMF_GridDestroy(NXS_Grid, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
isCreated = ESMF_RouteHandleIsCreated(NXS_RouteHandle, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
if (isCreated) then
call ESMF_FieldRegridRelease(NXS_RouteHandle, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
isCreated = ESMF_StateIsCreated(NXS_Diag_State, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
if (isCreated) then
call StateFinalize(NXS_Diag_State, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
call ESMF_StateDestroy(NXS_Diag_State, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
isCreated = ESMF_StateIsCreated(NXS_Expt_State, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
if (isCreated) then
call StateFinalize(NXS_Expt_State, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
call ESMF_StateDestroy(NXS_Expt_State, rc=localrc)
if (ESMF_LogFoundError(rcToCheck=localrc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__, &
rcToReturn=rc)) return ! bail out
end if
end subroutine Finalize
!EOC
!------------------------------------------------------------------------------
! Harvard-NASA Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !IROUTINE: Model_GetSpecies
!
! !DESCRIPTION: subroutine Model\_GetSpecies returns 'model' species
! information from the HEMCO standalone input file.
!\\
!\\
! !INTERFACE:
!
subroutine Model_GetSpecies( HcoConfig, &
nModelSpec, ModelSpecNames, &
ModelSpecIDs, ModelSpecMW, &
ModelSpecK0, ModelSpecCR, &
ModelSpecPKA, RC )
!
! !USES:
!
use HCO_inquireMod, only : findfreeLUN
use HCO_EXTLIST_Mod, only : GetExtOpt, CoreNr
!
! !OUTPUT PARAMETERS:
!
type(ConfigObj), pointer :: HcoConfig
integer, intent(OUT) :: nModelSpec
character(len= 31), pointer :: ModelSpecNames (:)
integer, pointer :: ModelSpecIDs (:)
real(hp), pointer :: ModelSpecMW (:)
real(hp), pointer :: ModelSpecK0 (:)
real(hp), pointer :: ModelSpecCR (:)
real(hp), pointer :: ModelSpecPKA (:)
integer, intent(OUT) :: RC
!
! !REVISION HISTORY:
! 13 Sep 2013 - C. Keller - Initial Version
!EOP
!------------------------------------------------------------------------------
!BOC
!
! LOCAL VARIABLES:
!
integer :: I, N, LNG, LOW, UPP
integer :: IU_FILE, IOS
logical :: FOUND, EOF
character(len=255) :: MSG, LOC
character(len=255) :: MySpecFile
character(len=2047) :: DUM
!=================================================================
! Model_GetSpecies begins here
!=================================================================
! For error handling
LOC = 'Model_GetSpecies (hcoi_standalone_mod.F90)'
! Try to get SpecFile from configuration file (in settings)
call GetExtOpt ( HcoConfig, CoreNr, 'SpecFile', &
OptValChar=MySpecFile, Found=FOUND, RC=RC )
!if ( RC /= HCO_SUCCESS ) return
if ( FOUND ) then
SpecFile = MySpecFile
else
MSG = 'Please provide filename with species definitions ' // &
'in the configuration file settings, e.g. ' // &
'SpecFile: MySpecies.rc'
call HCO_Error ( HcoConfig%Err, MSG, RC, THISLOC=LOC )
return
end if
! Find a free file LUN
IU_FILE = findFreeLUN()
! Open spec file
OPEN( IU_FILE, FILE=trim(SpecFile), STATUS='OLD', IOSTAT=IOS )
if ( IOS /= 0 ) then
MSG = 'Error 1 reading ' // trim(SpecFile)
call HCO_Error( HcoConfig%Err, MSG, RC, THISLOC=LOC )
return
end if
! Get number of species
nModelSpec = 0
do
call GetNextLine( IU_FILE, DUM, EOF, RC )
if ( EOF ) EXIT
if ( RC /= HCO_SUCCESS ) then
MSG = 'Error encountered in reading SpecFile!. Please ' // &
'doublecheck that all species information has ' // &
'been correctly entered.'
call HCO_Error ( HcoConfig%Err, MSG, RC, THISLOC=LOC )
end if
nModelSpec = nModelSpec + 1
end do
! Make sure we have one species
if ( nModelSpec == 0 ) then
MSG = 'Species file ' // trim(SpecFile) // &
' does not seem to have any content. ' // &
'You must define at least one species.'
call HCO_Error( HcoConfig%Err, MSG, RC, THISLOC=LOC )
end if
! Go back to line one
REWIND( IU_FILE )
! Get next valid line
! call GetNextLine( IU_FILE, DUM, EOF, RC )
! if ( RC /= HCO_SUCCESS .OR. EOF ) then
! MSG = 'Error 2 reading ' // trim(SpecFile)
! call HCO_Error( MSG, RC, THISLOC=LOC )
! return
! end if
!
! LNG = len(trim(DUM))
! LOW = NextCharPos ( trim(DUM), HCO_COL(), 1 )
! if ( LOW < 0 .OR. LOW == LNG ) then
! MSG = 'Cannot extract index after colon: ' // trim(DUM)
! call HCO_Error( MSG, RC, THISLOC=LOC )
! return
! end if
! LOW = LOW + 1
! READ ( DUM(LOW:LNG), * ) nModelSpec
! Allocate species arrays
allocate(ModelSpecNames (nModelSpec))
allocate(ModelSpecIDs (nModelSpec))
allocate(ModelSpecMW (nModelSpec))
allocate(ModelSpecK0 (nModelSpec))
allocate(ModelSpecCR (nModelSpec))
allocate(ModelSpecPKA (nModelSpec))
! Assign variables to each species
do N = 1, nModelSpec
call GetNextLine( IU_FILE, DUM, EOF, RC )
if ( RC /= HCO_SUCCESS .OR. EOF ) then
write(MSG,100) N, trim(SpecFile)
call HCO_Error( HcoConfig%Err, MSG, RC, THISLOC=LOC )
return
end if
! Start reading line from beginning
LNG = len(trim(DUM))
LOW = 0
! Read species ID, name, molecular weight, emitted molecular weight,
! molecular coefficient, and Henry coefficients K0, CR, pKa (in this
! order).
do I = 1, 8
! Get lower and upper index of species ID (first entry in row).
! Skip all leading spaces.
UPP = LOW
do WHILE( UPP == LOW .AND. LOW /= LNG )
LOW = LOW + 1
if ( LOW > LNG ) then
write(MSG,101) I, trim(DUM)
call HCO_Error( MSG, RC, THISLOC=LOC )
return
end if
UPP = NextCharPos( trim(DUM), HCO_SPC, LOW )
if ( UPP < 0 ) UPP = LNG
end do
if ( I < 8 ) then
UPP = UPP - 1 ! Don't read space
end if
! Error check
if ( UPP > LNG ) then
write(MSG,*) 'Error reading species property ', I, &
' on line ', trim(DUM), '. Each ', &
'species definition line is expected ', &
'to have 8 entries (ID, Name, MW, MWemis, ', &
'MolecRatio, K0, CR, PKA, e.g.: ', &
'1 CO 28.0 28.0 1.0 0.0 0.0 0.0'
call HCO_Error ( HcoConfig%Err, MSG, RC, THISLOC=LOC )
return
end if
! Read into vector
SELECT CASE ( I )
CASE ( 1 )
READ( DUM(LOW:UPP), * ) ModelSpecIDs(N)
CASE ( 2 )
READ( DUM(LOW:UPP), * ) ModelSpecNames(N)
CASE ( 3 )
READ( DUM(LOW:UPP), * ) ModelSpecMW(N)
CASE ( 4 )
! EmMW - Do nothing
CASE ( 5 )
! MolecRatio - Do nothing
CASE ( 6 )
READ( DUM(LOW:UPP), * ) ModelSpecK0(N)
CASE ( 7 )
READ( DUM(LOW:UPP), * ) ModelSpecCR(N)
CASE ( 8 )
READ( DUM(LOW:UPP), * ) ModelSpecPKA(N)
end SELECT
! Continue from upper position (+1 to skip space). The
! while loop at the beginning of the do-loop will advance
! low by another one position, so the next character
! search will start at position UPP + 2, which is exactly
! what we want (UPP is the position BEFORE the space!).
LOW = UPP + 1
end do !I
end do !N
! Close file
CLOSE( IU_FILE )
! Make sure that the species indexing starts at 1
if ( MINVAL( ModelSpecIDs ) /= 1 ) then
MSG = 'Error encountered in reading SpecFile!. The species ' // &
'ID numbers do not start at 1! Please check SpecFile ' // &
'for typos.'
call HCO_Error ( HcoConfig%Err, MSG, RC, THISLOC=LOC )
return
end if
! Make sure that the ID of the last species is the same as nModelSpec
if ( MAXVAL( ModelSpecIDs ) /= nModelSpec ) then
MSG = 'Error encountered in reading SpecFile!. The ID number ' // &
'of the last species does not match the number of species ' // &
'that were read from SpecFile! Please check SpecFile for ' //&
'typos.'
call HCO_Error ( HcoConfig%Err, MSG, RC, THISLOC=LOC )
return
end if
! Return w/ success
RC = HCO_SUCCESS
100 FORMAT( 'Error reading species ', i3, ' in ', a )
101 FORMAT( 'Cannot extract element ', i1, ' in ', a )
end subroutine Model_GetSpecies
!EOC
!------------------------------------------------------------------------------
! Harvard-NASA Emissions Component (HEMCO) !
!------------------------------------------------------------------------------
!BOP
!
! !IROUTINE: Set_Grid
!
! !DESCRIPTION: subroutine SET\_GRID reads the grid information from the
! HEMCO standalone grid file and sets all HEMCO grid arrays accordingly.
! The grid file is expected to contain information on the grid edge lon/lat
! range, as well as the number of grid cells in longitude and latitude
! direction.
!\\
!\\
! !INTERFACE:
!
subroutine SET_Grid( HcoState, RC )
!
! !USES:
!
use HCO_inquireMod, only : findFreeLUN
use HCO_ExtList_Mod, only : HCO_GetOpt, GetExtOpt, CoreNr
use HCO_VertGrid_Mod, only : HCO_VertGrid_Define
use HCO_GeoTools_Mod, only : HCO_SetPBLm
!
! !INPUT/OUTPUT PARAMETERS:
!
type(HCO_STATE), pointer :: HcoState
integer, intent(inout) :: RC
!
! !REVISION HISTORY:
! 13 Sep 2013 - C. Keller - Initial Version
! 11 May 2015 - C. Keller - Now provide lon/lat edges instead of assuming
! global grid.
! 10 Sep 2015 - C. Keller - Allow to provide mid-points instead of edges.
!EOP
!------------------------------------------------------------------------------
!BOC
!
! LOCAL VARIABLES:
!
! Scalars
integer :: NX, NY, NZ
integer :: I, J, N, LNG, LOW, UPP
integer :: IU_FILE, IOS, STRT
real(hp) :: XMIN, XMAX
real(hp) :: YMIN, YMAX
real(hp) :: DVAL
real(hp) :: DLON, DLAT
real(hp) :: PI_180, YDGR, YSN, SIN_DELTA, AM2
logical :: FOUND, EOF
! Arrays
integer :: SZ(3)
real(hp) :: RG(4)
real(hp), allocatable :: Ap(:), Bp(:)