-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmass1profile.in
executable file
·544 lines (432 loc) · 13.8 KB
/
mass1profile.in
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
#!@PERL@
# -*- cperl -*-
# -------------------------------------------------------------
# file: mass1profile.pl
# -------------------------------------------------------------
# -------------------------------------------------------------
# Copyright (c) 2017 Battelle Memorial Institute
# Licensed under modified BSD License. A copy of this license can be
# found in the LICENSE file in the top level directory of this
# distribution.
# -------------------------------------------------------------
# -------------------------------------------------------------
# Created April 13, 1998 by
# Last Change: 2017-08-29 10:20:32 d3g096
# -------------------------------------------------------------
# RCS ID: $Id$
# -------------------------------------------------------------
# documentation
# -------------------------------------------------------------
=pod
=head1 NAME
mass1profile - process MASS1 profile files and compute statistics
=head1 SYNOPSIS
perl B<doprofile.pl>
=head1 DESCRIPTION
This script is used to do statistical analyses on profiles produced by MASS1.
This script has primarily been used when profiles are dumped at daily
intervals from MASS1. The statistical analyses would probably not be
relevant otherwise.
This script is part of the @PACKAGE@ distribution (Version @VERSION@)
=head2 Operation Modes
=over 4
=item B<By Rivermile>
Statistics are computed at each rivermile, and reported by rivermile.
=item B<At a Single Rivermile>
Statistics are computed for each day of the year (1 through 366) at
the specified rivermile.
=item B<For a Single Day>
Statistics are computed for each rivermile, but only for those values
which fall in the day of year specified.
=item B<For a Single Month>
Statistics are computed for each rivermile, but only for those values
which fall in the month specified.
=back
=head2 Field Names
=over 4
=item I<wselev>
water surface elevation,
=item I<disch>
discharge,
=item I<vel>
average cross section velocity,
=item I<depth>
cross section hydraulic depth,
=item I<conc>
contaminent concentration, and
=item I<temp>
temperature.
=back
=head1 OPTIONS
=over 4
=item B<-v>
if included B<doprofile.pl> will produce a message on standard error
noting which profile time plane is being read
=item B<-f> I<field_name>
selects the profile field for which statistics are computed (see
L<Field Names> above)
=item B<-X> I<file>
=item B<-r> I<rivermile>
select a single rivermile for which statistics are computed, producing
a table of statistics for each day of the year
=item B<-d> I<day>
select a single day of year for which statistics are computed,
producing a table of statistics for each rivermile
=back
=head1 INTERNALS
=over 4
=item Perl Modules Required
L<Getopt::Std;>, L<Date::Manip>
=back
=cut
# -------------------------------------------------------------
# Required Modules
# -------------------------------------------------------------
use strict;
use Getopt::Std;
use Date::Manip;
# -------------------------------------------------------------
# variable initialization
# -------------------------------------------------------------
my $program;
($program = $0) =~ s/.*[\/\\]//;
my $usage;
$usage =
"usage: ${program} [-r rm | -d day | -l value ] [-L value] [-v] [-f fld] [-X date_list_file] [-o output] [file]";
# initialize Date::Manip
Date::Manip::Date_Init('SetDate=now,UTC');
# this will hold the data for which
# statistics are computed
my $profiledat = {};
# fields in profile file
my $fldind =
{
rm => 3, wselev => 4, disch => 5, vel => 6,
depth => 7, conc => 8, temp => 9, sat => 10,
press => 11, thalweg => 12, area => 13, twidth => 14
};
# percentiles to compute
my @percentiles = (0.0, 0.05, 0.10, 0.25, 0.50, 0.75, 0.90, 0.95, 1.00);
# choices are: byrm, onedate,
# startlevel, endlevel, onerm, and one
# profile
my $mode = 'byrm';
# operation mode variables
my $verbose = 0;
my $field = $fldind->{temp};
my $xdatelist = undef;
my $dorm = undef;
my $dodate = undef;
my $domonth = undef;
my $dolevel = undef;
my $dolevelcnt;
my $default_level_cnt = 7;
# -------------------------------------------------------------
# sub ReadDateList
#
# This routine reads, from a file, a list of date ranges. Each line
# of the file contains 4 fields: start date, start time, end date, end
# time. The fields need to be readable by Date::Manip::ParseDate. If
# all of the ranges are read successfully, a reference to an array is
# returned, otherwise undef is returned.
# -------------------------------------------------------------
sub ReadDateList {
my $fname = shift;
my $dl = [];
my $cnt = 0;
unless (open(IN, "<$fname")) {
printf(STDERR "${main::program}: error: unable to open file \"%s\" for reading\n",
$fname);
return undef;
}
while (<IN>) {
my @fld = split;
my $start = Date::Manip::ParseDate($fld[0] . $fld[1]);
my $end = Date::Manip::ParseDate($fld[2] . $fld[3]);
if (! $start) {
printf(STDERR "${main::program}: error: unable to parse date: $start\n");
return undef;
}
if (! $end) {
printf(STDERR "${main::program}: error: unable to parse date: $end\n");
return undef;
}
push(@{$dl}, [ $start, $end ]);
}
close(IN);
return $dl;
}
# -------------------------------------------------------------
# EndOfYear
# This routine determines if the specified date is the last day of the
# year.
# -------------------------------------------------------------
sub EndOfYear {
my $date = shift; # a parsed date
my $year = Date::Manip::UnixDate($date, "%Y") + 0.0;
my $day = Date::Manip::UnixDate($date, "%j") + 0.0;
if (Date::Manip::Date_LeapYear($year)) {
return ($day == 366);
}
return ($day == 365);
}
# -------------------------------------------------------------
# DateGood
# This routine looks for the passed date in the date list ranges. If
# the date falls in any of the ranges, undef is returned.
# -------------------------------------------------------------
sub DateGood {
my $date = shift; # a parsed date
my $dl = shift; # built by ReadDateList
my $i;
foreach $i (@{$dl}) {
my $date1 = $i->[0];
my $date2 = $i->[1];
if (($date ge $date1) and ($date le $date2) ) {
return 0;
}
}
return 1;
}
# -------------------------------------------------------------
# handle command line
# -------------------------------------------------------------
my %opts = ();
die "$usage\n" unless getopts("vf:o:r:X:d:m:l:L:n:x:", \%opts);
$verbose = 1 if ($opts{v});
if ($opts{r}) {
$dorm = $opts{r};
$mode = 'onerm';
} elsif ($opts{d}) {
my $tmpdate;
my $tmpdatestr = $opts{d};
if (! ($tmpdate = Date::Manip::ParseDate($tmpdatestr))) {
printf(STDERR
"$program: error: selected date \"%s\" for -d not understood\n",
$tmpdatestr);
die "$usage\n";
}
$dodate = Date::Manip::UnixDate($tmpdate, "%j");
$mode = 'onedate';
} elsif ($opts{l}) {
$dolevel = $opts{l};
$mode = 'startlevel';
} elsif ($opts{L}) {
$dolevel = $opts{L};
$mode = 'endlevel';
} elsif ($opts{x}) {
my $tmpdate;
my $tmpdatestr = $opts{x};
if (! ($tmpdate = Date::Manip::ParseDate($tmpdatestr))) {
printf(STDERR
"$program: error: selected date \"%s\" for -x not understood\n",
$tmpdatestr);
die "$usage\n";
}
$dodate = Date::Manip::UnixDate($tmpdate, "%q");
$mode = 'oneprofile';
} elsif ($opts{m}) {
$domonth = $opts{m};
if ($domonth <= 0 || $domonth > 12) {
printf(STDERR
"$program: error: month number \"%s\" for -m not understood\n",
$opts{m});
die "$usage\n";
}
$mode = 'onemonth';
} else {
$mode = 'byrm';
}
if ($opts{n}) {
$dolevelcnt = $opts{n};
} else {
$dolevelcnt = $default_level_cnt;
}
if ($opts{o}) {
my $name = $opts{o};
unless (open(OUTPUT, ">$name")) {
printf(STDERR "$program: error: unable to open file \"%s\" for writing\n",
$name);
die "$usage\n";
}
} else {
unless (open(OUTPUT, ">&STDOUT")) {
die "$program: error: Unable to dup STDOUT\n";
}
}
if ($opts{X}) {
my $name = $opts{X};
$xdatelist = ReadDateList($name);
unless($xdatelist) {
printf(STDERR "$program: error: error reading exclude date list in \"$name\"\n");
die "$usage\n";
}
} else {
$xdatelist = [];
}
if ($opts{f}) {
my $test = $opts{f};
my $found = 0;
foreach (keys %{$fldind}) {
if ($_ eq $test) {
$field = $fldind->{$test};
$found = 1;
last;
}
}
if (! $found) {
printf(STDERR "$program: error: specified field (-f %s) not understood\n", $test);
die "$usage\n";
}
}
# -------------------------------------------------------------
# main program
# -------------------------------------------------------------
my $rm;
my $date = 0;
my $jdate;
my $lastdate = 0;
my $datestr;
my $dategood = undef;
my $year = undef;
my $lastyear = undef;
my $endofyear = undef;
my $levelcnt = undef;
my $leveldone = undef;
my $levelendcnt = undef;
while (<>) {
chop;
if (/Date:\s+(\d\d)-(\d\d)-(\d\d\d\d)\s+Time:\s+(\d\d):(\d\d):(\d\d)/) {
# handle the date and decide if we are interested in this date or
# not
$datestr = "$1/$2/$3 $4:$5:$6";
$lastdate = $jdate;
$date = Date::Manip::ParseDate($datestr);
$endofyear = EndOfYear($date);
$jdate = Date::Manip::UnixDate($date, "%j") + 0.0;
$dategood = DateGood($date, $xdatelist);
$year = Date::Manip::UnixDate($date, "%Y") + 0.0;
if ($year != $lastyear) {
$levelcnt = undef;
$levelendcnt = undef;
$leveldone = undef;
$lastyear = $year;
}
if ($mode eq 'onedate') {
$dategood = ($dategood && ($jdate == $dodate));
} elsif ($mode eq 'oneprofile') {
$dategood = ($dodate eq Date::Manip::UnixDate($date, "%q"));
} elsif ($mode eq 'onemonth') {
$dategood = ($domonth == Date::Manip::UnixDate($date, "%m") + 0);
} elsif ($mode eq 'startlevel' && $levelcnt ) {
my $test = 1;
my $i;
foreach $i (keys %{$levelcnt}) {
$test = ($test && (scalar(@{$levelcnt->{$i}}) > $dolevelcnt));
}
$leveldone = $test;
$dategood = ($dategood && not $leveldone);
} elsif ($mode eq 'endlevel' && $levelendcnt) {
my $test = 1;
my $i;
foreach $i (keys %{$levelendcnt}) {
$test = ($test && (scalar(@{$levelendcnt->{$i}}) > $dolevelcnt));
}
$leveldone = $test;
$dategood = ($dategood && not $leveldone);
}
if ($dategood) {
print STDERR "Processing $datestr \r" if ($verbose);
} else {
print STDERR "Skipping $datestr \r" if ($verbose);
}
} elsif (/^\s+\d+\s+\d+/ && ($jdate ne $lastdate) && $dategood) {
my @fld = split;
$rm = $fld[$fldind->{rm}];
if ($mode eq 'byrm' || $mode eq 'onedate' || $mode eq 'onemonth') {
if (!defined($profiledat->{$rm})) {
$profiledat->{$rm} = [];
}
push(@{$profiledat->{$rm}}, $fld[$field]);
} elsif ($mode eq 'onerm') {
if ($rm == $dorm) {
if (!defined($profiledat->{$jdate})) {
$profiledat->{$jdate} = [];
}
push(@{$profiledat->{$jdate}}, $fld[$field]);
}
} elsif ($mode eq 'startlevel' || $mode eq 'endlevel') {
$levelcnt = {} unless ($levelcnt);
$levelcnt->{$rm} = [] unless ($levelcnt->{$rm});
$levelendcnt = {} unless ($levelendcnt);
$levelendcnt->{$rm} = [] unless ($levelendcnt->{$rm});
if ($fld[$field] >= $dolevel) {
my $last;
if ($last = $levelcnt->{$rm}->[scalar(@{$levelcnt->{$rm}}) - 1]) {
if ($last - $jdate > 1) {
$levelcnt->{$rm} = [];
}
}
push(@{$levelcnt->{$rm}}, $jdate);
if ($mode eq 'startlevel' &&
scalar(@{$levelcnt->{$rm}}) == $dolevelcnt) {
if (!defined($profiledat->{$rm})) {
$profiledat->{$rm} = [];
}
push(@{$profiledat->{$rm}},
${$levelcnt->{$rm}}->[scalar(@{$levelcnt->{$rm}}) - 1]);
}
} elsif ($mode eq 'endlevel' &&
$fld[$field] < $dolevel &&
(scalar(@{$levelcnt->{$rm}}) >= $dolevelcnt ||
($endofyear))) {
my $last;
if ($last = $levelendcnt->{$rm}->[scalar(@{$levelendcnt->{$rm}}) - 1]) {
if ($last - $jdate > 1) {
$levelendcnt->{$rm} = [];
}
}
push(@{$levelendcnt->{$rm}}, $jdate) unless ($endofyear);
if (scalar(@{$levelendcnt->{$rm}}) == $dolevelcnt ||
($endofyear && scalar(@{$levelendcnt->{$rm}}) > 0)) {
if (!defined($profiledat->{$rm})) {
$profiledat->{$rm} = [];
}
push(@{$profiledat->{$rm}},
${$levelendcnt->{$rm}}->[0]);
}
}
} elsif ($mode eq 'oneprofile') {
die "This should not happen!\n";
} else {
die "This should not happen!\n";
}
}
}
print STDERR "\n" if ($verbose);
my $outkey;
foreach $outkey (sort {$a <=> $b} keys %{$profiledat}) {
my @lst = sort {$a <=> $b} @{$profiledat->{$outkey}};
my $cnt = scalar(@lst);
my $p;
if ($mode eq 'startlevel' || $mode eq 'endlevel') {
printf(OUTPUT "%10.2f%10.2f%12d", $outkey, $dolevel, $cnt);
} else {
printf(OUTPUT "%10.2f%12d", $outkey, $cnt);
}
foreach $p (@percentiles) {
my $i = int((1.0 - $p)*($cnt - 1) + 0.5);
$i = 0 if ($i < 0);
$i = $cnt - 1 if ($i > $cnt - 1);
printf(OUTPUT "%12.2f", $lst[$i]);
}
my $sum = 0.0;
my $sum2 = 0.0;
foreach $p (@lst) {
$sum += $p;
$sum2 += $p**2;
}
printf(OUTPUT "%12.2f", $sum/$cnt); # mean
printf(OUTPUT "%12.2f", sqrt($sum2/$cnt - ($sum/$cnt)**2)); # std. dev.
printf(OUTPUT "\n");
}