patchPD0
author A.M. Thurnherr <athurnherr@yahoo.com>
Tue, 29 Jun 2021 12:21:32 -0400
changeset 59 4f4530fa35da
parent 52 5b07a9b89aee
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:
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     1
#!/usr/bin/perl
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     2
#======================================================================
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     3
#                    P A T C H P D 0 
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     4
#                    doc: Tue Aug 23 20:00:15 2016
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
     5
#                    dlm: Tue Apr 14 21:28:49 2020
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     6
#                    (c) 2010 A.M. Thurnherr
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
     7
#                    uE-Info: 103 26 NIL 0 0 72 2 2 4 NIL ofnI
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     8
#======================================================================
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
     9
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    10
$antsSummary = 'patch TRDI PD0 file with external attitude data';
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    11
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    12
# History:
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    13
#	Aug 23, 2016: - exported from IMP+LADCP
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    14
#	Aug 25, 2016: - completed basic structure
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    15
#	Nov 20, 2017: - major code cleanup
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    16
#				  - added -d) to keep original data source id
42
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    17
#	Dec  9, 2017: - added $antsSuppressCommonOptions = 1;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    18
#	Dec 23, 2017: - added support for -c
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    19
#				  - BUG: not backward compatible with old IMP files any more
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
    20
#	Jun 13, 2017: - added pitch and roll to -o
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
    21
#				  - BUG: ??? does -o handle pitch and roll ANOMALIES correctly?
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
    22
#	Jun 30, 2019: - -o did not work with single argument
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    23
#	Apr 14, 2020: - adapted to use for moored ADCP data as well
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    24
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    25
# PATCH-FILE REQUIREMENTS (ANTS format)
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    26
#	- %[L]ADCP_pitch.mu %[L]ADCP_roll.mu	mean ADCP pitch and roll
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    27
#	- %IM[UP]_hdg_offset					heading offset of external IMU
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    28
#	- [L]ADCP_ens							ADCP ensemble number
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    29
#	- pitch, roll							external pitch/roll *anomalies* 
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    30
#	- hdg									external heading rotated into ADCP coord system
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    31
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    32
# PATCHED PD0 FILE:
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    33
#
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    34
#	- pitch = RDI_pitch(mean_LADCP_gimbal_pitch + rotated_external_pitch_anomaly)
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    35
#	- roll  = mean_LADCP_roll + rotated_external_roll_anomaly
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    36
#	- hdg	= external_hdg - heading_offset
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    37
#
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    38
#	- unless -d is used, every patched ensemble has set the DATA_SOURCE_ID as follows; 
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    39
#	  PROCESSING SOFTWARE NEEDS TO BE ABLE TO DEAL WITH DSID values != 0x7F
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    40
#		0xA0	no values patched
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    41
#		0xA1	heading patched
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    42
#		0xA2	roll patched
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    43
#		0xA3	roll & heading patched
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    44
#		0xA4	pitch patched
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    45
#		0xA5	pitch & heading patched
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    46
#		0xA6	pitch & roll patched
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    47
#		0xA7	pitch, roll & heading patched
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    48
#
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    49
#	- additionally, all velocities from ensembles with missing pitch/roll/heading
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    50
#	  values are removed unless -k is set
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    51
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    52
($ANTS)    = (`which ANTSlib` =~ m{^(.*)/[^/]*$});
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    53
($ADCP_TOOLS) = ($0 =~ m{^(.*)/[^/]*$});
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    54
42
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    55
$antsMinLibVersion = 7.0;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    56
$ADCP_tools_minVersion  = 2.1;
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    57
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    58
require "$ANTS/ants.pl";
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    59
require "$ANTS/libvec.pl";
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    60
require "$ANTS/libstats.pl";
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    61
require "$ADCP_TOOLS/ADCP_tools_lib.pl";
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    62
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    63
$antsParseHeader = 0;
42
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    64
$antsSuppressCommonOptions = 1;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    65
&antsUsage('cdhko:pr',2,
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    66
	'[patch -p)itch] [-r)oll] [-h)eading] (none patches all)',
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
    67
	'[patch -c)lock with pre-Y2K RTC values]',
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
    68
	'[-o) <[pitch,roll,]heading-offset>] [-k)eep velocities of unpatched ensembles]',
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    69
	'[keep original -d)ata-source id]',
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    70
	'<original PD0 file> <patched PD0 file> [external attitude file]');
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    71
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    72
$opt_p = $opt_r = $opt_h = 1
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    73
	unless ($opt_p || $opt_r || $opt_h);
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    74
42
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    75
$RDI_PD0_IO::OVERRIDE_Y2K_CLOCK = $opt_c;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
    76
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    77
$ADCP_file  = &antsFileArg();
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    78
$outPD0 = $ARGV[0]; shift;
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    79
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    80
#----------------------------------------------------------------------
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    81
# Step 1: Read ADCP Data
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    82
#----------------------------------------------------------------------
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    83
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    84
readData($ADCP_file,\%ADCP);												# TRDI PD0 file
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    85
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    86
#----------------------------------------------------------------------
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    87
# Step 2: Process External Attidue Input to Patch PD0 file
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    88
#----------------------------------------------------------------------
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    89
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
    90
