-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLIS_ESMF_Extensions.F90
5183 lines (4662 loc) · 195 KB
/
LIS_ESMF_Extensions.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
!=========================================================================================
! LIS ESMF Extensions Module
!=========================================================================================
! CONFIGURATION IDENTIFICATION $HeadURL$
! CONFIGURATION IDENTIFICATION @(#)$Id$
!=========================================================================================
! ESMF macros for logging
#define FILENAME "LIS_ESMF_Extensions.F90"
#define CONTEXT line=__LINE__,file=FILENAME,method=METHOD
#define PASSTHRU msg=ESMF_LOGERR_PASSTHRU,CONTEXT
! Define ESMF real kind to match LIS single/double precision
#if defined(SINGLE)
#define ESMF_KIND_RX ESMF_KIND_R4
#define ESMF_TYPEKIND_RX ESMF_TYPEKIND_R4
#else
#define ESMF_KIND_RX ESMF_KIND_R8
#define ESMF_TYPEKIND_RX ESMF_TYPEKIND_R8
#endif
! Macros for debugging
#define DEBUG_ESMF_IMPORT___disabled
#define DEBUG_ESMF_EXPORT___disabled
!=========================================================================================
! LIS ESMF Extensions Module
!=========================================================================================
module LIS_ESMF_Extensions
use ESMF
use NUOPC
use NETCDF
implicit none
private
public :: LIS_ESMF_GridWrite
public :: LIS_ESMF_DecompWrite
public :: LIS_ESMF_MAPPRESET_GLOBAL
public :: LIS_ESMF_MAPPRESET_CONUS
public :: LIS_ESMF_MAPPRESET_IRENE
public :: LIS_ESMF_MAPPRESET_FRONTRANGE
public :: LIS_ESMF_FieldFill
public :: LIS_ESMF_FillField
public :: LIS_ESMF_FillArray
public :: LIS_ESMF_FillFieldBundle
public :: LIS_ESMF_FillState
public :: LIS_ESMF_NetcdfReadIXJX
public :: LIS_ESMF_NetcdfIsPresent
public :: LIS_ESMF_LogStateList
public :: LIS_ESMF_LogState
public :: LIS_ESMF_LogFieldConnections
public :: LIS_ESMF_LogGrid
public :: LIS_ESMF_LogFieldList
public :: LIS_ESMF_LogField
public :: LIS_ESMF_LogFieldLclVal
public :: LIS_ESMF_LogArrayLclVal
public :: LIS_ESMF_LogFarrayLclVal
public :: LIS_ESMF_LogCplList
!==============================================================================
!
! INTERFACE BLOCKS
!
!==============================================================================
interface LIS_ESMF_GridWrite
module procedure LIS_ESMF_GridWrite_coords
module procedure LIS_ESMF_GridWrite_preset
module procedure LIS_ESMF_GridWrite_default
end interface
interface LIS_ESMF_NclScriptWrite
module procedure LIS_ESMF_NclScriptWrite_coords
module procedure LIS_ESMF_NclScriptWrite_preset
module procedure LIS_ESMF_NclScriptWrite_default
end interface
interface LIS_ESMF_FerretScriptWrite
module procedure LIS_ESMF_FerretScriptWrite_coords
module procedure LIS_ESMF_FerretScriptWrite_preset
module procedure LIS_ESMF_FerretScriptWrite_default
end interface
interface LIS_ESMF_FillState
module procedure LIS_ESMF_FillState_I4
module procedure LIS_ESMF_FillState_I8
module procedure LIS_ESMF_FillState_R4
module procedure LIS_ESMF_FillState_R8
module procedure LIS_ESMF_FillState_SCHEME
end interface
interface LIS_ESMF_FillFieldBundle
module procedure LIS_ESMF_FillFieldBundle_I4
module procedure LIS_ESMF_FillFieldBundle_I8
module procedure LIS_ESMF_FillFieldBundle_R4
module procedure LIS_ESMF_FillFieldBundle_R8
module procedure LIS_ESMF_FillFieldBundle_SCHEME
end interface
interface LIS_ESMF_FillField
module procedure LIS_ESMF_FillField_I4
module procedure LIS_ESMF_FillField_I8
module procedure LIS_ESMF_FillField_R4
module procedure LIS_ESMF_FillField_R8
end interface
interface LIS_ESMF_FillArray
module procedure LIS_ESMF_FillArray_I4
module procedure LIS_ESMF_FillArray_I8
module procedure LIS_ESMF_FillArray_R4
module procedure LIS_ESMF_FillArray_R8
end interface
interface LIS_ESMF_NetcdfReadIXJX
module procedure LIS_ESMF_NetcdfReadIXJX_Field
module procedure LIS_ESMF_NetcdfReadIXJX_Array
module procedure LIS_ESMF_NetcdfReadIXJX_I4
module procedure LIS_ESMF_NetcdfReadIXJX_I8
module procedure LIS_ESMF_NetcdfReadIXJX_R4
module procedure LIS_ESMF_NetcdfReadIXJX_R8
end interface
interface LIS_ESMF_LogFarrayLclVal
module procedure LIS_ESMF_LogFarrayLclVal_I41D
module procedure LIS_ESMF_LogFarrayLclVal_I42D
module procedure LIS_ESMF_LogFarrayLclVal_I43D
module procedure LIS_ESMF_LogFarrayLclVal_I81D
module procedure LIS_ESMF_LogFarrayLclVal_I82D
module procedure LIS_ESMF_LogFarrayLclVal_I83D
module procedure LIS_ESMF_LogFarrayLclVal_R41D
module procedure LIS_ESMF_LogFarrayLclVal_R42D
module procedure LIS_ESMF_LogFarrayLclVal_R43D
module procedure LIS_ESMF_LogFarrayLclVal_R81D
module procedure LIS_ESMF_LogFarrayLclVal_R82D
module procedure LIS_ESMF_LogFarrayLclVal_R83D
end interface
!==============================================================================
!
! DERIVED TYPES
!
!==============================================================================
type MapDesc
character(len=16) :: name
logical :: global
real :: minLat
real :: maxLat
real :: minLon
real :: maxLon
endtype MapDesc
!==============================================================================
!
! PRESET VALUES
!
!==============================================================================
type(MapDesc),parameter :: &
LIS_ESMF_MAPPRESET_GLOBAL = MapDesc("GLOBAL",.TRUE.,-90.0,90.0,-180.0,-180.0), &
LIS_ESMF_MAPPRESET_CONUS = MapDesc("CONUS",.FALSE.,18.0,49.0,235.0,298.0), &
LIS_ESMF_MAPPRESET_IRENE = MapDesc("IRENE",.FALSE.,10.0,60.0,240.0,320.0), &
LIS_ESMF_MAPPRESET_FRONTRANGE = MapDesc("FRONTRANGE",.FALSE.,38.5,41.0,-107.0,-103.5)
character(len=*),parameter :: NUOPC_COPY_FWD = 'from ESMF_Array to FORTRAN array'
character(len=*),parameter :: NUOPC_COPY_BWD = 'from FORTRAN array to ESMF_Array'
contains
!-----------------------------------------------------------------------------
#define METHOD "LIS_ESMF_GridWrite"
!BOP
! !IROUTINE: LIS_ESMF_GridWrite - Write Grid data to file
! !INTERFACE:
! call using generic interface: LIS_ESMF_GridWrite
subroutine LIS_ESMF_GridWrite_coords(grid, nclScript, mapName, minCoords, &
maxCoords, fileName, overwrite, status, timeslice, iofmt, relaxedflag, rc)
! ! ARGUMENTS
type(ESMF_Grid), intent(in) :: grid
logical, intent(in) :: nclScript
character(len=*), intent(in) :: mapName
real, intent(in) :: minCoords(2)
real, intent(in) :: maxCoords(2)
character(len=*), intent(in), optional :: fileName
logical, intent(in), optional :: overwrite
type(ESMF_FileStatus_Flag), intent(in), optional :: status
integer, intent(in), optional :: timeslice
type(ESMF_IOFmt_Flag), intent(in), optional :: iofmt
logical, intent(in), optional :: relaxedflag
integer, intent(out), optional :: rc
! !DESCRIPTION:
! Write the data in {\tt grid} to {\tt file} if supported by the
! {\tt iofmt}.
!
! The arguments are:
! \begin{description}
! \item[grid]
! The {\tt ESMF\_Grid} object whose data is to be written.
! \item[{[nclScript]}]
! If {\tt .true.} then NUOPC will write an NCL script that can be used to
! generate grid graphics. Default is {\tt .false.}.
! \item[{[mapName]}]
! Map name to be written to NCL script.
! \item[{[minCoords]}]
! Minimum map coordinates to be written to NCL script.
! \item[{[maxCoords]}]
! Maximum map coordinates to be written to NCL script.
! \item[fileName]
! The name of the file to write to. If not present then the file will
! be written to the grid's name.
! \item[{[overwrite]}]
! A logical flag, the default is .false., i.e., existing Field data may
! {\em not} be overwritten. If .true., the
! data corresponding to each field's name will be
! be overwritten. If the {\tt timeslice} option is given, only data for
! the given timeslice may be overwritten.
! Note that it is always an error to attempt to overwrite a NetCDF
! variable with data which has a different shape.
! \item[{[status]}]
! The file status. Valid options are {\tt ESMF\_FILESTATUS\_NEW},
! {\tt ESMF\_FILESTATUS\_OLD}, {\tt ESMF\_FILESTATUS\_REPLACE}, and
! {\tt ESMF\_FILESTATUS\_UNKNOWN} (default).
! \item[{[timeslice]}]
! Time slice counter. Must be positive. The behavior of this
! option may depend on the setting of the {\tt overwrite} flag:
! \begin{description}
! \item[{\tt overwrite = .false.}:]\ If the timeslice value is
! less than the maximum time already in the file, the write will fail.
! \item[{\tt overwrite = .true.}:]\ Any positive timeslice value is valid.
! \end{description}
! By default, i.e. by omitting the {\tt timeslice} argument, no
! provisions for time slicing are made in the output file,
! however, if the file already contains a time axis for the variable,
! a timeslice one greater than the maximum will be written.
! \item[{[iofmt]}]
! The IO format. Valid options are {\tt ESMF\_IOFMT\_BIN} and
! {\tt ESMF\_IOFMT\_NETCDF}. If not present, file names with a {\tt .bin}
! extension will use {\tt ESMF\_IOFMT\_BIN}, and file names with a {\tt .nc}
! extension will use {\tt ESMF\_IOFMT\_NETCDF}. Other files default to
! {\tt ESMF\_IOFMT\_NETCDF}.
! \item[{[relaxedflag]}]
! If {\tt .true.}, then no error is returned even if the call cannot write
! the file due to library limitations. Default is {\tt .false.}.
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOP
!-----------------------------------------------------------------------------
! local variables
type(MapDesc) :: map
if (present(rc)) rc = ESMF_SUCCESS
map = MapDesc(trim(mapName),.FALSE., &
minCoords(2),maxCoords(2),minCoords(1),maxCoords(1))
call LIS_ESMF_GridWrite(grid, fileName=fileName, overwrite=overwrite, &
status=status, timeslice=timeslice, iofmt=iofmt, &
relaxedflag=relaxedflag, nclScript=nclScript, map=map, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
end subroutine
#undef METHOD
!-----------------------------------------------------------------------------
#define METHOD "LIS_ESMF_GridWrite"
!BOP
! !IROUTINE: LIS_ESMF_GridWrite - Write Grid data to file
! !INTERFACE:
! call using generic interface: LIS_ESMF_GridWrite
subroutine LIS_ESMF_GridWrite_preset(grid, nclScript, mapPreset, &
fileName, overwrite, status, timeslice, iofmt, relaxedflag, rc)
! ! ARGUMENTS
type(ESMF_Grid), intent(in) :: grid
logical, intent(in) :: nclScript
character(len=*), intent(in) :: mapPreset
character(len=*), intent(in), optional :: fileName
logical, intent(in), optional :: overwrite
type(ESMF_FileStatus_Flag), intent(in), optional :: status
integer, intent(in), optional :: timeslice
type(ESMF_IOFmt_Flag), intent(in), optional :: iofmt
logical, intent(in), optional :: relaxedflag
integer, intent(out), optional :: rc
! !DESCRIPTION:
! Write the data in {\tt grid} to {\tt file} if supported by the
! {\tt iofmt}.
!
! The arguments are:
! \begin{description}
! \item[grid]
! The {\tt ESMF\_Grid} object whose data is to be written.
! \item[{[nclScript]}]
! If {\tt .true.} then NUOPC will write an NCL script that can be used to
! generate grid graphics. Default is {\tt .false.}.
! \item[{[mapPreset]}]
! Map preset to use when writting NCL script.
! \item[fileName]
! The name of the file to write to. If not present then the file will
! be written to the grid's name.
! \item[{[overwrite]}]
! A logical flag, the default is .false., i.e., existing Field data may
! {\em not} be overwritten. If .true., the
! data corresponding to each field's name will be
! be overwritten. If the {\tt timeslice} option is given, only data for
! the given timeslice may be overwritten.
! Note that it is always an error to attempt to overwrite a NetCDF
! variable with data which has a different shape.
! \item[{[status]}]
! The file status. Valid options are {\tt ESMF\_FILESTATUS\_NEW},
! {\tt ESMF\_FILESTATUS\_OLD}, {\tt ESMF\_FILESTATUS\_REPLACE}, and
! {\tt ESMF\_FILESTATUS\_UNKNOWN} (default).
! \item[{[timeslice]}]
! Time slice counter. Must be positive. The behavior of this
! option may depend on the setting of the {\tt overwrite} flag:
! \begin{description}
! \item[{\tt overwrite = .false.}:]\ If the timeslice value is
! less than the maximum time already in the file, the write will fail.
! \item[{\tt overwrite = .true.}:]\ Any positive timeslice value is valid.
! \end{description}
! By default, i.e. by omitting the {\tt timeslice} argument, no
! provisions for time slicing are made in the output file,
! however, if the file already contains a time axis for the variable,
! a timeslice one greater than the maximum will be written.
! \item[{[iofmt]}]
! The IO format. Valid options are {\tt ESMF\_IOFMT\_BIN} and
! {\tt ESMF\_IOFMT\_NETCDF}. If not present, file names with a {\tt .bin}
! extension will use {\tt ESMF\_IOFMT\_BIN}, and file names with a {\tt .nc}
! extension will use {\tt ESMF\_IOFMT\_NETCDF}. Other files default to
! {\tt ESMF\_IOFMT\_NETCDF}.
! \item[{[relaxedflag]}]
! If {\tt .true.}, then no error is returned even if the call cannot write
! the file due to library limitations. Default is {\tt .false.}.
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOP
!-----------------------------------------------------------------------------
! no local variables
if (present(rc)) rc = ESMF_SUCCESS
select case (trim(mapPreset))
case ('global','GLOBAL','Global')
call LIS_ESMF_GridWrite(grid, fileName=fileName, overwrite=overwrite, &
timeslice=timeslice, iofmt=iofmt, relaxedflag=relaxedflag, &
nclScript=nclScript, map=LIS_ESMF_MAPPRESET_GLOBAL, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
case ('conus','CONUS','Conus')
call LIS_ESMF_GridWrite(grid, fileName=fileName, overwrite=overwrite, &
timeslice=timeslice, iofmt=iofmt, relaxedflag=relaxedflag, &
nclScript=nclScript, map=LIS_ESMF_MAPPRESET_CONUS, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
case ('irene','IRENE','Irene')
call LIS_ESMF_GridWrite(grid, fileName=fileName, overwrite=overwrite, &
timeslice=timeslice, iofmt=iofmt, relaxedflag=relaxedflag, &
nclScript=nclScript, map=LIS_ESMF_MAPPRESET_IRENE, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
case ('frontrange','FRONTRANGE','FrontRange')
call LIS_ESMF_GridWrite(grid, fileName=fileName, overwrite=overwrite, &
timeslice=timeslice, iofmt=iofmt, relaxedflag=relaxedflag, &
nclScript=nclScript, map=LIS_ESMF_MAPPRESET_FRONTRANGE, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
case default
call ESMF_LogSetError(rcToCheck=ESMF_RC_ARG_VALUE, &
msg="Unknown map preset value "//trim(mapPreset)//".", &
CONTEXT, rcToReturn=rc)
return
endselect
end subroutine
#undef METHOD
!-----------------------------------------------------------------------------
#define METHOD "LIS_ESMF_GridWrite"
!BOP
! !IROUTINE: LIS_ESMF_GridWrite - Write Grid data to file
! !INTERFACE:
! call using generic interface: LIS_ESMF_GridWrite
subroutine LIS_ESMF_GridWrite_default(grid, fileName, overwrite, status, &
timeslice, iofmt, relaxedflag, nclScript, map, rc)
! ! ARGUMENTS
type(ESMF_Grid), intent(in) :: grid
character(len=*), intent(in), optional :: fileName
logical, intent(in), optional :: overwrite
type(ESMF_FileStatus_Flag), intent(in), optional :: status
integer, intent(in), optional :: timeslice
type(ESMF_IOFmt_Flag), intent(in), optional :: iofmt
logical, intent(in), optional :: relaxedflag
logical, intent(in), optional :: nclScript
type(MapDesc), intent(in), optional :: map
integer, intent(out), optional :: rc
! !DESCRIPTION:
! Write the data in {\tt grid} to {\tt file} if supported by the
! {\tt iofmt}.
!
! The arguments are:
! \begin{description}
! \item[field]
! The {\tt ESMF\_Field} object whose data is to be written.
! \item[fileName]
! The name of the file to write to. If not present then the file will
! be written to the grid's name.
! \item[{[overwrite]}]
! A logical flag, the default is .false., i.e., existing Field data may
! {\em not} be overwritten. If .true., the
! data corresponding to each field's name will be
! be overwritten. If the {\tt timeslice} option is given, only data for
! the given timeslice may be overwritten.
! Note that it is always an error to attempt to overwrite a NetCDF
! variable with data which has a different shape.
! \item[{[status]}]
! The file status. Valid options are {\tt ESMF\_FILESTATUS\_NEW},
! {\tt ESMF\_FILESTATUS\_OLD}, {\tt ESMF\_FILESTATUS\_REPLACE}, and
! {\tt ESMF\_FILESTATUS\_UNKNOWN} (default).
! \item[{[timeslice]}]
! Time slice counter. Must be positive. The behavior of this
! option may depend on the setting of the {\tt overwrite} flag:
! \begin{description}
! \item[{\tt overwrite = .false.}:]\ If the timeslice value is
! less than the maximum time already in the file, the write will fail.
! \item[{\tt overwrite = .true.}:]\ Any positive timeslice value is valid.
! \end{description}
! By default, i.e. by omitting the {\tt timeslice} argument, no
! provisions for time slicing are made in the output file,
! however, if the file already contains a time axis for the variable,
! a timeslice one greater than the maximum will be written.
! \item[{[iofmt]}]
! The IO format. Valid options are {\tt ESMF\_IOFMT\_BIN} and
! {\tt ESMF\_IOFMT\_NETCDF}. If not present, file names with a {\tt .bin}
! extension will use {\tt ESMF\_IOFMT\_BIN}, and file names with a {\tt .nc}
! extension will use {\tt ESMF\_IOFMT\_NETCDF}. Other files default to
! {\tt ESMF\_IOFMT\_NETCDF}.
! \item[{[relaxedflag]}]
! If {\tt .true.}, then no error is returned even if the call cannot write
! the file due to library limitations. Default is {\tt .false.}.
! \item[{[nclScript]}]
! If {\tt .true.} then NUOPC will write an NCL script that can be used to
! generate grid graphics. Default is {\tt .false.}.
! \item[{[map]}]
! Derived type including the map name and boundary coordinates.
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOP
!-----------------------------------------------------------------------------
! local variables
logical :: ioCapable
logical :: doItFlag
character(len=64) :: lfileName
character(len=64) :: gridName
type(ESMF_Array) :: array
type(ESMF_ArrayBundle) :: arraybundle
logical :: isPresent
integer :: dimCount
integer :: dimIndex
integer,allocatable :: coordDimCount(:)
integer :: coordDimMax
integer :: stat
logical :: lnclScript
logical :: hasCorners
if (present(rc)) rc = ESMF_SUCCESS
ioCapable = (ESMF_IO_PIO_PRESENT .and. &
(ESMF_IO_NETCDF_PRESENT .or. ESMF_IO_PNETCDF_PRESENT))
doItFlag = .true. ! default
if (present(relaxedFlag)) then
doItFlag = .not.relaxedflag .or. (relaxedflag.and.ioCapable)
endif
if (doItFlag) then
if (present(fileName)) then
lfileName = trim(fileName)
else
call ESMF_GridGet(grid, name=gridName, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
lfileName = trim(gridName)//".nc"
endif
arraybundle = ESMF_ArrayBundleCreate(rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
! -- centers --
call ESMF_GridGetCoord(grid, staggerLoc=ESMF_STAGGERLOC_CENTER, &
isPresent=isPresent, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
if (isPresent) then
call ESMF_GridGetCoord(grid, coordDim=1, &
staggerLoc=ESMF_STAGGERLOC_CENTER, array=array, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArraySet(array, name="lon_center", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArrayBundleAdd(arraybundle,(/array/),rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_GridGetCoord(grid, coordDim=2, &
staggerLoc=ESMF_STAGGERLOC_CENTER, array=array, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArraySet(array, name="lat_center", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArrayBundleAdd(arraybundle,(/array/),rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
endif
! -- corners --
call ESMF_GridGetCoord(grid, staggerLoc=ESMF_STAGGERLOC_CORNER, &
isPresent=hasCorners, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
if (hasCorners) then
call ESMF_GridGetCoord(grid, coordDim=1, &
staggerLoc=ESMF_STAGGERLOC_CORNER, array=array, rc=rc)
if (.not. ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) then
call ESMF_ArraySet(array, name="lon_corner", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArrayBundleAdd(arraybundle,(/array/),rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
endif
call ESMF_GridGetCoord(grid, coordDim=2, &
staggerLoc=ESMF_STAGGERLOC_CORNER, array=array, rc=rc)
if (.not. ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) then
call ESMF_ArraySet(array, name="lat_corner", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArrayBundleAdd(arraybundle,(/array/),rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
endif
endif
! -- mask --
call ESMF_GridGetItem(grid, itemflag=ESMF_GRIDITEM_MASK, &
staggerLoc=ESMF_STAGGERLOC_CENTER, isPresent=isPresent, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
if (isPresent) then
call ESMF_GridGetItem(grid, staggerLoc=ESMF_STAGGERLOC_CENTER, &
itemflag=ESMF_GRIDITEM_MASK, array=array, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArraySet(array, name="mask", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArrayBundleAdd(arraybundle,(/array/),rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
endif
! -- area --
call ESMF_GridGetItem(grid, itemflag=ESMF_GRIDITEM_AREA, &
staggerLoc=ESMF_STAGGERLOC_CENTER, isPresent=isPresent, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
if (isPresent) then
call ESMF_GridGetItem(grid, staggerLoc=ESMF_STAGGERLOC_CENTER, &
itemflag=ESMF_GRIDITEM_AREA, array=array, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArraySet(array, name="area", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArrayBundleAdd(arraybundle,(/array/),rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
endif
call ESMF_ArrayBundleWrite(arraybundle, &
fileName=trim(lfileName),rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_ArrayBundleDestroy(arraybundle,rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
if (present(nclScript)) then
lnclScript = nclScript
else
lnclScript = .FALSE.
endif
if (lnclScript) then
call ESMF_GridGet(grid,dimCount=dimCount,rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
! allocate coordDim info accord. to dimCount and tileCount
allocate(coordDimCount(dimCount), stat=stat)
if (ESMF_LogFoundAllocError(statusToCheck=stat, &
msg="Allocation of coordinate dimensions memory failed.", &
CONTEXT, rcToReturn=rc)) return ! bail out
! get coordDim info
call ESMF_GridGet(grid, coordDimCount=coordDimCount, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
coordDimMax = 0
do dimIndex=1,dimCount
coordDimMax = MAX(coordDimMax,coordDimCount(dimIndex))
enddo
! deallocate coordDim info accord. to dimCount and tileCount
deallocate(coordDimCount, stat=stat)
if (ESMF_LogFoundDeallocError(statusToCheck=stat, &
msg="Deallocation of coordinate dimensions memory failed.", &
CONTEXT, rcToReturn=rc)) return ! bail out
if (coordDimMax == 1) then
call LIS_ESMF_NclScriptWrite(gridFile=lfileName, map=map, &
uniformRect=.TRUE., writeCorners=hasCorners, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
else
call LIS_ESMF_NclScriptWrite(gridFile=lfileName, map=map, &
writeCorners=hasCorners, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
endif
endif
endif
end subroutine
#undef METHOD
!-----------------------------------------------------------------------------
#define METHOD "LIS_ESMF_NclScriptWrite"
!BOP
! !IROUTINE: LIS_ESMF_NclScriptWrite_coords - Write NCL Script to file
! !INTERFACE:
! call using generic interface: LIS_ESMF_NclScriptWrite
subroutine LIS_ESMF_NclScriptWrite_coords(gridFile,mapName,minCoords,maxCoords, &
title,nclFile,uniformRect,writeCorners,rc)
! ! ARGUMENTS
character(len=*),intent(in) :: gridFile
character(len=*),intent(in) :: mapName
real,intent(in) :: minCoords(2)
real,intent(in) :: maxCoords(2)
character(len=*),intent(in),optional :: title
character(len=*),intent(in),optional :: nclFile
logical,intent(in),optional :: uniformRect
logical,intent(in),optional :: writeCorners
integer,intent(out),optional :: rc
! !DESCRIPTION:
! Write NCL script that can be used to generate NCL grid visual.
!
! The arguments are:
! \begin{description}
! \item[gridFile]
! NetCDF grid file name used plot data on coordinates.
! \item[mapName]
! Map name.
! \item[minCoords]
! Minimum coordinate limits for plot.
! \item[maxCoords]
! Maximum coordinate limits for plot.
! \item[title]
! Grid plot title.
! \item[nclFile]
! NCL script file name
! \item[uniformRect]
! Repeat coordinates for uniform rectangular grids.
! \item[writeCorners]
! Plot corners. Default plot center coordinates.
! \end{description}
!
!EOP
!-----------------------------------------------------------------------------
! local variables
type(MapDesc) :: map
if (present(rc)) rc = ESMF_SUCCESS
map = MapDesc(trim(mapName),.FALSE., &
minCoords(2),maxCoords(2),minCoords(1),maxCoords(1))
call LIS_ESMF_NclScriptWrite(gridFile, map=map, title=title, &
nclFile=nclFile, uniformRect=uniformRect, writeCorners=writeCorners, &
rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
end subroutine
#undef METHOD
!-----------------------------------------------------------------------------
#define METHOD "LIS_ESMF_NclScriptWrite"
!BOP
! !IROUTINE: LIS_ESMF_NclScriptWrite_preset - Write NCL Script to file
! !INTERFACE:
! call using generic interface: LIS_ESMF_NclScriptWrite
subroutine LIS_ESMF_NclScriptWrite_preset(gridFile,mapPreset, &
title,nclFile,uniformRect,writeCorners,rc)
! ! ARGUMENTS
character(len=*),intent(in) :: gridFile
character(len=*),intent(in) :: mapPreset
character(len=*),intent(in),optional :: title
character(len=*),intent(in),optional :: nclFile
logical,intent(in),optional :: uniformRect
logical,intent(in),optional :: writeCorners
integer,intent(out),optional :: rc
! !DESCRIPTION:
! Write NCL script that can be used to generate NCL grid visual.
!
! The arguments are:
! \begin{description}
! \item[gridFile]
! NetCDF grid file name used plot data on coordinates.
! \item[mapPreset]
! Preset coordinate limits.
! \item[title]
! Grid plot title.
! \item[nclFile]
! NCL script file name
! \item[uniformRect]
! Repeat coordinates for uniform rectangular grids.
! \item[writeCorners]
! Plot corners. Default plot center coordinates.
! \end{description}
!
!EOP
!-----------------------------------------------------------------------------
! local variables
if (present(rc)) rc = ESMF_SUCCESS
select case (trim(mapPreset))
case ('global','GLOBAL','Global')
call LIS_ESMF_NclScriptWrite(gridFile, &
map=LIS_ESMF_MAPPRESET_GLOBAL, title=title, nclFile=nclFile, &
uniformRect=uniformRect, writeCorners=writeCorners, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
case ('conus','CONUS','Conus')
call LIS_ESMF_NclScriptWrite(gridFile, &
map=LIS_ESMF_MAPPRESET_CONUS, title=title, nclFile=nclFile, &
uniformRect=uniformRect, writeCorners=writeCorners, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
case ('irene','IRENE','Irene')
call LIS_ESMF_NclScriptWrite(gridFile, &
map=LIS_ESMF_MAPPRESET_IRENE, title=title, nclFile=nclFile, &
uniformRect=uniformRect, writeCorners=writeCorners, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
case ('frontrange','FRONTRANGE','FrontRange')
call LIS_ESMF_NclScriptWrite(gridFile, &
map=LIS_ESMF_MAPPRESET_FRONTRANGE, title=title, nclFile=nclFile, &
uniformRect=uniformRect, writeCorners=writeCorners, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
case default
call ESMF_LogSetError(rcToCheck=ESMF_RC_ARG_VALUE, &
msg="Unknown map preset value "//trim(mapPreset)//".", &
CONTEXT, rcToReturn=rc)
return
endselect
end subroutine
#undef METHOD
!-----------------------------------------------------------------------------
#define METHOD "LIS_ESMF_NclScriptWrite"
!BOP
! !IROUTINE: LIS_ESMF_NclScriptWrite_default - Write NCL Script to file
! !INTERFACE:
! call using generic interface: LIS_ESMF_NclScriptWrite
subroutine LIS_ESMF_NclScriptWrite_default(gridFile, map, title, &
nclFile, uniformRect, writeCorners, rc)
! ! ARGUMENTS
character(len=*),intent(in) :: gridFile
type(MapDesc),intent(in) :: map
character(len=*),intent(in),optional :: title
character(len=*),intent(in),optional :: nclFile
logical,intent(in),optional :: uniformRect
logical,intent(in),optional :: writeCorners
integer,intent(out),optional :: rc
! !DESCRIPTION:
! Write NCL script that can be used to generate NCL grid visual.
!
! The arguments are:
! \begin{description}
! \item[gridFile]
! NetCDF grid file name used plot data on coordinates.
! \item[map]
! Coordinate limits.
! \item[title]
! Grid plot title.
! \item[nclFile]
! NCL script file name
! \item[uniformRect]
! Repeat coordinates for uniform rectangular grids.
! \item[writeCorners]
! Plot corners. Default plot center coordinates.
! \end{description}
!
!EOP
!-----------------------------------------------------------------------------
! local variables
type(ESMF_VM) :: vm
integer :: lpe
character(len=64) :: ltitle
character(len=64) :: lnclFile
character(len=64) :: fileBase
logical :: luniformRect
logical :: lcorners
integer :: markExt
integer :: fUnit
integer :: stat
character(len=10) :: varlat
character(len=10) :: varlon
if (present(rc)) rc = ESMF_SUCCESS
! Get current VM and pet number
call ESMF_VMGetCurrent(vm, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
call ESMF_VMGet(vm, localPet=lpe, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
if (lpe /= 0) return
markExt = index(gridFile,".",back=.TRUE.)
if (markExt > 1) then
fileBase = gridFile(1:markExt-1)
elseif (markExt == 1) then
fileBase = "grid"
elseif (len(trim(gridFile)) > 0) then
fileBase = trim(gridFile)
else
fileBase = "grid"
endif
if (present(nclFile)) then
lnclFile = trim(nclFile)
else
lnclFile = trim(fileBase)//".ncl"
endif
if (present(title)) then
ltitle = trim(title)
else
ltitle = trim(fileBase)//" "//trim(map%name)
endif
if (present(uniformRect)) then
luniformRect = uniformRect
else
luniformRect = .FALSE.
endif
if (present(writeCorners)) then
lcorners = writeCorners
else
lcorners = .FALSE.
endif
if (lcorners) then
varlat = 'lat_corner'
varlon = 'lon_corner'
else
varlat = 'lat_center'
varlon = 'lon_center'
endif
call ESMF_UtilIOUnitGet(fUnit, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
open (fUnit,file=trim(lnclFile),action="write", &
status="new",iostat=stat)
if (stat /= 0) then
call ESMF_LogSetError(rcToCheck=ESMF_RC_FILE_OPEN, &
msg="Cound not open "//trim(lnclFile)//".", &
CONTEXT, rcToReturn=rc)
return
endif
write (fUnit,"(A)") '; NUOPC_FileWriteMapNCL used to generate this file'
write (fUnit,"(A)") '; execute ncl <this file> to generate grid png file'
write (fUnit,"(A)") 'begin'
write (fUnit,"(A)") ' in = addfile("'//trim(gridFile)//'","r")'
write (fUnit,"(A)") ' wks = gsn_open_wks("png","'//trim(fileBase)//'")'
write (fUnit,"(A)") ' res = True'
write (fUnit,"(A)") ' res@gsnMaximize = True'
write (fUnit,"(A)") ' res@gsnDraw = False'
write (fUnit,"(A)") ' res@gsnFrame = False'
write (fUnit,"(A)") ' res@tiMainString = "'//trim(ltitle)//'"'
write (fUnit,"(A)") ' res@pmTickMarkDisplayMode = "Always"'
write (fUnit,"(A)") '; '//trim(map%name)//' Map Grid'
if (.NOT. map%global) then
write (fUnit,"(A,F0.3)") &
' res@mpMinLatF = ',map%minLat
write (fUnit,"(A,F0.3)") &
' res@mpMaxLatF = ',map%maxLat
write (fUnit,"(A,F0.3)") &
' res@mpMinLonF = ',map%minLon
write (fUnit,"(A,F0.3)") &
' res@mpMaxLonF = ',map%maxLon
endif
write (fUnit,"(A)") ' map = gsn_csm_map_ce(wks,res)'
write (fUnit,"(A)") ' hgt = in->lon_center(:,:)'
write (fUnit,"(A)") ' dimlon = getfilevardimsizes(in,"'//trim(varlon)//'")'
write (fUnit,"(A)") ' dimlat = getfilevardimsizes(in,"'//trim(varlat)//'")'
if (luniformRect) then
write (fUnit,"(A)") ' hgt@lat2d = conform_dims((/dimlon(0),dimlat(1)/),'// &
'in->'//trim(varlat)//'(:,0),1)'
write (fUnit,"(A)") ' hgt@lon2d = conform_dims((/dimlon(0),dimlat(1)/),'// &
'in->'//trim(varlon)//'(0,:),0)'
else
write (fUnit,"(A)") ' hgt@lat2d = in->'//trim(varlat)//'(:,:)'
write (fUnit,"(A)") ' hgt@lon2d = in->'//trim(varlon)//'(:,:)'
endif
write (fUnit,"(A)") ' pres = True'
if (lcorners) then
write (fUnit,"(A)") ' pres@gsnCoordsAsLines = True'
else
write (fUnit,"(A)") ' pres@gsnCoordsAsLines = False'
write (fUnit,"(A)") ' if (dimlon(0)*dimlat(1) .gt. 99) then'
write (fUnit,"(A)") ' pres@gsMarkerIndex = 1'
write (fUnit,"(A)") ' end if'
endif
write (fUnit,"(A)") ' gsn_coordinates(wks,map,hgt,pres)'
write (fUnit,"(A)") 'end'
close (fUnit,iostat=stat)
if (stat /= 0) then
call ESMF_LogSetError(rcToCheck=ESMF_RC_FILE_CLOSE, &
msg="Cound not close "//trim(lnclFile)//".", &
CONTEXT, rcToReturn=rc)
return
endif
end subroutine
#undef METHOD
!-----------------------------------------------------------------------------
#define METHOD "LIS_ESMF_FerretScriptWrite"
!BOP
! !IROUTINE: LIS_ESMF_FerretScriptWrite_coords - Write Ferret Script to file
! !INTERFACE:
! call using generic interface: LIS_ESMF_FerretScriptWrite
subroutine LIS_ESMF_FerretScriptWrite_coords(varName, dataFile, &
gridFile, slices, mapName, minCoords, maxCoords, scale, jnlFile, &
uniformRect,rc)
! ! ARGUMENTS
character(len=*),intent(in) :: varName
character(len=*),intent(in) :: dataFile
character(len=*),intent(in) :: gridFile
integer,intent(in) :: slices(:)
character(len=*),intent(in) :: mapName
real,intent(in) :: minCoords(2)
real,intent(in) :: maxCoords(2)
real,intent(in),optional :: scale(3)
character(len=*),intent(in),optional :: jnlFile
logical,intent(in),optional :: uniformRect
integer,intent(out),optional :: rc
! !DESCRIPTION:
! Write Ferret script that can be used to generate Ferret field visual.
!
! The arguments are:
! \begin{description}
! \item[varName]
! NetCDF variable name in dataFile.
! \item[dataFile]
! NetCDF field data file name.
! \item[gridFile]
! NetCDF grid file name used plot data on coordinates.
! \item[slices]
! Array of slice numbers to for which to create plots.
! \item[mapName]
! Map name used to create map description.
! \item[minCoords]
! Minimum coordinates used to plot data.
! \item[maxCoords]
! Maximum coordinates used to plot data.
! \item[scale]
! Field data scale (/ minimum value, maximum value, step size /)
! \item[jnlFile]
! Output script filename.
! \item[uniformRect]
! Repeat coordinates for uniform rectangular grids.
! \end{description}
!
!EOP
!-----------------------------------------------------------------------------
! local variables
type(MapDesc) :: map
if (present(rc)) rc = ESMF_SUCCESS
map = MapDesc(trim(mapName),.FALSE., &
minCoords(2),maxCoords(2),minCoords(1),maxCoords(1))
call LIS_ESMF_FerretScriptWrite(varName,dataFile,gridFile,slices, &
map=map,scale=scale,jnlFile=jnlFile, uniformRect=uniformRect,rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, PASSTHRU)) return ! bail out
end subroutine
#undef METHOD
!-----------------------------------------------------------------------------
#define METHOD "LIS_ESMF_FerretScriptWrite"