PD02grd
author A.M. Thurnherr <athurnherr@yahoo.com>
Tue, 29 Jun 2021 12:21:32 -0400
changeset 59 4f4530fa35da
parent 41 d7ab920c1de6
permissions -rwxr-xr-x
V2.4 - New Features: - support for Nortek PD0 files - patchPD0 support for moored ADCP data - bug fixes & minor improvements
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     1
#!/usr/local/bin/perl
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     2
#======================================================================
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
     3
#                    P D 0 2 G R D 
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     4
#                    doc: Wed Aug 30 11:51:22 2006
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
     5
#                    dlm: Wed Dec  6 09:18:49 2017
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     6
#                    (c) 2006 A.M. Thurnherr
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
     7
#                    uE-Info: 33 21 NIL 0 0 72 2 2 4 NIL ofnI
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     8
#======================================================================
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     9
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
    10
# make GMT grd files from RDI PD0 file
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    11
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    12
# HISTORY:
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    13
#	Aug 30, 2006: - created at end of GRAVILUCK cruise
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    14
#	Aug 31, 2006: - BUG: ensembles/bins were numbered from 0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    15
#				  - added -d)imensional coords
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    16
#	Sep  1, 2006: - fiddled with registration
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    17
#	Sep 19, 2007: - adapted to new [RDI_BB_Read.pl]
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    18
#	Jun 18, 2009: - BUG: xysize had been called xyside
33
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
    19
#   Mar 17, 2016: - adapted to new Getopt library
34
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 33
diff changeset
    20
#	May 19, 2016: - adapted to velBeamToEarth()
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
    21
#	Dec  6, 2017: - renamed from RDI2grd
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    22
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    23
# NOTES:
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    24
#	- regular grids only => no dimensional time axis for data collected
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    25
#	  in multi-ensemble burst mode!
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    26
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    27
# TODO:
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    28
#	- implement soundspeed corretion
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    29
#	- add temporal pre-averaging
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    30
33
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
    31
use Getopt::Std;
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    32
$0 =~ m{(.*/)[^/]+};
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
    33
