-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathfield_manager.F90
3679 lines (3234 loc) · 137 KB
/
field_manager.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
!***********************************************************************
!* GNU Lesser General Public License
!*
!* This file is part of the GFDL Flexible Modeling System (FMS).
!*
!* FMS is free software: you can redistribute it and/or modify it under
!* the terms of the GNU Lesser General Public License as published by
!* the Free Software Foundation, either version 3 of the License, or (at
!* your option) any later version.
!*
!* FMS is distributed in the hope that it will be useful, but WITHOUT
!* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
!* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
!* for more details.
!*
!* You should have received a copy of the GNU Lesser General Public
!* License along with FMS. If not, see <http://www.gnu.org/licenses/>.
!***********************************************************************
!> @defgroup field_manager_mod field_manager_mod
!> @ingroup field_manager
!> @brief Reads entries from a field table and stores this
!! information along with the type of field it belongs to.
!!
!> This allows the component models to query the field manager to see if non-default
!! methods of operation are desired. In essence the field table is a
!! powerful type of namelist. Default values can be provided for all the
!! fields through a namelist, individual fields can be modified through
!! the field table however.
!!
!> @author William Cooke
!!
!! An example of field table entries could be
!! <PRE>
!! "tracer","atmos_mod","sphum"
!!
!! "tracer","atmos_mod","sf6"
!! "longname","sulf_hex"
!! "advection_scheme_horiz","2nd_order"
!! "Profile_type","Fixed","surface_value = 0.0E+00"/
!!
!! "prog_tracers","ocean_mod","age_global"
!! horizontal-advection-scheme = mdfl_sweby
!! vertical-advection-scheme = mdfl_sweby
!! restart_file = ocean_age.res.nc
!! </PRE>
!!
!! The field table consists of entries in the following format.
!!
!! The first line of an entry should consist of three quoted strings.
!!
!! The first quoted string will tell the field manager what type of
!! field it is.
!!
!! The second quoted string will tell the field manager which model the
!! field is being applied to.
!! The supported types at present are
!!<PRE>
!! "coupler_mod" for the coupler,
!! "atmos_mod" for the atmosphere model,
!! "ocean_mod" for the ocean model,
!! "land_mod" for the land model, and,
!! "ice_mod" for the ice model.
!!</PRE>
!! The third quoted string should be a unique name that can be used as a
!! query.
!!
!! The second and following lines of each entry are called methods in
!! this context. Methods can be developed within any module and these
!! modules can query the field manager to find any methods that are
!! supplied in the field table.
!!
!! These lines can be coded quite flexibly.
!!
!! The line can consist of two or three quoted strings or a simple unquoted
!! string.
!!
!! If the line consists two or three quoted strings, then the first string will
!! be an identifier that the querying module will ask for.
!!
!! The second string will be a name that the querying module can use to
!! set up values for the module.
!!
!! The third string, if present, can supply parameters to the calling module that can be
!! parsed and used to further modify values.
!!
!! If the line consists of a simple unquoted string then quotes are not allowed
!! in any part of the line.
!!
!! An entry is ended with a backslash (/) as the final character in a
!! row.
!!
!! Comments can be inserted in the field table by having a # as the
!! first character in the line.
!!
!! In the example above we have three field entries.
!!
!! The first is a simple declaration of a tracer called "sphum".
!!
!! The second is for a tracer called "sf6". In this case a field named
!! "longname" will be given the value "sulf_hex". A field named
!! "advection_scheme_horiz" will be given the value "2nd_order". Finally a field
!! name "Profile_type" will be given a child field called "Fixed", and that field
!! will be given a field called "surface_value" with a real value of 0.0E+00.
!!
!! The third entry is an example of a oceanic age tracer. Note that the
!! method lines are formatted differently here. This is the flexibility mentioned
!! above.
!!
!! With these formats, a number of restrictions are required.
!!
!! The following formats are equally valid.
!!<PRE>
!! "longname","sulf_hex"
!! "longname = sulf_hex"
!! longname = sulf_hex
!!</PRE>
!! However the following is not valid.
!!<PRE>
!! longname = "sulf_hex"
!!</PRE>
!!
!! In the SF6 example above the last line of the entry could be written in the
!! following ways.
!!<PRE>
!! "Profile_type","Fixed","surface_value = 0.0E+00"/
!! Profile_type/Fixed/surface_value = 0.0E+00/
!!</PRE>
!!
!! Values supplied with fields are converted to the various types with the
!! following assumptions.
!!<PRE>
!! Real values : These values contain a decimal point or are in exponential format.
!! These values only support e or E format for exponentials.
!! e.g. 10.0, 1e10 and 1E10 are considered to be real numbers.
!!
!! Integer values : These values only contain numbers.
!! e.g 10 is an integer. 10.0 and 1e10 are not.
!!
!! Logical values : These values are supplied as one of the following formats.
!! T, .T., TRUE, .TRUE.
!! t, .t., true, .true.
!! F, .F., FALSE, .FALSE.
!! f, .f., false, .false.
!! These will be converted to T or F in a dump of the field.
!!
!! Character strings : These values are assumed to be strings if a character
!! other than an e (or E) is in the value. Numbers can be suppled in the value.
!! If the value does not meet the criteria for a real, integer or logical type,
!! it is assumed to be a character type.
!!</PRE>
!! The entries within the field table can be designed by the individual
!! authors of code to allow modification of their routines.
!!
!> @addtogroup field_manager_mod
!> @{
module field_manager_mod
!TODO this variable can be removed when the legacy table is no longer used
#ifndef MAXFIELDS_
#define MAXFIELDS_ 250
#endif
!TODO this variable can be removed when the legacy table is not longer used
#ifndef MAXFIELDMETHODS_
#define MAXFIELDMETHODS_ 250
#endif
!
! <CONTACT EMAIL="William.Cooke@noaa.gov"> William Cooke
! </CONTACT>
!
! <REVIEWER EMAIL="Richard.Slater@noaa.gov"> Richard D. Slater
! </REVIEWER>
!
! <REVIEWER EMAIL="Matthew.Harrison@noaa.gov"> Matthew Harrison
! </REVIEWER>
!
! <REVIEWER EMAIL="John.Dunne@noaa.gov"> John P. Dunne
! </REVIEWER>
use mpp_mod, only : mpp_error, &
FATAL, &
NOTE, &
WARNING, &
mpp_pe, &
mpp_root_pe, &
stdlog, &
stdout, &
input_nml_file
use fms_mod, only : lowercase, &
write_version_number, &
check_nml_error
use fms2_io_mod, only: file_exists, get_instance_filename
use platform_mod, only: r4_kind, r8_kind, FMS_PATH_LEN, FMS_FILE_LEN
#ifdef use_yaml
use fm_yaml_mod
#endif
implicit none
private
#include<file_version.h>
logical :: module_is_initialized = .false.
public :: field_manager_init !< (nfields, [table_name]) returns number of fields
public :: field_manager_end !< ()
public :: find_field_index !< (model, field_name) or (list_path)
public :: find_field_index_old !< (model, field_name) returns index of field_name in
public :: find_field_index_new
public :: get_field_info !< (n,fld_type,fld_name,model,num_methods)
!! Returns parameters relating to field n.
public :: get_field_method !< (n, m, method) Returns the m-th method of field n
public :: get_field_methods !< (n, methods) Returns the methods related to field n
public :: parse !< (text, label, values) Overloaded function to parse integer,
!! real or character. Parse returns the number of values
!! decoded (> 1 => an array of values)
public :: fm_change_list !< (list) return success
public :: fm_change_root !< (list) return success
public :: fm_dump_list !< (list [, recursive]) return success
public :: fm_exists !< (field) return success
public :: fm_get_index !< (field) return index
public :: fm_get_current_list !< () return path
public :: fm_get_length !< (list) return length
public :: fm_get_type !< (field) return string
public :: fm_get_value !< (entry, value [, index]) return success !! generic
public :: fm_get_value_integer !< as above (overloaded function)
public :: fm_get_value_logical !< as above (overloaded function)
public :: fm_get_value_real_r4 !< as above (overloaded function)
public :: fm_get_value_real_r8 !< as above (overloaded function)
public :: fm_get_value_string !< as above (overloaded function)
public :: fm_init_loop !< (list, iter)
public :: fm_loop_over_list !< (list, name, type, index) return success
!! (iter, name, type, index) return success
public :: fm_new_list !< (list [, create] [, keep]) return index
public :: fm_new_value !< (entry, value [, create] [, index]) return index !! generic
public :: fm_new_value_integer !< as above (overloaded function)
public :: fm_new_value_logical !< as above (overloaded function)
public :: fm_new_value_real_r4 !< as above (overloaded function)
public :: fm_new_value_real_r8 !< as above (overloaded function)
public :: fm_new_value_string !< as above (overloaded function)
public :: fm_reset_loop !< ()
public :: fm_return_root !< () return success
public :: fm_modify_name !< (oldname, newname) return success
public :: fm_query_method !< (name, method_name, method_control) return success and
!! name and control strings
public :: fm_find_methods !< (list, methods, control) return success and name and
!! control strings.
public :: fm_copy_list !< (list, suffix, [create]) return index
private :: create_field ! (list_p, name) return field pointer
private :: dump_list ! (list_p, recursive, depth) return success
private :: find_base ! (field, path, base)
private :: find_field ! (field, list_p) return field pointer
private :: find_head ! (field, head, rest)
private :: find_list ! (list, list_p, create) return field pointer
private :: get_field ! (field, list_p) return field pointer
private :: initialize_module_variables ! ()
private :: make_list ! (list_p, name) return field pointer
!> The length of a character string representing the field name.
integer, parameter, public :: fm_field_name_len = 48
!! TODO this should be removed in favor of the global FMS_PATH_LEN
!! when possible, currently used in ocean_BGC and land_lad2
!> The length of a character string representing the field path.
integer, parameter, public :: fm_path_name_len = FMS_PATH_LEN
!> The length of a character string representing character values for the field.
integer, parameter, public :: fm_string_len = 1024
!> The length of a character string representing the various types that the values of the field can take.
integer, parameter, public :: fm_type_name_len = 8
!> Number of models (ATMOS, OCEAN, LAND, ICE, COUPLER).
integer, parameter, public :: NUM_MODELS = 5
!> The value returned if a field is not defined.
integer, parameter, public :: NO_FIELD = -1
!> Atmospheric model.
integer, parameter, public :: MODEL_ATMOS = 1
!> Ocean model.
integer, parameter, public :: MODEL_OCEAN = 2
!> Land model.
integer, parameter, public :: MODEL_LAND = 3
!> Ice model.
integer, parameter, public :: MODEL_ICE = 4
!> Ice model.
integer, parameter, public :: MODEL_COUPLER = 5
!> Model names, e.g. MODEL_NAMES(MODEL_OCEAN) is 'oceanic'
character(len=11), parameter, public, dimension(NUM_MODELS) :: &
MODEL_NAMES=(/'atmospheric','oceanic ','land ','ice ','coupler '/)
!> @}
!> @brief This method_type is a way to allow a component module to alter the parameters it needs
!! for various tracers.
!!
!> In essence this is a way to modify a namelist. A namelist can supply
!! default parameters for all tracers. This method will allow the user to modify these
!! default parameters for an individual tracer. An example could be that the user wishes to
!! use second order advection on a tracer and also use fourth order advection on a second
!! tracer within the same model run. The default advection could be second order and the
!! field table would then indicate that the second tracer requires fourth order advection.
!! This would be parsed by the advection routine.
!> @ingroup field_manager_mod
type, public :: method_type
character(len=fm_string_len) :: method_type !< This string represents a tag that a module
!! using this method can key on. Typically this should
!! contain some reference to the module that is calling it.
character(len=fm_string_len) :: method_name !< This is the name of a method which the module
!! can parse and use to assign different default values to
!! a field method.
character(len=fm_string_len) :: method_control !< This is the string containing parameters that
!! the module can use as values for a field method. These should
!! override default values within the module.
end type
!> This method_type is the same as method_type except that the
!! method_control string is not present. This is used when you wish to
!! change to a scheme within a module but do not need to pass
!! parameters. See @ref method_type for member information.
!> @ingroup field_manager_mod
type, public :: method_type_short
character(len=fm_string_len) :: method_type
character(len=fm_string_len) :: method_name
end type
!> This is the same as method_type except that the
!! method_control and method_name strings are not present. This is used
!! when you wish to change to a scheme within a module but do not need
!! to pass parameters.
!> @ingroup field_manager_mod
type, public :: method_type_very_short
character(len=fm_string_len) :: method_type
end type
!> Iterator over the field manager list
!> @ingroup field_manager_mod
type, public :: fm_list_iter_type
type(field_def), pointer :: ptr => NULL() !< pointer to the current field
end type fm_list_iter_type
!> @ingroup field_manager_mod
type(method_type), public :: default_method
!> @brief Returns an index corresponding to the given field name.
!!
!> Model number can be given for old method.
!! <br>Example usage:
!! @code{.F90}
!! value=find_field_index( model, field_name )
!! value=find_field_index( field_name )
!! @endcode
!> @ingroup field_manager_mod
interface find_field_index
module procedure find_field_index_old
module procedure find_field_index_new
end interface
!> @brief A function to parse an integer or an array of integers,
!! a real or an array of reals, a string or an array of strings.
!!
!> Parse is an integer function that decodes values from a text string.
!! The text string has the form: "label=list" where "label" is an
!! arbitrary user defined label describing the values being decoded,
!! and "list" is a list of one or more values separated by commas.
!! The values may be integer, real, or character.
!! Parse returns the number of values decoded.
!! <br>Example usage:
!! @code{.F90}
!! number = parse(text, label, value)
!! @endcode
!> @ingroup field_manager_mod
interface parse
module procedure parse_real_r4
module procedure parse_real_r8
module procedure parse_reals_r4
module procedure parse_reals_r8
module procedure parse_integer
module procedure parse_integers
module procedure parse_string
module procedure parse_strings
end interface
!> @brief An overloaded function to assign a value to a field.
!!
!> Allocate and initialize a new value and return the index.
!! If an error condition occurs the parameter NO_FIELD is returned.
!!
!! If the type of the field is changing (e.g. real values being transformed to
!! integers), then any previous values for the field are removed and replaced
!! by the value passed in the present call to this function.
!!
!! If append is present and .true., then index cannot be greater than 0 if
!! it is present.
!! <br> Example usage:
!! @code{.F90}
!! field_index= fm_new_value(name, value, [create], [index], [append])
!! @endcode
!> @ingroup field_manager_mod
interface fm_new_value
module procedure fm_new_value_integer
module procedure fm_new_value_logical
module procedure fm_new_value_real_r4
module procedure fm_new_value_real_r8
module procedure fm_new_value_string
end interface
!> @brief An overloaded function to find and extract a value for a named field.
!!
!> Find and extract the value for name. The value may be of type real,
!! integer, logical or character. If a single value from an array of values
!! is required, an optional index can be supplied.
!! Return true for success and false for failure
!! <br> Example usage:
!! @code{.F90}
!! success = fm_get_value(name, value, index)
!! @endcode
!> @ingroup field_manager_mod
interface fm_get_value
module procedure fm_get_value_integer
module procedure fm_get_value_logical
module procedure fm_get_value_real_r4
module procedure fm_get_value_real_r8
module procedure fm_get_value_string
end interface
!> @brief A function for looping over a list.
!!
!> Loop over the list, setting the name, type and index
!! of the next field. Return false at the end of the loop.
!! <br> Example usage:
!! @code{.F90}
!! success = fm_loop_over_list(list, name, field_type, index)
!! @endcode
!> @ingroup field_manager_mod
interface fm_loop_over_list
module procedure fm_loop_over_list_new
module procedure fm_loop_over_list_old
end interface
character(len=17), parameter :: module_name = 'field_manager_mod'
character(len=33), parameter :: error_header = '==>Error from '//trim(module_name)//': '
character(len=35), parameter :: warn_header = '==>Warning from '//trim(module_name)//': '
character(len=32), parameter :: note_header = '==>Note from '//trim(module_name)//': '
character(len=1), parameter :: comma = ","
character(len=1), parameter :: list_sep = '/'
!TODO these variable can be removed when the legacy table is no longer used
character(len=1), parameter :: comment = '#'
character(len=1), parameter :: dquote = '"'
character(len=1), parameter :: equal = '='
character(len=1), parameter :: squote = "'"
!
integer, parameter :: null_type = 0
integer, parameter :: integer_type = 1
integer, parameter :: list_type = 2
integer, parameter :: logical_type = 3
integer, parameter :: real_type = 4
integer, parameter :: string_type = 5
integer, parameter :: num_types = 5
integer, parameter :: array_increment = 10
!TODO these variable can be removed when the legacy table is no longer used
integer, parameter :: MAX_FIELDS = MAXFIELDS_
integer, parameter :: MAX_FIELD_METHODS = MAXFIELDMETHODS_
!
!> @brief Private type for internal use
!> @ingroup field_manager_mod
type, private :: field_mgr_type
character(len=fm_field_name_len) :: field_type
character(len=fm_string_len) :: field_name
integer :: model, num_methods
type(method_type), dimension(:), allocatable :: methods !< methods associated with this field name
end type field_mgr_type
!TODO These two types: field_names_type and field_names_type_short
!! will no longer be needed when the legacy field table is not used
!> @brief Private type for internal use
!> @ingroup field_manager_mod
type, private :: field_names_type
character(len=fm_field_name_len) :: fld_type
character(len=fm_field_name_len) :: mod_name
character(len=fm_string_len) :: fld_name
end type field_names_type
!> @brief Private type for internal use
!> @ingroup field_manager_mod
type, private :: field_names_type_short
character(len=fm_field_name_len) :: fld_type
character(len=fm_field_name_len) :: mod_name
end type field_names_type_short
!> @brief Private type for internal use
!> @ingroup field_manager_mod
type, private :: field_def
character (len=fm_field_name_len) :: name
integer :: index
type (field_def), pointer :: parent => NULL()
integer :: field_type
integer :: length
integer :: array_dim
integer :: max_index
type (field_def), pointer :: first_field => NULL()
type (field_def), pointer :: last_field => NULL()
integer, allocatable, dimension(:) :: i_value
logical, allocatable, dimension(:) :: l_value
real(r8_kind), allocatable, dimension(:) :: r_value !< string to real conversion will be done at r8;
!! all real values will be stored as r8_kind.
character(len=fm_string_len), allocatable, dimension(:) :: s_value
type (field_def), pointer :: next => NULL()
type (field_def), pointer :: prev => NULL()
end type field_def
!> @addtogroup field_manager_mod
!> @{
type(field_mgr_type), dimension(:), allocatable, private :: fields !< fields of field_mgr_type
character(len=FMS_PATH_LEN) :: loop_list
character(len=fm_type_name_len) :: field_type_name(num_types)
character(len=fm_field_name_len) :: save_root_name
! The string set is the set of characters.
character(len=52) :: set = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
! If a character in the string being parsed matches a character within
! the string set_nonexp then the string being parsed cannot be a number.
character(len=50) :: set_nonexp = "ABCDFGHIJKLMNOPQRSTUVWXYZabcdfghijklmnopqrstuvwxyz"
! If a character in the string being parsed matches a character within
! the string setnum then the string may be a number.
character(len=13) :: setnum = "0123456789+-."
integer :: num_fields = 0
type (field_def), pointer :: loop_list_p => NULL()
type (field_def), pointer :: current_list_p => NULL()
type (field_def), pointer :: root_p => NULL()
type (field_def), pointer :: save_root_parent_p => NULL()
type (field_def), target, save :: root
logical :: use_field_table_yaml = .false. !< .True. if using the field_table.yaml,
!! .false. if using the legacy field_table
namelist /field_manager_nml/ use_field_table_yaml
contains
!> @brief Routine to initialize the field manager.
!!
!> This routine reads from a file containing yaml paramaters.
!! These yaml parameters contain information on which schemes are
!! needed within various modules. The field manager does not
!! initialize any of those schemes however. It simply holds the
!! information and is queried by the appropriate module.
!!
!! The routine has two loops. The first loop initializes the my_table object
!! and counts the number of fields contained therein. The second loop is the
!! main loop that acts on each field in the my_table object, defining a list
!! object (in the field_manager definition) from which various fm routines may be
!! called, as well as populating the "fields" object and the "methods" objects
!! within each field object. The "fields" and "methods" objects are then used
!! with the subroutine new_name to append various characteristics to the list
!! object. Note that the "fields" and "methods" objects are also used with other
!! fm routines in a bit of a parallel system.
subroutine field_manager_init(nfields, table_name)
integer, intent(out), optional :: nfields !< number of fields
character(len=fm_string_len), intent(in), optional :: table_name !< Name of the field table, default
if (module_is_initialized) then
if(present(nfields)) nfields = num_fields
return
endif
call initialize_module_variables()
!TODO the use_field_table_yaml namelist can be removed when the legacy table is no longer in used
if (use_field_table_yaml) then
!Crash if you are not compiling with -Duse_yaml or if the field_table is present
#ifndef use_yaml
call mpp_error(FATAL, "You cannot have use_field_table_yaml=.true. without compiling with -Duse_yaml")
#else
if (file_exists("field_table")) &
call mpp_error(FATAL, "You cannot have the legacy field_table if use_field_table_yaml=.true.")
call mpp_error(NOTE, "You are using the yaml version of the field_table")
call read_field_table_yaml(nfields, table_name)
#endif
else
if (file_exists("field_table.yaml")) &
call mpp_error(FATAL, "You cannot have the yaml field_table if use_field_table_yaml=.false.")
call mpp_error(NOTE, "You are using the legacy version of the field_table")
call read_field_table_legacy(nfields, table_name)
endif
end subroutine field_manager_init
#ifdef use_yaml
!> @brief Routine to read and parse the field table yaml
subroutine read_field_table_yaml(nfields, table_name)
integer, intent(out), optional :: nfields !< number of fields
character(len=*), intent(in), optional :: table_name !< Name of the field table file, default is 'field_table.yaml'
character(len=FMS_FILE_LEN) :: tbl_name !< field_table yaml file
character(len=fm_string_len) :: method_control !< field_table yaml file
integer :: h, i, j, k, l, m !< dummy integer buffer
type (fmTable_t) :: my_table !< the field table
integer :: model !< model assocaited with the current field
character(len=FMS_PATH_LEN) :: list_name !< field_manager list name
character(len=fm_string_len) :: subparamvalue !< subparam value to be used when defining new name
character(len=fm_string_len) :: fm_yaml_null !< useful hack when OG subparam does not contain an equals sign
integer :: current_field !< field index within loop
integer :: index_list_name !< integer used as check for "no field"
integer :: subparamindex !< index to identify whether subparams exist for this field
logical :: fm_success !< logical for whether fm_change_list was a success
logical :: subparams !< logical whether subparams exist in this iteration
character(len=FMS_FILE_LEN) :: filename !< Name of the expected field_table.yaml
if (.not.PRESENT(table_name)) then
tbl_name = 'field_table.yaml'
else
tbl_name = trim(table_name)
endif
call get_instance_filename(tbl_name, filename)
if (index(trim(filename), "ens_") .ne. 0) then
if (file_exists(filename) .and. file_exists(tbl_name)) &
call mpp_error(FATAL, "Both "//trim(tbl_name)//" and "//trim(filename)//" exists, pick one!")
!< If the end_* file does not exist, revert back to tbl_name
!! where every ensemble is using the same yaml
if (.not. file_exists(filename)) filename = tbl_name
endif
if (.not. file_exists(trim(filename))) then
if(present(nfields)) nfields = 0
return
endif
! Construct my_table object
call build_fmTable(my_table, trim(filename))
do h=1,size(my_table%types)
do i=1,size(my_table%types(h)%models)
do j=1,size(my_table%types(h)%models(i)%variables)
num_fields = num_fields + 1
end do
end do
end do
allocate(fields(num_fields))
current_field = 0
do h=1,size(my_table%types)
do i=1,size(my_table%types(h)%models)
select case (my_table%types(h)%models(i)%name)
case ('coupler_mod')
model = MODEL_COUPLER
case ('atmos_mod')
model = MODEL_ATMOS
case ('ocean_mod')
model = MODEL_OCEAN
case ('land_mod')
model = MODEL_LAND
case ('ice_mod')
model = MODEL_ICE
case default
call mpp_error(FATAL, trim(error_header)//'The model name is unrecognised : &
&'//trim(my_table%types(h)%models(i)%name))
end select
do j=1,size(my_table%types(h)%models(i)%variables)
current_field = current_field + 1
list_name = list_sep//lowercase(trim(my_table%types(h)%models(i)%name))//list_sep//&
lowercase(trim(my_table%types(h)%name))//list_sep//&
lowercase(trim(my_table%types(h)%models(i)%variables(j)%name))
index_list_name = fm_new_list(list_name, create = .true.)
if ( index_list_name == NO_FIELD ) &
call mpp_error(FATAL, trim(error_header)//'Could not set field list for '//trim(list_name))
fm_success = fm_change_list(list_name)
fields(current_field)%model = model
fields(current_field)%field_name = lowercase(trim(my_table%types(h)%models(i)%variables(j)%name))
fields(current_field)%field_type = lowercase(trim(my_table%types(h)%name))
fields(current_field)%num_methods = size(my_table%types(h)%models(i)%variables(j)%keys)
allocate(fields(current_field)%methods(fields(current_field)%num_methods))
if(fields(current_field)%num_methods.gt.0) then
subparams = (size(my_table%types(h)%models(i)%variables(j)%attributes) .gt. 0)
do k=1,size(my_table%types(h)%models(i)%variables(j)%keys)
fields(current_field)%methods(k)%method_type = &
lowercase(trim(my_table%types(h)%models(i)%variables(j)%keys(k)))
fields(current_field)%methods(k)%method_name = &
lowercase(trim(my_table%types(h)%models(i)%variables(j)%values(k)))
if (.not.subparams) then
call new_name(list_name, my_table%types(h)%models(i)%variables(j)%keys(k),&
my_table%types(h)%models(i)%variables(j)%values(k) )
else
subparamindex=-1
do l=1,size(my_table%types(h)%models(i)%variables(j)%attributes)
if(lowercase(trim(my_table%types(h)%models(i)%variables(j)%attributes(l)%paramname)).eq.&
lowercase(trim(fields(current_field)%methods(k)%method_type))) then
subparamindex = l
exit
end if
end do
if (subparamindex.eq.-1) then
call new_name(list_name, my_table%types(h)%models(i)%variables(j)%keys(k),&
my_table%types(h)%models(i)%variables(j)%values(k) )
else
do m=1,size(my_table%types(h)%models(i)%variables(j)%attributes(subparamindex)%keys)
method_control = " "
subparamvalue = " "
if (trim(my_table%types(h)%models(i)%variables(j)%values(k)).eq.'fm_yaml_null') then
fm_yaml_null = ''
else
fm_yaml_null = trim(my_table%types(h)%models(i)%variables(j)%values(k))//'/'
end if
method_control = trim(my_table%types(h)%models(i)%variables(j)%keys(k))//"/"//&
&trim(fm_yaml_null)//&
&trim(my_table%types(h)%models(i)%variables(j)%attributes(subparamindex)%keys(m))
subparamvalue = trim(my_table%types(h)%models(i)%variables(j)%attributes(subparamindex)%values(m))
call new_name(list_name, method_control, subparamvalue)
end do
end if
end if
end do
end if
end do
end do
end do
if (present(nfields)) nfields = num_fields
end subroutine read_field_table_yaml
!> @brief Subroutine to add new values to list parameters.
!!
!> This subroutine uses input strings list_name, method_name
!! and val_name_in to add new values to the list. Given
!! list_name a new list item is created that is named
!! method_name and is given the value or values in
!! val_name_in. If there is more than 1 value in
!! val_name_in, these values should be comma-separated.
subroutine new_name_yaml ( list_name, method_name_in , val_name_in)
character(len=*), intent(in) :: list_name !< The name of the field that is of interest here.
character(len=*), intent(in) :: method_name_in !< The name of the method that values are
!! being supplied for.
character(len=*), intent(inout) :: val_name_in !< The value or values that will be parsed and
!! used as the value when creating a new field or fields.
character(len=fm_string_len) :: method_name !< name of method to be attached to new list
character(len=fm_string_len) :: val_name !< value name (to be converted to appropriate type)
integer, dimension(:), allocatable :: end_val !< end values in comma separated list
integer, dimension(:), allocatable :: start_val !< start values in comma separated list
integer :: i !< loop index
integer :: index_t !< appending index
integer :: num_elem !< number of elements in comma list
integer :: val_int !< value when converted to integer
integer :: val_type !< value type represented as integer for use in select case
logical :: append_new !< whether or not to append to existing list structure
logical :: val_logic !< value when converted to logical
real(r8_kind) :: val_real !< value when converted to real.
!! All strings will be converted to r8_kind reals.
call strip_front_blanks(val_name_in)
method_name = trim(method_name_in)
call strip_front_blanks(method_name)
index_t = 1
num_elem = 1
append_new = .false.
! If the array of values being passed in is a comma delimited list then count
! the number of elements.
do i = 1, len_trim(val_name_in)
if ( val_name_in(i:i) == comma ) then
num_elem = num_elem + 1
endif
enddo
allocate(start_val(num_elem))
allocate(end_val(num_elem))
start_val(1) = 1
end_val(:) = len_trim(val_name_in)
num_elem = 1
do i = 1, len_trim(val_name_in)
if ( val_name_in(i:i) == comma ) then
end_val(num_elem) = i-1
start_val(num_elem+1) = i+1
num_elem = num_elem + 1
endif
enddo
do i = 1, num_elem
if ( i .gt. 1 .or. index_t .eq. 0 ) then
append_new = .true.
index_t = 0 ! If append is true then index must be <= 0
endif
val_type = string_type ! Assume it is a string
val_name = val_name_in(start_val(i):end_val(i))
call strip_front_blanks(val_name)
if ( scan(val_name(1:1), setnum ) > 0 ) then
if ( scan(val_name, set_nonexp ) .le. 0 ) then
if ( scan(val_name, '.') > 0 .or. scan(val_name, 'e') > 0 .or. scan(val_name, 'E') > 0) then
read(val_name, *) val_real
val_type = real_type
else
read(val_name, *) val_int
val_type = integer_type
endif
endif
endif
if ( len_trim(val_name) == 1 .or. len_trim(val_name) == 3) then
if ( val_name == 't' .or. val_name == 'T' .or. val_name == '.t.' .or. val_name == '.T.' ) then
val_logic = .TRUE.
val_type = logical_type
endif
if ( val_name == 'f' .or. val_name == 'F' .or. val_name == '.f.' .or. val_name == '.F.' ) then
val_logic = .FALSE.
val_type = logical_type
endif
endif
if ( trim(lowercase(val_name)) == 'true' .or. trim(lowercase(val_name)) == '.true.' ) then
val_logic = .TRUE.
val_type = logical_type
endif
if ( trim(lowercase(val_name)) == 'false' .or. trim(lowercase(val_name)) == '.false.' ) then
val_logic = .FALSE.
val_type = logical_type
endif
select case(val_type)
case (integer_type)
if ( fm_new_value( method_name, val_int, create = .true., index = index_t, append = append_new ) < 0 ) &
call mpp_error(FATAL, trim(error_header)//'Could not set "' // trim(val_name) // '" for '//trim(method_name)//&
' (I) for '//trim(list_name))
case (logical_type)
if ( fm_new_value( method_name, val_logic, create = .true., index = index_t, append = append_new) < 0 ) &
call mpp_error(FATAL, trim(error_header)//'Could not set "' // trim(val_name) // '" for '//trim(method_name)//&
' (L) for '//trim(list_name))
case (real_type)
if ( fm_new_value( method_name, val_real, create = .true., index = index_t, append = append_new) < 0 ) &
call mpp_error(FATAL, trim(error_header)//'Could not set "' // trim(val_name) // '" for '//trim(method_name)//&
' (R) for '//trim(list_name))
case (string_type)
if ( fm_new_value( method_name, val_name, create = .true., index = index_t, append = append_new) < 0 ) &
call mpp_error(FATAL, trim(error_header)//'Could not set "' // trim(val_name) // '" for '//trim(method_name)//&
' (S) for '//trim(list_name))
case default
call mpp_error(FATAL, trim(error_header)//'Could not find a valid type to set the '//trim(method_name)//&
' for '//trim(list_name))
end select
enddo
deallocate(start_val)
deallocate(end_val)
end subroutine new_name_yaml
#endif
!> @brief Routine to read and parse the field table yaml
!!
!> This routine reads from a file containing formatted strings.
!! These formatted strings contain information on which schemes are
!! needed within various modules. The field manager does not
!! initialize any of those schemes however. It simply holds the
!! information and is queried by the appropriate module.
subroutine read_field_table_legacy(nfields, table_name)
integer, intent(out), optional :: nfields !< number of fields
character(len=fm_string_len), intent(in), optional :: table_name !< Name of the field table, default
!! is 'field_table'
character(len=1024) :: record
character(len=fm_string_len) :: control_str
character(len=FMS_PATH_LEN) :: list_name
character(len=fm_string_len) :: method_name
character(len=fm_string_len) :: name_str
character(len=fm_string_len) :: type_str
character(len=fm_string_len) :: val_name
character(len=fm_string_len) :: tbl_name
integer :: control_array(MAX_FIELDS,3)
integer :: endcont
integer :: icount
integer :: index_list_name
integer :: iunit
integer :: l
integer :: log_unit
integer :: ltrec
integer :: m
integer :: midcont
integer :: model
integer :: startcont
integer :: io_status
logical :: flag_method
logical :: fm_success
type(field_names_type_short) :: text_names_short
type(field_names_type) :: text_names
type(method_type_short) :: text_method_short
type(method_type) :: text_method
type(method_type_very_short) :: text_method_very_short
if (.not.PRESENT(table_name)) then
tbl_name = 'field_table'
else
tbl_name = trim(table_name)
endif
if (.not. file_exists(trim(tbl_name))) then
if(present(nfields)) nfields = 0
return
endif
allocate(fields(MAX_FIELDS))
open(newunit=iunit, file=trim(tbl_name), action='READ', iostat=io_status)
if(io_status/=0) call mpp_error(FATAL, 'field_manager_mod: Error in opening file '//trim(tbl_name))
!write_version_number should precede all writes to stdlog from field_manager
call write_version_number("FIELD_MANAGER_MOD", version)
log_unit = stdlog()
do while (.TRUE.)
read(iunit,'(a)',end=89,err=99) record
write( log_unit,'(a)' )record
if (record(1:1) == "#" ) cycle
ltrec = LEN_TRIM(record)
if (ltrec .le. 0 ) cycle ! Blank line
icount = 0
do l= 1, ltrec
if (record(l:l) == '"' ) then
icount = icount + 1
endif
enddo
if (icount > 6 ) then
call mpp_error(FATAL,trim(error_header)//'Too many fields in field table header entry.'//trim(record))
endif
select case (icount)
case (6)
read(record,*,end=79,err=79) text_names
text_names%fld_type = lowercase(trim(text_names%fld_type))
text_names%mod_name = lowercase(trim(text_names%mod_name))
text_names%fld_name = lowercase(trim(text_names%fld_name))
case(4)
! If there is no control string then the last string can be omitted and there are only 4 '"' in the record.
read(record,*,end=79,err=79) text_names_short
text_names%fld_type = lowercase(trim(text_names_short%fld_type))
text_names%mod_name = lowercase(trim(text_names_short%mod_name))
text_names%fld_name = lowercase(trim(text_names_short%mod_name))
case(2)
! If there is only the method_type string then the last 2 strings need to be blank and there
! are only 2 '"' in the record.
read(record,*,end=79,err=79) text_names_short
text_names%fld_type = lowercase(trim(text_names_short%fld_type))
text_names%mod_name = lowercase(trim(text_names_short%mod_name))
text_names%fld_name = lowercase(trim(text_names_short%mod_name))
case default
! There is an unterminated or unquoted string in the field table entry.
text_names%fld_type = " "
text_names%mod_name = lowercase(trim(record))
text_names%fld_name = " "
end select
! Create a list with Rick Slaters field manager code
list_name = list_sep//trim(text_names%mod_name)//list_sep//trim(text_names%fld_type)//&
list_sep//trim(text_names%fld_name)
index_list_name = fm_new_list(list_name, create = .true.)
if ( index_list_name == NO_FIELD ) &
call mpp_error(FATAL, trim(error_header)//'Could not set field list for '//trim(list_name))
fm_success = fm_change_list(list_name)
select case (text_names%mod_name)
case ('coupler_mod')
model = MODEL_COUPLER
case ('atmos_mod')
model = MODEL_ATMOS
case ('ocean_mod')
model = MODEL_OCEAN
case ('land_mod')
model = MODEL_LAND
case ('ice_mod')
model = MODEL_ICE
case default
call mpp_error(FATAL, trim(error_header)//'The model name is unrecognised : '//trim(text_names%mod_name))
end select
if (find_field_index(list_name) > 0) then
num_fields = num_fields + 1
if (num_fields > MAX_FIELDS) call mpp_error(FATAL,trim(error_header)//'max fields exceeded')
fields(num_fields)%model = model
fields(num_fields)%field_name = lowercase(trim(text_names%fld_name))
fields(num_fields)%field_type = lowercase(trim(text_names%fld_type))
fields(num_fields)%num_methods = 0
allocate(fields(num_fields)%methods(MAX_FIELD_METHODS))
call check_for_name_duplication
! Check to see that the first line is not the only line
if ( record(LEN_TRIM(record):LEN_TRIM(record)) == list_sep) cycle