&antsIn();																	# load first IMP record
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    91
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    92
my($ensF) = &fnrNoErr('ADCP_ens');
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    93
$ensF = &fnr('LADCP_ens') unless defined($ensF);
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    94
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    95
my($pitchF) = &fnr('pitch');
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    96
my($rollF)	= &fnr('roll');
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    97
my($hdgF)	= &fnr('hdg');
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
    98
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
    99
my($ADCP_pitch_mean) = $P{'ADCP_pitch.mu'};
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   100
$ADCP_pitch_mean = &antsRequireParam('LADCP_pitch.mu')
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   101
	unless numberp($ADCP_pitch_mean);
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   102
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   103
my($ADCP_roll_mean) = $P{'ADCP_roll.mu'};
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   104
$ADCP_roll_mean = &antsRequireParam('LADCP_roll.mu')
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   105
	unless numberp($ADCP_roll_mean);
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   106
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   107
my($pofs,$rofs) = (0,0);													# apply externally supplied offset(s)
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   108
my($rho,$crho,$srho);												
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   109
if (defined($opt_o)) {
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   110
	my($pofs,$rofs,$hofs) = split(/,/,$opt_o);
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   111
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   112
	if (defined($rofs)) {													# pitch and roll offsets supplied
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   113
		croak("$0: cannot decode -o $opt_o\n")
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   114
			unless numbersp($pofs,$rofs,$hofs);
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   115
	} else {																# no pitch and roll, only heading
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   116
		$hofs = $pofs; 
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   117
		$pofs = undef;
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   118
    }
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   119
	croak("$0: cannot decode -o $opt_o\n")									
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   120
		unless numberp($hofs);
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   121
																			# set up heading correction
42
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
   122
	&antsAddParams('IMU_hdg_offset',$P{IMP_hdg_offset})						# backward compatibility
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 40
diff changeset
   123
		if defined($P{IMP_hdg_offset});
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   124
	$rho  = $hofs - &antsRequireParam('IMU_hdg_offset');					# calculate correction relative to already applied one 
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   125
	$crho = cos(rad($rho));
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   126
	$srho = sin(rad($rho));
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   127
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   128
	if (defined($pofs)) {													# rotate IMP pitch and roll offsets into new ADCP frame
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   129
		my($IMP_pitch_mean) =  &antsRequireParam('IMP_pitch.mu') * $crho
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   130
									+ &antsRequireParam('IMP_roll.mu') * $srho;
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   131
		my($IMP_roll_mean)  = -&antsRequireParam('IMP_pitch.mu') * $srho
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   132
									+ &antsRequireParam('IMP_roll.mu') * $crho;
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   133
		$ADCP_pitch_mean = $IMP_pitch_mean - $pofs;						# apply externally supplied offsets
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   134
		$ADCP_roll_mean  = $IMP_roll_mean  - $rofs;
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   135
	}
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   136
}
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   137
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   138
my($pr_missing,$hdg_missing) = (0,0);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   139
my($missing_pr_block_len,$missing_hdg_block_len);
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   140
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   141
do {
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   142
	my($ADCP_deployed);
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   143
	my($ens) = $P{RECNO};
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   144
	die("assertion failed [$ants_[0][$ensF] != $ADCP{ENSEMBLE}[$ens]->{NUMBER} --- 1-$ADCP{ENSEMBLE}[0]->{NUMBER} + $P{RECNO} + $d]")
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   145
		unless ($ants_[0][$ensF] == $ADCP{ENSEMBLE}[$ens]->{NUMBER});
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   146
	$ADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} = 0xA0;
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   147
	
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   148
	if (numbersp($ants_[0][$pitchF],$ants_[0][$rollF])) {					# valid IMP data -> patch ADCP ensemble
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   149
		$ADCP_deployed = 1;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   150
		$missing_pr_block_len = 0;
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   151
		if (defined($opt_o)) {												# -o set: rotate pitch and roll into correct coordinates
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   152
			my($rot_p) = ($ants_[$r][$pitchF]  * $crho +
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   153
						  $ants_[$r][$rollF]   * $srho);
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   154
			my($rot_r) = (-$ants_[$r][$pitchF] * $srho +
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   155
						   $ants_[$r][$rollF]  * $crho);
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   156
			$ants_[$r][$pitchF] = $rot_p;
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   157
			$ants_[$r][$rollF]	= $rot_r;
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   158
        } 
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   159
		if ($opt_p) {														# patch pitch
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   160
			$ADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= ($opt_p<<2);
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   161
			$ADCP{ENSEMBLE}[$ens]->{PITCH} = RDI_pitch($ADCP_pitch_mean + $ants_[0][$pitchF],
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   162
														$ADCP_roll_mean  + $ants_[0][$rollF]);
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   163
		}
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   164
		if ($opt_r) {														# patch roll
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   165
			$ADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= ($opt_r<<1);
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   166
			$ADCP{ENSEMBLE}[$ens]->{ROLL} = $ADCP_roll_mean + $ants_[0][$rollF];
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   167
		}
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   168
    } else {																# no valid IMP pitch and roll => invalidate ADCP data
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   169
    	$pr_missing++ if $ADCP_deployed;									# don't count missing before deployment
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   170
    	$missing_pr_block_len++;
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   171
		unless ($opt_k)  {
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   172
	    	clearEns(\%ADCP,$ens);
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   173
	    	$ADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID}= 0xA0;
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   174
	    }
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   175
    }
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   176
	    
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   177
    if (numberp($ants_[0][$hdgF])) {										# valid IMP heading
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   178
    	$ADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID} |= $opt_h;
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   179
		$missing_hdg_block_len = 0;
