forked from StormSurgeLive/asgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol_file_gen.pl
executable file
·1234 lines (1223 loc) · 47.6 KB
/
control_file_gen.pl
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
#!/usr/bin/env perl
#--------------------------------------------------------------------------
# control_file_gen.pl
#
# This script uses the template fort.15 file and the ATCF formatted fort.22
# file as input and produces a fort.15 file as output. The name of the template
# file and the fort.22 file to be used as input must be specified on the
# command line.
#
# It optionally accepts the csdate (YYYYMMDDHH24), that is, the
# calendar time that corresponds to t=0 in simulation time. If it is
# not provided, the first line in the fort.22 file is used as the cold start
# time, and this time is written to stdout.
#
# It optionally accepts the time in a hotstart file in seconds since cold
# start.
#
# If the time of a hotstart file has been supplied, the fort.15 file
# will be set to hotstart.
#
# It optionally accepts the end time (YYYYMMDDHH24) at which the simulation
# should stop (e.g., if it has gone too far inland to continue to be
# of interest).
#
# If the --name option is set to nowcast, the RNDAY will be calculated such
# that the run will end at the nowcast time.
#
# The --dt option can be used to specify the time step size if it is
# different from the default of 3.0 seconds.
#
# The --bladj option can be used to specify the Boundary Layer Adjustment
# parameter for the Holland model (not used by the asymmetric wind vortex
# model, NWS=9.
#
# The NHSINC will be calculated such that a hotstart file is always generated
# on the last time step of the run.
#
# usage:
# %perl control_file_gen.pl [--cst csdate] [--hst hstime]
# [--dt timestep] [--nowcast] [--controltemplate templatefile] < storm1_fort.22
#
#--------------------------------------------------------------------------
# Copyright(C) 2006--2016 Jason Fleming
# Copyright(C) 2006, 2007 Brett Estrade
#
# This file is part of the ADCIRC Surge Guidance System (ASGS).
#
# The ASGS is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# ASGS 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 General Public License
# along with the ASGS. If not, see <http://www.gnu.org/licenses/>.
#--------------------------------------------------------------------------
#
#jgf20120124: standalone usage example for making a fort.15 for the
# ec95d mesh. It was for a tides-only run.
#perl ~/asgs/trunk/control_file_gen.pl --controltemplate ~/asgs/trunk/input/ec_95_fort.15_template --gridname fort.14 --cst 2012010100 --endtime 7 --dt 30.0 --nws 0 --scriptdir ~/asgs/trunk --hsformat binary --stormdir . --name hindcast --fort63freq 3600.0
$^W++;
use strict;
use Getopt::Long;
use Date::Pcalc;
use Cwd;
#
my $fort61freq=0; # output frequency in SECONDS
my $fort61append; # if defined, output files will be appended across hotstarts
my $fort62freq=0; # output frequency in SECONDS
my $fort62append; # if defined, output files will be appended across hotstarts
my $fort63freq=0; # output frequency in SECONDS
my $fort63append; # if defined, output files will be appended across hotstarts
my $fort64freq=0; # output frequency in SECONDS
my $fort64append; # if defined, output files will be appended across hotstarts
my $fort7172freq=0; # output frequency in SECONDS
my $fort7172append; # if defined, output files will be appended across hotstart
my $fort7374freq=0; # output frequency in SECONDS
my $fort7374append; # if defined, output files will append across hotstarts
my ($fort61, $fort62, $fort63, $fort64, $fort7172, $fort7374);
our $sparseoutput; # if defined, then fort.63 and fort.64 will be sparse ascii
my $hsformat="binary"; # input param for hotstart format: binary or netcdf
my ($fort61netcdf, $fort62netcdf, $fort63netcdf, $fort64netcdf, $fort7172netcdf, $fort7374netcdf); # for netcdf (not ascii) output
my $hotswan = "on"; # "off" if swan has to be cold started (only on first nowcast)
our $netcdf4; # if defined, then netcdf files should use netcdf4 formatting
#
my @TRACKS = (); # should be few enough to store all in an array for easy access
my $controltemplate;
my $elevstations="null"; # file containing list of adcirc elevation stations
my $velstations="null"; # file with list of adcirc velocity stations
my $metstations="null"; # file with list of adcirc meteorological stations
my $swantemplate;
my $metfile;
my $gridname="nc6b";
our $csdate;
our ($cy, $cm, $cd, $ch, $cmin, $cs); # ADCIRC cold start time
our ($ny, $nm, $nd, $nh, $nmin, $ns); # current ADCIRC time
our ($ey, $em, $ed, $eh, $emin, $es); # ADCIRC end time
our ($oy, $om, $od, $oh, $omin, $os); # OWI start time
my $numelevstations="0"; # number and list of adcirc elevation stations
my $numvelstations="0"; # number and list of adcirc velocity stations
my $nummetstations="0"; # number and list of adcirc meteorological stations
my $startdatetime; # formatted for swan fort.26
my $enddatetime; # formatted for swan fort.26
my $hstime; # time, in seconds, of hotstart file (since coldstart)
my $hstime_days; # time, in days, of hotstart file (since coldstart)
our $endtime; # time at which the run should end (days since coldstart)
our $dt=3.0; # adcirc time step, in seconds
my $swandt=600.0; # swan time step, in seconds
my $bladj=0.9;
our $enstorm; # ensemble name of the storm
my $nhcName="STORMNAME"; # storm name given by the nhc
my $tau=0; # forecast period
my $dir=getcwd();
my $nws=0;
my $advisorynum="0";
our $stormDir = "null"; # directory where the fort.15 file will be written
our $advisdir; # the directory for this run
my $scriptdir = "."; # the directory containing asgs_main.sh
my $particles; # flag to produce fulldomain current velocity files at an
# increment of 30 minutes
our $NHSINC; # time step increment at which to write hot start files
our $NHSTAR; # writing and format of ADCIRC hotstart output file
our $RNDAY; # total run length from cold start, in days
my $nffr = -1; # for flux boundaries; -1: top of fort.20 corresponds to hs
my $ihot; # whether or not ADCIRC should READ a hotstart file
my $fdcv; # line that controls full domain current velocity output
our $wtiminc; # parameters related to met and wave timing
our $rundesc; # description of run, 1st line in fort.15
our $ensembleid; # run id, 2nd line in fort.15
our $waves = "off"; # set to "on" if adcirc is coupled with swan is being run
our $specifiedRunLength; # time in days for run if there is no externally specified forcing
my $inundationOutput = "off"; # on inundationOutput=.true. in fort.15 template
my ($m2nf, $s2nf, $n2nf, $k2nf, $k1nf, $o1nf, $p1nf, $q1nf); # nodal factors
my ($m2eqarg, $s2eqarg, $n2eqarg, $k2eqarg, $k1eqarg, $o1eqarg, $p1eqarg, $q1eqarg); # equilibrium arguments
my $periodicflux="null"; # the name of a file containing the periodic flux unit discharge data for constant inflow boundaries
my $fluxdata;
GetOptions("controltemplate=s" => \$controltemplate,
"stormdir=s" => \$stormDir,
"swantemplate=s" => \$swantemplate,
"elevstations=s" => \$elevstations,
"velstations=s" => \$velstations,
"metstations=s" => \$metstations,
"metfile=s" => \$metfile,
"name=s" => \$enstorm,
"gridname=s" => \$gridname,
"cst=s" => \$csdate,
"endtime=s" => \$endtime,
"dt=s" => \$dt,
"swandt=s" => \$swandt,
"bladj=s" => \$bladj,
"nws=s" => \$nws,
"advisorynum=s" => \$advisorynum,
"nhcName=s" => \$nhcName,
"hstime=s" => \$hstime,
"advisdir=s" => \$advisdir,
"scriptdir=s" => \$scriptdir,
"fort61freq=s" => \$fort61freq,
"fort62freq=s" => \$fort62freq,
"fort63freq=s" => \$fort63freq,
"fort64freq=s" => \$fort64freq,
"fort7172freq=s" => \$fort7172freq,
"fort7374freq=s" => \$fort7374freq,
"fort61append" => \$fort61append,
"fort62append" => \$fort62append,
"fort63append" => \$fort63append,
"fort64append" => \$fort64append,
"fort7172append" => \$fort7172append,
"fort7374append" => \$fort7374append,
"fort61netcdf" => \$fort61netcdf,
"fort62netcdf" => \$fort62netcdf,
"fort63netcdf" => \$fort63netcdf,
"fort64netcdf" => \$fort64netcdf,
"fort7172netcdf" => \$fort7172netcdf,
"fort7374netcdf" => \$fort7374netcdf,
"netcdf4" => \$netcdf4,
"sparse-output" => \$sparseoutput,
"hsformat=s" => \$hsformat,
"hotswan=s" => \$hotswan,
"periodicflux=s" => \$periodicflux
);
#
# parse out the pieces of the cold start date
$csdate=~ m/(\d\d\d\d)(\d\d)(\d\d)(\d\d)/;
$cy = $1;
$cm = $2;
$cd = $3;
$ch = $4;
$cmin = 0.0;
$cs = 0.0;
#
# initialize "now" to a reasonable value
$ny = $1;
$nm = $2;
$nd = $3;
$nh = $4;
$nmin = $cmin;
$ns = $cs;
#
# determine whether SWAN has been turned on
my $waves_digit = int($nws / 100);
if ( abs($waves_digit) == 3 ) {
$waves = "on";
stderrMessage("INFO","Wave forcing is active.");
}
stderrMessage("DEBUG","nws is $nws and waves digit is $waves_digit.");
#
#
#----------------------------------------------------
#
# A D C I R C C O N T R O L F I L E
#
# open template file for fort.15
unless (open(TEMPLATE,"<$controltemplate")) {
stderrMessage("ERROR","Failed to open the fort.15 template file $controltemplate for reading: $!.");
die;
}
#
# open output control file
if ( $stormDir eq "null" ) {
$stormDir = $advisdir."/".$enstorm;
}
unless (open(STORM,">$stormDir/fort.15")) {
stderrMessage("ERROR","Failed to open the output control file $stormDir/fort.15: $!");
die;
}
stderrMessage("INFO","The fort.15 file will be written to the directory $stormDir.");
#
# call subroutine that knows how to fill in the fort.15 for each particular
# type of forcing
if ( abs($nws) == 19 || abs($nws) == 319 || abs($nws) == 20 || abs($nws) == 320 || abs($nws) == 8 || abs($nws) == 308 ) {
stderrMessage("DEBUG","Setting parameters appropriately for vortex model.");
&vortexModelParameters($nws);
} elsif ( abs($nws) == 12 || abs($nws) == 312 ) {
if ( abs($nws) == 12 || abs($nws) == 312 ) {
&owiParameters();
} elsif ( defined $specifiedRunLength ) {
stderrMessage("DEBUG","The duration of this $enstorm run is specially defined.");
&customParameters();
} elsif ( $enstorm eq "hindcast" ) {
stderrMessage("DEBUG","This is a hindcast.");
&hindcastParameters();
}
#
# we want a hotstart file if this is a nowcast or hindcast
if ( $enstorm eq "nowcast" || $enstorm eq "hindcast" ) {
$NHSTAR = 1;
if ( $hsformat eq "netcdf" ) {
$NHSTAR = 3;
if ( defined $netcdf4 ) {
$NHSTAR = 5;
}
}
} else {
$NHSTAR = 0;
$NHSINC = 99999;
}
#
# we always look for a fort.68 file, and since we only write one hotstart
# file during the run, we know we will always be left with a fort.67 file.
if ( defined $hstime ) {
$ihot = 68;
if ( $hsformat eq "netcdf" ) {
$ihot = 368;
if ( defined $netcdf4 ) {
$ihot = 568;
}
}
} else {
$ihot = 0;
$nffr = 0;
}
# [de]activate output files with time step increment and with(out) appending.
my $fort61specifier = &getSpecifier($fort61freq,$fort61append,$fort61netcdf);
my $fort62specifier = &getSpecifier($fort62freq,$fort62append,$fort62netcdf);
$fort61 = $fort61specifier . " 0.0 365.0 " . &getIncrement($fort61freq,$dt);
$fort62 = $fort62specifier . " 0.0 365.0 " . &getIncrement($fort62freq,$dt);
#
my $fort63specifier = &getSpecifier($fort63freq,$fort63append,$fort63netcdf);
my $fort64specifier = &getSpecifier($fort64freq,$fort64append,$fort64netcdf);
if ( defined $sparseoutput ) {
unless ( defined $fort63netcdf ) {
$fort63specifier *= 4;
}
unless ( defined $fort64netcdf ) {
$fort64specifier *= 4;
}
}
$fort63 = $fort63specifier . " 0.0 365.0 " . &getIncrement($fort63freq,$dt);
$fort64 = $fort64specifier . " 0.0 365.0 " . &getIncrement($fort64freq,$dt);
my $fort7172specifier = &getSpecifier($fort7172freq,$fort7172append,$fort7172netcdf);
my $fort7374specifier = &getSpecifier($fort7374freq,$fort7374append,$fort7374netcdf);
# Casey 121009: Debug for sparse output.
if ( defined $sparseoutput ) {
unless ( defined $fort7374netcdf ) {
$fort7374specifier *= 4;
}
}
$fort7172 = $fort7172specifier . " 0.0 365.0 " . &getIncrement($fort7172freq,$dt);
$fort7374 = $fort7374specifier . " 0.0 365.0 " . &getIncrement($fort7374freq,$dt);
if ( $nws eq "0" ) {
$fort7172 = "NO LINE HERE";
$fort7374 = "NO LINE HERE";
}
# add swan time step to WTIMINC line if waves have been activated
if ( $waves eq "on" ) {
$wtiminc.=" $swandt"
}
#
# determine if tide_fac.x executable is present, and if so, generate
# nodal factors and equilibrium arguments
my $tides = "off";
if ( -e "$scriptdir/tides/tide_fac.x" && -x "$scriptdir/tides/tide_fac.x" ) {
my $tide_fac_message = `$scriptdir/tides/tide_fac.x --length $RNDAY --year $cy --month $cm --day $cd --hour $ch --outputformat simple --outputdir $stormDir`;
if ( $tide_fac_message =~ /ERROR|WARNING/ ) {
stderrMessage("WARNING","There was an issue when running $scriptdir/tides/tide_fac.x: $tide_fac_message.");
} else {
stderrMessage("INFO","Nodal factors and equilibrium arguments were written to the file $stormDir/tide_fac.out.");
# open data file
unless (open(TIDEFAC,"<$stormDir/tide_fac.out")) {
stderrMessage("ERROR","Failed to open the file '$advisdir/$enstorm/tide_fac.out' for reading: $!.");
die;
}
# parse out nodal factors and equilibrium arguments from the
# various constituents
$tides = "on";
stderrMessage("INFO","Parsing tidal node factors and equilibrium arguments.");
while(<TIDEFAC>) {
my @constituent = split;
if ( $constituent[0] eq "M2" ) {
$m2nf = $constituent[1];
$m2eqarg = $constituent[2];
} elsif ( $constituent[0] eq "S2" ) {
$s2nf = $constituent[1];
$s2eqarg = $constituent[2];
} elsif ( $constituent[0] eq "N2" ) {
$n2nf = $constituent[1];
$n2eqarg = $constituent[2];
} elsif ( $constituent[0] eq "K2" ) {
$k2nf = $constituent[1];
$k2eqarg = $constituent[2];
} elsif ( $constituent[0] eq "K1" ) {
$k1nf = $constituent[1];
$k1eqarg = $constituent[2];
} elsif ( $constituent[0] eq "O1" ) {
$o1nf = $constituent[1];
$o1eqarg = $constituent[2];
} elsif ( $constituent[0] eq "P1" ) {
$p1nf = $constituent[1];
$p1eqarg = $constituent[2];
} elsif ( $constituent[0] eq "Q1" ) {
$q1nf = $constituent[1];
$q1eqarg = $constituent[2];
} else {
stderrMessage("WARNING","Tidal constituent named '$constituent[0]' was unrecognized.");
}
}
close(TIDEFAC);
}
} else {
stderrMessage("INFO","The executable that generates the tidal node factors and equilibrium arguments ($scriptdir/tides/tide_fac.x) was not found. Updated nodal factors and equilibrium arguments will not be generated.");
}
#
# load up stations
$numelevstations = &getStations($elevstations,"elevation");
$numvelstations = &getStations($velstations,"velocity");
if ( $nws eq "0" ) {
stderrMessage("INFO","NWS is zero; meteorological stations will not be written to the fort.15 file.");
$nummetstations = "NO LINE HERE";
} else {
$nummetstations = &getStations($metstations,"meteorology");
}
# load up the periodicflux data
$fluxdata = &getPeriodicFlux($periodicflux);
#
stderrMessage("INFO","Filling in ADCIRC control template (fort.15).");
while(<TEMPLATE>) {
# if we are looking at the first line, fill in the name of the storm
# and the advisory number, if available
s/%StormName%/$rundesc/;
# if we are looking at the DT line, fill in the time step (seconds)
s/%DT%/$dt/;
# if we are looking at the RNDAY line, fill in the total run time (days)
s/%RNDAY%/$RNDAY/;
# set whether or not we are going to read a hotstart file
s/%IHOT%/$ihot/;
# fill in the parameter that selects which wind model to use
s/%NWS%/$nws/;
# fill in the parameter that selects which wind model to use
s/%NFFR%/$nffr/;
# fill in nodal factors and equilibrium arguments
if ( $tides eq "on" ) {
s/%M2NF%/$m2nf/; s/%M2EQARG%/$m2eqarg/;
s/%S2NF%/$s2nf/; s/%S2EQARG%/$s2eqarg/;
s/%N2NF%/$n2nf/; s/%N2EQARG%/$n2eqarg/;
s/%K2NF%/$k2nf/; s/%K2EQARG%/$k2eqarg/;
s/%K1NF%/$k1nf/; s/%K1EQARG%/$k1eqarg/;
s/%O1NF%/$o1nf/; s/%O1EQARG%/$o1eqarg/;
s/%P1NF%/$p1nf/; s/%P1EQARG%/$p1eqarg/;
s/%Q1NF%/$q1nf/; s/%Q1EQARG%/$q1eqarg/;
}
# fill in the timestep increment that hotstart files will be written at
s/%NHSINC%/$NHSINC/;
# fill in whether or not we want a hotstart file out of this
s/%NHSTAR%/$NHSTAR/;
# fill in ensemble name -- this is in the comment line
s/%EnsembleID%/$ensembleid/;
# may be asymmetric parameters, or wtiminc, rstiminc, etc
s/%WTIMINC%/$wtiminc/;
# periodic non-zero inflow
s/%PERIODICFLUX%/$fluxdata/;
# elevation stations
s/%NUMELEVSTATIONS%/$numelevstations/;
# velocity stations
s/%NUMVELSTATIONS%/$numvelstations/;
# meteorological stations
s/%NUMMETSTATIONS%/$nummetstations/;
# output options
s/%FORT61%/$fort61/;
s/%FORT62%/$fort62/;
s/%FORT63%/$fort63/;
s/%FORT64%/$fort64/;
s/%FORT7172%/$fort7172/;
s/%FORT7374%/$fort7374/;
s/%CSYEAR%/$cy/;
s/%CSMONTH%/$cm/;
s/%CSDAY%/$cd/;
s/%CSHOUR%/$ch/;
s/%CSMIN%/$cmin/;
s/%CSSEC%/$cs/;
if (/inundationOutput=.[tT]/) {
$inundationOutput = "on";
}
unless (/NO LINE HERE/) {
print STORM $_;
}
}
close(TEMPLATE);
close(STORM);
#
#
# S W A N C O N T R O L F I L E
#
if ( $waves eq "on" ) {
# open template file for fort.26
unless (open(TEMPLATE,"<$swantemplate")) {
stderrMessage("ERROR","Failed to open the swan template file $swantemplate for reading: $!.");
die;
}
#
# open output fort.26 file
unless (open(STORM,">$stormDir/fort.26")) {
stderrMessage("ERROR","Failed to open the output control file $stormDir/fort.26: $!.");
die;
}
stderrMessage("INFO","The fort.26 file will be written to the directory $stormDir.");
#
$startdatetime = sprintf("%4d%02d%02d.%02d0000",$ny,$nm,$nd,$nh);
$enddatetime = sprintf("%4d%02d%02d.%02d0000",$ey,$em,$ed,$eh);
my $swanhs = "INIT HOTSTART MULTIPLE 'swan.68'";
if ( $hotswan eq "off" ) {
$swanhs = "\$ swan will coldstart";
}
#
stderrMessage("INFO","Filling in swan control template (fort.26).");
while(<TEMPLATE>) {
# if we are looking at the first line, fill in the name of the storm
# and the advisory number, if available
s/%StormName%/$rundesc/;
# if we are looking at the DT line, fill in the time step (seconds)
s/%swandt%/$swandt/;
# fill in ensemble name -- this is in the comment line
s/%EnsembleID%/$ensembleid/;
# may be asymmetric parameters, or wtiminc, rstiminc, etc
s/%WTIMINC%/$wtiminc/;
#
s/%hotstart%/$swanhs/;
# swan start time -- corresponds to adcirc hot start time
s/%startdatetime%/$startdatetime/;
# swan end time%
s/%enddatetime%/$enddatetime/;
print STORM $_;
}
close(TEMPLATE);
close(STORM);
}
#
# append run.properties file
# set components
my $model = "PADCIRC";
my $model_type = "SADC";
my $wind_model = "vortex-nws$nws";
my $run_type = "Forecast";
my $cycle_hour = sprintf("%02d",$nh);
my $currentdate = substr($ny,2,2) . sprintf("%02d%02d",$nm,$nd); # start time
my $date1 = sprintf("%4d%02d%02dT%02d%02d",$ny,$nm,$nd,$nh,$nmin); # start time
my $date2 = sprintf("%4d%02d%02dT%02d%02d",$ny,$nm,$nd,$nh,$nmin); # 1st output
my $date3 = sprintf("%4d%02d%02dT%02d%02d",$ey,$em,$ed,$eh,$emin); # end time
my $runstarttime = sprintf("%4d%02d%02d%02d",$ny,$nm,$nd,$nh); # start time
my $runendtime = sprintf("%4d%02d%02d%02d",$ey,$em,$ed,$eh); # end time
if ( $waves eq "on" ) {
$model_type = "SPDS";
$model = "PADCSWAN";
}
if ( $nws == 0 ) {
$wind_model = "none";
}
if ( abs($nws) == 12 || abs($nws) == 312 ) {
$wind_model = "WNAMAW12-NCP";
$cycle_hour = sprintf("%02d",$oh);
$currentdate = substr($oy,2,2) . sprintf("%02d%02d",$om,$od); # start time
$date1 = sprintf("%4d%02d%02dT%02d%02d",$oy,$om,$od,$oh,$omin);
}
if ( $enstorm eq "nowcast" ) {
$run_type = "Nowcast";
} elsif ( $enstorm eq "hindcast" ) {
$run_type = "Hindcast";
}
# last time step of model output contained in the file
my $rp_fname = $model_type . $gridname . "-UNC_" . $wind_model . "_" . $date1 . "_" . $date2 . "_" . $date3 . "_" . $cycle_hour . "_run.properties";
my $prodid = $model_type . $gridname . "-UNC_" . $wind_model . "_" . $date1 . "_" . $date2 . "_" . $date3 . "_" . $cycle_hour . "<field>_Z.nc.gz";
stderrMessage("INFO","Opening run.properties file for writing.");
unless (open(RUNPROPS,">>$stormDir/run.properties")) {
stderrMessage("ERROR","Failed to open the $stormDir/run.properties file for writing: $!.");
die;
}
# If we aren't using a vortex met model, we don't have a track
# file, but the CERA web app still needs to have values for these
# properties. In the case of a vortex met model, these values are
# filled in by the storm_track_gen.pl script.
if ( abs($nws) != 19 && abs($nws) != 319 && abs($nws) != 20 && abs($nws) != 320 ) {
printf RUNPROPS "track_raw_dat : notrack\n";
printf RUNPROPS "track_raw_fst : notrack\n";
printf RUNPROPS "track_modified : notrack\n";
}
printf RUNPROPS "year : $ny\n";
printf RUNPROPS "directory storm : $stormDir\n";
printf RUNPROPS "mesh : $gridname\n";
printf RUNPROPS "RunType : $run_type\n";
printf RUNPROPS "ADCIRCgrid : $gridname\n";
printf RUNPROPS "stormname : $nhcName\n";
printf RUNPROPS "currentcycle : $cycle_hour\n";
printf RUNPROPS "currentdate : $currentdate\n";
printf RUNPROPS "advisory : $advisorynum\n";
printf RUNPROPS "prodID : $prodid\n";
if (defined $hstime) {
printf RUNPROPS "InitialHotStartTime : $hstime\n";
}
printf RUNPROPS "RunStartTime : $runstarttime\n";
printf RUNPROPS "RunEndTime : $runendtime\n";
printf RUNPROPS "ColdStartTime : $csdate\n";
printf RUNPROPS "WindModel : $wind_model\n";
printf RUNPROPS "Model : $model\n";
# write the names of the output files to the run.properties file
stderrMessage("INFO","Writing file names and formats to run.properties file.");
&writeFileName("fort.61",$fort61specifier);
&writeFileName("fort.62",$fort62specifier);
&writeFileName("fort.63",$fort63specifier);
&writeFileName("fort.64",$fort64specifier);
&writeFileName("fort.71",$fort7172specifier);
&writeFileName("fort.72",$fort7172specifier);
&writeFileName("fort.73",$fort7374specifier);
&writeFileName("fort.74",$fort7374specifier);
&writeFileName("maxele.63",$fort63specifier);
&writeFileName("maxvel.63",$fort64specifier);
&writeFileName("maxwvel.63",$fort7374specifier);
&writeFileName("minpr.63",$fort7374specifier);
if ( $waves eq "on" ) {
&writeFileName("maxrs.63",$fort7374specifier);
&writeFileName("swan_DIR.63",$fort7374specifier);
&writeFileName("swan_DIR_max.63",$fort7374specifier);
&writeFileName("swan_HS.63",$fort7374specifier);
&writeFileName("swan_HS_max.63",$fort7374specifier);
&writeFileName("swan_TMM10.63",$fort7374specifier);
&writeFileName("swan_TMM10_max.63",$fort7374specifier);
&writeFileName("swan_TPS.63",$fort7374specifier);
&writeFileName("swan_TPS_max.63",$fort7374specifier);
}
if ($inundationOutput eq "on") {
&writeFileName("initiallydry.63",$fort63specifier);
&writeFileName("inundationtime.63",$fort63specifier);
&writeFileName("maxinundepth.63",$fort63specifier);
&writeFileName("everdried.63",$fort63specifier);
&writeFileName("endrisinginun.63",$fort63specifier);
}
close(RUNPROPS);
stderrMessage("INFO","Wrote run.properties file $stormDir/run.properties.");
exit;
#
#
#--------------------------------------------------------------------------
# S U B W R I T E F I L E N A M E
#
# If an output file will be available, its name and type is written
# to the run.properties file.
#--------------------------------------------------------------------------
sub writeFileName () {
my $identifier = shift;
my $specifier = shift;
#
my $format = "ascii"; # default output file format
my $filename = $identifier; # default (ascii) name of output file
#
# if there won't be any output of this type, just return without
# writing anything to the run.properties file
if ( $specifier == 0 ) {
return;
}
# create the hash for relating the basic file identifier with the
# long winded file type description
my %ids_descs;
$ids_descs{"fort.61"} = "Water Surface Elevation Stations";
$ids_descs{"fort.62"} = "Water Current Velocity Stations";
$ids_descs{"fort.63"} = "Water Surface Elevation";
$ids_descs{"fort.64"} = "Water Current Velocity";
$ids_descs{"fort.71"} = "Barometric Pressure Stations";
$ids_descs{"fort.72"} = "Wind Velocity Stations";
$ids_descs{"fort.73"} = "Barometric Pressure";
$ids_descs{"fort.74"} = "Wind Velocity";
$ids_descs{"maxele.63"} = "Maximum Water Surface Elevation";
$ids_descs{"maxvel.63"} = "Maximum Current Speed";
$ids_descs{"maxwvel.63"} = "Maximum Wind Speed";
$ids_descs{"minpr.63"} = "Minimum Barometric Pressure";
$ids_descs{"maxrs.63"} = "Maximum Wave Radiation Stress";
$ids_descs{"swan_DIR.63"} = "Mean Wave Direction";
$ids_descs{"swan_DIR_max.63"} = "Maximum Mean Wave Direction";
$ids_descs{"swan_HS.63"} = "Significant Wave Height";
$ids_descs{"swan_HS_max.63"} = "Maximum Significant Wave Height";
$ids_descs{"swan_TMM10.63"} = "Mean Wave Period";
$ids_descs{"swan_TMM10_max.63"} = "Maximum Mean Wave Period";
$ids_descs{"swan_TPS.63"} = "Peak Wave Period";
$ids_descs{"swan_TPS_max.63"} = "Maximum Peak Wave Period";
$ids_descs{"initiallydry.63"} = "Initially Dry";
$ids_descs{"inundationtime.63"} = "Inundation Time";
$ids_descs{"maxinundepth.63"} = "Maximum Inundation Depth";
$ids_descs{"everdried.63"} = "Ever Dried";
$ids_descs{"endrisinginun.63"} = "End Rising Inundation";
#
if ( abs($specifier) == 3 || abs($specifier) == 5 ) {
$filename = $filename . ".nc";
$format = "netcdf";
}
if ( abs($specifier) == 4 ) {
$format = "sparse-ascii";
}
printf RUNPROPS "$ids_descs{$identifier} File Name : $filename\n";
printf RUNPROPS "$ids_descs{$identifier} Format : $format\n";
}
#
#
#--------------------------------------------------------------------------
# S U B G E T S P E C I F I E R
#
# Determines the correct output specifier for output files based on
# the output frequency, whether or not the files should be appended,
# and whether or not the netcdf format is used (ascii is the default).
#--------------------------------------------------------------------------
sub getSpecifier () {
my $freq = shift;
my $append = shift;
my $netcdf = shift;
my $specifier;
if ( $freq == 0 ) {
$specifier = "0";
} else {
if ( defined $append ) {
$specifier = "1";
} else {
$specifier = "-1";
}
if ( defined $netcdf ) {
if ( defined $netcdf4 ) {
$specifier *= 5;
} else {
$specifier *= 3;
}
}
}
return $specifier;
}
#
#--------------------------------------------------------------------------
# S U B G E T I N C R E M E N T
#
# Determines the correct time step increment based on the output frequency
# and time step size.
#--------------------------------------------------------------------------
sub getIncrement () {
my $freq = shift;
my $timestepsize = shift;
my $increment;
if ( $freq == 0 ) {
$increment = "99999";
} else {
$increment = int($freq/$timestepsize);
}
return $increment;
}
#
#--------------------------------------------------------------------------
# S U B G E T S T A T I O N S
#
# Pulls in the stations from an external file.
#--------------------------------------------------------------------------
sub getStations () {
my $station_file = shift;
my $station_type = shift;
#
my $numstations = "";
my $station_var = "NSTAE";
if ( $station_type eq "velocity" ) {
$station_var = "NSTAV";
}
if ( $station_type eq "meteorology" ) {
$station_var = "NSTAM";
}
if ( $station_file =~ /null/) {
$numstations = "0 ! $station_var" ;
stderrMessage("INFO","There are no $station_type stations.");
return $numstations; # early return
}
unless (open(STATIONS,"<$station_file")) {
stderrMessage("ERROR","Failed to open the $station_type stations file $station_file for reading: $!.");
die;
}
my $number=0;
while (<STATIONS>) {
$_ =~ s/^\s+//;
next if (substr($_,0,1) eq '#'); # skip comment lines in the stations file
$numstations.=$_;
$number++;
}
close(STATIONS);
stderrMessage("INFO","There are $number $station_type stations in the file '$station_file'.");
chomp($numstations);
# need to add this as a sort of comment in the fort.15 for the post
# processing script station_transpose.pl to find
$numstations = $number . " " . $station_file . " " . $station_var . "\n" . $numstations;
return $numstations;
}
#
#-------------------------------------------------------------------------
# S U B G E T P E R I O D I C F L U X
#
# gets data for periodic non-zero inflow boundaries from a separate file
# for example, a file generated by the riverFlow.pl script during
# model configuration.
#-------------------------------------------------------------------------
sub getPeriodicFlux {
my $flux_file=shift;
if ($flux_file =~ /null/){
stderrMessage("INFO","No periodic inflow boundary data file was specified/");
return
}
unless (open(FLUXFILE,"<$flux_file")) {
stderrMessage("ERROR","Failed to open $flux_file for reading: $!.");
die;
}
my $fluxdata='';
while (<FLUXFILE>){
$fluxdata.=$_;
}
close(FLUXFILE);
stderrMessage("INFO","Inserting periodic inflow boundary data from $flux_file.");
chomp $fluxdata;
return $fluxdata;
}
#
#
#--------------------------------------------------------------------------
# S U B H I N D C A S T P A R A M E T E R S
#
# Determines parameter values for the control file when running
# ADCIRC during a hindcast with no met forcing.
#--------------------------------------------------------------------------
sub hindcastParameters () {
$rundesc = "cs:$csdate"."0000 cy: ASGS hindcast";
$RNDAY = $endtime; #FIX: this should be a date, not days
$NHSINC = int(($RNDAY*86400.0)/$dt);
($ey,$em,$ed,$eh,$emin,$es) =
Date::Pcalc::Add_Delta_DHMS($cy,$cm,$cd,$ch,$cmin,$cs,$endtime,0,0,0);
$nws = 0;
$ensembleid = "$endtime day hindcast run";
$wtiminc = "NO LINE HERE";
stderrMessage("DEBUG","Finished setting hindcast parameters.");
}
#
#--------------------------------------------------------------------------
# S U B C U S T O M P A R A M E T E R S
#
# Determines parameter values for the control file when running
# ADCIRC without external forcing (e.g., for running ADCIRC tests).
#--------------------------------------------------------------------------
sub customParameters () {
$rundesc = "cs:$csdate"."0000 cy: ASGS";
my $alreadyElapsedDays = 0.0;
if ( defined $hstime && $hstime != 0 ) {
$alreadyElapsedDays = $hstime / 86400.0;
}
$RNDAY = $specifiedRunLength + $alreadyElapsedDays;
$NHSINC = int(($RNDAY*86400.0)/$dt);
$nws = 0;
$ensembleid = "$enstorm $specifiedRunLength day run";
#
# determine the relationship between the start of the NAM data and the
# current time in the ADCIRC run
if ( defined $hstime && $hstime != 0 ) {
# now add the hotstart seconds
($ny,$nm,$nd,$nh,$nmin,$ns) =
Date::Pcalc::Add_Delta_DHMS($cy,$cm,$cd,$ch,0,0,0,0,0,$hstime);
} else {
# the hotstart time was not provided, or it was provided and is equal to 0
# therefore the current ADCIRC time is the cold start time, t=0
$ny = $cy;
$nm = $cm;
$nd = $cd;
$nh = $ch;
$nmin = 0;
$ns = 0;
}
($ey,$em,$ed,$eh,$emin,$es) =
Date::Pcalc::Add_Delta_DHMS($ny,$nm,$nd,$nh,0,0,$specifiedRunLength,0,0,0);
$wtiminc = "NO LINE HERE";
# create the runme file, if this is a nowcast that has an ending time
# that is later than the previous hotstart
if ( $enstorm eq "nowcast" && $specifiedRunLength != 0 ) {
open(RUNME,">$stormDir/runme") || die "ERROR: control_file_gen.pl: Failed to open runme file for writing in the directory $stormDir: $!.";
printf RUNME "$specifiedRunLength day nowcast\n";
close(RUNME);
}
stderrMessage("DEBUG","Finished setting specified run length.");
}
#
#--------------------------------------------------------------------------
# S U B O W I P A R A M E T E R S
#
# Determines parameter values for the control file when running
# ADCIRC with OWI formatted meteorological data (NWS12).
#--------------------------------------------------------------------------
sub owiParameters () {
#
# open met file
open(METFILE,"<$stormDir/fort.22") || die "ERROR: control_file_gen.pl: Failed to open OWI (NWS12) file $stormDir/fort.22 for reading: $!.";
my $line = <METFILE>;
close(METFILE);
$line =~ /^# (\d+)/;
$wtiminc = $1;
#
# determine the relationship between the start of the NAM data and the
# current time in the ADCIRC run
if ( defined $hstime && $hstime != 0 ) {
# now add the hotstart seconds
($ny,$nm,$nd,$nh,$nmin,$ns) =
Date::Pcalc::Add_Delta_DHMS($cy,$cm,$cd,$ch,0,0,0,0,0,$hstime);
} else {
# the hotstart time was not provided, or it was provided and is equal to 0
# therefore the current ADCIRC time is the cold start time, t=0
$ny = $cy;
$nm = $cm;
$nd = $cd;
$nh = $ch;
$nmin = 0;
$ns = 0;
}
#
# open file that will contain the hotstartdate
open(HSD,">$stormDir/hotstartdate") || die "ERROR: control_file_gen.pl: Failed to open the HOTSTARTDATE file $stormDir/hotstartdate: $!.";
my $hotstartdate = sprintf("%4d%02d%02d%02d",$ny,$nm,$nd,$nh);
stderrMessage("INFO","The file containing the hotstartdate '$hotstartdate' will be written to the directory $stormDir.");
printf HSD $hotstartdate;
close(HSD);
# determine the date time of the start of the OWI files
my @fort221 = glob($stormDir."/NAM*.221");
$fort221[0] =~ /NAM_(\d+)/;
my $owistart = $1;
# create run description
$rundesc = "cs:$csdate"."0000 cy:$owistart ASGS NAM";
$owistart =~ m/(\d\d\d\d)(\d\d)(\d\d)(\d\d)/;
$oy = $1;
$om = $2;
$od = $3;
$oh = $4;
$omin = 0;
$os = 0;
#
# get difference
(my $ddays, my $dhrs, my $dmin, my $dsec)
= Date::Pcalc::Delta_DHMS(
$ny,$nm,$nd,$nh,0,0,
$oy,$om,$od,$oh,0,0);
# find the difference in seconds
my $blank_time = $ddays*86400.0 + $dhrs*3600.0 + $dmin*60.0 + $dsec;
stderrMessage("INFO","Blank time is '$blank_time'.");
# calculate the number of blank snaps (or the number of
# snaps to be skipped in the OWI file if it starts before the
# current time in the ADCIRC run)
my $nwbs = int($blank_time/$wtiminc);
stderrMessage("INFO","nwbs is '$nwbs'");
#
# create the fort.22 output file, which is the wind input file for ADCIRC
open(MEMBER,">$stormDir/fort.22") || die "ERROR: control_file_gen.pl: Failed to open file for ensemble member '$enstorm' to write $stormDir/fort.22 file: $!.";
printf MEMBER "1\n"; # nwset
printf MEMBER "$nwbs\n"; # nwbs
printf MEMBER "1.0\n"; # dwm
close(MEMBER);
#
# determine the date time of the end of the OWI files
$fort221[0] =~ /(\d+).221$/;
my $owiend = $1;
stderrMessage("INFO","The OWI file ends at '$owiend'.");
$owiend =~ m/(\d\d\d\d)(\d\d)(\d\d)(\d\d)/;
$ey = $1;
$em = $2;
$ed = $3;
$eh = $4;
$emin = 0;
$es = 0;
#
# get difference
(my $ddays, my $dhrs, my $dmin, my $dsec)
= Date::Pcalc::Delta_DHMS(
$cy,$cm,$cd,$ch,0,0,
$ey,$em,$ed,$eh,0,0);
# find the new total run length in days
$RNDAY = $ddays + $dhrs/24.0 + $dmin/1440.0 + $dsec/86400.0;
# determine the number of hours of this run, from hotstart to end
(my $ddays, my $dhrs, my $dmin, my $dsec)
= Date::Pcalc::Delta_DHMS(
$ny,$nm,$nd,$nh,0,0,
$ey,$em,$ed,$eh,0,0);
my $addHours = $ddays*24.0 + $dhrs + $dmin/60.0 + $dsec/3600.0;
$ensembleid = $addHours . " hour " . $enstorm . " run";
$NHSINC = int(($RNDAY*86400.0)/$dt);
#
# create the runme file, if this is a nowcast
if ( $enstorm eq "nowcast" ) {
open(RUNME,">$stormDir/runme") || die "ERROR: control_file_gen.pl: Failed to open runme file for writing in the directory $stormDir: $!.";
printf RUNME "$ensembleid\n";
}
close(RUNME);
}
#
#--------------------------------------------------------------------------
# S U B V O R T E X M O D E L P A R A M E T E R S
#
# Determines parameter values for the control file when running
# the with tropical cyclone vortex models (asymmetric, nws=19; or
# generalized asymmetric holland model, nws=20).
#--------------------------------------------------------------------------
sub vortexModelParameters () {
my $nws = shift;
my $geofactor = 1; # turns on Coriolis for GAHM; this is the default
$ensembleid = $enstorm;
my $geofactor = 1; # turns on Coriolis for GAHM; this is the default
#
# open met file containing datetime data
unless (open(METFILE,"<$metfile")) {
stderrMessage("ERROR","Failed to open meteorological (ATCF-formatted) fort.22 file '$metfile' for reading: $!.");
die;
}
stderrMessage("DEBUG","Successfully opened meteorological (ATCF-formatted) fort.22 file '$metfile' for reading.");
#
# determine date time at end of hindcast
#
# Build track list
while (<METFILE>) {
chomp($_);
my @tmp = ();
# split and remove any spaces
foreach my $item (split(',',$_)) {
$item =~ s/\s*//g;
push(@tmp,$item);
}
# 2d array of arrays; [@tmp] creates an anon array in each
# element of @TRACK
push(@TRACKS,[@tmp]);
}
#
# find last hindcast line
my $track;
my $nowcast;
foreach $track (reverse(@TRACKS)) {
if (@{$track}[4] =~ m/BEST/) {
if ( $nhcName eq "STORMNAME" ) {
# We need to get the storm name from the last hindcast line
if ( defined $track->[27] ) {
$nhcName = $track->[27];
} else {
stderrMessage("WARNING","The name of the storm does not appear in the hindcast.");