forked from ESCOMP/CDEPS
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdshr_strdata_mod.F90
2151 lines (1914 loc) · 106 KB
/
dshr_strdata_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 dshr_strdata_mod
! holds data and methods to advance data models
! Obtain the model domain and the stream domain for each stream
use ESMF , only : ESMF_Mesh, ESMF_RouteHandle, ESMF_Field, ESMF_FieldBundle
use ESMF , only : ESMF_Clock, ESMF_VM, ESMF_VMGet, ESMF_VMGetCurrent
use ESMF , only : ESMF_DistGrid, ESMF_SUCCESS, ESMF_MeshGet, ESMF_DistGridGet
use ESMF , only : ESMF_VMBroadCast, ESMF_MeshIsCreated, ESMF_MeshCreate
use ESMF , only : ESMF_CALKIND_NOLEAP, ESMF_CALKIND_GREGORIAN
use ESMF , only : ESMF_CalKind_Flag, ESMF_Time, ESMF_TimeInterval
use ESMF , only : ESMF_TimeIntervalGet, ESMF_TYPEKIND_R8, ESMF_FieldCreate
use ESMF , only : ESMF_FILEFORMAT_ESMFMESH, ESMF_FieldCreate
use ESMF , only : ESMF_FieldBundleCreate, ESMF_MESHLOC_ELEMENT, ESMF_FieldBundleAdd
use ESMF , only : ESMF_POLEMETHOD_ALLAVG, ESMF_EXTRAPMETHOD_NEAREST_STOD
use ESMF , only : ESMF_REGRIDMETHOD_BILINEAR, ESMF_REGRIDMETHOD_NEAREST_STOD
use ESMF , only : ESMF_REGRIDMETHOD_CONSERVE, ESMF_NORMTYPE_FRACAREA, ESMF_NORMTYPE_DSTAREA
use ESMF , only : ESMF_ClockGet, operator(-), operator(==), ESMF_CALKIND_NOLEAP
use ESMF , only : ESMF_FieldReGridStore, ESMF_FieldRedistStore, ESMF_UNMAPPEDACTION_IGNORE
use ESMF , only : ESMF_TERMORDER_SRCSEQ, ESMF_FieldRegrid, ESMF_FieldFill, ESMF_FieldIsCreated
use ESMF , only : ESMF_REGION_TOTAL, ESMF_FieldGet, ESMF_TraceRegionExit, ESMF_TraceRegionEnter
use ESMF , only : ESMF_LOGMSG_INFO, ESMF_LogWrite
use shr_kind_mod , only : r8=>shr_kind_r8, r4=>shr_kind_r4, i2=>shr_kind_I2
use shr_kind_mod , only : cs=>shr_kind_cs, cl=>shr_kind_cl, cxx=>shr_kind_cxx, cx=>shr_kind_cx
use shr_sys_mod , only : shr_sys_abort
use shr_const_mod , only : shr_const_pi, shr_const_cDay, shr_const_spval
use shr_cal_mod , only : shr_cal_calendarname, shr_cal_timeSet
use shr_cal_mod , only : shr_cal_noleap, shr_cal_gregorian
use shr_cal_mod , only : shr_cal_date2ymd, shr_cal_ymd2date, shr_cal_leapyear
use shr_orb_mod , only : shr_orb_decl, shr_orb_cosz, shr_orb_undef_real
#ifdef CESMCOUPLED
use shr_pio_mod , only : shr_pio_getiosys, shr_pio_getiotype, shr_pio_getioformat
#endif
use shr_string_mod , only : shr_string_listgetname, shr_string_listisvalid, shr_string_listgetnum
use dshr_stream_mod , only : shr_stream_streamtype, shr_stream_getModelFieldList, shr_stream_getStreamFieldList
use dshr_stream_mod , only : shr_stream_taxis_cycle, shr_stream_taxis_extend, shr_stream_findBounds
use dshr_stream_mod , only : shr_stream_getCurrFile, shr_stream_setCurrFile, shr_stream_getMeshFilename
use dshr_stream_mod , only : shr_stream_init_from_inline, shr_stream_init_from_esmfconfig
#ifndef DISABLE_FoX
use dshr_stream_mod , only : shr_stream_init_from_xml
#endif
use dshr_stream_mod , only : shr_stream_getnextfilename, shr_stream_getprevfilename, shr_stream_getData
use dshr_tinterp_mod , only : shr_tInterp_getCosz, shr_tInterp_getAvgCosz, shr_tInterp_getFactors
use dshr_methods_mod , only : dshr_fldbun_getfldptr, dshr_fldbun_getfieldN, dshr_fldbun_fldchk, chkerr
use dshr_methods_mod , only : dshr_fldbun_diagnose, dshr_fldbun_regrid, dshr_field_getfldptr
use pio , only : file_desc_t, iosystem_desc_t, io_desc_t, var_desc_t
use pio , only : pio_openfile, pio_closefile, pio_nowrite
use pio , only : pio_seterrorhandling, pio_initdecomp, pio_freedecomp
use pio , only : pio_inquire, pio_inq_varid, pio_inq_varndims, pio_inq_vardimid
use pio , only : pio_inq_dimlen, pio_inq_vartype, pio_inq_dimname, pio_inq_dimid
use pio , only : pio_double, pio_real, pio_int, pio_offset_kind, pio_get_var
use pio , only : pio_read_darray, pio_setframe, pio_fill_double, pio_get_att, pio_inq_att
use pio , only : PIO_BCAST_ERROR, PIO_RETURN_ERROR, PIO_NOERR, PIO_INTERNAL_ERROR, PIO_SHORT
implicit none
private
public :: shr_strdata_type
public :: shr_strdata_init_from_config
public :: shr_strdata_init_from_inline
public :: shr_strdata_setOrbs
public :: shr_strdata_advance
public :: shr_strdata_get_stream_domain ! public since needed by dshr_mod
public :: shr_strdata_get_stream_pointer ! get a pointer into a stream's fldbun_model field bundle
public :: shr_strdata_get_stream_count
public :: shr_strdata_get_stream_fieldbundle
public :: shr_strdata_print
private :: shr_strdata_init_model_domain
private :: shr_strdata_get_stream_nlev
private :: shr_strdata_readLBUB
interface shr_strdata_get_stream_pointer
module procedure shr_strdata_get_stream_pointer_1d
module procedure shr_strdata_get_stream_pointer_2d
end interface shr_strdata_get_stream_pointer
! public data members:
integer :: debug = 0 ! local debug flag
character(len=*) ,parameter, public :: shr_strdata_nullstr = 'null'
character(len=*) ,parameter :: shr_strdata_unset = 'NOT_SET'
integer ,parameter :: main_task = 0
! note that the fields in fldbun_stream_lb and fldbun_stream_ub contain the the names fldlist_model
type shr_strdata_perstream
character(CL) :: stream_meshfile ! stream mesh file from stream txt file
type(ESMF_Mesh) :: stream_mesh ! stream mesh created from stream mesh file
type(io_desc_t) :: stream_pio_iodesc ! stream pio descriptor
logical :: stream_pio_iodesc_set =.false. ! true=>pio iodesc has been set
type(ESMF_RouteHandle) :: routehandle ! stream n -> model mesh mapping
character(CL), allocatable :: fldlist_stream(:) ! names of stream file fields
character(CL), allocatable :: fldlist_model(:) ! names of stream model fields
integer :: stream_nlev ! number of vertical levels in stream
real(r8), allocatable :: stream_vlevs(:) ! values of vertical levels in stream
integer :: stream_lb ! index of the Lowerbound (LB) in fldlist_stream
integer :: stream_ub ! index of the Upperbound (UB) in fldlist_stream
type(ESMF_Field) :: field_stream ! a field on the stream data domain
type(ESMF_Field) :: field_stream_vector ! a vector field on the stream data domain
type(ESMF_FieldBundle), allocatable :: fldbun_data(:) ! stream field bundle interpolated to model grid spatially
type(ESMF_FieldBundle) :: fldbun_model ! stream n field bundle interpolated to model grid and time
integer :: ymdLB = -1 ! stream ymd lower bound
integer :: todLB = -1 ! stream tod lower bound
integer :: ymdUB = -1 ! stream ymd upper bound
integer :: todUB = -1 ! stream tod upper bound
real(r8) :: dtmin = 1.0e30_r8
real(r8) :: dtmax = 0.0_r8
logical :: override_annual_cycle = .false.
type(ESMF_Field) :: field_coszen ! needed for coszen time interp
end type shr_strdata_perstream
type shr_strdata_type
type(shr_strdata_perstream), allocatable :: pstrm(:) ! stream info
type(shr_stream_streamType), pointer :: stream(:)=> null() ! stream datatype
logical :: mainproc
integer :: io_type ! pio info
integer :: io_format ! pio info
integer :: modeldt = 0 ! model dt in seconds
type(ESMF_Mesh) :: model_mesh ! model mesh
real(r8), pointer :: model_lon(:) => null() ! model longitudes
real(r8), pointer :: model_lat(:) => null() ! model latitudes
integer :: model_nxg ! model global domain lon size
integer :: model_nyg ! model global domain lat size
integer :: model_nzg ! model global domain vertical size
integer :: model_lsize ! model local domain size
integer, pointer :: model_gindex(:) ! model global index spzce
integer :: model_gsize ! model global domain size
type(ESMF_CLock) :: model_clock ! model clock
character(CL) :: model_calendar = shr_cal_noleap ! model calendar for ymd,tod
integer :: ymd, tod ! model time
type(iosystem_desc_t), pointer :: pio_subsystem => null() ! pio info
real(r8) :: eccen = SHR_ORB_UNDEF_REAL ! cosz t-interp info
real(r8) :: mvelpp = SHR_ORB_UNDEF_REAL ! cosz t-interp info
real(r8) :: lambm0 = SHR_ORB_UNDEF_REAL ! cosz t-interp info
real(r8) :: obliqr = SHR_ORB_UNDEF_REAL ! cosz t-interp info
real(r8), allocatable :: tavCoszen(:) ! cosz t-interp data
end type shr_strdata_type
real(r8) ,parameter :: deg2rad = SHR_CONST_PI/180.0_r8
character(*) ,parameter :: u_FILE_u = &
__FILE__
!===============================================================================
contains
!===============================================================================
integer function shr_strdata_get_stream_count(sdat)
type(shr_strdata_type) , intent(in) :: sdat
if(associated(sdat%stream)) then
shr_strdata_get_stream_count = size(sdat%stream)
else
shr_strdata_get_stream_count = 0
endif
end function shr_strdata_get_stream_count
!===============================================================================
type(ESMF_FieldBundle) function shr_strdata_get_stream_fieldbundle(sdat, ns, name)
! input/output variables
type(shr_strdata_type) , intent(in) :: sdat
integer , intent(in) :: ns ! stream number
character(len=*) , intent(in) :: name
if(trim(name) .eq. 'model') then
shr_strdata_get_stream_fieldbundle = sdat%pstrm(ns)%fldbun_model
else if (trim(name) .eq. 'model_lb') then
shr_strdata_get_stream_fieldbundle = sdat%pstrm(ns)%fldbun_data(sdat%pstrm(ns)%stream_lb)
else if (trim(name) .eq. 'model_ub') then
shr_strdata_get_stream_fieldbundle = sdat%pstrm(ns)%fldbun_data(sdat%pstrm(ns)%stream_ub)
else if (trim(name) .eq. 'stream_lb') then
call shr_sys_abort("should not be here")
else if (trim(name) .eq. 'stream_ub') then
call shr_sys_abort("should not be here")
else
call shr_sys_abort(trim(name)//' is not a recognized stream bundle name')
endif
end function shr_strdata_get_stream_fieldbundle
!===============================================================================
subroutine shr_strdata_init_from_config(sdat, streamfilename, model_mesh, clock, compname, logunit, rc)
! input/output variables
type(shr_strdata_type) , intent(inout) :: sdat
character(len=*) , intent(in) :: streamfilename
type(ESMF_Mesh) , intent(in) :: model_mesh
type(ESMF_Clock) , intent(in) :: clock
character(len=*) , intent(in) :: compname
integer , intent(in) :: logunit
integer , intent(out) :: rc
! local variables
type(ESMF_VM) :: vm
integer :: localPet
character(len=*), parameter :: subname='(shr_strdata_init_from_config)'
! ----------------------------------------------
rc = ESMF_SUCCESS
call ESMF_LogWrite(subname//' called', ESMF_LOGMSG_INFO)
#ifdef CESMCOUPLED
! Initialize sdat pio
sdat%pio_subsystem => shr_pio_getiosys(trim(compname))
sdat%io_type = shr_pio_getiotype(trim(compname))
sdat%io_format = shr_pio_getioformat(trim(compname))
#endif
call ESMF_VMGetCurrent(vm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_VMGet(vm, localPet=localPet, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Initialize sdat streams (read xml file for streams)
sdat%mainproc = (localPet == main_task)
#ifdef DISABLE_FoX
call shr_stream_init_from_esmfconfig(streamfilename, sdat%stream, logunit, &
sdat%pio_subsystem, sdat%io_type, sdat%io_format, rc=rc)
#else
call shr_stream_init_from_xml(streamfilename, sdat%stream, sdat%mainproc, logunit, &
sdat%pio_subsystem, sdat%io_type, sdat%io_format, trim(compname), rc=rc)
#endif
allocate(sdat%pstrm(shr_strdata_get_stream_count(sdat)))
! Initialize sdat model domain
sdat%model_mesh = model_mesh
call shr_strdata_init_model_domain(sdat, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Now finish initializing sdat
call shr_strdata_init(sdat, clock, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end subroutine shr_strdata_init_from_config
!===============================================================================
subroutine shr_strdata_init_from_inline(sdat, my_task, logunit, compname, &
model_clock, model_mesh, &
stream_meshfile, stream_lev_dimname, stream_mapalgo, &
stream_filenames, stream_fldlistFile, stream_fldListModel, &
stream_yearFirst, stream_yearLast, stream_yearAlign, &
stream_offset, stream_taxmode, stream_dtlimit, stream_tintalgo, &
stream_src_mask, stream_dst_mask, stream_name, rc)
! input/output variables
type(shr_strdata_type) , intent(inout) :: sdat ! stream data type
integer , intent(in) :: my_task ! my mpi task
integer , intent(in) :: logunit ! stdout logunit
character(len=*) , intent(in) :: compname ! component name (e.g. ATM, OCN, ...)
type(ESMF_Clock) , intent(in) :: model_clock ! model clock
type(ESMF_Mesh) , intent(in) :: model_mesh ! model mesh
character(*) , intent(in) :: stream_meshFile ! full pathname to stream mesh file
character(*) , intent(in) :: stream_lev_dimname ! name of vertical dimension in stream
character(*) , intent(in) :: stream_mapalgo ! stream mesh -> model mesh mapping type
character(*) , intent(in) :: stream_filenames(:) ! stream data filenames (full pathnamesa)
character(*) , intent(in) :: stream_fldListFile(:) ! file field names, colon delim list
character(*) , intent(in) :: stream_fldListModel(:) ! model field names, colon delim list
integer , intent(in) :: stream_yearFirst ! first year to use
integer , intent(in) :: stream_yearLast ! last year to use
integer , intent(in) :: stream_yearAlign ! align yearFirst with this model year
integer , intent(in) :: stream_offset ! offset in seconds of stream data
character(*) , intent(in) :: stream_taxMode ! time axis mode
real(r8) , intent(in) :: stream_dtlimit ! ratio of max/min stream delta times
character(*) , intent(in) :: stream_tintalgo ! time interpolation algorithm
integer, optional , intent(in) :: stream_src_mask ! source mask value
integer, optional , intent(in) :: stream_dst_mask ! destination mask value
character(*), optional , intent(in) :: stream_name ! name of stream
integer, optional , intent(out) :: rc ! error code
! local variables
integer :: src_mask = 0
integer :: dst_mask = 0
! ----------------------------------------------
rc = ESMF_SUCCESS
! Initialize sdat%logunit and sdat%mainproc
sdat%mainproc = (my_task == main_task)
#ifdef CESMCOUPLED
! Initialize sdat pio
sdat%pio_subsystem => shr_pio_getiosys(trim(compname))
sdat%io_type = shr_pio_getiotype(trim(compname))
sdat%io_format = shr_pio_getioformat(trim(compname))
#endif
! Check source and destination mask, defaults are 0
if (present(stream_src_mask)) src_mask = stream_src_mask
if (present(stream_dst_mask)) dst_mask = stream_dst_mask
! Initialize sdat%pstrm - ASSUME only 1 stream
allocate(sdat%pstrm(1))
! Initialize sdat model domain
sdat%model_mesh = model_mesh
call shr_strdata_init_model_domain(sdat, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Initialize sdat stream - ASSUME only 1 stream
call shr_stream_init_from_inline(sdat%stream, &
sdat%pio_subsystem, sdat%io_type, sdat%io_format, &
stream_meshfile, stream_lev_dimname, stream_mapalgo, &
stream_yearFirst, stream_yearLast, stream_yearAlign, &
stream_offset, stream_taxmode, stream_tintalgo, stream_dtlimit, &
stream_fldlistFile, stream_fldListModel, stream_fileNames, &
logunit, trim(compname), src_mask, dst_mask)
! Now finish initializing sdat
call shr_strdata_init(sdat, model_clock, stream_name, rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
end subroutine shr_strdata_init_from_inline
!===============================================================================
subroutine shr_strdata_init_model_domain( sdat, rc)
! ----------------------------------------------
! Initialize sdat model domain info
! ----------------------------------------------
! input/output variables
type(shr_strdata_type) , intent(inout) :: sdat
integer , intent(out) :: rc
! local variables
integer :: n ! generic counters
type(ESMF_DistGrid) :: distGrid
integer :: tileCount
integer, allocatable :: elementCountPTile(:)
integer :: spatialDim ! number of dimension in mesh
integer :: numOwnedElements ! local size of mesh
real(r8), allocatable :: ownedElemCoords(:) ! mesh lat and lons
character(len=*), parameter :: subname='(shr_strdata_init_model_domain)'
! ----------------------------------------------
rc = ESMF_SUCCESS
! initialize sdat%lsize
call ESMF_MeshGet(sdat%model_mesh, elementdistGrid=distGrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
call ESMF_DistGridGet(distGrid, localDe=0, elementCount=sdat%model_lsize, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! initialize sdat%model_gindex
allocate(sdat%model_gindex(sdat%model_lsize))
call ESMF_DistGridGet(distGrid, localDe=0, seqIndexList=sdat%model_gindex, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! initialize sdat%model_gsize
call ESMF_DistGridGet(distGrid, tileCount=tileCount, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
allocate(elementCountPTile(tileCount))
call ESMF_distGridGet(distGrid, elementCountPTile=elementCountPTile, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
sdat%model_gsize = 0
do n = 1,size(elementCountPTile)
sdat%model_gsize = sdat%model_gsize + elementCountPTile(n)
end do
deallocate(elementCountPTile)
! determine sdat%model_lon, sdat%model_lat
call ESMF_MeshGet(sdat%model_mesh, spatialDim=spatialDim, &
numOwnedElements=numOwnedElements, elementdistGrid=distGrid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
allocate(ownedElemCoords(spatialDim*numOwnedElements))
call ESMF_MeshGet(sdat%model_mesh, ownedElemCoords=ownedElemCoords)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
allocate(sdat%model_lon(numOwnedElements))
allocate(sdat%model_lat(numOwnedElements))
do n = 1, numOwnedElements
sdat%model_lon(n) = ownedElemCoords(2*n-1)
sdat%model_lat(n) = ownedElemCoords(2*n)
end do
end subroutine shr_strdata_init_model_domain
!===============================================================================
subroutine shr_strdata_init(sdat, model_clock, stream_name, rc)
! input/output variables
type(shr_strdata_type) , intent(inout), target :: sdat
type(ESMF_Clock) , intent(in) :: model_clock
character(*), optional , intent(in) :: stream_name
integer , intent(out) :: rc
! local variables
type(ESMF_Mesh), pointer :: stream_mesh
type(ESMF_CalKind_Flag) :: esmf_caltype ! esmf calendar type
character(CS) :: calendar ! calendar name
integer :: ns ! stream index
integer :: m ! generic index
character(CX) :: fileName ! generic file name
integer :: nfld ! loop stream field index
type(ESMF_Field) :: lfield ! temporary
type(ESMF_Field) :: lfield_dst ! temporary
integer :: srcTermProcessing_Value = 0 ! should this be a module variable?
integer :: localpet
logical :: fileExists
type(ESMF_VM) :: vm
logical :: mainproc
integer :: nvars
integer :: i, stream_nlev, index
character(CL) :: stream_vector_names
character(len=*), parameter :: subname='(shr_sdat_init)'
! ----------------------------------------------
rc = ESMF_SUCCESS
call ESMF_VmGetCurrent(vm, rc=rc)
call ESMF_VMGet(vm, localpet=localPet, rc=rc)
mainproc= (localPet==main_task)
! Loop over streams
do ns = 1,shr_strdata_get_stream_count(sdat)
! Initialize calendar for stream n
call ESMF_VMBroadCast(vm, sdat%stream(ns)%calendar, CS, 0, rc=rc)
! Set pointer for stream_mesh
stream_mesh => sdat%pstrm(ns)%stream_mesh
! Create the target stream mesh from the stream mesh file
! TODO: add functionality if the stream mesh needs to be created from a grid
call shr_stream_getMeshFileName (sdat%stream(ns), filename)
if (filename /= 'none' .and. mainproc) then
inquire(file=trim(filename),exist=fileExists)
if (.not. fileExists) then
write(sdat%stream(1)%logunit,'(a)') "ERROR: file does not exist: "//trim(fileName)
call shr_sys_abort(subName//"ERROR: file does not exist: "//trim(fileName))
end if
endif
!
! We do not yet have mask information, but we are required to set it here and change it
! later.
!
if(filename /= 'none') then
stream_mesh = ESMF_MeshCreate(trim(filename), fileformat=ESMF_FILEFORMAT_ESMFMESH, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
endif
! Determine the number of stream levels
call shr_strdata_get_stream_nlev(sdat, ns, rc=rc)
stream_nlev = sdat%pstrm(ns)%stream_nlev
! Determine field names for stream fields with both stream file names and data model names
nvars = sdat%stream(ns)%nvars
! Allocate memory
allocate(sdat%pstrm(ns)%fldList_model(nvars))
call shr_stream_getModelFieldList(sdat%stream(ns), sdat%pstrm(ns)%fldlist_model)
allocate(sdat%pstrm(ns)%fldlist_stream(nvars))
call shr_stream_getStreamFieldList(sdat%stream(ns), sdat%pstrm(ns)%fldlist_stream)
! Create field bundles on model mesh
if (sdat%stream(ns)%readmode=='single') then
sdat%pstrm(ns)%stream_lb = 1
sdat%pstrm(ns)%stream_ub = 2
allocate(sdat%pstrm(ns)%fldbun_data(2))
if (mainproc) then
write(sdat%stream(1)%logunit,'(a,i8)') trim(subname)//" Creating field bundle array fldbun_data of size 2 for stream ",&
ns
end if
else if(sdat%stream(ns)%readmode=='full_file') then
! TODO: add this in
endif
! Create spatially interpolated (but not time interpolated) field bundle - fldbun_data array
do i=1,size(sdat%pstrm(ns)%fldbun_data)
sdat%pstrm(ns)%fldbun_data(i) = ESMF_FieldBundleCreate(rc=rc) ! stream mesh
enddo
do nfld = 1, nvars
do i=1,size(sdat%pstrm(ns)%fldbun_data)
if (sdat%pstrm(ns)%stream_nlev > 1) then
lfield = ESMF_FieldCreate(sdat%model_mesh, ESMF_TYPEKIND_r8, &
name=trim(sdat%pstrm(ns)%fldlist_model(nfld)), &
ungriddedLbound=(/1/), ungriddedUbound=(/stream_nlev/), gridToFieldMap=(/2/), &
meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
else
lfield = ESMF_FieldCreate(sdat%model_mesh, ESMF_TYPEKIND_r8, &
name=trim(sdat%pstrm(ns)%fldlist_model(nfld)), &
meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
end if
call ESMF_FieldBundleAdd(sdat%pstrm(ns)%fldbun_data(i), (/lfield/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (mainproc) then
if (i == 1) then
write(sdat%stream(1)%logunit,'(a,i8)') " adding field "//trim(sdat%pstrm(ns)%fldlist_model(nfld))//&
" to fldbun_data for stream ",ns
end if
end if
enddo
end do
! Create spatially and time interpolated field bundle - fldbun_model
sdat%pstrm(ns)%fldbun_model = ESMF_FieldBundleCreate(rc=rc) ! time interpolation on model mesh
do nfld = 1, nvars
! create temporary fields on model mesh and add the fields to the field bundle
if (stream_nlev > 1) then
lfield = ESMF_FieldCreate(sdat%model_mesh, ESMF_TYPEKIND_r8, &
ungriddedLbound=(/1/), ungriddedUbound=(/stream_nlev/), gridToFieldMap=(/2/), &
name=trim(sdat%pstrm(ns)%fldlist_model(nfld)), &
meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
else
lfield = ESMF_FieldCreate(sdat%model_mesh, ESMF_TYPEKIND_r8, &
name=trim(sdat%pstrm(ns)%fldlist_model(nfld)), &
meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
end if
call ESMF_FieldBundleAdd(sdat%pstrm(ns)%fldbun_model, (/lfield/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end do
! Create a field on the model mesh for coszen time interpolation for this stream if needed
if (trim(sdat%stream(ns)%tinterpalgo) == 'coszen') then
if (stream_nlev > 1) then
sdat%pstrm(ns)%field_coszen = ESMF_FieldCreate(sdat%model_mesh, ESMF_TYPEKIND_r8, &
name='tavCosz', &
ungriddedLbound=(/1/), ungriddedUbound=(/stream_nlev/), gridToFieldMap=(/2/), &
meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
else
sdat%pstrm(ns)%field_coszen = ESMF_FieldCreate(sdat%model_mesh, ESMF_TYPEKIND_r8, &
name='tavCosz', &
meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
end if
endif
! ------------------------------------
! Create the sdat route handles for mapping the stream -> model
! ------------------------------------
! create the source and destination fields needed to create the route handles
! assume that all fields in a stream share the same mesh and there is only a unique model mesh
! can do this outside of a stream loop by just using the first stream index
if(ESMF_MeshIsCreated(stream_mesh)) then
if (stream_nlev > 1) then
sdat%pstrm(ns)%field_stream = ESMF_FieldCreate(stream_mesh, &
ESMF_TYPEKIND_r8, meshloc=ESMF_MESHLOC_ELEMENT, &
ungriddedLbound=(/1/), ungriddedUbound=(/stream_nlev/), gridToFieldMap=(/2/), &
rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else
sdat%pstrm(ns)%field_stream = ESMF_FieldCreate(stream_mesh, &
ESMF_TYPEKIND_r8, meshloc=ESMF_MESHLOC_ELEMENT, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
call ESMF_FieldFill(sdat%pstrm(ns)%field_stream, dataFillScheme="const", const1=1.0_r8, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
end if
endif
! Why not use fldbun_model rather than fldbun_data?
index = sdat%pstrm(ns)%stream_lb
call dshr_fldbun_getFieldN(sdat%pstrm(ns)%fldbun_data(index), 1, lfield_dst, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (.not. ESMF_MeshIsCreated(stream_mesh)) then
sdat%stream(ns)%mapalgo = 'none'
else
if (trim(sdat%stream(ns)%mapalgo) == "bilinear") then
call ESMF_FieldRegridStore(sdat%pstrm(ns)%field_stream, lfield_dst, &
routehandle=sdat%pstrm(ns)%routehandle, &
regridmethod=ESMF_REGRIDMETHOD_BILINEAR, &
polemethod=ESMF_POLEMETHOD_ALLAVG, &
extrapMethod=ESMF_EXTRAPMETHOD_NEAREST_STOD, &
dstMaskValues=(/sdat%stream(ns)%dst_mask_val/), &
srcMaskValues=(/sdat%stream(ns)%src_mask_val/), &
srcTermProcessing=srcTermProcessing_Value, ignoreDegenerate=.true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else if (trim(sdat%stream(ns)%mapalgo) == 'redist') then
call ESMF_FieldRedistStore(sdat%pstrm(ns)%field_stream, lfield_dst, &
routehandle=sdat%pstrm(ns)%routehandle, &
ignoreUnmatchedIndices = .true., rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else if (trim(sdat%stream(ns)%mapalgo) == 'nn') then
call ESMF_FieldReGridStore(sdat%pstrm(ns)%field_stream, lfield_dst, &
routehandle=sdat%pstrm(ns)%routehandle, &
regridmethod=ESMF_REGRIDMETHOD_NEAREST_STOD, &
dstMaskValues=(/sdat%stream(ns)%dst_mask_val/), &
srcMaskValues=(/sdat%stream(ns)%src_mask_val/), &
srcTermProcessing=srcTermProcessing_Value, ignoreDegenerate=.true., &
unmappedaction=ESMF_UNMAPPEDACTION_IGNORE, rc=rc)
else if (trim(sdat%stream(ns)%mapalgo) == 'consf') then
call ESMF_FieldReGridStore(sdat%pstrm(ns)%field_stream, lfield_dst, &
routehandle=sdat%pstrm(ns)%routehandle, &
regridmethod=ESMF_REGRIDMETHOD_CONSERVE, &
normType=ESMF_NORMTYPE_FRACAREA, &
dstMaskValues=(/sdat%stream(ns)%dst_mask_val/), &
srcMaskValues=(/sdat%stream(ns)%src_mask_val/), &
srcTermProcessing=srcTermProcessing_Value, ignoreDegenerate=.true., &
unmappedaction=ESMF_UNMAPPEDACTION_IGNORE, rc=rc)
else if (trim(sdat%stream(ns)%mapalgo) == 'consd') then
call ESMF_FieldReGridStore(sdat%pstrm(ns)%field_stream, lfield_dst, &
routehandle=sdat%pstrm(ns)%routehandle, &
regridmethod=ESMF_REGRIDMETHOD_CONSERVE, &
normType=ESMF_NORMTYPE_DSTAREA, &
dstMaskValues=(/sdat%stream(ns)%dst_mask_val/), &
srcMaskValues=(/sdat%stream(ns)%src_mask_val/), &
srcTermProcessing=srcTermProcessing_Value, ignoreDegenerate=.true., &
unmappedaction=ESMF_UNMAPPEDACTION_IGNORE, rc=rc)
else if (trim(sdat%stream(ns)%mapalgo) == 'none') then
! single point stream data, no action required.
else
call shr_sys_abort('ERROR: map algo '//trim(sdat%stream(ns)%mapalgo)//' is not supported')
end if
end if
end do ! end of loop over streams
! Check for vector pairs in the stream - BOTH ucomp and vcomp MUST BE IN THE SAME STREAM
do ns = 1,shr_strdata_get_stream_count(sdat)
stream_mesh => sdat%pstrm(ns)%stream_mesh
stream_nlev = sdat%pstrm(ns)%stream_nlev
stream_vector_names = trim(sdat%stream(ns)%stream_vectors)
! check that vector field list is a valid colon delimited string
if (trim(stream_vector_names) /= 'null') then
! check that for now u and v are only for single level fields
if (stream_nlev > 1) then
! TODO: add support for u and v for multi level fields
call shr_sys_abort(subname//': vector fields are not currently supported for multi-level fields')
end if
! check that stream vector names are valid
if (.not. shr_string_listIsValid(stream_vector_names)) then
write(sdat%stream(1)%logunit,*) trim(subname),' vec fldlist invalid m=',m,trim(stream_vector_names)
call shr_sys_abort(subname//': vec fldlist invalid:'//trim(stream_vector_names))
endif
! check that only 2 fields are contained for any vector pairing
if (shr_string_listGetNum(stream_vector_names) /= 2) then
write(sdat%stream(1)%logunit,*) trim(subname),' vec fldlist ne 2 m=',m,trim(stream_vector_names)
call shr_sys_abort(subname//': vec fldlist ne 2:'//trim(stream_vector_names))
endif
! create stream vector field
sdat%pstrm(ns)%field_stream_vector = ESMF_FieldCreate(stream_mesh, &
ESMF_TYPEKIND_r8, name='stream_vector', meshloc=ESMF_MESHLOC_ELEMENT, &
ungriddedLbound=(/1/), ungriddedUbound=(/2/), gridToFieldMap=(/2/), rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
if (mainproc) then
write(sdat%stream(1)%logunit,'(a,i8)') "creating ESMF stream vector field with names" //&
trim(stream_vector_names)//" for stream ",ns
end if
end if
enddo
! initialize sdat model clock and calendar
sdat%model_clock = model_clock
call ESMF_ClockGet(sdat%model_clock, calkindflag=esmf_caltype, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (esmf_caltype == ESMF_CALKIND_NOLEAP) then
sdat%model_calendar = trim(shr_cal_noleap)
else if (esmf_caltype == ESMF_CALKIND_GREGORIAN) then
sdat%model_calendar = trim(shr_cal_gregorian)
else
call shr_sys_abort(subname//" ERROR bad ESMF calendar name "//trim(calendar))
end if
! print sdat output
if (mainproc) then
if (present(stream_name)) then
call shr_strdata_print(sdat, trim(stream_name))
else
call shr_strdata_print(sdat, 'stream_data')
end if
write(sdat%stream(1)%logunit,*) ' successfully initialized sdat'
endif
end subroutine shr_strdata_init
!===============================================================================
subroutine shr_strdata_get_stream_nlev(sdat, stream_index, rc)
! Obtain the number of vertical levels for the stream - this is obtained
! from the lev_dimname string
! input/output variables
type(shr_strdata_type) , intent(inout) :: sdat
integer , intent(in) :: stream_index
integer , intent(out) :: rc
! local variables
type(ESMF_VM) :: vm
type(file_desc_t) :: pioid
integer :: rcode
character(CX) :: filename
integer :: dimid
type(var_desc_t) :: varid
integer :: stream_nlev
integer :: old_handle ! previous setting of pio error handling
character(CS) :: units
character(*), parameter :: subname = '(shr_strdata_set_stream_domain) '
! ----------------------------------------------
rc = ESMF_SUCCESS
! Set ungridded dimension to the number of vertical level - must read this in
if (trim(sdat%stream(stream_index)%lev_dimname) == 'null') then
stream_nlev = 1
else
call ESMF_VMGetCurrent(vm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
if (sdat%mainproc) then
call shr_stream_getData(sdat%stream(stream_index), 1, filename)
end if
call ESMF_VMBroadCast(vm, filename, CX, 0, rc=rc)
rcode = pio_openfile(sdat%pio_subsystem, pioid, sdat%io_type, trim(filename), pio_nowrite)
rcode = pio_inq_dimid(pioid, trim(sdat%stream(stream_index)%lev_dimname), dimid)
rcode = pio_inq_dimlen(pioid, dimid, stream_nlev)
allocate(sdat%pstrm(stream_index)%stream_vlevs(stream_nlev))
rcode = pio_inq_varid(pioid, trim(sdat%stream(stream_index)%lev_dimname), varid)
rcode = pio_get_var(pioid, varid, sdat%pstrm(stream_index)%stream_vlevs)
! Determine vertical coordinates units - assume that default is m
call pio_seterrorhandling(pioid, PIO_BCAST_ERROR, old_handle)
rcode = pio_inq_att(pioid, varid, 'units')
call pio_seterrorhandling(pioid, old_handle)
if (rcode == PIO_NOERR) then
rcode = pio_get_att(pioid, varid, 'units', units)
if (trim(units) == 'centimeters' .or. trim(units) == 'cm') then
sdat%pstrm(stream_index)%stream_vlevs(:) = sdat%pstrm(stream_index)%stream_vlevs(:) / 100.
end if
end if
call pio_closefile(pioid)
end if
if (sdat%mainproc) then
write(sdat%stream(1)%logunit,*) trim(subname)//' stream_nlev = ',stream_nlev
if (stream_nlev /= 1) then
write(sdat%stream(1)%logunit,*)' stream vertical levels = ',sdat%pstrm(stream_index)%stream_vlevs
end if
end if
! Set stream_nlev in the per-stream sdat info
sdat%pstrm(stream_index)%stream_nlev = stream_nlev
end subroutine shr_strdata_get_stream_nlev
!===============================================================================
subroutine shr_strdata_get_stream_domain(sdat, stream_index, fldname, flddata, rc)
! Obtain the data for fldname from the stream domain data
! input/output variables
type(shr_strdata_type) , intent(inout) :: sdat
integer , intent(in) :: stream_index
character(len=*) , intent(in) :: fldname
real(r8) , pointer :: flddata(:)
integer , intent(out) :: rc
! local variables
type(ESMF_VM) :: vm
type(var_desc_t) :: varid
type(file_desc_t) :: pioid
integer :: rcode
character(CX) :: filename
type(io_desc_t) :: pio_iodesc
real(r4), allocatable :: data_real(:)
real(r8), allocatable :: data_double(:)
integer :: pio_iovartype
integer :: lsize
character(*), parameter :: subname = '(shr_strdata_set_stream_domain) '
! ----------------------------------------------
rc = ESMF_SUCCESS
call ESMF_VMGetCurrent(vm, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Determine the file to open
if (sdat%mainproc) then
call shr_stream_getData(sdat%stream(stream_index), 1, filename)
end if
call ESMF_VMBroadCast(vm, filename, CX, 0, rc=rc)
! Open the file
rcode = pio_openfile(sdat%pio_subsystem, pioid, sdat%io_type, trim(filename), pio_nowrite)
! Create the pio iodesc for fldname
call shr_strdata_set_stream_iodesc(sdat, sdat%pstrm(stream_index), trim(fldname), pioid, rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
! Now read in the data for fldname
lsize = size(flddata)
rcode = pio_inq_varid(pioid, trim(fldname), varid)
rcode = pio_inq_vartype(pioid, varid, pio_iovartype)
if (pio_iovartype == PIO_REAL) then
allocate(data_real(lsize))
call pio_read_darray(pioid, varid, pio_iodesc, data_real, rcode)
flddata(:) = real(data_real(:), kind=r8)
deallocate(data_real)
else if (pio_iovartype == PIO_DOUBLE) then
allocate(data_double(lsize))
call pio_read_darray(pioid, varid, pio_iodesc, data_double, rcode)
flddata(:) = data_double(:)
deallocate(data_double)
else
call shr_sys_abort(subName//"ERROR: only real and double types are supported for stream domain read")
end if
! Free the memory associate with the iodesc and close the file
call pio_freedecomp(pioid, pio_iodesc)
call pio_closefile(pioid)
end subroutine shr_strdata_get_stream_domain
!===============================================================================
subroutine shr_strdata_advance(sdat, ymd, tod, logunit, istr, timers, rc)
! -------------------------------------------------------
! Mismatching calendars: 4 cases
! (0) The stream calendar and model calendar are identical
! (1) The stream is a no leap calendar and the model is gregorian
! (2) The stream is a gregorian calendar and the model is a noleap calendar
! (3) The calendars mismatch and none of the above
! -------------------------------------------------------
!
! ymdmod and todmod are the ymd and tod to time interpolate to.
! Generally, these are just the model date and time. Also, always
! use the stream calendar for time interpolation for reasons
! described below. When there is a calendar mismatch, support Feb
! 29 in a special way as needed to get reasonable values. Note
! that when Feb 29 needs to be treated specially, a discontinuity
! will be introduced. The size of that discontinuity will depend
! on the time series input data.
!
! (0) The stream calendar and model calendar are identical:
! In this case it is still possible to have a mismatch if both are gregorian.
! These cases are:
! - Model is no_leap, data is Gregorian and leapyear date 2/29 is encountered in data - skip date
! - Model is Gregorian, data is no_leap and leapyear date 2/29 is encountered in model - repeat 2/28 data
! - Model is Gregorian, data is gregorian but leapyears do not align.
! - if in model leap year repeat data from 2/28
! - if in data leap year skip date 2/29
!
!
! (1) The stream is a no leap calendar and the model is gregorian:
! Time interpolate on the noleap calendar. If the model date is Feb 29,
! compute stream data for Feb 28 by setting ymdmod and todmod to Feb 28.
! This results in duplicate stream data on Feb 28 and Feb 29 and a
! discontinuity at the start of Feb 29. This could potentially be fixed
! by using the gregorian calendar for time interpolation when the input data
! is relatively infrequent (say greater than daily) with the following concerns.
! - The forcing will not be reproduced identically on the same day with
! with climatological inputs data
! - Input data with variable input frequency might behave funny
! - An arbitrary discontinuity will be introduced in the time
! interpolation method based upon the logic chosen to transition
! from reproducing Feb 28 on Feb 29 and interpolating to Feb 29.
! - The time gradient of data will change by adding a day arbitrarily.
!
! (2) The stream is a gregorian calendar and the model is a noleap calendar:
! Then just time interpolate on the gregorian calendar. This causes Feb 29
! stream data to be skipped and lead to a discontinuity at the start of March 1.
!
! (3) If the calendars mismatch and neither of the three cases above
! are recognized, then abort.
! -------------------------------------------------------
! input/output variables
type(shr_strdata_type) ,intent(inout) :: sdat
integer ,intent(in) :: ymd ! current model date
integer ,intent(in) :: tod ! current model date
integer ,intent(in) :: logunit
character(len=*) ,intent(in) :: istr
logical ,intent(in),optional :: timers
integer ,intent(out) :: rc
! local variables
integer :: ns ! stream index
integer :: nf ! field index
integer :: i,lev,n ! generic indices
logical , allocatable :: newData(:)
integer , allocatable :: ymdmod(:) ! modified model dates to handle Feb 29
real(r8), allocatable :: coszen(:) ! cosine of zenith angle
integer :: todmod ! modified model dates to handle Feb 29
character(len=32) :: lstr ! local string
real(r8) :: flb,fub ! factor for lb and ub
real(r8) ,pointer :: dataptr1d(:) ! pointer into field bundle
real(r8) ,pointer :: dataptr1d_lb(:) ! pointer into field bundle
real(r8) ,pointer :: dataptr1d_ub(:) ! pointer into field bundle
real(r8) ,pointer :: dataptr2d(:,:) ! pointer into field bundle
real(r8) ,pointer :: dataptr2d_lb(:,:)! pointer into field bundle
real(r8) ,pointer :: dataptr2d_ub(:,:)! pointer into field bundle
real(r8), pointer :: nu_coords(:) ! allocatable local element mesh lat and lons
real(r8), pointer :: nv_coords(:) ! allocatable local element mesh lat and lons
real(r8), pointer :: data2d_src(:,:) ! pointer into field bundle
real(r8), pointer :: data2d_dst(:,:) ! pointer into field bundle
real(r8), pointer :: data_u_src(:) ! pointer into field bundle
real(r8), pointer :: data_v_src(:) ! pointer into field bundle
real(r8), pointer :: data_u_dst(:) ! pointer into field bundle
real(r8), pointer :: data_v_dst(:) ! pointer into field bundle
type(ESMF_Time) :: timeLB, timeUB ! lb and ub times
type(ESMF_TimeInterval) :: timeint ! delta time
character(CL) :: calendar
integer :: dday ! delta days
real(r8) :: dtime ! delta time
integer :: year,month,day ! date year month day
integer :: datayear,datamonth,dataday ! data date year month day
integer :: nstreams
integer :: stream_index
real(r8) ,parameter :: solZenMin = 0.001_r8 ! minimum solar zenith angle
integer ,parameter :: tadj = 2
character(len=*) ,parameter :: timname = "_strd_adv"
character(*) ,parameter :: subname = "(shr_strdata_advance) "
character(*) ,parameter :: F00 = "('(shr_strdata_advance) ',a)"
character(*) ,parameter :: F01 = "('(shr_strdata_advance) ',a,a,i4,2(f10.5,2x))"
!-------------------------------------------------------------------------------
rc = ESMF_SUCCESS
nullify(dataptr1d)
nullify(dataptr1d_ub)
nullify(dataptr1d_lb)
nullify(dataptr2d)
nullify(dataptr2d_ub)
nullify(dataptr2d_lb)
nullify(nu_coords)
nullify(nv_coords)
nullify(data2d_src)
nullify(data2d_dst)
nullify(data_u_src)
nullify(data_v_src)
nullify(data_u_dst)
nullify(data_v_dst)
nstreams = shr_strdata_get_stream_count(sdat)
if (nstreams < 1) return ! TODO: is this needed
lstr = trim(istr)
! To avoid an unused dummy variable warning
if(present(timers)) then
write(sdat%stream(1)%logunit,*) trim(subname),'optional variable timers present but unused'
endif
call ESMF_TraceRegionEnter(trim(lstr)//trim(timname)//'_total')
sdat%ymd = ymd
sdat%tod = tod
if (nstreams > 0) then
allocate(newData(nstreams))
allocate(ymdmod(nstreams))
do ns = 1,nstreams
! ---------------------------------------------------------
! Consistency checks
! ---------------------------------------------------------
! case(0)
ymdmod(ns) = ymd
todmod = tod
calendar = trim(sdat%stream(ns)%calendar)
call shr_cal_date2ymd (ymd,year,month,day)
if (trim(sdat%model_calendar) /= trim(sdat%stream(ns)%calendar)) then
if (( trim(sdat%model_calendar) == trim(shr_cal_gregorian)) .and. &
(trim(sdat%stream(ns)%calendar) == trim(shr_cal_noleap))) then
! case (1), set feb 29 = feb 28
call shr_cal_date2ymd (ymd,year,month,day)
if (month == 2 .and. day == 29) then
call shr_cal_ymd2date(year,2,28,ymdmod(ns))
endif
calendar = shr_cal_noleap
endif
endif
! ---------------------------------------------------------
! Determine if new data is read in - if so then copy
! fldbun_stream_ub to fldbun_stream_lb and read in new fldbun_stream_ub data
! ---------------------------------------------------------
call ESMF_TraceRegionEnter(trim(lstr)//trim(timname)//'_readLBUB')
select case(sdat%stream(ns)%readmode)
case ('single')
call shr_strdata_readLBUB(sdat, ns, ymdmod(ns), todmod, newData(ns), trim(lstr)//'_readLBUB', rc=rc)
if (ChkErr(rc,__LINE__,u_FILE_u)) return
case ('full_file')
! TODO: need to put in capability to read all stream data at once
case default
write(logunit,F00) "ERROR: Unsupported readmode : ", trim(sdat%stream(ns)%readmode)
call shr_sys_abort(subName//"ERROR: Unsupported readmode: "//trim(sdat%stream(ns)%readmode))
end select
if (debug > 0 .and. sdat%mainproc) then
write(sdat%stream(1)%logunit,*) trim(subname),' newData flag = ',ns,newData(ns)
write(sdat%stream(1)%logunit,*) trim(subname),' LB ymd,tod = ',ns,sdat%pstrm(ns)%ymdLB,sdat%pstrm(ns)%todLB
write(sdat%stream(1)%logunit,*) trim(subname),' UB ymd,tod = ',ns,sdat%pstrm(ns)%ymdUB,sdat%pstrm(ns)%todUB
endif
! ---------------------------------------------------------
! If new data is read in:
! ---------------------------------------------------------
if (newData(ns)) then
if (trim(sdat%model_calendar) /= trim(sdat%stream(ns)%calendar)) then
if ((trim(sdat%model_calendar) == trim(shr_cal_noleap)) .and. &
(trim(sdat%stream(ns)%calendar) == trim(shr_cal_gregorian))) then
call shr_cal_date2ymd(sdat%pstrm(ns)%ymdUB, datayear, datamonth, dataday)
if(datamonth==3 .and. dataday==1 .and. month==2 .and. day==28) then
calendar = shr_cal_noleap
endif
! case (2), feb 29 input data will be skipped automatically
else if (.not. ( trim(sdat%model_calendar) == trim(shr_cal_gregorian)) .and. &
(trim(sdat%stream(ns)%calendar) == trim(shr_cal_noleap))) then
! case (3), abort
write(logunit,*) trim(subname),' ERROR: mismatch calendar ', &
trim(sdat%model_calendar),':',trim(sdat%stream(ns)%calendar)
call shr_sys_abort(trim(subname)//' ERROR: mismatch calendar ')
endif