-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuntimeProc.pl
executable file
·195 lines (148 loc) · 5.18 KB
/
RuntimeProc.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
#!/usr/bin/perl -s
my $help = $h;
my $verbose = $v;
my $preplot = $p;
my $kill = $k;
my $targz = $g;
my $repeat = $r;
my $dir = $d;
my $nametar = $n;
use strict;
use warnings;
use Cwd qw(getcwd);
# Some defaults
my $cwd = getcwd;
my $INFO = 'RuntimeProc.pl';
my $ERROR = "ERROR in $INFO";
my $STOP = 4;
my $outdir = "$cwd" . "/" . "GM/IO2";
my $preplot_found = 0;
my $tardir = "ALL-DATFILES";
my $success = "BATSRUS.SUCCESS";
&show_help if $help;
# Sanity check
die "-n argument only works together with -g!\n" if ($nametar and not $targz);
die "$ERROR: the pTEC script is not in run directory: $cwd\n"
unless glob( $cwd . '/pTEC' );
# Overwrite the path to the output directory if needed
$outdir = "$cwd" . "/" . "$dir" if $dir;
# Remove trailing '/' of name if user included it
$outdir =~ s/\/+$//;
print "Output directory is at $outdir\n" if $verbose;
if ($repeat){
print "$INFO running on ", `hostname`;
print "$INFO will stop in $STOP days. Started on ", `date`;
}
# Time counter for repeat option
my $time_start = time();
# Prevent repeat counter to terminate at restart of successfull model
# The $success file is only deleted once BATSRUS (re)starts
my $startup = 1;
# Concatenate the simulation files from all processors
REPEAT:{
chdir $cwd;
# NOTE: for proper functioning of pTEC an additional '/' has to be appended
&shell("./pTEC b=${outdir}/");
# Escape only if code finished or exceeding of script time
last REPEAT if -e $success and not $startup;
print "$success already exists at startup. Ignoring it for repeat count.\n"
if -e $success and $verbose;
$startup = 0 if $startup;
if ($repeat){
last REPEAT if (time - $time_start) > $STOP*3600*24;
sleep $repeat;
redo REPEAT;
}
}
# Find all .dat files in the output directory
my @datfiles = glob( $outdir . '/*.dat' );
&find_preplot if $preplot;
if ( $preplot and $preplot_found ){
foreach my $file (@datfiles){
my $datfile = $file;
if ( $datfile !~ /\.dat$/ ){
warn "WARNING: for preplotting the extension should be .dat -> "
. "$file\n";
next;
}
# Preplot file
&shell("preplot $datfile");
# Check if plt file is produced
my $pltfile = $datfile;
$pltfile =~ s/.dat/.plt/;
die "$ERROR: while using preplot no $pltfile was produced\n"
unless -s $pltfile;
}
}
if ($targz){
die "$ERROR: no file matches @datfiles! Archiving cancelled...\n"
unless @datfiles;
print "Archiving files...\n";
chdir $outdir;
$tardir = $nametar if $nametar;
&shell("mkdir $tardir");
`cp @datfiles $tardir`;
&shell("tar -c -z -f ${tardir}.tar.gz $tardir");
`rm -r $tardir`;
}
# Remove .dat file
print "Deleting all .dat files in $outdir...\n" if $kill and $verbose;
unlink @datfiles if $kill;
exit 0;
#==============================================================================
# SUBROUTINES
#==============================================================================
sub shell{
my $command = join(" ", @_);
print "$command\n" if $verbose;
my $result = `$command`;
print $result if $verbose or $result =~ /error/i;
}
sub find_preplot{
my $dump = ".tmp";
my $searchpp = "(which preplot | grep -v \'Command not found\' > $dump)";
`$searchpp >/dev/null 2>&1`;
if (-s $dump){
print "--- Preplot binary found at: " . `cat $dump` . "\n";
`rm -f $dump`;
$preplot_found = 1;
}else{
print "--- Preplot binary not found in PATH! Ignoring '-p' flag\n";
`rm -f $dump`;
}
}
sub show_help{
print
"Purpose:
Post-process BATSRUS output files created by each processor.
The pTEC script needs to be included in the current working directory.
Usage:
RuntimeProc.pl [-h] [-v] [-p] [-g] [-k] [-n=NAME] [-r=REPEAT] [-d=DIRNAME]
-h Print help message and exit.
-v Print verbose info to screen.
-p Preplot .dat files in the output directory.
-g Archive and compress all .dat files into a gzipped tarball in
the output directory.
-k Delete all .dat files in the output directory.
-n=NAME Specify NAME for gzipped tarball in the output directory.
Default ALL-DATFILES.tar.gz.
-r=REPEAT Repeat post-processing of .dat files every REPEAT seconds.
-d=DIRNAME Specify the directory DIRNAME containing the .dat files relative
to the current working directory.
Default is GM/IO2/ relative to current working directory.
Examples:
Post-process output files into .dat files located in GM/IO2:
RuntimeProc.pl
Repeat post-processing output files in GM/IO2 every 60 seconds:
RuntimeProc.pl -r=60
Post-process output files in the 'Run12' directory relative to the current
working directory, preplot to plt files, and print verbose info:
RuntimeProc.pl -v -p -d=Run12
Post-process output files in the 'NewRun' directory relative to the current
working directory, compress and archive the .dat files, compress the .dat files
into a gzipped tarball 'B0_q5', and delete the .dat files:
RuntimeProc.pl -g -k -d=NewRun -n=B0_q5
"
,"\n\n";
exit;
}