File: PredictionValue.cc

package info (click to toggle)
xtide 2.9.5-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,996 kB
  • ctags: 2,141
  • sloc: cpp: 20,379; sh: 1,044; makefile: 224; yacc: 114; lex: 58
file content (222 lines) | stat: -rw-r--r-- 5,499 bytes parent folder | download | duplicates (4)
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
// $Id: PredictionValue.cc 2641 2007-09-02 21:31:02Z flaterco $
/*
    PredictionValue:  A quantity in units of feet, meters, knots, or
    knots squared.  See also, Amplitude.

    Copyright (C) 1998  David Flater.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "common.hh"


PredictionValue::PredictionValue ():
  _value(0.0),
  _units(Units::zulu) {}


PredictionValue::PredictionValue (Units::PredictionUnits units,
                                  double value):
  _value(value),
  _units(units) {
  assert (value == 0.0 || units != Units::zulu);
}


PredictionValue::PredictionValue (Amplitude a):
  _value(a.val()),
  _units(a.Units()) {}


PredictionValue::PredictionValue (NullablePredictionValue npv):
  _value(npv.val()),
  _units(npv.Units()) {}


// No return.
static void convbarf (Units::PredictionUnits fromunits,
                      Units::PredictionUnits tounits) {
  Dstr details ("From ");
  details += Units::longName (fromunits);
  details += " to ";
  details += Units::longName (tounits);
  Global::barf (Error::IMPOSSIBLE_CONVERSION, details);
}


void PredictionValue::Units (Units::PredictionUnits units) {
  if (_units == units)
    Global::barf (Error::NO_CONVERSION, Error::nonfatal);
  else {
    switch (_units) {
    case Units::zulu:
      assert (_value == 0.0);
      break;
    case Units::feet:
      if (units == Units::meters)
	_value *= 0.3048;
      else
	convbarf (_units, units);
      break;
    case Units::meters:
      if (units == Units::feet)
	_value /= 0.3048;
      else
	convbarf (_units, units);
      break;
    case Units::knotsSquared:
      if (units == Units::knots) {
	// This is not mathematically correct, but it is tidematically correct.
	if (_value < 0)
	  _value = -sqrt(fabs(_value));
	else
	  _value = sqrt(_value);
      } else
	convbarf (_units, units);
      break;
    case Units::knots:
      if (units == Units::knotsSquared) {
	// This is used only in Station::predictTideEvents to set up
	// the mark level.
	// This is not mathematically correct, but it is tidematically correct.
	if (_value < 0)
	  _value = -(_value*_value);
	else
	  _value *= _value;
      } else
	convbarf (_units, units);
      break;
    default:
      assert (false);
    }
    _units = units;
  }
}


void PredictionValue::operator-= (PredictionValue subtrahend) {
  operator+= (-subtrahend);
}


void PredictionValue::operator/= (double divisor) {
  _value /= divisor;
}


void PredictionValue::convertAndAdd (PredictionValue addend) {
  if (addend._units == Units::zulu) {
    assert (addend._value == 0.0);
    return;
  }
  if (_units != Units::zulu && _units != addend._units)
    addend.Units (_units);
  operator+= (addend);
}


// These insist that both values must have exactly the same units.
// 0 Zulu semantics are not (yet) implemented for these operations.
const PredictionValue operator+ (PredictionValue a, PredictionValue b) {
  assert (a.Units() == b.Units());
  return PredictionValue (a.Units(), a.val()+b.val());
}
const PredictionValue operator- (PredictionValue a, PredictionValue b) {
  assert (a.Units() == b.Units());
  return PredictionValue (a.Units(), a.val()-b.val());
}
const double operator/ (PredictionValue a, PredictionValue b) {
  assert (a.Units() == b.Units());
  return a.val()/b.val();
}


const PredictionValue operator/ (PredictionValue b, double a) {
  b /= a;
  return b;
}


static void harmonize (PredictionValue &a, PredictionValue &b) {
  if (a.Units() != b.Units()) {
    if (a.Units() == Units::zulu)
      a.Units (b.Units());
    else
      b.Units (a.Units());
  }
}


const bool operator> (PredictionValue a, PredictionValue b) {
  harmonize (a, b);
  return (a.val() > b.val());
}


const bool operator< (PredictionValue a, PredictionValue b) {
  harmonize (a, b);
  return (a.val() < b.val());
}


const bool operator<= (PredictionValue a, PredictionValue b) {
  harmonize (a, b);
  return (a.val() <= b.val());
}


const bool operator>= (PredictionValue a, PredictionValue b) {
  harmonize (a, b);
  return (a.val() >= b.val());
}


const bool operator== (PredictionValue a, PredictionValue b) {
  harmonize (a, b);
  return (a.val() == b.val());
}


const bool operator!= (PredictionValue a, PredictionValue b) {
  harmonize (a, b);
  return (a.val() != b.val());
}


const PredictionValue operator- (PredictionValue a) {
  return PredictionValue (a.Units(), -a.val());
}


const PredictionValue abs (PredictionValue a) {
  return PredictionValue (a.Units(), fabs(a.val()));
}


// Print in the form -XX.YY units (padding as needed)
void PredictionValue::print (Dstr &text_out) const {
  char temp[80];
  sprintf (temp, "% 6.2f %s", _value, Units::longName(_units));
  text_out = temp;
}


void PredictionValue::printnp (Dstr &text_out) const {
  char temp[80];
  sprintf (temp, "%2.2f %s", _value, Units::shortName(_units));
  text_out = temp;
}

// Cleanup2006 Done