-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCLMBuildNamelist.pm
4745 lines (4113 loc) · 210 KB
/
CLMBuildNamelist.pm
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
# build-namelist
#
# This script builds the namelists for CLM
#
# The simplest use of build-namelist is to execute it from the build directory where configure
# was run. By default it will use the config_cache.xml file that was written by configure to
# determine the build time properties of the executable, and will write the files that contain
# the output namelists in that same directory. But if multiple runs are to made using the
# same executable, successive invocations of build-namelist will overwrite previously generated
# namelist files. So generally the best strategy is to invoke build-namelist from the run
# directory and use the -config option to provide the filepath of the config_cache.xml file.
#
#
# Date Contributor Modification
# -------------------------------------------------------------------------------------------
# 2009-01-20 Vertenstein Original version
# 2010-04-27 Kluzek Add ndep streams capability
# 2011-07-25 Kluzek Add multiple ensemble's of namelists
# 2012-03-23 Kluzek Add megan namelist and do checking on it
# 2012-07-01 Kluzek Add some common CESM namelist options
# 2013-12 Andre Refactor everything into subroutines
# 2013-12 Muszala Add Ecosystem Demography functionality
#--------------------------------------------------------------------------------------------
package CLMBuildNamelist;
require 5;
use strict;
#use warnings;
#use diagnostics;
use Cwd qw(getcwd abs_path);
use File::Basename qw(dirname);
use English;
use Getopt::Long;
use IO::File;
use File::Glob ':glob';
#-------------------------------------------------------------------------------
#
# Define a small number of global variables
#
#-------------------------------------------------------------------------------
(my $ProgName = $0) =~ s!(.*)/!!; # name of this script
my $ProgDir = $1;
$ProgName = "CLM " . "$ProgName";
my $cwd = abs_path(getcwd()); # absolute path of the current working directory
my $log; # Log messages object -- will be set in main, declaring it global here means it can be used everywhere
#-------------------------------------------------------------------------------
sub usage {
die <<EOF;
SYNOPSIS
build-namelist [options]
Create the namelist for CLM
REQUIRED OPTIONS
-cimeroot "directory" Path to cime directory
-config "filepath" Read the given CLM configuration cache file.
Default: "config_cache.xml".
-d "directory" Directory where output namelist file will be written
Default: current working directory.
-envxml_dir "directory" Directory name of env_*.xml case files to read in.
(if read they allow user_nl_clm and CLM_BLDNML_OPTS to expand
variables [for example to use \$DIN_LOC_ROOT])
(default current directory)
-lnd_frac "domainfile" Land fraction file (the input domain file)
-res "resolution" Specify horizontal grid. Use nlatxnlon for spectral grids;
dlatxdlon for fv grids (dlat and dlon are the grid cell size
in degrees for latitude and longitude respectively)
"-res list" to list valid resolutions.
(default: 0.9x1.25)
-sim_year "year" Year to simulate for input datasets
(i.e. PtVg, 1850, 2000, 2010, 1850-2000, 1850-2100)
"-sim_year list" to list valid simulation years
(default 2000)
OPTIONS
-bgc "value" Build CLM with BGC package [ sp | cn | bgc | fates ]
(default is sp).
CLM Biogeochemistry mode
sp = Satellite Phenology (SP)
This toggles off the namelist variable: use_cn
cn = Carbon Nitrogen model (CN)
(or CLM45CN if phys=clm4_5/clm5_0)
This toggles on the namelist variable: use_cn
bgc = Carbon Nitrogen with methane, nitrification, vertical soil C,
CENTURY decomposition
(or CLM45BGC if phys=clm4_5/clm5_0)
This toggles on the namelist variables:
use_cn, use_lch4, use_nitrif_denitrif, use_vertsoilc, use_century_decomp
fates = FATES/Ecosystem Demography with below ground BGC
This toggles on the namelist variables:
use_fates, use_vertsoilc, use_century_decomp
(Only for CLM4.5/CLM5.0)
-[no-]chk_res Also check [do NOT check] to make sure the resolution and
land-mask is valid.
-clm_accelerated_spinup "on|off" Setup in a configuration to run as fast as possible for doing a throw-away
simulation in order to get the model to a spun-up state. So do things like
turn off expensive options and setup for a low level of history output.
If CLM4.5/CLM5.0 and bgc it also includes a prognostic Carbon model (cn or bgc)
, also by default turn on Accelerated Decomposition mode which
is controlled by the namelist variable spinup_state.
BGC Spinup for CLM4.5/5.0 Only (for CLM4.0 BGC spinup is controlled from configure)
Turn on given spinup mode for BGC setting of CN
on : Turn on Accelerated Decomposition (spinup_state = 1 or 2)
off : run in normal mode (spinup_state = 0)
Default is set by clm_accelerated_spinup mode.
Spinup is now a two step procedure. First, run the model
with clm_accelerated_spinup = "on". Then run the model for a while with
spinup_state = 0. The exit spinup step happens automatically
on the first timestep when using a restart file from spinup
mode.
The spinup state is saved to the restart file.
If the values match between the model and the restart
file it proceeds as directed.
If the restart file is in spinup mode and the model is in
normal mode, then it performs the exit spinup step
and proceeds in normal mode after that.
If the restart file has normal mode and the model is in
spinup, then it enters spinup. This is useful if you change
a parameter and want to rapidly re-equilibrate without doing
a cold start.
-clm_demand "list" List of variables to require on clm namelist besides the usuals.
"-clm_demand list" to list valid options.
(can include a list member "null" which does nothing)
-clm_start_type "type" Start type of simulation
(default, cold, arb_ic, startup, continue, or branch)
(default=do the default type for this configuration)
(cold=always start with arbitrary initial conditions)
(arb_ic=start with arbitrary initial conditions if
initial conditions do not exist)
(startup=ensure that initial conditions are being used)
-clm_usr_name "name" Dataset resolution/descriptor for personal datasets.
Default: not used
Example: 1x1pt_boulderCO_c090722 to describe location,
number of pts, and date files created
-co2_type "value" Set CO2 the type of CO2 variation to use.
-co2_ppmv "value" Set CO2 concentration to use when co2_type is constant (ppmv).
-crop Toggle for prognostic crop model. (default is off)
(can ONLY be turned on when BGC type is CN or BGC)
This turns on the namelist variable: use_crop
-csmdata "dir" Root directory of CESM input data.
Can also be set by using the CSMDATA environment variable.
-drydep Produce a drydep_inparm namelist that will go into the
"drv_flds_in" file for the driver to pass dry-deposition to the atm.
Default: -no-drydep
(Note: buildnml copies the file for use by the driver)
-dynamic_vegetation Toggle for dynamic vegetation model. (default is off)
(can ONLY be turned on when BGC type is 'cn' or 'bgc')
This turns on the namelist variable: use_cndv
-fire_emis Produce a fire_emis_nl namelist that will go into the
"drv_flds_in" file for the driver to pass fire emissions to the atm.
(Note: buildnml copies the file for use by the driver)
-glc_nec <name> Glacier number of elevation classes [0 | 3 | 5 | 10 | 36]
(default is 0) (standard option with land-ice model is 10)
-help [or -h] Print usage to STDOUT.
-light_res <value> Resolution of lightning dataset to use for CN fire (hcru or T62)
-ignore_ic_date Ignore the date on the initial condition files
when determining what input initial condition file to use.
-ignore_ic_year Ignore just the year part of the date on the initial condition files
when determining what input initial condition file to use.
-ignore_warnings Allow build-namelist to continue, rather than stopping on
warnings
-infile "filepath" Specify a file (or list of files) containing namelists to
read values from.
If used with a CLM build with multiple ensembles (ninst_lnd>1)
and the filename entered is a directory to files of the
form filepath/filepath and filepath/filepath_\$n where \$n
is the ensemble member number. the "filepath/filepath"
input namelist file is the master input namelist file
that is applied to ALL ensemble members.
(by default for CESM this is setup for files of the
form \$CASEDIR/user_nl_clm/user_nl_clm_????)
-inputdata "filepath" Writes out a list containing pathnames for required input datasets in
file specified.
-irrig "value" If .true. week surface datasets with irrigation turned on. (only allowed for CLM4.0 physics)
Default: .false.
(for CLM4.5/CLM5.0 physics set the namelist flag irrigate=.true.)
-l_ncpl "LND_NCPL" Number of CLM coupling time-steps in a day.
-lnd_tuning_mode "value" Use the parameters tuned for the given configuration (CLM version and atmospheric forcing)
-mask "landmask" Type of land-mask (default, navy, gx3v5, gx1v5 etc.)
"-mask list" to list valid land masks.
-namelist "namelist" Specify namelist settings directly on the commandline by supplying
a string containing FORTRAN namelist syntax, e.g.,
-namelist "&clm_inparm dt=1800 /"
-no-megan DO NOT PRODUCE a megan_emis_nl namelist that will go into the
"drv_flds_in" file for the driver to pass VOCs to the atm.
MEGAN (Model of Emissions of Gases and Aerosols from Nature)
(Note: buildnml copies the file for use by the driver)
-[no-]note Add note to output namelist [do NOT add note] about the
arguments to build-namelist.
-output_reals <file> Output real parameters to the given output file.
-ssp_rcp "value" Shared Socioeconomic Pathway (SSP) and
Representative Concentration Pathway (RCP) combination to use for
future scenarios.
"-ssp_rcp list" to list valid ssp_rcp settings.
-s Turns on silent mode - only fatal messages issued.
-test Enable checking that input datasets exist on local filesystem.
-use_case "case" Specify a use case which will provide default values.
"-use_case list" to list valid use-cases.
-verbose [or -v] Turn on verbose echoing of informational messages.
-version Echo the SVN tag name used to check out this CLM distribution.
-vichydro Toggle to turn on VIC hydrologic parameterizations (default is off)
This turns on the namelist variable: use_vichydro
Note: The precedence for setting the values of namelist variables is (highest to lowest):
0. namelist values set by specific command-line options, like, -d, -sim_year
(i.e. compset choice and CLM_BLDNML_OPTS, CLM_ACCELERATED_SPINUP, LND_TUNING_MODE env_run variables)
(NOTE: If you try to contradict these settings by methods below, an error will be triggered)
1. values set on the command-line using the -namelist option,
(i.e. CLM_NAMELIST_OPTS env_run variable)
2. values read from the file(s) specified by -infile,
(i.e. user_nl_clm files)
3. datasets from the -clm_usr_name option,
(i.e. CLM_USRDAT_NAME env_run variable)
4. values set from a use-case scenario, e.g., -use_case
(i.e. CLM_NML_USE_CASE env_run variable)
5. values from the namelist defaults file.
EOF
}
#-------------------------------------------------------------------------------
sub process_commandline {
# Process command-line options and return the hash
my ($nl_flags) = @_;
# Save the command line arguments to the script. NOTE: this must be
# before GetOptions() is called because items are removed from from
# the array!
$nl_flags->{'cmdline'} = "@ARGV";
my %opts = ( cimeroot => undef,
config => "config_cache.xml",
csmdata => undef,
clm_usr_name => undef,
co2_type => undef,
co2_ppmv => undef,
clm_demand => "null",
help => 0,
glc_nec => "default",
light_res => "default",
l_ncpl => undef,
lnd_tuning_mode => "default",
lnd_frac => undef,
dir => "$cwd",
ssp_rcp => "default",
sim_year => "default",
clm_accelerated_spinup=> "default",
chk_res => undef,
note => undef,
drydep => 0,
output_reals_filename => undef,
fire_emis => 0,
megan => "default",
irrig => "default",
res => "default",
silent => 0,
ignore_warnings => 0,
mask => "default",
test => 0,
bgc => "default",
crop => 0,
dynamic_vegetation => 0,
envxml_dir => ".",
vichydro => 0,
maxpft => "default",
);
GetOptions(
"cimeroot=s" => \$opts{'cimeroot'},
"clm_demand=s" => \$opts{'clm_demand'},
"co2_ppmv=f" => \$opts{'co2_ppmv'},
"co2_type=s" => \$opts{'co2_type'},
"config=s" => \$opts{'config'},
"csmdata=s" => \$opts{'csmdata'},
"clm_usr_name=s" => \$opts{'clm_usr_name'},
"envxml_dir=s" => \$opts{'envxml_dir'},
"drydep!" => \$opts{'drydep'},
"fire_emis!" => \$opts{'fire_emis'},
"ignore_warnings!" => \$opts{'ignore_warnings'},
"chk_res!" => \$opts{'chk_res'},
"note!" => \$opts{'note'},
"megan!" => \$opts{'megan'},
"glc_nec=i" => \$opts{'glc_nec'},
"light_res=s" => \$opts{'light_res'},
"irrig=s" => \$opts{'irrig'},
"d:s" => \$opts{'dir'},
"h|help" => \$opts{'help'},
"ignore_ic_date" => \$opts{'ignore_ic_date'},
"ignore_ic_year" => \$opts{'ignore_ic_year'},
"infile=s" => \$opts{'infile'},
"lnd_frac=s" => \$opts{'lnd_frac'},
"lnd_tuning_mode=s" => \$opts{'lnd_tuning_mode'},
"l_ncpl=i" => \$opts{'l_ncpl'},
"inputdata=s" => \$opts{'inputdata'},
"mask=s" => \$opts{'mask'},
"namelist=s" => \$opts{'namelist'},
"res=s" => \$opts{'res'},
"ssp_rcp=s" => \$opts{'ssp_rcp'},
"s|silent" => \$opts{'silent'},
"sim_year=s" => \$opts{'sim_year'},
"output_reals=s" => \$opts{'output_reals_filename'},
"clm_accelerated_spinup=s" => \$opts{'clm_accelerated_spinup'},
"clm_start_type=s" => \$opts{'clm_start_type'},
"test" => \$opts{'test'},
"use_case=s" => \$opts{'use_case'},
"bgc=s" => \$opts{'bgc'},
"crop!" => \$opts{'crop'},
"dynamic_vegetation" => \$opts{'dynamic_vegetation'},
"vichydro" => \$opts{'vichydro'},
"maxpft=i" => \$opts{'maxpft'},
"v|verbose" => \$opts{'verbose'},
"version" => \$opts{'version'},
) or usage();
# Give usage message.
usage() if $opts{'help'};
# Check for unparsed arguments
if (@ARGV) {
print "ERROR: unrecognized arguments: @ARGV\n";
usage();
}
return %opts;
}
#-------------------------------------------------------------------------------
sub check_for_perl_utils {
my $cfgdir = shift;
my $opts_ref = shift;
# Determine CIME root directory and perl5lib root directory
my $cimeroot = $opts_ref->{'cimeroot'};
if ( ! defined($cimeroot) ) {
$cimeroot = "$cfgdir/../cime";
if ( -d $cimeroot ) {
} elsif ( -d "$cfgdir/../../../cime" ) {
$cimeroot = "$cfgdir/../../../cime";
} else {
die <<"EOF";
** Cannot find the root of the cime directory enter it using the -cimeroot option
Did you run the checkout_externals scripts?
EOF
}
}
my $perl5lib_dir = "$cimeroot/utils/perl5lib";
#-----------------------------------------------------------------------------
# Add $perl5lib_dir to the list of paths that Perl searches for modules
my @dirs = ( $ProgDir, $cfgdir, "$perl5lib_dir");
unshift @INC, @dirs;
require config_files::clm_phys_vers;
require namelist_files::LogMessages;
my $locallog = namelist_files::LogMessages->new( $ProgName, $opts_ref );
# The XML::Lite module is required to parse the XML files.
(-f "$perl5lib_dir/XML/Lite.pm") or
$locallog->fatal_error("Cannot find perl module \"XML/Lite.pm\" in directory\n" .
"\"$perl5lib_dir\"");
# The Build::Config module provides utilities to access the configuration information
# in the config_cache.xml file
(-f "$perl5lib_dir/Build/Config.pm") or
$locallog->fatal_error("Cannot find perl module \"Build/Config.pm\" in directory\n" .
"\"$perl5lib_dir\"");
# The Build::NamelistDefinition module provides utilities to validate that the output
# namelists are consistent with the namelist definition file
(-f "$perl5lib_dir/Build/NamelistDefinition.pm") or
$locallog->fatal_error("Cannot find perl module \"Build/NamelistDefinition.pm\" in directory\n" .
"\"$perl5lib_dir\"");
# The Build::NamelistDefaults module provides a utility to obtain default values of namelist
# variables based on finding a best fit with the attributes specified in the defaults file.
(-f "$perl5lib_dir/Build/NamelistDefaults.pm") or
$locallog->fatal_error("Cannot find perl module \"Build/NamelistDefaults.pm\" in directory\n" .
"\"$perl5lib_dir\"");
# The Build::Namelist module provides utilities to parse input namelists, to query and modify
# namelists, and to write output namelists.
(-f "$perl5lib_dir/Build/Namelist.pm") or
$locallog->fatal_error("Cannot find perl module \"Build/Namelist.pm\" in directory\n" .
"\"$perl5lib_dir\"");
# required cesm perl modules
require XML::Lite;
require Build::Config;
require Build::NamelistDefinition;
require Build::NamelistDefaults;
require Build::Namelist;
require Config::SetupTools;
}
#-------------------------------------------------------------------------------
sub read_configure_definition {
# Read the configure definition and specific config_cache file for this case
# configure are the build-time settings for CLM
my ($cfgdir, $opts) = @_;
$log->verbose_message("Setting CLM configuration script directory to $cfgdir");
# Create a configuration object from the default config_definition file
my $configfile;
if ( -f $opts->{'config'} ) {
$configfile = $opts->{'config'};
} else {
$configfile = "$cfgdir/config_files/config_definition.xml";
}
# Check that configuration cache file exists.
$log->verbose_message("Using CLM configuration cache file $opts->{'config'}");
if ( $configfile ne $opts->{'config'} ) {
$log->fatal_error("Cannot find configuration cache file: \"$opts->{'config'}\"");
}
my $cfg = Build::Config->new("$configfile");
return $cfg;
}
#-----------------------------------------------------------------------------------------------
sub read_namelist_definition {
my ($cfgdir, $opts, $nl_flags, $physv) = @_;
# The namelist definition file contains entries for all namelist
# variables that can be output by build-namelist.
my $phys = $physv->as_filename( );
my @nl_definition_files = ( "$cfgdir/namelist_files/namelist_definition_drv.xml",
"$cfgdir/namelist_files/namelist_definition_drv_flds.xml",
"$cfgdir/namelist_files/namelist_definition_$phys.xml" );
foreach my $nl_defin_file ( @nl_definition_files ) {
(-f "$nl_defin_file") or $log->fatal_error("Cannot find namelist definition file \"$nl_defin_file\"");
$log->verbose_message("Using namelist definition file $nl_defin_file");
}
# Create a namelist definition object. This object provides a
# method for verifying that the output namelist variables are in the
# definition file, and are output in the correct namelist groups.
my $definition = Build::NamelistDefinition->new( shift(@nl_definition_files) );
foreach my $nl_defin_file ( @nl_definition_files ) {
$definition->add( "$nl_defin_file" );
}
return $definition;
}
#-----------------------------------------------------------------------------------------------
sub read_envxml_case_files {
# read the contents of the env*.xml files in the case directory
my ($opts) = @_;
my %envxml = ();
if ( defined($opts->{'envxml_dir'}) ) {
(-d $opts->{'envxml_dir'}) or $log->fatal_error( "envxml_dir is not a directory" );
my @files = glob( $opts->{'envxml_dir'}."/env_*xml" );
($#files >= 0) or $log->fatal_error( "there are no env_*xml files in the envxml_dir" );
foreach my $file (@files) {
$log->verbose_message( "Open env.xml file: $file" );
my $xml = XML::Lite->new( "$file" );
my @e = $xml->elements_by_name('entry');
while ( my $e = shift @e ) {
my %a = $e->get_attributes();
$envxml{$a{'id'}} = $a{'value'};
}
}
foreach my $attr (keys %envxml) {
if ( $envxml{$attr} =~ m/\$/ ) {
$envxml{$attr} = SetupTools::expand_xml_var( $envxml{$attr}, \%envxml );
}
}
} else {
$log->fatal_error( "The -envxml_dir option was NOT given and it is a REQUIRED option" );
}
return( %envxml );
}
#-----------------------------------------------------------------------------------------------
sub read_namelist_defaults {
my ($cfgdir, $opts, $nl_flags, $cfg, $physv) = @_;
my $phys = $physv->as_filename( );
# The namelist defaults file contains default values for all required namelist variables.
my @nl_defaults_files = ( "$cfgdir/namelist_files/namelist_defaults_overall.xml",
"$cfgdir/namelist_files/namelist_defaults_$phys.xml",
"$cfgdir/namelist_files/namelist_defaults_drv.xml",
"$cfgdir/namelist_files/namelist_defaults_fire_emis.xml",
"$cfgdir/namelist_files/namelist_defaults_drydep.xml" );
# Add the location of the use case defaults files to the options hash
$opts->{'use_case_dir'} = "$cfgdir/namelist_files/use_cases";
if (defined $opts->{'use_case'}) {
if ( $opts->{'use_case'} ne "list" ) {
unshift( @nl_defaults_files, "$opts->{'use_case_dir'}/$opts->{'use_case'}.xml" );
}
}
foreach my $nl_defaults_file ( @nl_defaults_files ) {
(-f "$nl_defaults_file") or $log->fatal_error("Cannot find namelist defaults file \"$nl_defaults_file\"");
$log->verbose_message("Using namelist defaults file $nl_defaults_file");
}
# Create a namelist defaults object. This object provides default
# values for variables contained in the input defaults file. The
# configuration object provides attribute values that are relevent
# for the CLM executable for which the namelist is being produced.
my $defaults = Build::NamelistDefaults->new( shift( @nl_defaults_files ), $cfg);
foreach my $nl_defaults_file ( @nl_defaults_files ) {
$defaults->add( "$nl_defaults_file" );
}
return $defaults;
}
#-------------------------------------------------------------------------------
sub check_cesm_inputdata {
# Check that the CESM inputdata root directory has been specified. This must be
# a local or nfs mounted directory.
my ($opts, $nl_flags) = @_;
$nl_flags->{'inputdata_rootdir'} = undef;
if (defined($opts->{'csmdata'})) {
$nl_flags->{'inputdata_rootdir'} = $opts->{'csmdata'};
}
elsif (defined $ENV{'CSMDATA'}) {
$nl_flags->{'inputdata_rootdir'} = $ENV{'CSMDATA'};
}
else {
$log->fatal_error("CESM inputdata root directory must be specified by either -csmdata\n" .
"argument or by the CSMDATA environment variable.");
}
if ( ! defined($ENV{'DIN_LOC_ROOT'}) ) {
$ENV{'DIN_LOC_ROOT'} = $nl_flags->{'inputdata_rootdir'};
}
if ($opts->{'test'}) {
(-d $nl_flags->{'inputdata_rootdir'}) or $log->fatal_error("CESM inputdata root is not a directory: \"$nl_flags->{'inputdata_rootdir'}\"");
}
$log->verbose_message("CESM inputdata root directory: $nl_flags->{'inputdata_rootdir'}");
}
#-------------------------------------------------------------------------------
sub process_namelist_user_input {
# Process the user input in general by order of precedence. At each point
# we'll only add new values to the namelist and not overwrite
# previously specified specified values which have higher
# precedence. The one exception to this rule are the specifc command-line
# options which are done last as if the user contradicts these settings
# CLM build-namelist will abort with an error.
#
# 1. values set on the command-line using the -namelist option,
# (i.e. CLM_NAMELIST_OPTS env_run variable)
# 2. values read from the file(s) specified by -infile,
# (i.e. user_nl_clm files)
# After the above are done the command line options are processed and they
# are made sure the user hasn't contradicted any of their settings with
# anything above. Because of this they are condsidered to have the highest
# precedence.
# 0. namelist values set by specific command-line options, like, -d, -sim_year
# (i.e. CLM_BLDNML_OPTS env_run variable)
# The results of these are needed for the final two user input
# 3. datasets from the -clm_usr_name option,
# (i.e. CLM_USRDAT_NAME env_run variable)
# 4. values set from a use-case scenario, e.g., -use_case
# (i.e. CLM_NML_USE_CASE env_run variable)
#
# Finally after all the above is done, the defaults are found from the
# namelist defaults file (outside of this routine).
#
my ($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $envxml_ref, $physv) = @_;
# Get the inputs that will be coming from the user...
process_namelist_commandline_namelist($opts, $definition, $nl, $envxml_ref);
process_namelist_commandline_infile($opts, $definition, $nl, $envxml_ref);
# Apply the commandline options and make sure the user didn't change it above
process_namelist_commandline_options($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv);
# The last two process command line arguments for usr_name and use_case
# They require that process_namelist_commandline_options was called before this
process_namelist_commandline_clm_usr_name($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $envxml_ref);
process_namelist_commandline_use_case($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $envxml_ref, $physv);
# Set the start_type by the command line setting for clm_start_type
process_namelist_commandline_clm_start_type($opts, $nl_flags, $definition, $defaults, $nl);
}
#-------------------------------------------------------------------------------
sub process_namelist_commandline_options {
# First process the commandline args that provide specific namelist values.
#
# First get the command-line specified overall values or their defaults
# Obtain default values for the following build-namelist input arguments
# : res, mask, ssp_rcp, sim_year, sim_year_range, and clm_accelerated_spinup.
#
# NOTE: cfg only needs to be passed to functions that work with
# clm4_0 compile time functionality!
my ($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv) = @_;
setup_cmdl_chk_res($opts, $defaults);
setup_cmdl_resolution($opts, $nl_flags, $definition, $defaults);
setup_cmdl_mask($opts, $nl_flags, $definition, $defaults, $nl);
setup_cmdl_bgc($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv);
setup_cmdl_fire_light_res($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv);
setup_cmdl_spinup($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv);
setup_cmdl_crop($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv);
setup_cmdl_maxpft($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv);
setup_cmdl_glc_nec($opts, $nl_flags, $definition, $defaults, $nl);
setup_cmdl_irrigation($opts, $nl_flags, $definition, $defaults, $nl, $physv);
setup_cmdl_ssp_rcp($opts, $nl_flags, $definition, $defaults, $nl);
setup_cmdl_simulation_year($opts, $nl_flags, $definition, $defaults, $nl);
setup_cmdl_dynamic_vegetation($opts, $nl_flags, $definition, $defaults, $nl, $physv);
setup_cmdl_fates_mode($opts, $nl_flags, $definition, $defaults, $nl, $physv);
setup_cmdl_vichydro($opts, $nl_flags, $definition, $defaults, $nl, $physv);
setup_cmdl_run_type($opts, $nl_flags, $definition, $defaults, $nl);
setup_cmdl_output_reals($opts, $nl_flags, $definition, $defaults, $nl, $physv);
setup_logic_lnd_tuning($opts, $nl_flags, $definition, $defaults, $nl, $physv);
}
#-------------------------------------------------------------------------------
sub setup_cmdl_chk_res {
my ($opts, $defaults) = @_;
my $var = "chk_res";
if ( ! defined($opts->{$var}) ) {
$opts->{$var} = $defaults->get_value($var);
}
}
sub setup_cmdl_resolution {
my ($opts, $nl_flags, $definition, $defaults) = @_;
my $var = "res";
my $val;
if ( $opts->{$var} ne "default" ) {
$val = $opts->{$var};
} else {
$val= $defaults->get_value($var);
}
$nl_flags->{'res'} = $val;
$log->verbose_message("CLM atm resolution is $nl_flags->{'res'}");
$opts->{$var} = $val;
if ( $opts->{'chk_res'} ) {
$val = "e_string( $nl_flags->{'res'} );
if ( ! $definition->is_valid_value( $var, $val ) ) {
my @valid_values = $definition->get_valid_values( $var );
if ( ! defined($opts->{'clm_usr_name'}) || $nl_flags->{'res'} ne $opts->{'clm_usr_name'} ) {
$log->fatal_error("$var has a value ($val) that is NOT valid. Valid values are: @valid_values");
}
}
}
}
#-------------------------------------------------------------------------------
sub setup_cmdl_mask {
my ($opts, $nl_flags, $definition, $defaults, $nl) = @_;
my $var = "mask";
my $val;
if ( $opts->{$var} ne "default" ) {
$val = $opts->{$var};
} else {
my %tmp = ( 'hgrid'=>$nl_flags->{'res'} );
$val = $defaults->get_value($var, \%tmp );
}
$nl_flags->{'mask'} = $val;
$opts->{'mask'} = $nl_flags->{'mask'};
if ( $opts->{'chk_res'} ) {
$val = "e_string( $val );
my $group = $definition->get_group_name($var);
$nl->set_variable_value($group, $var, $val);
if ( ! $definition->is_valid_value( $var, $val ) ) {
my @valid_values = $definition->get_valid_values( $var );
$log->fatal_error("$var has a value ($val) that is NOT valid. Valid values are: @valid_values");
}
}
$log->verbose_message("CLM land mask is $nl_flags->{'mask'}");
}
#-------------------------------------------------------------------------------
sub setup_cmdl_fates_mode {
#
# call this at least after crop check is called
#
my ($opts, $nl_flags, $definition, $defaults, $nl, $physv) = @_;
my $val;
my $var = "bgc_mode";
if ( $physv->as_long() == $physv->as_long("clm4_0") || $nl_flags->{'crop'} eq "on" ) {
if ( $nl_flags->{$var} eq "fates" ) {
# ED is not a clm4_0 option and should not be used with crop and not with clm4_0
$log->fatal_error("** Cannot turn fates mode on with crop or with clm4_0 physics." );
}
} elsif ($nl_flags->{"bgc_mode"} eq "fates" && ! &value_is_true($nl_flags->{"use_fates"}) ) {
$log->fatal_error("DEV_ERROR: internal logic error: bgc_mode = fates and use_fates = false.");
} else {
$var = "use_fates";
if ( &value_is_true($nl_flags->{$var}) ) {
# This section is a place-holder to test for modules that are not allowed with ED
# the defaults which are set in the logic section of the namelist builder will
# automatically set these correctly (well that is the assumption), but here we
# want to set a catch to fail and warn users if they explicitly set incompatible user namelist
# options
# my $var = "use_somevar";
# $val = $nl_flags->{$var};
# if ( defined($nl->get_value($var)) ) {
# if ( &value_is_true($nl->get_value($var)) ) {
# $log->fatal_error("$var was set to .true., which is incompatible when -bgc fates option is used.");
# }
# }
# The following variables may be set by the user and are compatible with use_fates
# no need to set defaults, covered in a different routine
my @list = ( "use_vertsoilc", "use_century_decomp", "use_lch4" );
foreach my $var ( @list ) {
if ( defined($nl->get_value($var)) ) {
$nl_flags->{$var} = $nl->get_value($var);
$val = $nl_flags->{$var};
my $group = $definition->get_group_name($var);
$nl->set_variable_value($group, $var, $val);
if ( ! $definition->is_valid_value( $var, $val ) ) {
my @valid_values = $definition->get_valid_values( $var );
$log->fatal_error("$var has a value ($val) that is NOT valid. Valid values are: @valid_values");
}
}
}
} else {
# dis-allow fates specific namelist items with non-fates runs
my @list = ( "use_fates_spitfire", "use_fates_planthydro", "use_fates_ed_st3", "use_fates_ed_prescribed_phys",
"use_fates_inventory_init", "fates_inventory_ctrl_filename","use_fates_logging","fates_parteh_mode" );
foreach my $var ( @list ) {
if ( defined($nl->get_value($var)) ) {
$log->fatal_error("$var is being set, but can ONLY be set when -bgc fates option is used.\n");
}
}
}
}
}
#-------------------------------------------------------------------------------
sub setup_cmdl_bgc {
# BGC - alias for group of biogeochemistry related use_XXX namelists
my ($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv) = @_;
my $val;
my $var = "bgc";
$val = $opts->{$var};
$nl_flags->{'bgc_mode'} = $val;
if ( $physv->as_long() == $physv->as_long("clm4_0") ) {
if ( $nl_flags->{'bgc_mode'} ne "default" ) {
$log->fatal_error("-bgc option used with clm4_0 physics. -bgc can ONLY be used with clm4_5/clm5_0 physics");
}
$nl_flags->{'bgc_mode'} = $cfg->get($var);
} else {
my $var = "bgc_mode";
if ( $nl_flags->{$var} eq "default" ) {
$nl_flags->{$var} = $defaults->get_value($var);
}
my $group = $definition->get_group_name($var);
$nl->set_variable_value($group, $var, quote_string( $nl_flags->{$var} ) );
if ( ! $definition->is_valid_value( $var, quote_string( $nl_flags->{$var}) ) ) {
my @valid_values = $definition->get_valid_values( $var );
$log->fatal_error("$var has a value (".$nl_flags->{$var}.") that is NOT valid. Valid values are: @valid_values");
}
$log->verbose_message("Using $nl_flags->{$var} for bgc.");
# now set the actual name list variables based on the bgc alias
if ($nl_flags->{$var} eq "cn" ) {
$nl_flags->{'use_cn'} = ".true.";
$nl_flags->{'use_fates'} = ".false.";
} elsif ($nl_flags->{$var} eq "bgc" ) {
$nl_flags->{'use_cn'} = ".true.";
$nl_flags->{'use_fates'} = ".false.";
} elsif ($nl_flags->{$var} eq "fates" ) {
$nl_flags->{'use_cn'} = ".false.";
$nl_flags->{'use_fates'} = ".true.";
} else {
$nl_flags->{'use_cn'} = ".false.";
$nl_flags->{'use_fates'} = ".false.";
}
if ( defined($nl->get_value("use_cn")) && ($nl_flags->{'use_cn'} ne $nl->get_value("use_cn")) ) {
$log->fatal_error("The namelist variable use_cn is inconsistent with the -bgc option");
}
if ( defined($nl->get_value("use_fates")) && ($nl_flags->{'use_fates'} ne $nl->get_value("use_fates")) ) {
$log->fatal_error("The namelist variable use_fates is inconsistent with the -bgc option");
}
{
# If the variable has already been set use it, if not set to the value defined by the bgc_mode
my @list = ( "use_lch4", "use_nitrif_denitrif", "use_vertsoilc", "use_century_decomp" );
my $ndiff = 0;
my %settings = ( 'bgc_mode'=>$nl_flags->{'bgc_mode'} );
foreach my $var ( @list ) {
my $default_setting = $defaults->get_value($var, \%settings );
if ( ! defined($nl->get_value($var)) ) {
$nl_flags->{$var} = $default_setting;
} else {
if ( $nl->get_value($var) ne $default_setting ) {
$ndiff += 1;
}
$nl_flags->{$var} = $nl->get_value($var);
}
$val = $nl_flags->{$var};
my $group = $definition->get_group_name($var);
$nl->set_variable_value($group, $var, $val);
if ( ! $definition->is_valid_value( $var, $val ) ) {
my @valid_values = $definition->get_valid_values( $var );
$log->fatal_error("$var has a value ($val) that is NOT valid. Valid values are: @valid_values");
}
}
# If all the variables are different report it as an error
if ( $ndiff == ($#list + 1) ) {
$log->fatal_error("You are contradicting the -bgc setting with the namelist variables: @list" );
}
}
# Now set use_cn and use_fates
foreach $var ( "use_cn", "use_fates" ) {
$val = $nl_flags->{$var};
$group = $definition->get_group_name($var);
$nl->set_variable_value($group, $var, $val);
if ( ! $definition->is_valid_value( $var, $val ) ) {
my @valid_values = $definition->get_valid_values( $var );
$log->fatal_error("$var has a value ($val) that is NOT valid. Valid values are: @valid_values");
}
}
}
if ( $physv->as_long() >= $physv->as_long("clm4_5") ) {
my $var = "use_fun";
if ( ! defined($nl->get_value($var)) ) {
add_default($opts, $nl_flags->{'inputdata_rootdir'}, $definition, $defaults, $nl, $var,
'phys'=>$nl_flags->{'phys'}, 'use_cn'=>$nl_flags->{'use_cn'},
'use_nitrif_denitrif'=>$nl_flags->{'use_nitrif_denitrif'} );
}
if ( (! &value_is_true($nl_flags->{'use_nitrif_denitrif'}) ) && &value_is_true($nl->get_value('use_fun')) ) {
$log->fatal_error("When FUN is on, use_nitrif_denitrif MUST also be on!");
}
}
} # end bgc
#-------------------------------------------------------------------------------
sub setup_cmdl_fire_light_res {
# light_res - alias for lightning resolution
my ($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv) = @_;
my $var = "light_res";
my $val = $opts->{$var};
if ( $physv->as_long() == $physv->as_long("clm4_0") ) {
if ( $val !~ /default|none/ ) {
$log->fatal_error("-$var option used with clm4_0 physics. -$var can ONLY be used with clm4_5/clm5_0 physics");
}
} else {
if ( $val eq "default" ) {
$nl_flags->{$var} = remove_leading_and_trailing_quotes($defaults->get_value($var));
} else {
my $fire_method = remove_leading_and_trailing_quotes( $nl->get_value('fire_method') );
if ( defined($fire_method) && $val ne "none" ) {
if ( $fire_method eq "nofire" ) {
$log->fatal_error("-$var option used with fire_method='nofire'. -$var can ONLY be used without the nofire option");
}
}
my $stream_fldfilename_lightng = remove_leading_and_trailing_quotes( $nl->get_value('stream_fldfilename_lightng') );
if ( defined($stream_fldfilename_lightng) && $val ne "none" ) {
$log->fatal_error("-$var option used while also explicitly setting stream_fldfilename_lightng filename which is a contradiction. Use one or the other not both.");
}
if ( ! &value_is_true($nl->get_value('use_cn')) ) {
$log->fatal_error("-$var option used CN is NOT on. -$var can only be used when CN is on (with bgc: cn or bgc)");
}
if ( &value_is_true($nl->get_value('use_cn')) && $val eq "none" ) {
$log->fatal_error("-$var option is set to none, but CN is on (with bgc: cn or bgc) which is a contradiction");
}
$nl_flags->{$var} = $val;
}
my $group = $definition->get_group_name($var);
$nl->set_variable_value($group, $var, quote_string($nl_flags->{$var}) );
if ( ! $definition->is_valid_value( $var, $nl_flags->{$var}, 'noquotes'=>1 ) ) {
my @valid_values = $definition->get_valid_values( $var );
$log->fatal_error("$var has a value (".$nl_flags->{$var}.") that is NOT valid. Valid values are: @valid_values");
}
$log->verbose_message("Using $nl_flags->{$var} for $var.");
#
# Set flag if cn-fires are on or not
#
$var = "cnfireson";
if ( $physv->as_long() >= $physv->as_long("clm4_5") && &value_is_true($nl->get_value('use_cn')) ) {
add_default($opts, $nl_flags->{'inputdata_rootdir'}, $definition, $defaults, $nl, 'fire_method');
}
my $fire_method = remove_leading_and_trailing_quotes( $nl->get_value('fire_method') );
if ( defined($fire_method) && ! &value_is_true($nl_flags->{'use_cn'}) ) {
$log->fatal_error("fire_method is being set even though bgc is NOT cn or bgc.");
}
if ( defined($fire_method) && $fire_method eq "nofire" ) {
$nl_flags->{$var} = ".false.";
} elsif ( &value_is_true($nl->get_value('use_cn')) ) {
$nl_flags->{$var} = ".true.";
} else {
$nl_flags->{$var} = ".false.";
}
}
}
#-------------------------------------------------------------------------------
sub setup_cmdl_crop {
my ($opts, $nl_flags, $definition, $defaults, $nl, $cfg, $physv) = @_;
$nl_flags->{'use_crop'} = ".false.";
my $val;
my $var = "crop";
if ( $physv->as_long() == $physv->as_long("clm4_0") ) {
$nl_flags->{'crop'} = $cfg->get($var);
if ( $nl_flags->{'crop'} eq "on" ) {
$nl_flags->{'use_crop'} = ".true.";
}
} else {
$val = $opts->{$var};
$nl_flags->{'crop'} = $val;
if ( $nl_flags->{'crop'} eq 1 ) {
$nl_flags->{'use_crop'} = ".true.";
}
if ( defined($nl->get_value("use_crop")) && ($nl_flags->{'use_crop'} ne $nl->get_value("use_crop")) ) {
$log->fatal_error("Namelist item use_crop contradicts the command-line option -crop, use the command line option");
}
if ( ($nl_flags->{'crop'} eq 1 ) && ($nl_flags->{'bgc_mode'} eq "sp") ) {
$log->fatal_error("** Cannot turn crop mode on mode bgc=sp\n" .
"**\n" .
"** Set the bgc mode to 'cn' or 'bgc' by the following means from highest to lowest precedence:\n" .
"** * by the command-line options -bgc cn\n" .
"** * by a default configuration file, specified by -defaults");
}
$var = "use_crop";
$val = ".false.";
if ($nl_flags->{'crop'} eq 1) {
$val = ".true.";
}
my $group = $definition->get_group_name($var);
$nl->set_variable_value($group, $var, $val);
if ( ! $definition->is_valid_value( $var, $val ) ) {
my @valid_values = $definition->get_valid_values( $var );
$log->fatal_error("$var has a value ($val) that is NOT valid. Valid values are: @valid_values");
}
}
}
#-------------------------------------------------------------------------------