File: HarmonicsFile.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 (463 lines) | stat: -rw-r--r-- 16,392 bytes parent folder | download | duplicates (2)
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
// $Id: HarmonicsFile.cc 2854 2007-12-02 18:17:19Z flaterco $

/*  HarmonicsFile  Hide details of interaction with libtcd.

    Although a few method signatures elsewhere in XTide have been
    optimized for compatibility with libtcd, at least 95% of the work
    of integrating XTide with a different database should be
    concentrated in re-implementing this class.

    Copyright (C) 1998  David Flater w/ contributions by Jan Depner.

    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"
#include "HarmonicsFile.hh"
#include "SubordinateStation.hh"
#include <tcd.h>


static bool haveInstance = false;


HarmonicsFile::HarmonicsFile (const Dstr &filename):
  _filename(filename) {

  // libtcd is stateful and cannot handle multiple harmonics files
  // simultaneously.  XTide currently has no need to open multiple
  // harmonics files simultaneously, so for now, the constructor will
  // barf if the attempt is made to have more than one instance at a
  // time.  If this class is modified to deal with different
  // databases, that trap can be removed.
  assert (!haveInstance);
  haveInstance = true;

  // Do some sanity checks before invoking libtcd.  (open_tide_db just
  // returns false regardless of what the problem was.)
  {
    FILE *fp = fopen (filename.aschar(), "rb");
    if (fp) {
      char c = fgetc (fp);
      if (c != '[') {
        Dstr details (filename);
        details += " is apparently not a TCD file.\n\
We do not use harmonics.txt or offsets.xml anymore.  Please see\n\
http://www.flaterco.com/xtide/files.html for a link to the current data.";
        Global::barf (Error::CORRUPT_HARMONICS_FILE, details);
      }
      fclose (fp);
    } else {
      // This should never happen.  We statted this file in Global.cc.
      Global::cantOpenFile (filename, Error::fatal);
    }
  }

  if (!open_tide_db (_filename.aschar())) {
    Dstr details (_filename);
    details += ": libtcd returned generic failure.";
    Global::barf (Error::CORRUPT_HARMONICS_FILE, details);
  }
  DB_HEADER_PUBLIC db = get_tide_db_header();
  _versionString = _filename;
  {
    int i;
    if ((i = _versionString.strrchr ('/')) != -1)
      _versionString /= (i+1);
  }
  _versionString += ' ';
  _versionString += (char *) db.last_modified;
  _versionString += " / ";
  _versionString += (char *) db.version;
}


const Dstr &HarmonicsFile::versionString() {
  return _versionString;
}


// libtcd's open_tide_db is called each time a HarmonicsFile is
// created.  Intentionally, there is no matching call to
// close_tide_db.  See http://www.flaterco.com/xtide/tcd_notes.html
// and XTide changelog for version 2.6.3.

HarmonicsFile::~HarmonicsFile() {
  assert (haveInstance);
  haveInstance = false;
}


StationRef * const HarmonicsFile::getNextStationRef () {
  TIDE_STATION_HEADER rec;
  long i;
  if ((i = get_next_partial_tide_record (&rec)) == -1) return NULL;
  assert (i >= 0);
  StationRef *sr = new StationRef (_filename,
                                   i,
                                   (char *)rec.name,
        ((rec.latitude != 0.0 || rec.longitude != 0.0) ?
        Coordinates(rec.latitude, rec.longitude) : Coordinates()),
                                   (char *)get_tzfile(rec.tzfile),
                                   (rec.record_type == REFERENCE_STATION));
  return sr;
}


static void parse_xfields (MetaFieldVector &metadata,
                           constCharPointer xfields) {
  assert (xfields);
  Dstr x (xfields);
  Dstr linebuf, name, value;
  x.getline (linebuf);
  while (!(linebuf.isNull())) {
    if (linebuf[0] == ' ') {
      if (!(name.isNull())) {
        linebuf /= 1;
        value += '\n';
        value += linebuf;
      }
    } else {
      if (!(name.isNull())) {
        metadata.push_back (MetaField (name, value));
        name = (char *)NULL;
        value = (char *)NULL;
      }
      int i = linebuf.strchr (':');
      if (i > 0) {
        name = linebuf;
        name -= (unsigned)i;
        value = linebuf.ascharfrom((unsigned)i+1);
      }
    }
    x.getline (linebuf);
  }
  if (!(name.isNull()))
    metadata.push_back (MetaField (name, value));
}


// Analogous to datum, levelAdds for hydraulic currents are always in
// knots not knots^2.  The database can specify either knots or
// knots^2 in the sub station record, but there is only one sensible
// interpretation.
static const Units::PredictionUnits levelAddUnits (const TIDE_RECORD &rec) {
  return Units::flatten (Units::parse (get_level_units(rec.level_units)));
}


static void appendOffsetsMetadata (MetaFieldVector &metadata,
				   const TIDE_RECORD &rec) {
  char tmp[80];
  Units::PredictionUnits lu = levelAddUnits(rec);

  metadata.push_back (MetaField ("Min time add",
    rec.min_time_add ? ret_time_neat (rec.min_time_add) : "NULL"));
  sprintf (tmp, "%+2.2f %s", rec.min_level_add, Units::shortName(lu));
  metadata.push_back (MetaField ("Min level add",
    rec.min_level_add ? tmp : "NULL"));
  sprintf (tmp, "%0.3f", rec.min_level_multiply);
  metadata.push_back (MetaField ("Min level mult",
    rec.min_level_multiply > 0.0 ? tmp : "NULL"));

  metadata.push_back (MetaField ("Max time add",
    rec.max_time_add ? ret_time_neat (rec.max_time_add) : "NULL"));
  sprintf (tmp, "%+2.2f %s", rec.max_level_add, Units::shortName(lu));
  metadata.push_back (MetaField ("Max level add",
    rec.max_level_add ? tmp : "NULL"));
  sprintf (tmp, "%0.3f", rec.max_level_multiply);
  metadata.push_back (MetaField ("Max level mult",
    rec.max_level_multiply > 0.0 ? tmp : "NULL"));

  if (Units::isCurrent(lu)) {
    metadata.push_back (MetaField ("Flood begins",
      rec.flood_begins == NULLSLACKOFFSET ? "NULL"
	: ret_time_neat (rec.flood_begins)));
    metadata.push_back (MetaField ("Ebb begins",
      rec.ebb_begins == NULLSLACKOFFSET ? "NULL"
	: ret_time_neat (rec.ebb_begins)));
  }
}


// refStationNativeUnits:  determination of hydraulic vs. regular
// current is made based on units at reference station.  Units are
// flattened for levelAdd offsets.
static void buildMetadata (const StationRef &sr,
                           MetaFieldVector &metadata,
                           const TIDE_RECORD &rec,
                           Units::PredictionUnits refStationNativeUnits,
                           CurrentBearing minCurrentBearing,
                           CurrentBearing maxCurrentBearing) {
  Dstr tmpbuf;

  metadata.push_back (MetaField ("Name", sr.name));
  metadata.push_back (MetaField ("In file", sr.harmonicsFileName));
  if (rec.legalese)
    metadata.push_back (MetaField ("Legalese", get_legalese(rec.legalese)));
  if (rec.station_id_context[0])
    metadata.push_back (MetaField ("Station ID context",
				     rec.station_id_context));
  if (rec.station_id[0])
    metadata.push_back (MetaField ("Station ID", rec.station_id));
  if (rec.date_imported)
    metadata.push_back (MetaField ("Date imported",
				    ret_date(rec.date_imported)));
  sr.coordinates.print (tmpbuf);
  metadata.push_back (MetaField ("Coordinates", tmpbuf));
  metadata.push_back (MetaField ("Country", get_country(rec.country)));
  metadata.push_back (MetaField ("Time zone", sr.timezone));
  metadata.push_back (MetaField ("Native units",
                                 Units::longName(refStationNativeUnits)));
  if (!(maxCurrentBearing.isNull())) {
    maxCurrentBearing.print (tmpbuf);
    metadata.push_back (MetaField ("Flood direction", tmpbuf));
  }
  if (!(minCurrentBearing.isNull())) {
    minCurrentBearing.print (tmpbuf);
    metadata.push_back (MetaField ("Ebb direction", tmpbuf));
  }
  if (rec.source[0])
    metadata.push_back (MetaField ("Source", rec.source));
  metadata.push_back (MetaField ("Restriction",
                                 get_restriction(rec.restriction)));
  if (rec.comments[0])
    metadata.push_back (MetaField ("Comments", rec.comments));
  if (rec.notes[0])
    metadata.push_back (MetaField ("Notes", rec.notes));
  parse_xfields (metadata, rec.xfields);

  switch (rec.header.record_type) {
  case REFERENCE_STATION:
    metadata.push_back (MetaField ("Type",
      (Units::isCurrent(refStationNativeUnits) ?
        (Units::isHydraulicCurrent(refStationNativeUnits) ?
	  "Reference station, hydraulic current" :
 	  "Reference station, current")
        : "Reference station, tide")));
    metadata.push_back (MetaField ("Meridian",
                                   ret_time_neat(rec.zone_offset)));
    if (!Units::isCurrent(refStationNativeUnits))
      metadata.push_back (MetaField ("Datum", get_datum(rec.datum)));
    if (rec.months_on_station)
      metadata.push_back (MetaField ("Months on station",
                                     rec.months_on_station));
    if (rec.last_date_on_station)
      metadata.push_back (MetaField ("Last date on station",
				      ret_date(rec.last_date_on_station)));
    if (rec.expiration_date)
      metadata.push_back (MetaField ("Expiration",
				      ret_date(rec.expiration_date)));
    metadata.push_back (MetaField ("Confidence", rec.confidence));
    break;

  case SUBORDINATE_STATION:
    metadata.push_back (MetaField ("Type",
      (Units::isCurrent(refStationNativeUnits) ?
        (Units::isHydraulicCurrent(refStationNativeUnits) ?
	  "Subordinate station, hydraulic current" :
 	  "Subordinate station, current")
        : "Subordinate station, tide")));
    metadata.push_back (MetaField ("Reference",
                                   get_station(rec.header.reference_station)));
    appendOffsetsMetadata (metadata, rec);
    break;

  default:
    assert (false);
  }
}


// Get constituents from a TIDE_RECORD, adjusting if needed.
static const ConstituentSet getConstituents (const TIDE_RECORD &rec,
					     SimpleOffsets adjustments) {
  assert (rec.header.record_type == REFERENCE_STATION);

  DB_HEADER_PUBLIC db = get_tide_db_header();
  Units::PredictionUnits amp_units
    (Units::parse (get_level_units (rec.level_units)));
  SafeVector<Constituent> constituents;

  // Null constituents are eliminated here.
  for (unsigned i=0; i<db.constituents; ++i)
    if (rec.amplitude[i] > 0.0)
      constituents.push_back (
        Constituent (get_speed(i),
                     db.start_year,
                     db.number_of_years,
		     get_equilibriums(i),
                     get_node_factors(i),
                     Amplitude (amp_units, rec.amplitude[i]),
                     rec.epoch[i]));

  assert (!constituents.empty());

  PredictionValue datum (Units::flatten(amp_units), rec.datum_offset);

  // Normalize the meridian to UTC.
  // To compensate for a negative meridian requires a positive offset.
  // (This adjustment is combined with any that were passed in.)

  // This is the only place where mutable offsets would make even a
  // little bit of sense.
  adjustments = SimpleOffsets (
    adjustments.timeAdd() - Interval(ret_time_neat(rec.zone_offset)),
    adjustments.levelAdd(),
    adjustments.levelMultiply());

  ConstituentSet cs (constituents, datum, adjustments);

  Dstr u (Global::settings["u"].s);
  if (u != "x")
    cs.setUnits (Units::parse (u));

  return cs;
}


static void getTideRecord (uint32_t recordNumber, TIDE_RECORD &rec) {
  require (read_tide_record ((NV_INT32)recordNumber, &rec)
           == (NV_INT32)recordNumber);
  if (Global::settings["in"].c == 'y' &&
      rec.header.record_type == REFERENCE_STATION)
    infer_constituents (&rec);
}


Station * const HarmonicsFile::getStation (const StationRef &stationRef) {
  Station *s = NULL;
  TIDE_RECORD rec;
  getTideRecord (stationRef.recordNumber, rec);

  Dstr note;
  if (rec.notes[0])
    note = rec.notes;
  CurrentBearing minCurrentBearing, maxCurrentBearing;
  bool isDegreesTrue = (!(strcmp((char *)get_dir_units(rec.direction_units),
    "degrees true")));
  if (rec.min_direction >= 0 && rec.min_direction < 360)
    minCurrentBearing = CurrentBearing (rec.min_direction, isDegreesTrue);
  if (rec.max_direction >= 0 && rec.max_direction < 360)
    maxCurrentBearing = CurrentBearing (rec.max_direction, isDegreesTrue);
  Dstr name ((char *)rec.header.name);
  if (rec.legalese) {
    name += " - ";
    name += get_legalese(rec.legalese);
  }

  switch (rec.header.record_type) {
  case REFERENCE_STATION:
    {
      MetaFieldVector metadata;
      buildMetadata (stationRef,
                     metadata,
                     rec,
		     Units::parse(get_level_units(rec.level_units)),
		     minCurrentBearing,
                     maxCurrentBearing);
      s = new Station (name,
                       stationRef,
                       getConstituents (rec, SimpleOffsets()),
                       note,
                       minCurrentBearing,
                       maxCurrentBearing,
                       metadata);
    }
    break;

  case SUBORDINATE_STATION:
    {
      TIDE_RECORD referenceStationRec;
      assert (rec.header.reference_station >= 0);
      getTideRecord (rec.header.reference_station, referenceStationRec);
      Units::PredictionUnits refStationNativeUnits
        (Units::parse(get_level_units(referenceStationRec.level_units)));

      MetaFieldVector metadata;
      buildMetadata (stationRef,
                     metadata,
                     rec,
                     refStationNativeUnits,
		     minCurrentBearing,
                     maxCurrentBearing);

      NullableInterval tempFloodBegins, tempEbbBegins;

      // For these, zero is not the same as null.
      if (rec.flood_begins != NULLSLACKOFFSET)
	tempFloodBegins = Interval (ret_time_neat (rec.flood_begins));
      if (rec.ebb_begins != NULLSLACKOFFSET)
	tempEbbBegins = Interval (ret_time_neat (rec.ebb_begins));

      Units::PredictionUnits lu = levelAddUnits(rec);
      HairyOffsets ho (
	SimpleOffsets (Interval(ret_time_neat(rec.max_time_add)),
		       PredictionValue(lu, rec.max_level_add),
		       rec.max_level_multiply),
	SimpleOffsets (Interval(ret_time_neat(rec.min_time_add)),
		       PredictionValue(lu, rec.min_level_add),
		       rec.min_level_multiply),
	tempFloodBegins,
        tempEbbBegins);

      // If the offsets can be reduced to simple, then we can adjust
      // the constituents and be done with it.  However, in the case
      // of hydraulic currents, level multipliers cannot be treated as
      // simple and applied to the constants because of the square
      // root operation--i.e., it's nonlinear.

      SimpleOffsets so;
      if ((!Units::isHydraulicCurrent(refStationNativeUnits) ||
           ho.maxLevelMultiply() == 1.0) && ho.trySimplify (so))

	// Can simplify.
        s = new Station (name,
                         stationRef,
                         getConstituents (referenceStationRec, so),
                         note,
                         minCurrentBearing,
                         maxCurrentBearing,
                         metadata);

      else

        // Cannot simplify.
        s = new SubordinateStation (name,
                                    stationRef,
                                    getConstituents (referenceStationRec,
                                                     SimpleOffsets()),
            	                    note,
                                    minCurrentBearing,
                                    maxCurrentBearing,
                                    metadata,
                                    ho);
    }
    break;

  default:
    assert (false);
  }

  // If this sanity check is deferred until Graph::draw it has
  // unhealthy consequences (clock windows can be killed while only
  // partially constructed; graph windows can double-barf due to
  // events queued on the X server).
  if (s->minLevel() >= s->maxLevel())
    Global::barf (Error::ABSURD_OFFSETS, s->name);

  return s;
}

// Cleanup2006 Done