-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstt_save_load.f90
3705 lines (3275 loc) · 139 KB
/
stt_save_load.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
!//=========================================================================
!// Oslo CTM3
!//=========================================================================
!// Amund Sovde Haslerud, August 2017
!//=========================================================================
!// Initialize STT and model fields.
!//=========================================================================
module stt_save_load
!// ----------------------------------------------------------------------
!// MODULE: stt_save_load
!// DESCRIPTION: Routines for setting STT and relevant fields from
!// restart files or monthly averages.
!// Also routine for saving restart file.
!//
!// Contains
!// subroutine load_restart_file
!// subroutine getField_and_interpolate
!// subroutine save_restart_file
!// Old versions
!// subroutine OSLO_CON_SAV
!// subroutine OSLO_CON_RUN
!// subroutine restart_from_CTM3avg
!// subroutine restart_from_CTM3avg_T42
!// subroutine OSLO_CON_RUN42
!// subroutine OSLO_CON_RUNxx
!// subroutine OSLO_RESTARTFILE_INFO
!//
!// Amund Sovde Haslerud, August 2017
!// ----------------------------------------------------------------------
character(len=*), parameter, private :: f90file = 'stt_save_load.f90'
!// ----------------------------------------------------------------------
public
private getField_and_interpolate
!// ----------------------------------------------------------------------
contains
!// ----------------------------------------------------------------------
subroutine load_restart_file(FILENAME, MODE)
!// --------------------------------------------------------------------
!// Read CTM3 restart file (netCDF4).
!// UCI CON_RUN modified to include non-transported Oslo components.
!// Reads component by component, and places the data in the current
!// array. Works fine when current components are less than in file.
!//
!// Can read ADDITIONAL restart files to fill in non-initialized
!// tracers. This is controlled by MODE:
!// MODE: 0: First call; initializes zeroinit.
!// !=0: Will add non-initialized tracers that are
!// available on the specified file. Skips AIR.
!//
!// Amund Sovde Haslerud, 2017
!// --------------------------------------------------------------------
use cmn_precision, only: r8, rMom
use cmn_size, only: IPAR, JPAR, LPAR, NPAR, NOTRPAR
use cmn_ctm, only: IYEAR, IDAY, JYEAR, JDAY, JMON, JDATE, NTM, &
AIR, STT, SUT, SVT, SWT, SUU, SVV, SWW, SUV, SUW, SVW
use cmn_chem, only: TNAME, TMASS
use utilities, only: get_free_fileid
use cmn_oslo, only: ZEROINIT, XZEROINIT, trsp_idx, chem_idx, &
XTNAME, XTMASS, Xtrsp_idx, Xchem_idx, XSTT
use netcdf
use ncutils, only: inq_netcdf_var, handle_error
!// --------------------------------------------------------------------
implicit none
!// --------------------------------------------------------------------
!// Input
character(len=*), intent(in):: FILENAME
integer, intent(in) :: MODE
!// Locals
real(r8) :: SUMT
real(r8), dimension(IPAR,JPAR,LPAR) :: R8XYZ
integer :: &
version, IN_IPAR, IN_JPAR, IN_LPAR, IN_NDAYS, IN_IYEAR, &
I, J, L, N, M, ND, XND
logical :: LREGRID
character(len=80) :: RTITLE2
integer :: &
var_id, &
lat_dim_id, lon_dim_id, lev_dim_id, &
lat_id, lon_id, lev_id, &
ilat_id, ilon_id, ilev_id, &
ihya_id, ihyb_id, &
version_id, &
time_id, ndays_id, iyear_id, &
status, ncid
integer, dimension(6) :: in_time
integer, dimension(3) :: dimIDs3d, dimLen3d
integer, allocatable, dimension(:) :: Xcomp_id
character(len=20) :: varname
!// The moments
character(len=3), dimension(9) :: &
moments = (/'SUT','SVT','SWT','SUU','SVV','SWW','SUV','SUW','SVW'/)
!// Fields read from file
real(r8), dimension(:), allocatable :: &
IN_XDEDG, IN_YDEDG, IN_ETAA, IN_ETAB
!// --------------------------------------------------------------------
character(len=*), parameter :: subr = 'load_restart_file'
!// --------------------------------------------------------------------
!// Initialize arrays; assume all are zero
if (MODE .eq. 0) then
ZEROINIT(:) = 1
if (NOTRPAR .gt. 0) XZEROINIT(:) = 1
end if
!//read in continuation restart file:
if (MODE .eq. 0) then
write(6,'(a)') f90file//':'//subr// &
': reading restart file: '//trim(FILENAME)
else
write(6,'(a)') f90file//':'//subr// &
': reading ADDITIONAL restart file: '//trim(FILENAME)
end if
!//---------------------------------------------------------------------
status = nf90_open(filename, nf90_NoWrite, ncid)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': in opening file '//trim(filename))
!//---------------------------------------------------------------------
write(6,'(a)') 'Restart file info:'
!// Fetch model info
status = nf90_get_att(ncid, NF90_GLOBAL, 'runtitle', RTITLE2)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get runtitle')
write(6,'(a)') ' Runtitle: '//trim(RTITLE2)
!// Model date
status = nf90_inq_varid(ncid, 'output_time', time_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get output_time varID')
status = nf90_get_var(ncid, time_id, in_time)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get output_time')
write(6,'(a,6i6)') ' file time: ',in_time
write(6,'(a,6i6)') ' model time: ',JYEAR,JMON,JDATE,0,0,0
!// Find NDAYS (NDAY+1 - NDAYI) = # integrated days
status = nf90_inq_varid(ncid, 'NDAYS', ndays_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get NDAYS ID')
status = nf90_get_var(ncid, ndays_id, IN_NDAYS)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get NDAYS')
write(6,'(a,i5)') ' Written after # days: ',IN_NDAYS
!// Find IYEAR
status = nf90_inq_varid(ncid, 'IYEAR', iyear_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get IYEAR ID')
status = nf90_get_var(ncid, iyear_id, IN_IYEAR)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get IYEAR')
write(6,'(a,i5)') ' IYEAR: ',IN_IYEAR
!// Fetch VERSION
status = nf90_inq_varid(ncid, 'VERSION', version_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get VERSION ID')
status = nf90_get_var(ncid, version_id, version)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get VERSION')
write(6,'(a,i5)') ' VERSION: ',VERSION
!//---------------------------------------------------------------------
!// Fetch resolution
status = nf90_inq_dimid(ncid, 'lon', lon_dim_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get lon_dim_id')
status = nf90_inquire_dimension(ncid, lon_dim_id, len=IN_IPAR)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get dimension IN_IPAR')
status = nf90_inq_dimid(ncid, 'lat', lat_dim_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get lat_dim_id')
status = nf90_inquire_dimension(ncid, lat_dim_id, len=IN_JPAR)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get dimension IN_JPAR')
status = nf90_inq_dimid(ncid, 'lev', lev_dim_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get lev_dim_id')
status = nf90_inquire_dimension(ncid, lev_dim_id, len=IN_LPAR)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get dimension IN_LPAR')
!// Resolution - print-out
write(6,'(a,3i5)') ' Resolution on file (IPAR,JPAR,LPAR): ', &
IN_IPAR, IN_JPAR, IN_LPAR
write(6,'(a,3i5)') ' Model resolution (IPAR,JPAR,LPAR): ', &
IPAR, JPAR, LPAR
!// Check if input data must be regridded
!//---------------------------------------------------------------------
if (IN_IPAR .eq. IPAR .and. IN_JPAR .eq. JPAR) then
LREGRID = .false.
else
!// Will only use vertical moments.
LREGRID = .true.
!// Print warning about it
write(6,'(a71)') '--------------------------------------------'// &
'---------------------------'
write(6,'(a)') 'WARNING WARNING WARNING WARNING WARNING WARNING'
write(6,'(a)') ' '//f90file//':'//subr//':'
write(6,'(a)') ' Restarting from a different horizontal resolution!'
write(6,'(a)') ' Only STT and vertical moments (SWT and SWW) '// &
'are interpolated!'
write(6,'(a)') 'WARNING WARNING WARNING WARNING WARNING WARNING'
write(6,'(a71)') '--------------------------------------------'// &
'---------------------------'
end if
!//---------------------------------------------------------------------
!// Allocate read-in array
allocate( IN_XDEDG(IN_IPAR+1), IN_YDEDG(IN_JPAR+1), &
IN_ETAA(IN_LPAR+1), IN_ETAB(LPAR+1) )
!//---------------------------------------------------------------------
!// IN_XDEDG
status = nf90_inq_varid(ncid, 'ilon', ilon_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get ilon ID')
status = nf90_get_var(ncid, ilon_id, IN_XDEDG)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get XDEDG')
!// IN_YDEDG
status = nf90_inq_varid(ncid, 'ilat', ilat_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get ilat ID')
status = nf90_get_var(ncid, ilat_id, IN_YDEDG)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get YDEDG')
!// IN_ETAA
status = nf90_inq_varid(ncid, 'ihya', ihya_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get ihya ID')
status = nf90_get_var(ncid, ihya_id, IN_ETAA)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get ETAA')
!// IN_ETAB
status = nf90_inq_varid(ncid, 'ihyb', ihyb_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get ihyb ID')
status = nf90_get_var(ncid, ihyb_id, IN_ETAB)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get ETAB')
!// Get variables
!//---------------------------------------------------------------------
!// AIR
if (MODE .eq. 0) then
!// Check variable
status = nf90_inq_varid(ncid, 'AIR', var_id)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)//': get AIR id')
!// Get field and interpolate
call getField_and_interpolate(ncid, var_id, filename, &
'AIR', f90file//':'//subr, &
IN_IPAR, IN_JPAR, IN_LPAR, IN_XDEDG, IN_YDEDG, &
IN_ETAA, IN_ETAB, LREGRID, R8XYZ)
AIR(:,:,:) = R8XYZ(:,:,:)
write(6,'(a)') f90file//':'//subr//': got AIR'
end if !// if (MODE .eq. 0) then
!// STT
!//---------------------------------------------------------------------
do N = 1, NPAR
if (ZEROINIT(N) .eq. 0) then
write(6,'(a)') f90file//':'//subr// &
': already initialised: '//trim(TNAME(N))
cycle
end if
!// Look for STT variable in file STT
varname = 'STT_'//trim(TNAME(N))
status = nf90_inq_varid(ncid, TRIM(varname), var_id)
if (status .eq. NF90_NOERR) then
!// Variable exists - get it
call getField_and_interpolate(ncid, var_id, filename, &
varname, f90file//':'//subr, &
IN_IPAR, IN_JPAR, IN_LPAR, IN_XDEDG, IN_YDEDG, &
IN_ETAA, IN_ETAB, LREGRID, R8XYZ)
!// Set STT and ZEROINIT
STT(:,:,:,N) = R8XYZ(:,:,:)
ZEROINIT(N) = 0
!// Get moments if they exist
do m = 1, 9
!// If regridding, only do vertical moments
if (LREGRID .and. .not. (moments(m) .eq. 'SWT' .or. &
moments(m) .eq. 'SWW')) cycle
!// Variable name
varname = moments(m)//'_'//trim(TNAME(N))
!// Check variable
status = nf90_inq_varid(ncid, TRIM(varname), var_id)
if (status .eq. NF90_NOERR) then
if (moments(m) .eq. 'SUT') then
SUT(:,:,:,N) = R8XYZ(:,:,:)
else if (moments(m) .eq. 'SVT') then
SVT(:,:,:,N) = R8XYZ(:,:,:)
else if (moments(m) .eq. 'SWT') then
SWT(:,:,:,N) = R8XYZ(:,:,:)
else if (moments(m) .eq. 'SUU') then
SUU(:,:,:,N) = R8XYZ(:,:,:)
else if (moments(m) .eq. 'SVV') then
SVV(:,:,:,N) = R8XYZ(:,:,:)
else if (moments(m) .eq. 'SWW') then
SWW(:,:,:,N) = R8XYZ(:,:,:)
else if (moments(m) .eq. 'SUV') then
SUV(:,:,:,N) = R8XYZ(:,:,:)
else if (moments(m) .eq. 'SUW') then
SUW(:,:,:,N) = R8XYZ(:,:,:)
else if (moments(m) .eq. 'SVW') then
SVW(:,:,:,N) = R8XYZ(:,:,:)
end if
else
!// This should never happen. If a tracer was in STT,
!// it should have moments.
write(6, '(a)') f90file//':'//subr// &
'Eroor: Found no moments for species '//&
trim(TNAME(N))//' - stopping'
stop
end if !// if (status .eq. NF90_NOERR) then
end do !// do m = 1, 9
write(6,'(a)') f90file//':'//subr// &
': got variable & moments: '//trim(TNAME(N))
else if (status .eq. NF90_ENOTVAR) then
!// Variable not found - look in XSTT
varname = 'XSTT_'//trim(TNAME(N))
status = nf90_inq_varid(ncid, TRIM(varname), var_id)
if (status .eq. NF90_NOERR) then
!// Variable exists - get it
call getField_and_interpolate(ncid, var_id, filename, &
varname, f90file//':'//subr, &
IN_IPAR, IN_JPAR, IN_LPAR, IN_XDEDG, IN_YDEDG, &
IN_ETAA, IN_ETAB, LREGRID, R8XYZ)
!// Set STT and ZEROINIT
STT(:,:,:,N) = R8XYZ(:,:,:)
ZEROINIT(N) = 0
write(6,'(a)') f90file//':'//subr// &
': got variable (XSTT->STT) : '//trim(TNAME(N))
end if !// if (status .eq. NF90_NOERR) then
else
!// Variable does not exist on file - print info
write(6,'(a)') f90file//':'//subr// &
': no such variable: '//trim(TNAME(N))
end if
!// If not in file STT or XSTT, component is not initialised.
end do !// do N = 1, NPAR
!// XSTT
do N = 1, NOTRPAR
if (XZEROINIT(N) .eq. 0) then
write(6,'(a)') f90file//':'//subr// &
': already initialised: '//trim(XTNAME(N))
cycle
end if
!// Look for XSTT variable in file XSTT
varname = 'XSTT_'//trim(XTNAME(N))
status = nf90_inq_varid(ncid, TRIM(varname), var_id)
if (status .eq. NF90_NOERR) then
!// Variable exists - get it
call getField_and_interpolate(ncid, var_id, filename, &
varname, f90file//':'//subr, &
IN_IPAR, IN_JPAR, IN_LPAR, IN_XDEDG, IN_YDEDG, &
IN_ETAA, IN_ETAB, LREGRID, R8XYZ)
!// Set XSTT and XZEROINIT
do J = 1, JPAR
do I = 1, IPAR
do L = 1, LPAR
XSTT(L,N,I,J) = R8XYZ(I,J,L)
end do
end do
end do
XZEROINIT(N) = 0
write(6,'(a)') f90file//':'//subr// &
': got variable (XSTT) : '//trim(XTNAME(N))
else if (status .eq. NF90_ENOTVAR) then
!// Variable not found - look in STT
varname = 'STT_'//trim(XTNAME(N))
status = nf90_inq_varid(ncid, TRIM(varname), var_id)
if (status .eq. NF90_NOERR) then
!// Variable exists - get it
call getField_and_interpolate(ncid, var_id, filename, &
varname, f90file//':'//subr, &
IN_IPAR, IN_JPAR, IN_LPAR, IN_XDEDG, IN_YDEDG, &
IN_ETAA, IN_ETAB, LREGRID, R8XYZ)
!// Set XSTT and XZEROINIT
do J = 1, JPAR
do I = 1, IPAR
do L = 1, LPAR
XSTT(L,N,I,J) = R8XYZ(I,J,L)
end do
end do
end do
XZEROINIT(N) = 0
write(6,'(a)') f90file//':'//subr// &
': got variable (STT->ZSTT) : '//trim(XTNAME(N))
end if !// if (status .eq. NF90_NOERR) then
else
!// Variable does not exist on file - print info
write(6,'(a)') f90file//':'//subr// &
': no such variable: '//trim(XTNAME(N))
end if
end do !// do N = 1, NOTRPAR
!//---------------------------------------------------------------------
!// close netcdf file
status = nf90_close(ncid)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': close file: '//trim(filename))
!//---------------------------------------------------------------------
deallocate( IN_XDEDG, IN_YDEDG, IN_ETAA, IN_ETAB )
!//---------------------------------------------------------------------
!// Print out info about the initialization field
!// --------------------------------------------------------------------
write(6,'(a71)') '--------------------------------------------'// &
'---------------------------'
!// Transported species
write(6,'(a)') 'Transported species:'
do N = 1, NPAR
!// Print out only if initialized
if (ZEROINIT(N) .eq. 1) cycle
SUMT = 0._r8
do L = 1, LPAR
do J = 1, JPAR
do I = 1, IPAR
SUMT = SUMT + STT(I,J,L,N)
end do
end do
end do
write(6,'(a,i4,1x,a10,0P,f8.2,1P,es14.6)') &
'tracer',N,TNAME(N),TMASS(N),SUMT
end do
!// Non-transported species
write(6,'(a)') 'Non-transported species:'
do N = 1, NOTRPAR
!// Print out only if initialized
if (XZEROINIT(N) .eq. 1) cycle
SUMT = 0._r8
do J = 1, JPAR
do I = 1, IPAR
do L = 1, LPAR
SUMT = SUMT + XSTT(L,N,I,J)
end do
end do
end do
write(6,'(a,i4,1x,a10,0P,f8.2,1P,es14.6)') &
'tracer',N,XTNAME(N),XTMASS(N),SUMT
end do
!// Which components are initialized as zero?
do N = 1, NPAR
if (ZEROINIT(N) .eq. 1) then
write(6,'(a,i3,1x,i3,a)') '*** Component trsp_idx/chem_idx ', &
N,chem_idx(N),' is initialized as ZERO!'
end if
end do
do N = 1, NOTRPAR
if (XZEROINIT(N) .eq. 1) then
write(6,'(a,i3,1x,i3,a)') '*** X-Component Xtrsp_idx/Xchem_idx ', &
N,Xchem_idx(N),' is initialized as ZERO!'
end if
end do
!// Info if your restart file conains more/less species
ND = sum(ZEROINIT)
if (NOTRPAR .gt. 0) then
XND = sum(XZEROINIT)
else
XND = 0
endif
write(6,'(a71)') '--------------------------------------------'// &
'---------------------------'
if ((ND + XND) .gt. 0) then
write(6,'(a,i3)') ' Transported species not initialized: ', ND
write(6,'(a,i3)') ' Non-Transported species not initialized: ', XND
write(6,'(a)') 'Program _may_ stop if critical components'// &
' are not initialized!'
else
write(6,'(a)') ' All species are initialized!'
end if
write(6,'(a71)') '--------------------------------------------'// &
'---------------------------'
!// --------------------------------------------------------------------
end subroutine load_restart_file
!// ----------------------------------------------------------------------
!// ----------------------------------------------------------------------
subroutine getField_and_interpolate(ncid, var_id, filename, &
varname, caller, &
IN_IPAR, IN_JPAR, IN_LPAR, IN_XDEDG, IN_YDEDG, &
IN_ETAA, IN_ETAB, LREGRID, R8XYZ)
!// --------------------------------------------------------------------
!// This routine is bound to subroutine load_restart_file, and reads
!// a 3d field from a netCDF file, with ncid as file id and
!// var_id as variable id.
!// If resolution on file differs from model resolution, the field is
!// interpolated to model resoluion.
!// Vertical interpolation is not possible yet.
!//
!// Amund Sovde Haslerud, August 2017
!// --------------------------------------------------------------------
use cmn_precision, only: r8
use cmn_size, only: IPAR, JPAR, LPAR
use cmn_ctm, only: XDEDG, YDEDG
use regridding, only: E_GRID
use netcdf
use ncutils, only: handle_error
!// --------------------------------------------------------------------
implicit none
!// --------------------------------------------------------------------
!// Input
integer, intent(in) :: ncid, var_id, IN_IPAR, IN_JPAR, IN_LPAR
character(len=*), intent(in) :: filename, varname, caller
real(r8), intent(in) :: IN_XDEDG(IN_IPAR+1), IN_YDEDG(IN_JPAR+1)
real(r8), intent(in) :: IN_ETAA(IN_LPAR+1), IN_ETAB(IN_LPAR+1)
logical, intent(in) :: LREGRID
!// Output
real(r8), intent(out) :: R8XYZ(IPAR,JPAR,LPAR)
!// Local variables
integer :: status, I, J, L
integer, dimension(3) :: dimIDs3d, dimLen3d
real(r8) :: IN_FIELD(IN_IPAR, IN_JPAR, IN_LPAR)
!// --------------------------------------------------------------------
character(len=*), parameter :: subr = 'getField_and_interpolate'
!// --------------------------------------------------------------------
!// Initialise
R8XYZ(:,:,:) = 0._r8
!// Variable information
status = nf90_inquire_variable(ncid, var_id, dimIDs=dimIDs3d)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)// &
': inquire variable '//trim(varname)//' called by '//caller)
!// 1. Dimension of the variable
status = nf90_inquire_dimension(ncid,dimIDs3d(1),len=dimLen3d(1))
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)// &
': inquire dimesions 1 '//trim(varname)//' called by '//caller)
!// 2. Dimension of the variable
status = nf90_inquire_dimension(ncid,dimIDs3d(2),len=dimLen3d(2))
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)// &
': inquire dimesions 2 '//TRIM(varname)//' called by '//caller)
!// 3. Dimension of the variable
status = nf90_inquire_dimension(ncid,dimIDs3d(3),len=dimLen3d(3))
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)// &
': inquire dimesions 3 '//TRIM(varname)//' called by '//caller)
!// Check the dimensions
if ( (dimLen3d(1) .ne. IN_IPAR) .or. (dimLen3d(2) .ne. IN_JPAR) .or. &
(dimLen3d(3) .ne. IN_LPAR) ) then
write(6,'(a)') f90file//':'//subr//': resolution mismatch - stop'
write(6,'(a,3i5)') 'On file',dimLen3d
write(6,'(a,3i5)') 'On file',IN_IPAR, IN_JPAR, IN_LPAR
write(6,'(a)') 'Routine called by '//caller
stop
end if
!// Get variable
status = nf90_get_var(ncid, var_id, IN_FIELD)
if (status .ne. NF90_NOERR) call handle_error(status, &
f90file//':'//subr//':'//trim(filename)// &
': get '//trim(varname)//' called by '//caller)
!// Will so far only handle files where vertical resolution is the same
!// as in model run.
if (IN_LPAR .ne. LPAR) then
write(6,'(a,2(1x,i3))') f90file//':'//subr// &
': Vertical resolution of restart file must match! - stopping'
write(6,'(a,2(1x,i3))') 'restart file/IN_LPAR/LPAR: '//trim(filename),&
IN_LPAR, LPAR
write(6,'(a)') 'Routine called by '//caller
stop
end if
!// Regrid horizontally?
!// IMPORTANT: L-loop must be revised if vertical interpolation is
!// included.
if (.not. LREGRID) then
!// No regridding
do L = 1, LPAR
do J = 1, JPAR
do I = 1, IPAR
R8XYZ(I,J,L) = IN_FIELD(I,J,L)
end do
end do
end do
else
!// Must regrid horizontally
!$omp parallel private (L) &
!$omp shared (IN_XDEDG,IN_YDEDG,XDEDG,YDEDG,IN_FIELD,R8XYZ,&
!$omp IN_IPAR, IN_JPAR, IN_LPAR) &
!$omp default(NONE)
!$omp do
do L = 1, IN_LPAR
!// Interpolate
call E_GRID(IN_FIELD(:,:,L),IN_XDEDG,IN_YDEDG,IN_IPAR,IN_JPAR, &
R8XYZ(:,:,L),XDEDG,YDEDG,IPAR,JPAR,1)
end do
!$omp end do
!$omp end parallel
!// Possible future vertical regridding
end if
!// --------------------------------------------------------------------
end subroutine getField_and_interpolate
!// ----------------------------------------------------------------------
!// ----------------------------------------------------------------------
subroutine save_restart_file(NDAY, NDAYI)
!// --------------------------------------------------------------------
!// Save restart file for exact continuation run.
!// Written as netCDF4.
!//
!// Amund Sovde Haslerud, August 2017
!// --------------------------------------------------------------------
use cmn_precision, only: r8, r4, rMom
use cmn_size, only: IPARW, JPARW, LPARW, IPAR, JPAR, LPAR, NPAR, NOTRPAR, &
TNAMELEN
use cmn_met, only: METTYPE, metCYCLE, metREVNR, MET_ROOT, MPATH1, MPATH2, &
MYEAR
use cmn_ctm, only: IYEAR, JYEAR, JDAY, JMON, JDATE, IDAY, NTM, &
AIR, STT, SUT, SVT, SWT, SUU, SVV, SWW, SUV, SUW, SVW, &
XDGRD, YDGRD, XDEDG, YDEDG, ZGRD, ZEDG, ETAA, ETAB, AREAXY
use cmn_chem, only: TNAME, TMASS
use cmn_diag, only: RUNTITLE, metTypeInfo, resolutionInfo, &
nc4deflate_global, nc4shuffle_global
use cmn_oslo, only: chem_idx, Xchem_idx, XSTT, XTNAME, XTMASS
use netcdf
use ncutils, only: handle_error
!// --------------------------------------------------------------------
implicit none
!// --------------------------------------------------------------------
!// Input
integer, intent(in) :: NDAY, NDAYI
!// Locals
character(len=8) :: datestamp
character(len=10) :: CDATE
character(len=80) :: filename
integer :: N, I, J, L
real(r8),dimension(IPAR,JPAR,LPAR) :: XFIELD
integer, dimension(6) :: output_time
integer :: &
lat_dim_id, lon_dim_id, lev_dim_id, npar_dim_id, notrpar_dim_id, &
lat_id, lon_id, lev_id, &
ilat_dim_id, ilon_dim_id, ilev_dim_id, &
ilat_id, ilon_id, ilev_id, &
ihya_dim_id, ihyb_dim_id, &
ihya_id, ihyb_id, &
native_lon_id, native_lat_id, native_lev_id, &
areaxy_id, &
version_id, &
date_size_dim_id, &
time_id, nday_id, iyear_id, &
tracer_name_len_dim_id, tracer_name_id, xtracer_name_id, &
chemid_id, xchemid_id, molw_id, xmolw_id, &
air_id, &
status, ncid
integer, dimension(NPAR) :: comp_id, &
compSUT_id, compSVT_id, compSWT_id, &
compSUU_id, compSVV_id, compSWW_id, &
compSUV_id, compSUW_id, compSVW_id
integer, dimension(NOTRPAR) :: Xcomp_id
character(len=20) :: varname
!// Version number
integer, parameter :: version = 4
!// --------------------------------------------------------------------
integer, parameter :: nc4deflate = 9 !// Could use nc4deflate_global
integer, parameter :: nc4shuffle = 1 !// Could use nc4shuffle_global
!// --------------------------------------------------------------------
character(len=*), parameter :: subr = 'OSLO_CON_SAV_NC4'
!// --------------------------------------------------------------------
!// Date stamp
write(datestamp(1:8),'(i4.4,2i2.2)') JYEAR,JMON,JDATE
!// File to save
filename = 'ctm3_restart_'//datestamp//'.nc'
output_time = (/JYEAR,JMON,JDATE,0,0,0/)
!//---------------------------------------------------------------------
write(6,'(a)') f90file//':'//subr//': creating file: '//trim(filename)
status=nf90_create(path=filename,cmode=nf90_netcdf4,ncid=ncid)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': in creating file')
!//---------------------------------------------------------------------
!//---------------------------------------------------------------------
!// Headers
!//---------------------------------------------------------------------
status=nf90_put_att(ncid,nf90_global,'model_info', &
'Oslo CTM3 is a 3D Chemical Transport Model')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': model_info')
status=nf90_put_att(ncid,nf90_global,'driving_meteorology',metTypeInfo)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': driving_meteorology')
status=nf90_put_att(ncid,nf90_global,'driving_meteorology_path',MPATH1)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': driving_meteorology_path')
status=nf90_put_att(ncid,nf90_global,'resolution_info',resolutionInfo)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': resolution_info')
status=nf90_put_att(ncid,nf90_global,'runtitle',trim(runtitle))
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': runtitle')
status=nf90_put_att(ncid,nf90_global,'contact_info','CICERO')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': contact_info')
!//---------------------------------------------------------------------
!//---------------------------------------------------------------------
!// Define dimensions
!//---------------------------------------------------------------------
!// Define spatial dimensions (lat, lon, lev)
status = nf90_def_dim(ncid,"lat",JPAR,lat_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define lat dim')
status = nf90_def_dim(ncid,"lon",IPAR,lon_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define lon dim')
status = nf90_def_dim(ncid,"lev",LPAR,lev_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define lev dim')
!// Define spatial dimensions (ilat, ilon, ilev)
status = nf90_def_dim(ncid,"ilat",JPAR+1,ilat_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define ilat dim')
status = nf90_def_dim(ncid,"ilon",IPAR+1,ilon_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define ilon dim')
status = nf90_def_dim(ncid,"ilev",LPAR+1,ilev_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define ilev dim')
!// NPAR
status = nf90_def_dim(ncid,"NPAR",NPAR,npar_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define NPAR dim')
!// NOTRPAR
status = nf90_def_dim(ncid,"NOTRPAR",NOTRPAR,notrpar_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define NOTRPAR dim')
!// Define size of date stamps
status = nf90_def_dim(ncid,"date_size",size(output_time),date_size_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define date_size dim')
!// Define length of tracer name string
status = nf90_def_dim(ncid,"tracer_name_len", &
TNAMELEN, tracer_name_len_dim_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define tracer_name_len dim')
!//---------------------------------------------------------------------
!// Define variables
!//---------------------------------------------------------------------
!// Define the lon/lat/lev
status = nf90_def_var(ncid,"lon",nf90_double,lon_dim_id,lon_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define lon variable')
status = nf90_def_var(ncid,"lat",nf90_double,lat_dim_id,lat_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define lat variable')
status = nf90_def_var(ncid,"lev",nf90_double,lev_dim_id,lev_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define lev variable')
!// Define ilon/ilat/ilev
status = nf90_def_var(ncid,"ilon",nf90_double,ilon_dim_id,ilon_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define ilon variable')
status = nf90_def_var(ncid,"ilat",nf90_double,ilat_dim_id,ilat_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define ilat variable')
status = nf90_def_var(ncid,"ilev",nf90_double,ilev_dim_id,ilev_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define ilev variable')
!// Putting attributes to lon/lat/lev variables
status = nf90_put_att(ncid,lon_id,'units','degree_east')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute units lon')
status = nf90_put_att(ncid,lat_id,'units','degree_north')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute units lat')
status = nf90_put_att(ncid,lev_id,'units','pressure [hPa]')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute units lev')
!// Putting attributes to ilon/ilat variables
status = nf90_put_att(ncid,ilon_id,'units','degree_east')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute units ilon')
status = nf90_put_att(ncid,ilat_id,'units','degree_north')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute units ilat')
status = nf90_put_att(ncid,ilev_id,'units','pressure [hPa]')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute units ilev')
!// Defining hybrid sigma coordinate A
status = nf90_def_var(ncid,"ihya",nf90_double,ilev_dim_id,ihya_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define ihya variable')
!// Attributes
status = nf90_put_att(ncid,ihya_id,'units','hPa')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute units ihya')
status = nf90_put_att(ncid,ihya_id,'description', &
'Sigma hybrid coordinate A.')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description ihya')
status = nf90_put_att(ncid,ihya_id, 'usage', &
'p_box_bottom(L) = ihya(L) + ihyb(L)*p_surface(I,J)')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute usage ihya')
!// Defining hybrid sigma coordinate B
status = nf90_def_var(ncid,"ihyb",nf90_double,ilev_dim_id,ihyb_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define ihyb variable')
!// Attributes
status = nf90_put_att(ncid,ihyb_id,'units','1')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute units ihyb')
status = nf90_put_att(ncid,ihyb_id, 'description', &
'Sigma hybrid coordinate B.')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description ihyb')
status = nf90_put_att(ncid,ihyb_id, 'usage', &
'p_box_bottom(L) = ihya(L) + ihyb(L)*p_surface(I,J)')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute usage ihyb')
!// Define file version number
status = nf90_def_var(ncid,"VERSION",nf90_int,version_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define VERSION variable')
status = nf90_put_att(ncid,version_id,'description', &
'Restart file version number')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description VERSION')
!// Define output time
status = nf90_def_var(ncid,"output_time",nf90_int,date_size_dim_id,time_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define output_time variable')
status = nf90_put_att(ncid,time_id,'description', &
'Output time [YYYY,MM,DD,hh,mm,ss]')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description output_time')
!// Put out NDAYS and IYEAR to indicate what kind of spinup
status = nf90_def_var(ncid,"NDAYS",nf90_int,nday_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define NDAYS variable')
status = nf90_put_att(ncid,nday_id,'description', &
'Numper of days since model start (NDAY-NDAYI+1)')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description NDAYS')
status = nf90_def_var(ncid,"IYEAR",nf90_int,iyear_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define IYEAR variable')
status = nf90_put_att(ncid,iyear_id,'description', &
'Initial year of model simulation')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description IYEAR')
!// Info about native size IPARW
status = nf90_def_var(ncid,"IPARW",nf90_int,native_lon_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define IPARW variable')
status = nf90_put_att(ncid,native_lon_id,'description', &
'Meteorological data native longitudinal resolution')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description IPARW')
!// Info about native size JPARW
status = nf90_def_var(ncid,"JPARW",nf90_int,native_lat_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define JPARW variable')
status = nf90_put_att(ncid,native_lat_id,'description', &
'Meteorological data native latitudinal resolution')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description JPARW')
!// Info about native size LPARW
status = nf90_def_var(ncid,"LPARW",nf90_int,native_lev_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define LPARW variable')
status = nf90_put_att(ncid,native_lev_id,'description', &
'Meteorological data vertical resolution')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description LPARW')
!// Tracer names
status = nf90_def_var(ncid, "tracer_name", nf90_char, &
(/tracer_name_len_dim_id, npar_dim_id/), tracer_name_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define tracer_name variable')
status = nf90_put_att(ncid,tracer_name_id,'description', &
'Tracer names - transported species')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description tracer_name')
!// Component IDs
status = nf90_def_var(ncid, "chem_idx", nf90_int, &
(/npar_dim_id/), chemid_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define chem_idx variable')
status = nf90_put_att(ncid,chemid_id,'description', &
'Component ID in Oslo CTM3 - transported species')
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': attribute description chem_idx')
!// Tracer mass (molecular weight)
status = nf90_def_var(ncid, "tracer_molweight", nf90_double, &
(/npar_dim_id/), molw_id)
if (status .ne. nf90_noerr) call handle_error(status, &
f90file//':'//subr//': define tracer_molweight variable')
status = nf90_put_att(ncid,molw_id,'description', &