43
b63fa355644c commit to merge with changes from EN620
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 42
diff changeset
   180
   		if (defined($opt_o)) {												# apply offset on -o; otherwise, data are correctly rotated
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   181
	    	$ants_[0][$hdgF] -= $rho;
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   182
	    	$ants_[0][$hdgF] += 360 if ($ants_[0][$hdgF] < 0);
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   183
	    }
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   184
		$ADCP{ENSEMBLE}[$ens]->{HEADING} = $ants_[0][$hdgF]				# patch heading
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   185
			if $opt_h;
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   186
	} else {																# no valid IMP heading => invalidate ADCP data
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   187
		$hdg_missing++ if $ADCP_deployed;
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   188
		$missing_hdg_block_len++;
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   189
		unless ($opt_k)  {
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   190
	    	clearEns(\%ADCP,$ens);
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   191
	    	$ADCP{ENSEMBLE}[$ens]->{DATA_SOURCE_ID}= 0xA0;
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   192
	    }
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   193
	}
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   194
} while (&antsIn());
50
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   195
$pr_missing  -= missing_pr_block_len;										# don't count final block (post recovery)
A.M. Thurnherr <athurnherr@yahoo.com>
parents: 43
diff changeset
   196
$hdg_missing -= missing_hdg_block_len;
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   197
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   198
$ADCP{ENSEMBLE}[0]->{DATA_SOURCE_ID} = 0x7F;								# ensure correct DSID (1st ens: orig; 2nd ens: this prog)
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   199
$ADCP{ENSEMBLE}[1]->{DATA_SOURCE_ID} = 0xA0
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   200
	unless ($ADCP{ENSEMBLE}[1]->{DATA_SOURCE_ID}&0xF0 == 0xA0);
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   201
52
5b07a9b89aee - adapted to work with IMPed moored ADCP data
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents: 50
diff changeset
   202
writeData($outPD0,\%ADCP);													# write new PD0
40
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   203
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   204
my($verb) = $opt_k ? 'retained' : 'cleared';
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   205
printf(STDERR "$outPD0: %d pitch/roll & %d heading values $verb\n",$pr_missing,$hdg_missing)
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   206
       if ($pr_missing+$hdg_missing);
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   207
        
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   208
exit(0);
6a46e9d31106 after Hamburg visit
Andreas Thurnherr <ant@ldeo.columbia.edu>
parents:
diff changeset
   209