-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwrfgc_io_pnetcdf.F
1403 lines (1269 loc) · 70 KB
/
wrfgc_io_pnetcdf.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
module wrfgc_io_pnetcdf
use mpi
use pnetcdf
! WRF
use module_domain_type, only: domain
use module_utility
use module_date_time
! GC
use GC_Stateful_Mod, only: Global_DiagList, Global_Input_Opt,Global_HcoConfig
use PRECISION_MOD
use Input_Opt_Mod, only: OptInput
use State_Chm_Mod, only: ChmState
use State_Met_Mod, only: MetState
use State_Diag_Mod, only: DgnState, Get_TagInfo, Get_NameInfo
use State_Grid_Mod, only: GrdState
USE Grid_Registry_Mod, ONLY : Lookup_Grid
use Registry_Mod
use registry_params_mod
use Charpak_Mod
use ERROR_MOD
use MetaHistContainer_Mod, ONLY: MetaHistContainer
USE HistContainer_Mod, ONLY : HistContainer
USE MetaHistItem_Mod
USE HistItem_Mod, ONLY : HistItem
! HEMCO
USE HCO_TYPES_MOD, ONLY : DiagnCollection, DiagnCont, DiagnBundle, ConfigObj
use HCO_Interface_Common, only : GetHcoDiagn
use HCO_State_GC_Mod, only : HcoState, ExtState
! WRF-GC
use WRFGC_History_Mod, only: CollectionList, CollectionCount, IndexVariables
! use DiagList_Mod, ONLY : CollList, ColItem, DgnList, DgnItem
implicit none
INTEGER, PUBLIC, PARAMETER :: GC_SUCCESS = 0 ! Routine returns success
INTEGER, PUBLIC, PARAMETER :: GC_FAILURE = -1 ! Routine returns failure
CHARACTER(LEN=9), PARAMETER, PUBLIC :: UNDEFINED_STR = 'not found'
public :: writeDiag
contains
subroutine check(err, message)
use mpi
use pnetcdf
implicit none
integer :: err
character(len=*) :: message
! It is a good idea to check returned value for possible error
if (err .NE. NF90_NOERR) then
write(*,*)'error: something wrong ', trim(message)
write(6,*) trim(message), " ",trim(nf90mpi_strerror(err))
call MPI_Abort(MPI_COMM_WORLD, -1, err)
end if
end subroutine check
subroutine construct_filename(time_str, filenametemplate1, filename, domainName)
character(len=*), intent(in) :: time_str, filenametemplate1, domainName
character(len=*), intent(out) :: filename
character :: c
character(len=5) :: cs
integer :: i, start, end, ifilename
character(len=255) :: filenametemplate
filename = 'not'
filenametemplate = filenametemplate1
start = 0
end = 0
ifilename = 1
do i = 1, 5
start = index(filenametemplate, "%")
if (start > 0) then
! copy the string before the %
filename(ifilename:ifilename+start-end-1) = filenametemplate(end+1:start-1)
ifilename = ifilename + start - end -1
! copy the string after the %
if (filenametemplate(start+2:start+2) == "2") then
end = start + 2
select case(filenametemplate(start+1:start+1))
case('m')
filename(ifilename:ifilename+1) = time_str(6:7)
case('d')
filename(ifilename: ifilename+1) = time_str(9:10)
case('h')
filename(ifilename:ifilename+1) = time_str(12:13)
case('n')
filename(ifilename:ifilename+1) = time_str(15:16)
end select
ifilename = ifilename + 2
else
filename(ifilename: ifilename+3) = time_str(1:4)
end = start + 2
ifilename = ifilename + 4
end if
filenametemplate(start:start) = '_'
end if
end do
filename(ifilename:ifilename+len_trim(filenametemplate)-end) = filenametemplate(end+1:len_trim(filenametemplate))
filename = trim(domainName)//filename
endsubroutine construct_filename
subroutine checkgc(RC, message)
use mpi
implicit none
integer :: RC
character(len=*) :: message
! It is a good idea to check returned value for possible error
if (RC .NE. GC_SUCCESS) then
call MPI_Abort(MPI_COMM_WORLD, -1, RC)
end if
end subroutine checkgc
subroutine writeDiag(debug_level, Input_Opt, State_Met, State_Chm, grid, State_Grid, State_Diag, its, ite, jts, jte, ide, jde, kte)
USE GC_Stateful_Mod, ONLY : GIGC_States
type(OptInput), intent(inout) :: Input_Opt
type(MetState), intent(inout) :: State_Met
type(ChmState), intent(inout) :: State_Chm
type(GrdState), intent(inout) :: State_Grid
type(DgnState), intent(in) :: State_Diag
type(domain), target :: grid
integer(kind=MPI_OFFSET_KIND):: i, time_index, time_count
integer, intent(in)::debug_level
INTEGER:: RC, N ! Success or failure?
integer :: time_unixs(1), dimensions(3)
integer(kind=MPI_OFFSET_KIND), intent(in) :: ide, jde, kte, its, ite, jts, jte
integer :: ncid, ierr, dimids(8), xtimei, varid, time_unix, nTags, year, month, day, hour, minute, second, time_interval
character(len=50) :: filename, time_unix_start
real :: xtime
type(WRFU_Clock) :: clock
TYPE(WRFU_Time) :: currTime, startTime, stopTime, referenceTimem, time
TYPE(WRFU_TimeInterval) :: timeStep
CHARACTER (LEN=64) :: currTime_str, startTime_str, stopTime_str, filename_time_str, time_str
CHARACTER (LEN=19) :: referenceTime_str, new_referenceTime_str
CHARACTER (LEN=64) :: diag_name, state_name
integer, dimension(2) :: s
type(MetaRegItem), pointer:: current_reg
! type(DgnItem), pointer :: current_diag
character(len=255) :: ItemTemplate, ItemName, ItemTemplateUC, ItemNameUC, ItemPrefix,SubStrs(255), tagId, &
tagName, ErrMsg, OutputName, Description, Units
character(len=80) :: ErrorLine
character(len=5) :: StateMetUC, StateChmUC, domainName
logical :: Found
type(HistContainer) , POINTER:: Current_container
type(MetaHistContainer), POINTER :: Current_metahistcontainer
type(HistItem), POINTER :: Current_Item
type(MetaHistItem), POINTER :: Current_metahistitem
REAL(f8), POINTER :: Grid_Lat (: )
REAL(f8), POINTER :: Grid_LatE(: )
REAL(f8), POINTER :: Grid_Lon (: )
REAL(f8), POINTER :: Grid_LonE(: )
REAL(f8), POINTER :: Source_0d_8 ! Ptr to 0D 8-byte data
REAL(f4), pointer :: Source_0d_4 ! Ptr to 0D 4-byte data
INTEGER, POINTER :: Source_0d_I ! Ptr to 0D integer data
REAL(f8), POINTER :: Source_1d_8(: ) ! Ptr to 1D 8-byte data
REAL(f4), POINTER :: Source_1d_4(: ) ! Ptr to 1D 4-byte data
INTEGER, POINTER :: Source_1d_I(: ) ! Ptr to 1D integer data
REAL(f8), POINTER :: Source_2d_8(:,: ) ! Ptr to 2D 8-byte data
REAL(f4), POINTER :: Source_2d_4(:,: ) ! Ptr to 2D 4-byte data
INTEGER, POINTER :: Source_2d_I(:,: ) ! Ptr to 2D integer data
REAL(f8), POINTER :: Source_3d_8(:,:,:) ! Ptr to 3D 8-byte data
REAL(f4), POINTER :: Source_3d_4(:,:,:) ! Ptr to 3D 4-byte data
INTEGER, POINTER :: Source_3d_I(:,:,:) ! Ptr to 3D integer data
REAL(f8), POINTER :: Data_0d ! 0D scalar
REAL(f8), POINTER :: Data_1d(: ) ! 1D vector
REAL(f8), POINTER :: Data_2d(:,: ) ! 2D array
REAL(f8), POINTER :: Data_3d(:,:,:) ! 3D array
REAL :: T1, T2 ! for measuring exctuion time
! for hemco
type(DiagnCollection), pointer :: hco_collection
type(DiagnCont), pointer :: hco_diag
logical :: do_diagn_now
integer :: delta_s
Source_0d_8 => NULL()
Source_1d_8 => NULL()
Source_1d_4 => NULL()
Source_1d_I => NULL()
Source_2d_8 => NULL()
Source_2d_4 => NULL()
Source_2d_I => NULL()
Source_3d_8 => NULL()
Source_3d_4 => NULL()
Source_3d_I => NULL()
Data_0d => NULL()
Data_1d => NULL()
Data_2d => NULL()
Data_3d => NULL()
RC = GC_SUCCESS
! do nothing if function called from chemics_init.F
if (.not. associated(GIGC_States(grid%id)%HcoState))then
return
endif
if (debug_level .gt. 200)then
write(*,*) 'The current output domain: ',State_Grid%ID, grid%id;
endif
write(domainName, '(A,I2.2)'), 'd', grid%id;
! get time
clock = grid%domain_clock
CALL WRFU_ClockGet( clock, CurrTime=currTime, StartTime=startTime, &
StopTime=stopTime, TimeStep=timeStep, rc=rc )
call wrf_timetoa( startTime, startTime_str )
call wrf_timetoa( currTime, currTime_str )
call wrf_timetoa( stopTime, stopTime_str )
xtime = grid%xtime
! get filename
! use wrf history interval as output interval
xtimei = int(xtime/grid%history_interval)
write(*,*)'before get collection, line 241'
! write hemco diagnostic
hco_collection => GIGC_States(State_Grid%id)%HcoState%Diagn%Collections
write(*,*)'after get collection, line 245'
do while(associated(hco_collection))
do_diagn_now = .False.
if (debug_level .ge. 500) then
write(*,*)'write hemco diagn: ', trim(hco_collection%PREFIX)
write(*,*)'frequency: ',hco_collection%deltaYMD, hco_collection%deltaHMS
endif
if ((hco_collection%deltaHMS == 1 .and. hco_collection%deltaYMD == 0) .or. hco_collection%deltaYMD == -1)then
! always output
do_diagn_now = .True.
write(filename, *)trim(hco_collection%PREFIX),"_",trim(domainName), "_", currTime_str(1:4),'_',currTime_str(6:7),"_", currTime_str(9:10),currTime_str(12:13),currTime_str(15:16), currTime_str(18:19), '.nc'
else if (hco_collection%lastYMD == -1)then
! first time output, using start time and update lastymd
do_diagn_now = .True.
read(startTime_str(1:4), *) year
read(startTime_str(6:7), *) month
read(startTime_str(9:10), *) day
read(startTime_str(12:13), *) hour
read(startTime_str(15:16), *) minute
read(startTime_str(18:19), *) second
write(filename, *)trim(hco_collection%PREFIX),"_",trim(domainName), "_", startTime_str(1:4),'_',currTime_str(6:7),"_", currTime_str(9:10),currTime_str(12:13),currTime_str(15:16), currTime_str(18:19), '.nc'
hco_collection%lastYMD = year*10000 + month*100 + day
hco_collection%lastHms = hour*10000 + minute*100 + second
else
! if interval > time interval, output and update lastymd
year = hco_collection%lastYMD / 10000
month = (hco_collection%lastYMD - year*10000) / 100
day = hco_collection%lastYMD - year*10000 - month*100
hour = hco_collection%lastHms / 10000
minute = (hco_collection%lastHms - hour*10000) / 100
second = hco_collection%lastHms - hour*10000 - minute*100
write(referenceTime_str,'(I4.4, "-", I2.2, "-", I2.2, " ", I2.2, ":", I2.2, ":", I2.2)') year, month, day, hour, minute, second
call geth_idts(currTime_str(1:19), referenceTime_str(1:19), time_interval)
year = hco_collection%deltaYMD / 10000
month = (hco_collection%deltaYMD - year*10000) /100
day = hco_collection%deltaYMD - year*10000 - month*100
hour = hco_collection%deltaHms / 10000
minute = (hco_collection%deltaHms - hour*10000) / 100
second = hco_collection%deltaHms - hour*10000 - minute*100
delta_s = year*365*24*60*60 + month*30*24*60*60 + day*24*60*60 + hour*60*60 + minute*60 + second
if (time_interval > delta_s) then
do_diagn_now = .True.
call geth_newdate(new_referenceTime_str, referenceTime_str, delta_s)
read(new_referenceTime_str(1:4), *) year
read(new_referenceTime_str(6:7), *) month
read(new_referenceTime_str(9:10), *) day
read(new_referenceTime_str(12:13), *) hour
read(new_referenceTime_str(15:16), *) minute
read(new_referenceTime_str(18:19), *) second
write(filename, *)trim(hco_collection%PREFIX),"_",trim(domainName), "_", new_referenceTime_str(1:4),'_',new_referenceTime_str(6:7),"_", new_referenceTime_str(9:10),new_referenceTime_str(12:13),new_referenceTime_str(15:16), new_referenceTime_str(18:19), '.nc'
hco_collection%lastYMD = year*10000 + month*100 + day
hco_collection%lastHms = hour*10000 + minute*100 + second
else if (time_interval<11*60)then
! if interval < 11 minutes, output
do_diagn_now = .True.
write(filename, *)trim(hco_collection%PREFIX),"_",trim(domainName), "_", stopTime_str(1:4),'_',stopTime_str(6:7),"_", stopTime_str(9:10),stopTime_str(12:13),stopTime_str(15:16), stopTime_str(18:19), '.nc'
ENDIF
endif
! do_diagn_now = .False.
! ! last time = -1
! if (hco_collection%lastYMD == -1) do_diagn_now = .True.
! if (hco_collection%deltaYMD == -1) do_diagn_now = .True.
! call geth_idts(stopTime_str(1:19), currTime_str(1:19), time_interval)
! ! if reach end of simulation, do diagn
! if (time_interval<11*60) do_diagn_now = .True.
! ! if current time - last time > delta time
! if ( .not. do_diagn_now)then
! year = hco_collection%lastYMD / 10000
! month = (hco_collection%lastYMD - year*10000) / 100
! day = hco_collection%lastYMD - year*10000 - month*100
! hour = hco_collection%lastHms / 10000
! minute = (hco_collection%lastHms - hour*10000) / 100
! second = hco_collection%lastHms - hour*10000 - minute*100
! write(referenceTime_str,&
! '(I4.4, "-", I2.2, "-", I2.2, " ", I2.2, ":", I2.2, ":", I2.2)'&
! ) year, month, day, hour, minute, second
! call geth_idts(currTime_str(1:19), referenceTime_str(1:19), time_interval)
! ! convert deltaymd and deltahms to second
! year = hco_collection%deltaYMD / 10000
! month = (hco_collection%deltaYMD - year*10000) /100
! day = hco_collection%deltaYMD - year*10000 - month*100
! hour = hco_collection%deltaHms / 10000
! minute = (hco_collection%deltaHms - hour*10000) / 100
! second = hco_collection%deltaHms - hour*10000 - minute*100
! delta_s = year*365*24*60*60 + month*30*24*60*60 + day*24*60*60 + hour*60*60 + minute*60 + second
! if (time_interval > delta_s) do_diagn_now = .True.
! endif
T1 = MPI_Wtime()
if ( do_diagn_now )then
! ! write(*,* )'wrfgc_io_pnetcdf: write hemco diagn.'
! ! update lastymd and last hms
! if (hco_collection%deltaYMD == -1)then
! ! always output
! write(filename, *)trim(hco_collection%PREFIX), "_", currTime_str(1:4),'_',currTime_str(6:7),"_", currTime_str(9:10),currTime_str(12:13),currTime_str(15:16), currTime_str(18:19), '.nc'
! else if (hco_collection%lastYMD == -1)then
! read(startTime_str(1:4), *) year
! read(startTime_str(6:7), *) month
! read(startTime_str(9:10), *) day
! read(startTime_str(12:13), *) hour
! read(startTime_str(15:16), *) minute
! read(startTime_str(18:19), *) second
! write(filename, *)trim(hco_collection%PREFIX), "_", startTime_str(1:4),'_',currTime_str(6:7),"_", currTime_str(9:10),currTime_str(12:13),currTime_str(15:16), currTime_str(18:19), '.nc'
! else if (time_interval<11*60)then
! write(filename, *)trim(hco_collection%PREFIX), "_", stopTime_str(1:4),'_',stopTime_str(6:7),"_", stopTime_str(9:10),stopTime_str(12:13),stopTime_str(15:16), stopTime_str(18:19), '.nc'
! else
! ! convert deltaymd and deltahms to second
! year = hco_collection%deltaYMD / 10000
! month = (hco_collection%deltaYMD - year*10000) /100
! day = hco_collection%deltaYMD - year*10000 - month*100
! hour = hco_collection%deltaHms / 10000
! minute = (hco_collection%deltaHms - hour*10000) / 100
! second = hco_collection%deltaHms - hour*10000 - minute*100
! delta_s = year*365*24*60*60 + month*30*24*60*60 + day*24*60*60 + hour*60*60 + minute*60 + second
! year = hco_collection%lastYMD / 10000
! month = (hco_collection%lastYMD - year*10000) / 100
! day = hco_collection%lastYMD - year*10000 - month*100
! hour = hco_collection%lastHms / 10000
! minute = (hco_collection%lastHms - hour*10000) / 100
! second = hco_collection%lastHms - hour*10000 - minute*100
! write(referenceTime_str,'(I4.4, "-", I2.2, "-", I2.2, " ", I2.2, ":", I2.2, ":", I2.2)') year, month, day, hour, minute, second
! call geth_newdate(new_referenceTime_str, referenceTime_str, delta_s)
! read(new_referenceTime_str(1:4), *) year
! read(new_referenceTime_str(6:7), *) month
! read(new_referenceTime_str(9:10), *) day
! read(new_referenceTime_str(12:13), *) hour
! read(new_referenceTime_str(15:16), *) minute
! read(new_referenceTime_str(18:19), *) second
! write(filename, *)trim(hco_collection%PREFIX), "_", new_referenceTime_str(1:4),'_',new_referenceTime_str(6:7),"_", new_referenceTime_str(9:10),new_referenceTime_str(12:13),new_referenceTime_str(15:16), new_referenceTime_str(18:19), '.nc'
! endif
! hco_collection%lastYMD = year*10000 + month*100 + day
! hco_collection%lastHms = hour*10000 + minute*100 + second
filename = trim(ADJUSTL(filename))
call create_file(filename, ncid, ide, jde, kte, dimids, State_Grid, Input_Opt)
ierr = nf90mpi_redef(ncid)
call check(ierr, 'redef')
! def var all at once
hco_diag => hco_collection%DiagnList
do while(associated(hco_diag))
if ( hco_diag%SpaceDim == 2 .and. associated(hco_diag%Arr2D))then
if (debug_level .ge. 500) then
write(*,*)'def hemco diagn 2d: name: ', hco_diag%cName
endif
if (associated(hco_diag%Arr2D%Val))then
ierr = nf90mpi_def_var(ncid, trim(hco_diag%cName), NF90_FLOAT, (/dimids(1), dimids(2), dimids(4)/), varid)
call check(ierr, 'def var')
else
write(*,*)'val not allocated'
endif
endif
if (hco_diag%SpaceDim == 3 .and. associated(hco_diag%Arr3D))then
if (debug_level .ge. 500) then
write(*,*)'def hemco diagn 3d : name: ', hco_diag%cName
endif
if (associated(hco_diag%Arr3D%Val))then
ierr = nf90mpi_def_var(ncid, trim(hco_diag%cName), NF90_FLOAT, (/dimids(1), dimids(2), dimids(3), dimids(4)/), varid)
call check(ierr, 'def var')
else
write(*,*)'val not allocated'
endif
endif
ierr = nf90mpi_put_att(ncid, varid, 'units', trim(hco_diag%OutUnit))
ierr = nf90mpi_put_att(ncid, varid, 'long_name', trim(hco_diag%long_name))
hco_diag => hco_diag%NextCont
enddo
ierr = nf90mpi_enddef(ncid)
call check(ierr, 'enddef')
! write dimension
allocate(Grid_Lat(State_Grid%NY), STAT=RC)
call checkgc(RC, 'allocate Grid_lat')
do i = 1,State_Grid%NY
Grid_Lat(i) = State_Grid%YMid(1, i)
enddo
call writeDimension('lat', Grid_Lat, ncid, jts, jte)
allocate(Grid_Lon(State_Grid%NX), STAT=RC)
call checkgc(RC, 'allocate Grid_lon')
do i = 1,State_Grid%NX
Grid_Lon(i) = State_Grid%XMid(i, 1)
enddo
call writeDimension('lon', Grid_Lon, ncid, its, ite)
allocate(Grid_LatE(State_Grid%NY+1), STAT=RC)
call checkgc(RC, 'allocate Grid_lat')
do i = 1,State_Grid%NY+1
Grid_LatE(i) = State_Grid%YEdge(1, i)
enddo
call writeDimension('lat_e', Grid_LatE, ncid, jts, jte+1)
allocate(Grid_LonE(State_Grid%NX+1), STAT=RC)
call checkgc(RC, 'allocate Grid_lon')
do i = 1,State_Grid%NX+1
Grid_LonE(i) = State_Grid%XEdge(i, 1)
enddo
call writeDimension('lon_e', Grid_LonE, ncid, its, ite+1)
! write time
ierr = nf90mpi_inq_varid(ncid, 'time', varid)
call check(ierr, 'inq varid time')
time_index = 1
time_count = 1
time_unix_start = "1970-01-01_00:00:00"
call geth_idts(currTime_str(1:19), time_unix_start(1:19), time_unix)
time_unixs(1) = time_unix
ierr = nf90mpi_put_var_all(ncid, varid, time_unixs, (/time_index/), (/time_count/))
call check(ierr, 'put var time')
! write data array
hco_diag => hco_collection%DiagnList
do while(associated(hco_diag))
Source_2d_4 => NULL()
Source_3d_4 => NULL()
if(debug_level .ge. 500)then
write(*,* )'write hemco diagn: ', hco_diag%cID
endif
if ( hco_diag%SpaceDim == 2 .and. associated(hco_diag%Arr2D))then
CALL GetHcoDiagn( HcoState, ExtState,hco_diag%cName, .false., RC, Ptr2D=Source_2d_4, &
COL=HcoState%Diagn%HcoDiagnIDDefault)
if (debug_level .ge. 500) then
write(*,*)'write hemco diagn 2d: name: ', hco_diag%cName
endif
if (associated(Source_2d_4))then
call write2file(trim(hco_diag%cName), Source_2d_4, &
Data_2d, its, ite, jts, jte, ide, jde, &
time_index, ncid, (/dimids(1:2), dimids(4)/),&
trim(hco_diag%OutUnit), trim(hco_diag%long_name))
else
write(*,*)'val not allocated'
endif
endif
if (hco_diag%SpaceDim == 3 .and. associated(hco_diag%Arr3D))then
CALL GetHcoDiagn( HcoState, ExtState,hco_diag%cName, .false., RC, Ptr3D=Source_3d_4, &
COL=HcoState%Diagn%HcoDiagnIDDefault)
if (debug_level .ge. 500) then
write(*,*)'write hemco diagn 3d : name: ', hco_diag%cName
endif
if (associated(Source_3d_4))then
call write2file3d(trim(hco_diag%cName), Source_3d_4, &
Data_3d, its, ite, jts, jte, ide, jde, &
kte, time_index, ncid, dimids, &
trim(hco_diag%OutUnit), trim(hco_diag%long_name))
else
write(*,*)'val not allocated'
endif
endif
Source_2d_4 => NULL()
Source_3d_4 => NULL()
hco_diag => hco_diag%NextCont
enddo
call closefile(ncid)
endif
T2 = MPI_Wtime()
write(*,*)'hemco write time: ', T2-T1
hco_collection => hco_collection%NextCollection
enddo
! clean up
hco_collection => null()
Grid_Lat => null()
Grid_Lon => null()
Grid_LatE => null()
Grid_LonE => null()
Source_2d_4 => NULL()
Source_3d_4 => NULL()
! write wrfchem diagnostic
Current_metahistcontainer => CollectionList(State_Grid%id)%p
do while(associated(Current_metahistcontainer))
! open file according to the reference time
Current_container => Current_metahistcontainer%Container
year = Current_container%ReferenceYmd / 10000
month = (Current_container%ReferenceYmd - year*10000) / 100
day = Current_container%ReferenceYmd - year*10000 - month*100
hour = Current_container%ReferenceHms / 10000
minute = (Current_container%ReferenceHms - hour*10000) / 100
second = Current_container%ReferenceHms - hour*10000 - minute*100
write(referenceTime_str, '(I4.4, "-", I2.2, "-", I2.2, " ", I2.2, ":", I2.2, ":", I2.2)') year, month, day, hour, minute, second
! if referencetime < starttime, create new file
! referencetime = starttime + n*fileclosetime
call geth_idts(referenceTime_str(1:19), startTime_str(1:19), time_interval)
! write(*,*)'file close time_interval: ', time_interval
! write(*,*)'reference time: ', trim(referenceTime_str)
! write(*,*)'start time: ', trim(startTime_str)
if (time_interval<0) then
! At the begining of simulation
call construct_filename(startTime_str, Current_container%Filename, filename, domainName)
call create_file(filename, Current_container%FileId, ide, jde, kte, dimids, State_Grid, Input_Opt)
read(currTime_str(1:4), *) year
read(currTime_str(6:7), *) month
read(currTime_str(9:10), *) day
read(currTime_str(12:13), *) hour
read(currTime_str(15:16), *) minute
read(currTime_str(18:19), *) second
! update current container
Current_container%ReferenceYmd = year*10000 + month*100 + day
Current_container%ReferenceHms = hour*10000 + minute*100 + second
Current_container%xDimId = dimids(1)
Current_container%yDimId = dimids(2)
Current_container%zDimId = dimids(3)
Current_container%tDimId = dimids(4)
Current_container%IsFileOpen = .true.
! write(*,*)'after create file: ', Current_container%FileId
! write(*,*)'file dimids: ', Current_container%xDimId, Current_container%yDimId, Current_container%zDimId, Current_container%tDimId
! write(*,*)'file is open: ', Current_container%IsFileOpen
else
! if currenttime > referencetime + fileclosetime, close file then create new file
call geth_idts(currTime_str(1:19), referenceTime_str(1:19), time_interval)
! write(*,*)'time interval: ', time_interval
if ( time_interval >= Current_container%FileCloseIvalSec)then
! write(*,*)'close file', Current_container%FileId
call closefile(Current_container%FileId)
call geth_newdate(new_referenceTime_str(1:19), referenceTime_str(1:19), int(Current_container%FileCloseIvalSec))
call construct_filename(new_referenceTime_str, Current_container%Filename, filename, domainName)
filename = trim(filename)
call create_file(filename, Current_container%FileId, ide, jde, kte, dimids, State_Grid, Input_Opt)
read(new_referenceTime_str(1:4), *) year
read(new_referenceTime_str(6:7), *) month
read(new_referenceTime_str(9:10), *) day
read(new_referenceTime_str(12:13), *) hour
read(new_referenceTime_str(15:16), *) minute
read(new_referenceTime_str(18:19), *) second
! update current container
Current_container%ReferenceYmd = year*10000 + month*100 + day
Current_container%ReferenceHms = hour*10000 + minute*100 + second
Current_container%xDimId = dimids(1)
Current_container%yDimId = dimids(2)
Current_container%zDimId = dimids(3)
Current_container%tDimId = dimids(4)
Current_container%IsFileOpen = .true.
else
! write(*,*)'is file opened?',Current_container%IsFileOpen
if (Current_container%IsFileOpen) then
! write(*,*)'file is open: ', Current_container%FileId
! if currenttime < referencetime + fileclosetime, use current file
dimids(1) = Current_container%xDimId
dimids(2) = Current_container%yDimId
dimids(3) = Current_container%zDimId
dimids(4) = Current_container%tDimId
else
! write(*,*)'file is not open'
! currenttime < referencetime + fileclosetime, but file is not open, open file
call construct_filename(referenceTime_str, Current_container%Filename, filename, domainName)
filename = trim(filename)
call create_file(filename, Current_container%FileId, ide, jde, kte, dimids, State_Grid, Input_Opt)
Current_container%xDimId = dimids(1)
Current_container%yDimId = dimids(2)
Current_container%zDimId = dimids(3)
Current_container%tDimId = dimids(4)
Current_container%IsFileOpen = .true.
endif
endif
endif
! write file according to currentcontainer%currentYmd
!
year = Current_container%CurrentYmd / 10000
month = (Current_container%CurrentYmd - year*10000) / 100
day = Current_container%CurrentYmd - year*10000 - month*100
hour = Current_container%CurrentHms / 10000
minute = (Current_container%CurrentHms - hour*10000) / 100
second = Current_container%CurrentHms - hour*10000 - minute*100
write(time_str, '(I4.4, "-", I2.2, "-", I2.2, " ", I2.2, ":", I2.2, ":", I2.2)') year, month, day, hour, minute, second
call geth_idts( currTime_str(1:19), time_str(1:19), time_interval)
! 1. interval < 0, write to current file, update container%currentYmd
! 2. interval > 0, interval < fileupdateinterval, skip
! 3. interval > 0, interval > fileupdateinterval, write to current file and update container%currentYmd
! write(*,*)'current diag: ', trim(Current_container%Name)
! write(*,*)'time_interval: ', time_interval
! write(*,*)'updateinterval: ', Current_container%UpdateIvalSec
! write(*,*)'current time:', trim(currTime_str)
! write(*,*)'last write time: ', trim(time_str)
if (time_interval <= 0 .or. time_interval >= Current_container%UpdateIvalSec)then
! write dimensions to variables
ierr = nf90mpi_inquire_dimension(Current_container%FileId, dimids(4), len=time_index)
call check(ierr, 'inq dim time')
! write(*,*)'time length: ', time_index
if (time_index == 0) then
if (debug_level .ge. 500) then
write(*,*)'def vars'
endif
Current_metahistitem => Current_container%HistItems
ierr = nf90mpi_redef(Current_container%FileId)
call check(ierr, 'redef')
! def var all at once
do while(associated(Current_metahistitem))
Current_Item => Current_metahistitem%Item
SELECT CASE(Current_Item%SpaceDim)
CASE( 3 )
ierr = nf90mpi_def_var( Current_container%FileId, \
trim(Current_Item%Name), \
NF90_FLOAT, \
(/dimids(1),dimids(2),dimids(3),dimids(4)/), \
varid)
call check(ierr, 'def var')
Current_Item%NcVarId = varid
CASE( 2 )
ierr = nf90mpi_def_var( Current_container%FileId, \
trim(Current_Item%Name), \
NF90_FLOAT, \
(/dimids(1),dimids(2),dimids(4)/), \
varid)
call check(ierr, 'def var')
Current_Item%NcVarId = varid
end select
ierr = nf90mpi_put_att(Current_container%FileId, \
varid,\
'units', \
trim(Current_Item%Units))
ierr = nf90mpi_put_att(Current_container%FileId, \
varid,\
'long_name', \
trim(Current_Item%LongName))
Current_metahistitem => Current_metahistitem%Next
enddo
ierr = nf90mpi_enddef(Current_container%FileId)
call check(ierr, 'enddef, may caused by too many variables in the collection')
if (debug_level .ge. 500) then
write(*,*)'write dimensions'
endif
allocate(Grid_Lat(State_Grid%NY), STAT=RC)
call checkgc(RC, 'allocate Grid_lat')
do i = 1,State_Grid%NY
Grid_Lat(i) = State_Grid%YMid(1, i)
enddo
call writeDimension('lat', Grid_Lat, Current_container%FileId, jts, jte)
allocate(Grid_Lon(State_Grid%NX), STAT=RC)
call checkgc(RC, 'allocate Grid_lon')
do i = 1,State_Grid%NX
Grid_Lon(i) = State_Grid%XMid(i, 1)
enddo
call writeDimension('lon', Grid_Lon, Current_container%FileId, its, ite)
allocate(Grid_LatE(State_Grid%NY+1), STAT=RC)
call checkgc(RC, 'allocate Grid_lat')
do i = 1,State_Grid%NY+1
Grid_LatE(i) = State_Grid%YEdge(1, i)
enddo
call writeDimension('lat_e', Grid_LatE, Current_container%FileId, jts, jte+1)
allocate(Grid_LonE(State_Grid%NX+1), STAT=RC)
call checkgc(RC, 'allocate Grid_lon')
do i = 1,State_Grid%NX+1
Grid_LonE(i) = State_Grid%XEdge(i, 1)
enddo
call writeDimension('lon_e', Grid_LonE, Current_container%FileId, its, ite+1)
! write index variables only for domain 1
if (State_Grid%ID == 1)then
write(*,*)'index variables: ', trim(IndexVariables(State_Grid%ID)%p%Item%Name)
Current_metahistitem => IndexVariables(State_Grid%ID)%p
do while(associated(Current_metahistitem))
Current_Item => Current_metahistitem%Item
SELECT CASE(Current_Item%SpaceDim)
CASE( 3 )
if (Current_Item%Source_KindVal == KINDVAL_F8) then
Current_Item%Data_3d = Current_Item%Source_3d_8
else if (Current_Item%Source_KindVal == KINDVAL_F4) then
Current_Item%Data_3d = Current_Item%Source_3d_4
else if (Current_Item%Source_KindVal == KINDVAL_I4) then
Current_Item%Data_3d = Current_Item%Source_3d_I
endif
CASE( 2 )
if (Current_Item%Source_KindVal == KINDVAL_F8) then
Current_Item%Data_2d = Current_Item%Source_2d_8
else if (Current_Item%Source_KindVal == KINDVAL_F4) then
Current_Item%Data_2d = Current_Item%Source_2d_4
else if (Current_Item%Source_KindVal == KINDVAL_I4) then
Current_Item%Data_2d = Current_Item%Source_2d_I
endif
CASE( 1 )
if (Current_Item%Source_KindVal == KINDVAL_F8) then
Current_Item%Data_1d = Current_Item%Source_1d_8
else if (Current_Item%Source_KindVal == KINDVAL_F4) then
Current_Item%Data_1d = Current_Item%Source_1d_4
else if (Current_Item%Source_KindVal == KINDVAL_I4) then
Current_Item%Data_1d = Current_Item%Source_1d_I
endif
CASE( 0 )
Current_Item%Data_0d = Current_Item%Source_0d_8
end select
SELECT CASE(Current_Item%Name)
! CASE('lat_bnds')
! ierr = nf90mpi_redef(Current_container%FileId)
! call check(ierr, 'redef')
! ierr = nf90mpi_def_var( Current_container%FileId, \
! 'lat_bnds', \
! NF90_FLOAT, \
! (/dimids(8),dimids(2)/), \
! varid)
! call check(ierr, 'def var lat_bnds')
! ierr = nf90mpi_enddef(Current_container%FileId)
! call check(ierr, 'enddef')
! ierr = nf90mpi_put_var_all( Current_container%FileId,\
! varid,\
! Current_Item%Data_2d,\
! (/int(1,kind=MPI_OFFSET_KIND),jts/),\
! (/int(2,kind=MPI_OFFSET_KIND),jte-jts+1/) )
! call check(ierr, 'put var latbnds')
! CASE('lon_bnds')
! ierr = nf90mpi_redef(Current_container%FileId)
! call check(ierr, 'redef')
! ierr = nf90mpi_def_var( Current_container%FileId, \
! 'lon_bnds', \
! NF90_FLOAT, \
! (/dimids(8),dimids(1)/), \
! varid)
! call check(ierr, 'def var lon_bnds')
! ierr = nf90mpi_enddef(Current_container%FileId)
! call check(ierr, 'enddef')
! ierr = nf90mpi_put_var_all( Current_container%FileId,\
! varid,\
! Current_Item%Data_2d,\
! (/int(1,kind=MPI_OFFSET_KIND),its/),\
! (/int(2,kind=MPI_OFFSET_KIND),ite-its+1/) )
! call check(ierr, 'put var lonbnds')
CASE('lev')
ierr = nf90mpi_inq_varid(Current_container%FileId, 'lev', varid)
call check(ierr, 'inq lev')
ierr = nf90mpi_put_var_all( Current_container%FileId,\
varid,\
Current_Item%Data_1d,\
(/int(1, MPI_OFFSET_KIND)/),\
(/kte/) )
call check(ierr, 'put var lev')
CASE('ilev')
ierr = nf90mpi_inq_varid(Current_container%FileId, 'ilev', varid)
call check(ierr, 'inq ilev')
ierr = nf90mpi_put_var_all( Current_container%FileId,\
varid,\
Current_Item%Data_1d,\
(/int(1, MPI_OFFSET_KIND)/),\
(/kte+1/) )
call check(ierr, 'put var ilev')
CASE('hyam')
ierr = nf90mpi_redef(Current_container%FileId)
call check(ierr, 'redef')
ierr = nf90mpi_def_var( Current_container%FileId, \
'hyam', \
NF90_FLOAT, \
dimids(3), \
varid)
call check(ierr, 'def var hyam')
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'units',\
Current_Item%Units)
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'long_name',\
Current_Item%LongName)
ierr = nf90mpi_enddef(Current_container%FileId)
call check(ierr, 'enddef')
ierr = nf90mpi_put_var_all( Current_container%FileId,\
varid,\
Current_Item%Data_1d,\
(/int(1, MPI_OFFSET_KIND)/),\
(/kte/) )
call check(ierr, 'put var hyam')
CASE('hybm')
ierr = nf90mpi_redef(Current_container%FileId)
call check(ierr, 'redef')
ierr = nf90mpi_def_var( Current_container%FileId, \
'hybm', \
NF90_FLOAT, \
dimids(3), \
varid)
call check(ierr, 'def var hybm')
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'units',\
Current_Item%Units)
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'long_name',\
Current_Item%LongName)
ierr = nf90mpi_enddef(Current_container%FileId)
call check(ierr, 'enddef')
ierr = nf90mpi_put_var_all( Current_container%FileId,\
varid,\
Current_Item%Data_1d,\
(/int(1, MPI_OFFSET_KIND)/),\
(/kte/) )
call check(ierr, 'put var hybm')
CASE('hyai')
ierr = nf90mpi_redef(Current_container%FileId)
call check(ierr, 'redef')
ierr = nf90mpi_def_var( Current_container%FileId, \
'hyai', \
NF90_FLOAT, \
dimids(7), \
varid)
call check(ierr, 'def var hyai')
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'units',\
Current_Item%Units)
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'long_name',\
Current_Item%LongName)
ierr = nf90mpi_enddef(Current_container%FileId)
call check(ierr, 'enddef')
ierr = nf90mpi_put_var_all( Current_container%FileId,\
varid,\
Current_Item%Data_1d,\
(/int(1, MPI_OFFSET_KIND)/),\
(/kte/) )
call check(ierr, 'put var hyai')
CASE('hybi')
ierr = nf90mpi_redef(Current_container%FileId)
call check(ierr, 'redef')
ierr = nf90mpi_def_var( Current_container%FileId, \
'hybi', \
NF90_FLOAT, \
dimids(7), \
varid)
call check(ierr, 'def var hybi')
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'units',\
Current_Item%Units)
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'long_name',\
Current_Item%LongName)
ierr = nf90mpi_enddef(Current_container%FileId)
call check(ierr, 'enddef')
ierr = nf90mpi_put_var_all( Current_container%FileId,\
varid,\
Current_Item%Data_1d,\
(/int(1, MPI_OFFSET_KIND)/),\
(/kte/) )
call check(ierr, 'put var hybi')
! ======= AREA is not working for now =======
! CASE('AREA')
! ierr = nf90mpi_redef(Current_container%FileId)
! call check(ierr, 'redef')
! ierr = nf90mpi_def_var( Current_container%FileId, \
! 'AREA', \
! NF90_FLOAT, \
! (/dimids(1), dimids(2)/), \
! varid)
! call check(ierr, 'def var AREA')
! ierr = nf90mpi_put_att( Current_container%FileId,\
! varid,\
! 'units',\
! Current_Item%Units)
! ierr = nf90mpi_put_att( Current_container%FileId,\
! varid,\
! 'long_name',\
! Current_Item%LongName)
! ierr = nf90mpi_enddef(Current_container%FileId)
! call check(ierr, 'enddef')
! ierr = nf90mpi_put_var_all( Current_container%FileId,\
! varid,\
! Current_Item%Data_2d,\
! (/its, jts/),\
! (/ite-its+1, jte-jts+1/) )
! call check(ierr, 'put var AREA')
CASE('P0')
ierr = nf90mpi_redef(Current_container%FileId)
call check(ierr, 'redef')
ierr = nf90mpi_def_var( Current_container%FileId, \
'P0', \
NF90_FLOAT, \
varid)
call check(ierr, 'def var P0')
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'units',\
Current_Item%Units)
ierr = nf90mpi_put_att( Current_container%FileId,\
varid,\
'long_name',\
Current_Item%LongName)
call check(ierr, 'put att P0')
ierr = nf90mpi_enddef(Current_container%FileId)
call check(ierr, 'enddef')
ierr = nf90mpi_put_var_all( Current_container%FileId,\
varid,\
Current_Item%Data_0d)
call check(ierr, 'put var P0')
END SELECT
Current_metahistitem => Current_metahistitem%Next
end do
endif
endif
! write time
ierr = nf90mpi_inq_varid(Current_container%FileId, 'time', varid)
call check(ierr, 'inq varid time')
time_index = time_index + 1
time_count = 1
time_unix_start = "1970-01-01_00:00:00"
call geth_idts(currTime_str(1:19), time_unix_start(1:19), time_unix)
time_unixs(1) = time_unix
ierr = nf90mpi_put_var_all(Current_container%FileId, varid, time_unixs, (/time_index/), (/time_count/))
call check(ierr, 'put var time')
! write the data
! if instantaneous, write the source data
dimensions = (/Current_container%nX, Current_container%nY, Current_container%nZ/)
if (To_UpperCase(trim(Current_container%UpdateMode)) == To_UpperCase('instantaneous'))then
Current_metahistitem => Current_container%HistItems
do while (associated(Current_metahistitem))
! write(*,*) 'in while loop: ', trim(Current_metahistitem%Item%Name)
Current_Item => Current_metahistitem%Item
! write(*,*) 'current item ', Current_Item%Name
if (wrf_debug_level .ge. 500) then
write(*,*)'write item: ', Current_Item%Name
write(*,*)'write item: ', Current_Item%Units
write(*,*)'write item: ', Current_Item%LongName
endif
write(*,*)'write item: ', Current_Item%LongName
call writeItemName( Current_Item%Name, Input_Opt, State_Diag, State_Chm, State_Met,&
its, ite, jts, jte, ide, jde, kte, time_index, Current_container%FileId, dimids(1:4), &
Current_Item%SpaceDim, dimensions, &
Current_Item%Source_0d_8, Current_Item%Source_1d_8, Current_Item%Source_2d_8, Current_Item%Source_3d_8, &
Source_0d_4, Current_Item%Source_1d_4, Current_Item%Source_2d_4, Current_Item%Source_3d_4, &
Source_0d_I, Current_Item%Source_1d_I, Current_Item%Source_2d_I, Current_Item%Source_3d_I, &
Current_Item%Units, Current_Item%LongName)
Current_metahistitem => Current_metahistitem%Next
enddo