3 # t-avg - Calculates statistics of a **series** of piv data.
5 # Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
6 # Gerber van der Graaf <gerber_graaf@users.sourceforge.net
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software Foundation,
20 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #------------------------------------------------------------------
23 $VERSION = q
$Id: t
-avg
.pl
,v
1.6 2006-03-04 12:37:08 gerber Exp
$;
24 $HELP = "Calculates time-avaraged mean and rms from a **series** of piv data";
25 $USAGE = "gpiv_t-avg [-h|-help] [-n|-none] [-p|-print] [-relrms] [-uv] [-v|-version]
26 [-dn|dir_name] [-fd|-first_dir N] [-ld|-last_dir N] [-dx|dir_prefix]
27 [-fb|-file_basename] [-fext|-file_extension] [-ff|-first_file N]
28 [-fl|-last_file N] [-fx|file_prefix]
33 -n: suppresses real execution
34 -p: prints process parameters/variables to stdout
35 -relrms: prints relative rms, related to the absolute mean value
36 -uv: writes u and v values to separate outputs
38 -dn: directory name to be processed
39 -df: starting at first directory number N
40 -dl: ending at last directory number N
41 -dx: prefix numbering to directory base name
42 -fb: file base name to be processed
43 -fext: add an extension after the file basename + number (without
45 -ff: starting at first file number N
46 -fl: ending at last file number N
47 -fx: prefix numbering to file base name
50 #----------------- Command arguments handling ----------
61 GetOptions
('h|help', 'n|none', 'p|print', 'v|version',
64 'dx|dir_prefix', 'fx|file_prefix',
65 'fb|file_basename=s' => \
$file_base_name,
66 'fext|file_extension=s' => \
$file_ext,
67 'ff|first_file=i' => \
$file_first_nr,
68 'fl|last_file=i' => \
$file_last_nr,
69 'dn|dir_name=s' => \
$dir_base_name,
70 'df|dir_first=i' => \
$dir_first_nr,
71 'dl|dir_last=i' => \
$dir_last_nr);
86 print ("Usage: $USAGE\n");
90 #---------------------------------------------------- Initializing variables
94 if (!$file_base_name) {$file_base_name = "";}
95 if (!$file_first_nr) {$file_first_nr = 0;} # First data set to be scanned
96 if (!$file_last_nr) {$file_last_nr = 2; } # Last data set to be scanned
99 if (!$dir_base_name) {
100 $dir_base_name_defined = 0; $dir_base_name = "./";
102 $dir_base_name_defined = 1;
104 if (!$dir_first_nr) {$dir_first_nr = 0;}
105 if (!$dir_last_nr) {$dir_last_nr = 0;}
110 $outfile_A = $dir_base_name.$file_base_name.".ta_U".".$file_ext".".piv";
111 $outfile_B = $dir_base_name.$file_base_name.".ta_V".".$file_ext".".piv";
113 $outfile_A = $dir_base_name.$file_base_name.".ta_U.piv";
114 $outfile_B = $dir_base_name.$file_base_name.".ta_V.piv";
118 $outfile_A = $dir_base_name.$file_base_name.".ta_mean".".$file_ext".".piv";
119 $outfile_B = $dir_base_name.$file_base_name.".ta_sdev".".$file_ext".".piv";
121 $outfile_A = $dir_base_name.$file_base_name.".ta_mean.piv";
122 $outfile_B = $dir_base_name.$file_base_name.".ta_sdev.piv";
126 if ($opt_n || ($file_base_name && $opt_p)) {
127 print ("\nfile_base_name=$file_base_name file_first_nr=$file_first_nr file_last_nr=$file_last_nr\n");
130 if ($opt_n || ($dir_base_name && $opt_p)) {
131 print ("\ndir_base_name=$dir_base_name dir_nr_first=$dir_first_nr dir_nr_last=$dir_last_nr\n");
134 #--------------------------------------------- Print filenames
135 if ($opt_p || $opt_n) {
136 printf ("Output: %s\n", $file_name);
138 printf ("Data U-file: %s\n", $outfile_A);
139 printf ("Data V-file: %s\n", $outfile_B);
141 printf ("Data mean file: %s\n", $outfile_A);
142 printf ("Data svar file: %s\n", $outfile_B);
146 #---------------------------------------- loops for directory and data sets:
147 for ($dir_nr = $dir_first_nr; $dir_nr <= $dir_last_nr; $dir_nr++) {
149 if ($dir_base_name_defined) {
151 $target_dir = $dir_base_name.$dir_nr."/";
153 $target_dir = $dir_nr.$dir_base_name."/";
156 $target_dir = $dir_base_name;
159 if ($opt_p || $opt_n) {
160 printf ("\ntarget_dir[%d] = %s\n", $dir_nr, $target_dir);
163 for ($file_nr = $file_first_nr; $file_nr <= $file_last_nr; $file_nr++) {
167 $file_name=$target_dir.$file_base_name.$file_nr;
168 if ($file_ext) {$file_name=$file_name.".$file_ext"};
171 $file_name=$target_dir.$file_nr.$file_base_name;
172 if ($file_ext) {$file_name=$file_name.".$file_ext"};
175 $file_name_piv = $file_name.".piv";
176 $infile = $file_name_piv;
179 #--------------------------------------------- Print input filename
180 if ($opt_p || $opt_n) {
181 printf ("Data piv file: %s\n", $file_name_piv);
185 open (IN
,"$infile") || die 'can\'t open $infile';
188 #----------------------------------------- Starts reading the lines with data
191 #---------------------------------- skip blank lines or ; sign at first col
192 if ($_ ne "" && (!/^(\#|;)/)) {
193 #------------------------------------ substitutes eventually komma's by dot
194 #------------------------------ for example at LaVision's PIV program DaVis
196 #---------------------------------------- removes eventually leading spaces
198 #------------------- splitting line $_ up in words; #same as @words = split
199 @words = split(/\s+/,$_);
202 #------------ As the total number of data is unknown in forward, the variables
203 # are set to zero when the first data set is scanned. Provided all data sets
204 # will have the same number (and positions!!)
213 $x_pos[$k] = $words[0];
214 $y_pos[$k] = $words[1];
217 $snr[$k] = $words[4];
218 $peak_nr[$k] = $words[5];
220 # to avoid warning message $snr is used twice
223 #------------------------------------------------ Take sum and sum of squares
224 if ($peak_nr[$k] != -1) {
225 $total_dx[$k] += $dx[$k];
226 $total_dy[$k] += $dy[$k];
227 $total_dx_2[$k] += $dx[$k]**2;
228 $total_dy_2[$k] += $dy[$k]**2;
238 printf ("\ndata = %d\n", $ndata);
243 close (IN
) || die 'can\'t close $infile';
248 #-------------------------------------- Calculating mean and rms or variances
250 for ($k=0; $k < $ndata; $k++) {
251 if ($count[$k] > 1) {
252 $mean_x[$k]= $total_dx[$k]/$count[$k];
253 $svar_x[$k]= sqrt(($total_dx_2[$k]/($count[$k]-1) -
254 $total_dx[$k]**2 / ($count[$k]*($count[$k]-1))));
256 $mean_y[$k]= $total_dy[$k]/$count[$k];
257 $svar_y[$k]= sqrt(($total_dy_2[$k]/($count[$k]-1) -
258 $total_dy[$k]**2 / ($count[$k]*($count[$k]-1))));
268 $mean_xy[$k] = sqrt(($mean_x[$k]**2 + $mean_y[$k]**2));
269 $svar_xy[$k] = sqrt(($svar_x[$k]**2 + $svar_y[$k]**2));
275 #----------------------------------------------------------- Writing results
276 open (OUT_A
,">$outfile_A") || die 'can\'t open $outfile_A';
277 open (OUT_B
,">$outfile_B") || die 'can\'t open $outfile_B';
279 printf(OUT_A
"#$VERSION");
280 printf(OUT_A
"\n# x y x_mean x_sdev count");
281 printf(OUT_B
"#$VERSION");
282 printf(OUT_B
"\n# x y y_mean y_sdev count");
284 for ($k=0; $k < $ndata; $k++) {
285 printf(OUT_A
"\n%-f %-f %-f %-f %-f %-2d",
286 $x_pos[$k], $y_pos[$k], $mean_x[$k], $svar_x[$k], $count[$k]);
288 printf(OUT_B
"\n%-f %-f %-f %-f %-f %-2d",
289 $x_pos[$k], $y_pos[$k], $mean_y[$k], $svar_y[$k], $count[$k]);
292 } elsif ($opt_relrms) {
293 printf(OUT_A
"#$VERSION");
294 printf(OUT_A
"\n# x y x_mean y_mean sdev/mean count");
295 printf(OUT_B
"#$VERSION");
296 printf(OUT_B
"\n# x y x_sdev y_sdev sdev sdev/mean count");
298 for ($k=0; $k < $ndata; $k++) {
299 printf(OUT_A
"\n%f %f %f %f %f %d",
300 $x_pos[$k], $y_pos[$k], $mean_x[$k], $mean_y[$k],
302 $svar_xy[$k] / $mean_xy[$k],
305 printf(OUT_B
"\n%f %f %f %f %f %f %d",
306 $x_pos[$k], $y_pos[$k], $svar_x[$k], $svar_y[$k],
307 $svar_xy[$k], $svar_xy[$k] / $mean_xy[$k], $count[$k]);
311 printf(OUT_A
"#$VERSION");
312 printf(OUT_A
"\n# x y x_mean y_mean sdev count");
313 printf(OUT_B
"#$VERSION");
314 printf(OUT_B
"\n# x y x_sdev y_sdev sdev count");
316 for ($k=0; $k < $ndata; $k++) {
317 printf(OUT_A
"\n%-f %-f %-f %-f %-f %-2d",
318 $x_pos[$k], $y_pos[$k], $mean_x[$k], $mean_y[$k],
319 $svar_xy[$k], $count[$k]);
321 printf(OUT_B
"\n%-f %-f %-f %-f %-f %-2d",
322 $x_pos[$k], $y_pos[$k], $svar_x[$k], $svar_y[$k],
323 $svar_xy[$k], $count[$k]);
327 close (OUT_A
) || die 'can\'t close $outfile';
328 close (OUT_B
) || die 'can\'t close $outfile';