require "$1RDI_PD0_IO.pl";
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    34
require "$1RDI_Coords.pl";
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    35
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    36
use NetCDF;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    37
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    38
sub dumpVar($$$$$)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    39
{
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    40
	my($var,$units,$long_name,$fname,$dimnum) = @_;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    41
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    42
	my($ncId) = NetCDF::create("$opt_b$var.grd",NetCDF::CLOBBER);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    43
	NetCDF::setfill($ncId,NetCDF::NOFILL);				# NetCDF library bug
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    44
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    45
	my($sid) = NetCDF::dimdef($ncId,'side',2);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    46
	my($aid) = NetCDF::dimdef($ncId,'xysize',($le-$fe+1)*($lastBin-$firstBin+1));
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    47
	    
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    48
	my($xrid) = NetCDF::vardef($ncId,'x_range',NetCDF::DOUBLE,[$sid]);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    49
	my($yrid) = NetCDF::vardef($ncId,'y_range',NetCDF::DOUBLE,[$sid]);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    50
	my($zrid) = NetCDF::vardef($ncId,'z_range',NetCDF::DOUBLE,[$sid]);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    51
	my($spid) = NetCDF::vardef($ncId,'spacing',NetCDF::DOUBLE,[$sid]);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    52
	my($dmid) = NetCDF::vardef($ncId,'dimension',NetCDF::LONG,[$sid]);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    53
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    54
	my($zvid) = NetCDF::vardef($ncId,'z',NetCDF::FLOAT,[$aid]);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    55
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    56
	NetCDF::attput($ncId,NetCDF::GLOBAL,'title',NetCDF::CHAR,$ARGV[0]);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    57
	NetCDF::attput($ncId,NetCDF::GLOBAL,'source',NetCDF::CHAR,$usage);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    58
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    59
	NetCDF::attput($ncId,$xrid,'units',NetCDF::CHAR,
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    60
		$opt_d ? 'day number' : 'ensemble number');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    61
	NetCDF::attput($ncId,$yrid,'units',NetCDF::CHAR,
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    62
		$opt_d ? 'm' : 'bin number');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    63
	NetCDF::attput($ncId,$zrid,'units',NetCDF::CHAR,$units);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    64
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    65
	NetCDF::attput($ncId,$zvid,'long_name',NetCDF::CHAR,$long_name);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    66
	NetCDF::attput($ncId,$zvid,'scale_factor',NetCDF::DOUBLE,1);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    67
	NetCDF::attput($ncId,$zvid,'add_offset',NetCDF::DOUBLE,0);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    68
	NetCDF::attput($ncId,$zvid,'node_offset',NetCDF::LONG,0);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    69
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    70
	NetCDF::endef($ncId);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    71
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    72
	if ($opt_d) {											# dimensional grid
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    73
		my($ft) = $dta{ENSEMBLE}[$fe]->{DAYNO};				# x_range(side)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    74
		my($lt) = $dta{ENSEMBLE}[$le]->{DAYNO};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    75
		NetCDF::varput1($ncId,$xrid,0,$ft);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    76
		NetCDF::varput1($ncId,$xrid,1,$lt);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    77
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    78
		NetCDF::varput1($ncId,$yrid,0,				 		# y_range(side)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    79
			$dta{DISTANCE_TO_BIN1_CENTER} + $firstBin*$dta{BIN_LENGTH});
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    80
		NetCDF::varput1($ncId,$yrid,1,
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    81
			$dta{DISTANCE_TO_BIN1_CENTER} + $lastBin*$dta{BIN_LENGTH});
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    82
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    83
		NetCDF::varput1($ncId,$spid,0,($lt-$ft)/($le-$fe));	# spacing(side)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    84
	    NetCDF::varput1($ncId,$spid,1,$dta{BIN_LENGTH});
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    85
	} else {												# non-dim grid
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    86
		NetCDF::varput1($ncId,$xrid,0,$fe+1);				# x_range(side)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    87
		NetCDF::varput1($ncId,$xrid,1,$le+1);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    88
		NetCDF::varput1($ncId,$yrid,0,$firstBin+1); 		# y_range(side)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    89
		NetCDF::varput1($ncId,$yrid,1,$lastBin+1);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    90
		NetCDF::varput1($ncId,$spid,0,1);					# spacing(side)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    91
	    NetCDF::varput1($ncId,$spid,1,1);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    92
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    93
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    94
	NetCDF::varput1($ncId,$dmid,0,$le-$fe+1);				# dimension(side)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    95
	NetCDF::varput1($ncId,$dmid,1,$lastBin-$firstBin+1);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    96
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    97
	my($min) =	9e99;										# z(xyside)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    98
	my($max) = -9e99;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    99
	my(@data);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   100
	for (my($b)=$lastBin; $b>=$firstBin; $b--) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   101
		for (my($e)=$fe; $e<=$le; $e++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   102
			my($v) = $dta{ENSEMBLE}[$e]->{$fname}[$b][$dimnum];
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   103
			$v = nan unless defined($v);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   104
			$min = $v if ($v < $min);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   105
			$max = $v if ($v > $max);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   106
			push(@data,$v);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   107
		}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   108
	}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   109
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   110
	my(@start) = (0);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   111
	my(@count) = (scalar(@data));
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   112
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   113
	NetCDF::varput($ncId,$zvid,\@start,\@count,\@data);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   114
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   115
	NetCDF::varput1($ncId,$zrid,0,$min);					# z_range(side)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   116
	NetCDF::varput1($ncId,$zrid,1,$max);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   117
	
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   118
	NetCDF::close($ncId);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   119
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   120
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   121
#------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   122
# Usage
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   123
#------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   124
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   125
$usage = "$0 @ARGV";
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   126
die("Usage: $0 [-M)agnetic <declination>] [-r)ange <first_ens,last_ens>] " .
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   127
			  "[output -b)ase <name>] [-d)imensional coordinates] " .
41
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 34
diff changeset
   128
			  "<PD0 file>\n")
33
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 0
diff changeset
   129
	unless (&getopts("b:dM:r:") && @ARGV == 1);
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   130
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   131
print(STDERR "WARNING: magnetic declination not set!\n")
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   132
	unless defined($opt_M);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   133
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   134
unless (defined($opt_b)) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   135
	$opt_b = "$ARGV[0]_";
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   136
	$opt_b =~ m{(.*)\.\d\d\d};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   137
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   138
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   139
($first_ens,$last_ens) = split(',',$opt_r)
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   140
	if defined($opt_r);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   141
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   142
#----------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   143
# Read Data
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   144
#----------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   145
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   146
print(STDERR "Reading $ARGV[0]...");
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   147
readData($ARGV[0],\%dta);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   148
printf(STDERR "%d complete ensembles\n",scalar(@{$dta{ENSEMBLE}}));
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   149
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   150
#--------------------------------------------------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   151
# Find Good Ensemble Range & Make Earth Coordinates
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   152
#--------------------------------------------------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   153
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   154
print(STDERR "Checking/transforming data...");
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   155
$dta{HEADING_BIAS} = -$opt_M;						# magnetic declination
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   156
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   157
if ($dta{BEAM_COORDINATES}) {						# coords used
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   158
	$beamCoords = 1;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   159
} elsif (!$dta{EARTH_COORDINATES}) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   160
	die("$ARGV[0]: only beam and earth coordinates implemented so far\n");
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   161
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   162
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   163
$lastGoodBin = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   164
for ($e=0; $e<=$#{$dta{ENSEMBLE}}; $e++) {			# check/transform velocities
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   165
	next if (defined($first_ens) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   166
			 $dta{ENSEMBLE}[$e]->{NUMBER} < $first_ens);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   167
	$P{first_ens} = $dta{ENSEMBLE}[$e]->{NUMBER},$fe = $e
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   168
		unless defined($P{first_ens});
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   169
	last if (defined($last_ens) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   170
			 $dta{ENSEMBLE}[$e]->{NUMBER} > $last_ens);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   171
	$P{last_ens} = $dta{ENSEMBLE}[$e]->{NUMBER};
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   172
	$le = $e;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   173
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   174
	die("3-beams used in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n")
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   175
		if ($dta{ENSEMBLE}[$e]->{N_BEAMS_USED} < 4);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   176
	die("BIT error in ensemble $dta{ENSEMBLE}[$e]->{NUMBER}\n")
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   177
		if defined($dta{ENSEMBLE}[$e]->{BUILT_IN_TEST_ERROR});
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   178
	die("Low gain in ensemble #$dta{ENSEMBLE}[$e]->{NUMBER}\n")
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   179
        if ($dta{ENSEMBLE}[$e]->{LOW_GAIN});
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   180
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   181
	for (my($b)=0; $b<$dta{N_BINS}; $b++) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   182
		next unless (defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][0]) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   183
					 defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][1]) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   184
					 defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][2]) &&
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   185
					 defined($dta{ENSEMBLE}[$e]->{VELOCITY}[$b][3]));
34
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 33
diff changeset
   186
		@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]} =
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 33
diff changeset
   187
			$beamCoords ? velBeamToEarth(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]})
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 33
diff changeset
   188
					    : velApplyHdgBias(\%dta,$e,@{$dta{ENSEMBLE}[$e]->{VELOCITY}[$b]});
0
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   189
		$dta{ENSEMBLE}[$e]->{GOOD_VEL}[$b] = 1;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   190
		$good_vels[$b]++;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   191
		$lastGoodBin = $b if ($b > $lastGoodBin);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   192
		$firstGoodEns = $e unless defined($firstGoodEns);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   193
		$lastGoodEns = $e;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   194
    }
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   195
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   196
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   197
unless (defined($opt_r)) {
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   198
	$fe = $firstGoodEns;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   199
	$le = $lastGoodEns;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   200
}
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   201
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   202
$firstBin = 0;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   203
$lastBin = $lastGoodBin;
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   204
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   205
print(STDERR "\n");
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   206
print(STDERR "Start: $dta{ENSEMBLE}[$fe]->{DATE} $dta{ENSEMBLE}[$fe]->{TIME}\n");
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   207
print(STDERR "End  : $dta{ENSEMBLE}[$le]->{DATE} $dta{ENSEMBLE}[$le]->{TIME}\n");
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   208
printf(STDERR "Bins : %d-%d\n",$firstBin+1,$lastBin+1);
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   209
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   210
#-----------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   211
# Write Data
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   212
#-----------
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   213
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   214
print(STDERR "Writing NetCDF files");
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   215
&dumpVar('u','[m/s]','eastward velocity' ,'VELOCITY',0); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   216
&dumpVar('v','[m/s]','northward velocity','VELOCITY',1); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   217
&dumpVar('w','[m/s]','vertical velocity' ,'VELOCITY',2); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   218
&dumpVar('e','[m/s]','error velocity'    ,'VELOCITY',3); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   219
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   220
&dumpVar('ea1','[count]','beam-1 echo amplitude','ECHO_AMPLITUDE',0); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   221
&dumpVar('ea2','[count]','beam-2 echo amplitude','ECHO_AMPLITUDE',1); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   222
&dumpVar('ea3','[count]','beam-3 echo amplitude','ECHO_AMPLITUDE',2); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   223
&dumpVar('ea4','[count]','beam-4 echo amplitude','ECHO_AMPLITUDE',3); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   224
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   225
&dumpVar('corr1','[count]','beam-1 correlation','CORRELATION',0); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   226
&dumpVar('corr2','[count]','beam-2 correlation','CORRELATION',1); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   227
&dumpVar('corr3','[count]','beam-3 correlation','CORRELATION',2); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   228
&dumpVar('corr4','[count]','beam-4 correlation','CORRELATION',3); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   229
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   230
&dumpVar('pcg1','[count]','beam-1 %-good','PERCENT_GOOD',0); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   231
&dumpVar('pcg2','[count]','beam-2 %-good','PERCENT_GOOD',1); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   232
&dumpVar('pcg3','[count]','beam-3 %-good','PERCENT_GOOD',2); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   233
&dumpVar('pcg4','[count]','beam-4 %-good','PERCENT_GOOD',3); print(STDERR '.');
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   234
print(STDERR "\n");
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   235
229a0d72d2ab first hg version
A.M. Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   236
exit(0);