File: libtcd.html

package info (click to toggle)
libtcd 2.2.2-2.1
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 1,652 kB
  • sloc: sh: 8,798; ansic: 3,795; makefile: 58
file content (1320 lines) | stat: -rw-r--r-- 60,335 bytes parent folder | download | duplicates (3)
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Tide Constituent Database</title>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
  </head>

<body bgcolor=white>

$Id: libtcd.html 1803 2007-01-22 15:29:56Z flaterco $

<h1>Tide Constituent Database</h1>

<P>Comments on this documentation should be returned to <a
href="mailto:dave@flaterco.com">David Flater</a>.</p>

<P>Contents</p>

<OL>
<li><a href="#intro">Introduction</a></li>
<li><a href="#compiling">Compiling libtcd</a></li>
<li><a href="#tiderec">Tide record (version 2.2)</a></li>
<li><a href="#header">Public database header (version 2.2)</a></li>
<li><a href="#API">libtcd 2.2 API</a></li>
<li><a href="#migration">V1 to V2 migration guide</a></li>
<li><a href="#changelog">Changelog</a></li>
<li><a href="#references">References</a></li>
</OL>

<h2 id="intro">1. Introduction</h2>

<p>libtcd provides a software API for reading and writing Tide
Constituent Database (TCD) files.</p>

<P>The TCD file format and schema are used by <a
href="http://www.flaterco.com/xtide/">XTide</a> to retrieve
constituent definitions (speeds, equilibrium arguments, and node
factors), harmonic constants, subordinate station offsets and
associated metadata for use in generating tide predictions.</P>

<P>The TCD file format and schema were originally designed by Jan
Depner to improve the performance of XTide and to meet additional
requirements of the U.S. Naval Oceanographic Office (NAVO).&nbsp; They
are now maintained primarily by David Flater.</p>

<P>The design goals for TCD included:</p>

<UL>
<li>Available without installing heavy external packages.</li>
<li>Efficient for indexing and loading of stations.</li>
<li>Compact file format.</li>
<li>Translatable without loss from legacy XTide .txt and .xml files.</li>
<li>Exportable to legacy .txt and .xml formats.</li>
<li>Modifiable by end user using tideEditor.</li>
<li>Able to detect tampering (usually, misguided attempts to
edit the database in a word processor).</li>
</UL>

<P>The TCD file format and schema and libtcd are in the public
domain.&nbsp; They are distributed in the hope that they will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.</p>

<P>For additional background, see <a
href="ftp://ftp.flaterco.com/xtide/TCD.pdf">Jan Depner, "Format for
the Oceanographic and Atmospheric Master Library (OAML) Tide
Constituent Database," rev. 2003-06-06</a>.</P>


<h2 id="compiling">2. Compiling and installing libtcd</h2>

<p>The normal route:</p>

<p><blockquote><pre>
bash-3.1$ ./configure
bash-3.1$ make
bash-3.1$ su
bash-3.1# make install
</pre></blockquote></p>

<p>libtcd is packaged with the popular and portable <a
href="http://sources.redhat.com/automake/automake.html">GNU
automake</a>, so all usual GNU tricks should work.&nbsp; Help
on configuration options can be found in the INSTALL file or
obtained by entering <code>./configure --help</code>.</p>


<h2 id="tiderec">3. Tide record (version 2.2)</h2>

<P>This section documents the tide record schema of the Tide
Constituent Database as it stands in libtcd version 2.2.</P>

<P>Fields designated as "header" fields are retrieved at indexing time
and stored in memory for quick access.&nbsp; The rest are retrieved
only when individual tide stations are loaded.</p>

<P>Type 1 records are for reference stations; type 2 records are for
subordinate stations.&nbsp; Some fields are only encoded for one
record type while others are encoded for both.</p>

<P>int<i>X</i> means signed <i>X</i>-bit integer.&nbsp; uint<i>X</i>
means unsigned <i>X</i>-bit integer.&nbsp; TCD files are encoded with
a bit-packing function that allows odd-sized fields to be stored
efficiently.&nbsp; int varying and uint varying indicate fields whose
lengths in bits are dynamically configured for each TCD file.</p>

<p>Floating-point numbers are encoded in the TCD file using
scaled integers.&nbsp; An integer with a scale of <i>X</i> represents
the real number obtained by dividing the integer by <i>X</i>.</p>

<P><i>Please note:</i>&nbsp; All character strings stored in a TCD file use
the character set <a
href="http://en.wikipedia.org/wiki/ISO_8859-1">ISO-8859-1</a>.</P>

<table border=1>
<tr>
<th>Record type</th>
<th>TIDE_RECORD field</th>
<th>TIDE_RECORD data type</th>
<th>File encoding</th>
<th>Encodable range</th>
<th>Semantics</th>
</tr>

<tr><td align=center>all</td><td align=center><code>header.record_number</code></td><td align=center>int32</td><td align=center>none</td><td align=center>N/A</td><td>Records in TCD file are implicitly numbered [0, N-1].</td></tr>
<tr><td align=center>all</td><td align=center><code>header.record_size</code></td><td align=center>uint32</td><td align=center>uint16</td><td align=center>[0, 65535]</td><td>Length of encoded record in bytes.</td></tr>
<tr><td align=center>all</td><td align=center><code>header.record_type</code></td><td align=center>uint8</td><td align=center>uint4</td><td align=center>[0, 15]</td><td>See <code>enum TIDE_RECORD_TYPE</code> in
<a href="#API">Section 5</a>.&nbsp; Type 1 is reference station.&nbsp; Type 2 is subordinate station.&nbsp; Others are as yet undefined.</td></tr>
<tr><td align=center>all</td><td align=center><code>header.latitude</code></td><td align=center>float64</td><td align=center>int25, scale 100000</td><td align=center>[-167.77216, 167.77215]</td><td>Latitude in degrees north; use [-90.00000, 90.00000].&nbsp; Lat,lng of 0,0 is interpreted as NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>header.longitude</code></td><td align=center>float64</td><td align=center>int26, scale 100000</td><td align=center>[-335.54432, 335.54431]</td><td>Longitude in degrees east; use [-180.00000, 180.00000].&nbsp; Lat,lng of 0,0 is interpreted as NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>header.reference_station</code></td><td align=center>int32</td><td align=center>int18</td><td align=center>[-131072, 131071]</td><td>Index, references record_number of reference station for type 2 records, or -1 for none.</td></tr>
<tr id="timezone"><td align=center>all</td><td align=center><code>header.tzfile</code></td><td align=center>int16</td><td align=center>uint10</td><td align=center>[0, 1023]</td><td>Index, references table of time zone values like ":America/New_York" that control which time zone output should be rendered into.&nbsp; These time zone values are defined in the <a href="ftp://elsie.nci.nih.gov/pub/">zoneinfo time zone database</a>
that is included with most flavors of Unix.&nbsp; Not to be
confused with <a href="#meridian"><code>zone_offset</code></a>, which actually affects the calibration
of the results relative to real time.&nbsp; See comments for <a href="#meridian"><code>zone_offset</code></a> below.</td></tr>
<tr><td align=center>all</td><td align=center><code>header.name</code></td><td align=center>char[90]</td><td align=center>0-terminated string</td><td align=center>Length constrained by TIDE_RECORD.</td><td>Station name.</td></tr>
<tr><td align=center>all</td><td align=center><code>country</code></td><td align=center>int16</td><td align=center>uint9</td><td align=center>[0, 511]</td><td>Index, references table of country names.</td></tr>
<tr><td align=center>all</td><td align=center><code>source</code></td><td align=center>char[90]</td><td align=center>0-terminated string</td><td align=center>Length constrained by TIDE_RECORD.</td><td>Where you got the data, or "" for NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>restriction</code></td><td align=center>uint8</td><td align=center>uint4</td><td align=center>[0, 15]</td><td>Index, references table of restrictions like "Public domain," "Non-commercial use only," "DoD/DoD contractors only."</td></tr>
<tr><td align=center>all</td><td align=center><code>comments</code></td><td align=center>char[10000]</td><td align=center>0-terminated string</td><td align=center>Length constrained by TIDE_RECORD.</td><td>Human-readable text that is not provided to the user of a data set at the time of use but may be retrieved on demand.&nbsp; Use "" for NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>notes</code></td><td align=center>char[10000]</td><td align=center>0-terminated string</td><td align=center>Length constrained by TIDE_RECORD.</td><td>Human-readable text that MAY be delivered to the user of a data set at the time
  of use.&nbsp; XTide will print the notes in modes where this is
  convenient but ignore them when it is not.</td></tr>
<tr><td align=center>all</td><td align=center><code>legalese</code></td><td
align=center>uint8</td><td align=center>uint4</td><td
align=center>[0, 15]</td><td> Index, references table of
human-readable legal notices that MUST be delivered to the user of a data set
at the time of use.&nbsp; Use 0 for NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>station_id_context</code></td><td align=center>char[90]</td><td align=center>0-terminated string</td><td align=center>Length constrained by TIDE_RECORD.</td><td>Name of the authority that has defined the Station ID, or "" for NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>station_id</code></td><td align=center>char[90]</td><td align=center>0-terminated string</td><td align=center>Length constrained by TIDE_RECORD.</td><td>An identifier for a tide station, defined by the authority specified in station_id_context, or "" for NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>date_imported</code></td><td align=center>uint32</td><td align=center>uint27</td><td align=center>[0, 134217727]</td><td>The date on which the data set was imported into the database, encoded Year * 10000 + Month [1, 12] * 100 + Day [1, 31], or 0 for NULL.&nbsp; N.B., this is not the date of creation of the TCD file, but the date
  on record in a tide data management package such as
  Harmbase.</td></tr>
<tr><td align=center>all</td><td align=center><code>xfields</code></td><td align=center>char[10000]</td><td align=center>0-terminated string</td><td align=center>Length constrained by TIDE_RECORD.</td><td>Space for backward-compatible addition of
expansion fields as text.&nbsp; Encoding:
<pre>
xfields: xfield*
xfield: field-name ":" field-value "\n"
field-name: [^:\n ]+
field-value: [^\n]* continuation*
continuation: "\n " field-value
</pre>
When decoding field-values, the string "\n " is replaced by "\n" to unmangle
multi-line values.&nbsp; (Yes, multi-line values remain
multi-line, unlike RFC 822 headers.)</td></tr>
<tr><td align=center>all</td><td align=center><code>direction_units</code></td><td align=center>uint8</td><td align=center>uint varying</td><td align=center>[0, 255]</td><td>Index, references table of units like "degrees true."&nbsp; This field is used to indicate the units of the direction fields.</td></tr>
<tr><td align=center>all</td><td align=center><code>min_direction</code></td><td align=center>int32</td><td align=center>uint9</td><td align=center>[0, 511]</td><td>Direction of ebb current.&nbsp; Use [0, 359], or 361 for NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>max_direction</code></td><td align=center>int32</td><td align=center>uint9</td><td align=center>[0, 511]</td><td>Direction of flood current.&nbsp; Use [0, 359], or 361 for NULL.</td></tr>
<tr><td align=center>all</td><td align=center><code>level_units</code></td><td align=center>uint8</td><td align=center>uint varying</td><td align=center>[0, 255]</td><td>Index, references table of units like "feet," "meters," "knots," "knots^2."&nbsp; This field indicates the units of the datum and the amplitudes in type 1 records and the units of the level_add fields in type 2 records.<a href="#unitsnote">*</a></td></tr>

<tr><td align=center>1</td><td align=center><code>datum_offset</code></td><td align=center>float32</td><td align=center>int28, scale 10000</td><td align=center>[-13421.7728, 13421.7727]</td><td>The datum (Z0).</td></tr>
<tr><td align=center>1</td><td align=center><code>datum</code></td><td align=center>int16</td><td align=center>uint7</td><td align=center>[0, 127]</td><td>Index, references table of datum kinds like "Mean Lower Low Water."&nbsp;
For currents, set to 0 (value should be ignored).</td></tr>
<tr id="meridian"><td align=center>1</td><td align=center><code>zone_offset</code></td><td align=center>int32</td><td align=center>int13</td><td align=center>[-4096, 4095]</td><td>The standard time to which epochs are adjusted, a.k.a. the meridian, in hours and minutes east of UTC, encoded Hours * 100 + Minutes.&nbsp;
<code>zone_offset</code> affects the calibration
of the predictions relative to real time.&nbsp; Not to be confused
with <a href="#timezone"><code>tzfile</code></a>, which only affects which time zone the results are
rendered into.&nbsp; For example, adjusting <code>zone_offset</code> will change
high tide from 5:00 EST to 4:00 EST, while adjusting <a href="#timezone"><code>tzfile</code></a> will change it from 5:00 EST to 4:00 CST (actually the same
"real time" in two different time zones).&nbsp; Technically, <code>zone_offset</code> could be done away with by specifying that all data should be calibrated with UTC; however, this would make it more difficult to identify data sets
with source data by visual inspection (all of the epochs would be different).
</td></tr>
<tr><td align=center>1</td><td align=center><code>expiration_date</code></td><td align=center>uint32</td><td align=center>uint27</td><td align=center>[0, 134217727]</td><td>Use-until date for data set, encoded Year * 10000 + Month [1, 12] * 100 + Day [1, 31], or 0 for NULL.</td></tr>
<tr><td align=center>1</td><td align=center><code>months_on_station</code></td><td align=center>uint16</td><td align=center>uint10</td><td align=center>[0, 1023]</td><td>Number of months in time series used to derive harmonic constants, or 0 for NULL.</td></tr>
<tr><td align=center>1</td><td align=center><code>last_date_on_station</code></td><td align=center>uint32</td><td align=center>uint27</td><td align=center>[0, 134217727]</td><td>Last date in time series used to derive harmonic constants, encoded Year * 10000 + Month [1, 12] * 100 + Day [1, 31], or 0 for NULL.</td></tr>
<tr><td align=center>1</td><td align=center><code>confidence</code></td><td align=center>uint8</td><td align=center>uint4</td><td align=center>[0, 15]</td><td>A meaningless indicator of data quality, generally initialized to 10.&nbsp; (Jan wants to keep this.)</td></tr>
<tr><td align=center>1</td><td align=center><code>amplitude</code></td><td align=center>float32[255]</td><td align=center>uint19, scale 10000 (each)</td><td align=center>[0.0000, 52.4287]</td><td>Amplitudes of constituents.&nbsp; Constituents with amplitude less than <code>AMPLITUDE_EPSILON</code> are not encoded.</td></tr>
<tr><td align=center>1</td><td align=center><code>epoch</code></td><td align=center>float32[255]</td><td align=center>uint16, scale 100 (each)</td><td align=center>[0.00, 655.35]</td><td>Epochs (phases) of constituents, in degrees.&nbsp; Use [0.00, 359.99].&nbsp; Constituents with amplitude less than <code>AMPLITUDE_EPSILON</code> are not encoded.</td></tr>

<tr><td align=center>2</td><td align=center><code>min_time_add</code></td><td align=center>int32</td><td align=center>int13</td><td align=center>[-4096, 4095]</td><td>The time adjustment for Low Tide or Max Ebb events, encoded Hours * 100 + Minutes.&nbsp; 0 and NULL are equivalent (no adjustment).<a href="#timewarpnote">**</a> <a href="#mincurnote">***</a></td></tr>
<tr><td align=center>2</td><td align=center><code>min_level_add</code></td><td align=center>float32</td><td align=center>int17, scale 1000</td><td align=center>[-65.536, 65.535]</td><td>Additive adjustment for Low Tide or Max Ebb events.<a href="#mincurnote">***</a>&nbsp; 0 and NULL are equivalent (no adjustment).</td></tr>
<tr><td align=center>2</td><td align=center><code>min_level_multiply</code></td><td align=center>float32</td><td align=center>uint16, scale 1000</td><td align=center>[0.000, 65.535]</td><td>Ratio for Low Tide or Max Ebb events, or 0 for NULL (equivalent to 1, no adjustment).<a href="#mincurnote">***</a></td></tr>
<tr><td align=center>2</td><td align=center><code>max_time_add</code></td><td align=center>int32</td><td align=center>int13</td><td align=center>[-4096, 4095]</td><td>The time adjustment for High Tide or Max Flood events, encoded Hours * 100 + Minutes.&nbsp; 0 and NULL are equivalent (no adjustment).<a href="#timewarpnote">**</a> <a href="#mincurnote">***</a></td></tr>
<tr><td align=center>2</td><td align=center><code>max_level_add</code></td><td align=center>float32</td><td align=center>int17, scale 1000</td><td align=center>[-65.536, 65.535]</td><td>Additive adjustment for High Tide or Max Flood events.<a href="#mincurnote">***</a>&nbsp; 0 and NULL are equivalent (no adjustment).</td></tr>
<tr><td align=center>2</td><td align=center><code>max_level_multiply</code></td><td align=center>float32</td><td align=center>uint16, scale 1000</td><td align=center>[0.000, 65.535]</td><td>Ratio for High Tide or Max Flood events, or 0 for NULL (equivalent to 1, no adjustment).<a href="#mincurnote">***</a></td></tr>
<tr><td align=center>2</td><td align=center><code>flood_begins</code></td><td align=center>int32</td><td align=center>int13</td><td align=center>[-4096, 4095]</td><td>The time adjustment for Slack Water or Min Flood before Max Flood, encoded Hours * 100 + Minutes, or 2560 for NULL.<a href="#timewarpnote">**</a></td></tr>
<tr><td align=center>2</td><td align=center><code>ebb_begins</code></td><td align=center>int32</td><td align=center>int13</td><td align=center>[-4096, 4095]</td><td>The time adjustment for Slack Water or Min Ebb before Max Ebb, encoded Hours * 100 + Minutes, or 2560 for NULL.<a href="#timewarpnote">**</a></td></tr>

</table>

<P id="unitsnote">* In case of units = knots^2 (hydraulic currents),
it should be understood that only the amplitudes of the constants are
such as to give results in knots squared.&nbsp; The datum and any level_add
corrections are in plain knots; i.e., the square root of the amplitude
is taken before the datum and any corrections are added in.
</p>

<P id="timewarpnote">** Time corrections DO NOT incorporate
adjustments to Local Standard Time when the reference and sub station
are in different time zones.&nbsp; Time corrections are specified as
if all calculations and predictions were done in UTC.&nbsp; Time zone
differences are handled by adjusting the <code>tzfile</code>
field.&nbsp; This differs from current NOS practice, in which LST
adjustments <i>are</i> incorporated.&nbsp; When this "time warp" is
undone for NOS sub stations that use reference stations across the
International Date Line, time corrections in excess of 24 hours can
result.</p>

<P id="mincurnote">*** See <a
href="http://www.flaterco.com/xtide/mincurrents.html">http://www.flaterco.com/xtide/mincurrents.html</a>
regarding usage for Min Flood and Min Ebb events.</p>


<h2 id="header">4. Public database header (version 2.2)</h2>

<P>This section describes the "public portion" of the database header
structure, which is declared in <code>tcd.h</code> and made available to
applications via the <code>get_tide_db_header</code> operation.</P>

<table border=1>
<tr><td><pre>
char version[90];
</pre></td><td>libtcd version string.</td></tr>
<tr><td><pre>
uint32 major_rev;
</pre></td><td>libtcd major revision number.</td></tr>
<tr><td><pre>
uint32 minor_rev;
</pre></td><td>libtcd minor revision number.</td></tr>
<tr><td><pre>
char last_modified[90];
</pre></td><td>Last modification of TCD file.</td></tr>
<tr><td><pre>
uint32 number_of_records;
</pre></td><td>Number of records in TCD file.</td></tr>
<tr><td><pre>
int32 start_year;
</pre></td><td>Year corresponding to 0 index in speed, equilibrium argument, and node factor arrays.</td></tr>
<tr><td><pre>
uint32 number_of_years;
</pre></td><td>Number of years in speed, equilibrium argument, and node factor arrays.</td></tr>
<tr><td><pre>
uint32 constituents;
</pre></td><td>Number of constituents.</td></tr>
<tr><td><pre>
uint32 level_unit_types;
</pre></td><td>Number of entries in table used by <code>get_level_units</code>.</td></tr>
<tr><td><pre>
uint32 dir_unit_types;
</pre></td><td>Number of entries in table used by <code>get_dir_units</code>.</td></tr>
<tr><td><pre>
uint32 restriction_types;
</pre></td><td>Number of entries in table used by <code>get_restriction</code>.</td></tr>
<tr><td><pre>
uint32 datum_types;
</pre></td><td>Number of entries in table used by <code>get_datum</code>.</td></tr>
<tr><td><pre>
uint32 countries;
</pre></td><td>Number of entries in table used by <code>get_country</code>.</td></tr>
<tr><td><pre>
uint32 tzfiles;
</pre></td><td>Number of entries in table used by <code>get_tzfile</code>.</td></tr>
<tr><td><pre>
uint32 legaleses;
</pre></td><td>Number of entries in table used by <code>get_legalese</code>.</td></tr>
<tr><td><pre>
uint32 pedigree_types;
</pre></td><td>For backward compatibility.&nbsp; Ignore.</td></tr>
</table>

<P>The encodings of constituent speeds, equilibrium arguments,
and node factors are as follows.</P>

<table border=1>
<tr><th>Name</th><th>Program data type</th><th>File encoding</th><th>Encodable range</th><th>Semantics</th></tr>
<tr><td align=center>Speed</td><td align=center>float64</td><td align=center>uint varying (max 31), scale 10000000, with offset</td><td align=center>[0.0000000, 214.7483647]<a href="#speednote">*</a></td><td>Speed of constituent in degrees per hour.</td></tr>
<tr><td align=center>Equilibrium argument</td><td align=center>float32</td><td align=center>uint varying (max 31), scale 100, with offset</td><td align=center>[0.00, bignum]</td><td>Equilibrium argument in degrees.&nbsp; Use [0.00, 359.99].</td></tr>
<tr><td align=center>Node factor</td><td align=center>float32</td><td align=center>uint varying (max 31), scale 10000, with offset</td><td align=center>[0.0000, bignum]</td><td>Node factor.</td></tr>
</table>

<P>The offsets are adjustments so that the minimum value in the data
set is represented with a zero and the maximum by max&minus;offset.&nbsp;
For node factors, this can save bits.&nbsp; However, for speeds
and equilibrium arguments they are a needless complication.&nbsp;
Speeds in excess of the limit quoted above will overflow the integers
before the offset is taken into account, and the minimum equilibrium
argument is always zero anyway.</P>

<P id="speednote">* The highest speed in the IHO list of constituents
as of 2003-07-31 is 203.904625 degrees/hour for 6MS14.</P>


<h2 id="API">5. libtcd 2.2 API</h2>

<P>The API is provided in the C language and is made available by
including the header file <code>tcd.h</code>.</P>

<table border=1>

<tr><td>
<pre>
#define NV_BYTE    int8_t
#define NV_INT16   int16_t
#define NV_INT32   int32_t
#define NV_INT64   int64_t
#define NV_U_BYTE  uint8_t
#define NV_U_INT16 uint16_t
#define NV_U_INT32 uint32_t
#define NV_U_INT64 uint64_t

#define NV_BOOL    unsigned char
#define NV_CHAR    char
#define NV_U_CHAR  unsigned char
#define NV_FLOAT32 float
#define NV_FLOAT64 double
</pre>
</td><td>
  This section may appear slightly different from one platform to the
  next.&nbsp; The build process for libtcd generates #includes and data
  type definitions as needed to provide integer types of specific
  sizes.
</td></tr>

<tr><td>
<pre>
#define NVFalse         0
#define NVTrue          1
#define NV_U_INT32_MAX  4294967295
#define NV_INT32_MAX    2147483647
#define NV_U_INT16_MAX  65535
#define NV_INT16_MAX    32767
</pre>
</td><td>
Defined values.
</td></tr>

<tr><td>
<pre>
#define LIBTCD_VERSION   "PFM Software - libtcd v2.2 - 2006-11-22"
#define LIBTCD_MAJOR_REV 2
#define LIBTCD_MINOR_REV 2
</pre>
</td><td>
These defines describe the version of libtcd to which the header file belongs.
</td></tr>
<tr><td>
<pre>
/* One-line character strings */
#define ONELINER_LENGTH      90
/* Verbose character strings */
#define MONOLOGUE_LENGTH  10000
#define MAX_CONSTITUENTS    255
</pre>
</td><td>
These defines describe the sizes of the fixed-size arrays in the
tide record.
</td></tr>
<tr><td>
<pre>
typedef struct ... DB_HEADER_PUBLIC;
typedef struct ... TIDE_STATION_HEADER;
typedef struct ... TIDE_RECORD;
</pre></td><td>
The structure for the database header, and the header and main part of
the tide record.</td></tr>
<tr><td>
<pre>
enum TIDE_RECORD_TYPE {REFERENCE_STATION=1, SUBORDINATE_STATION=2};
</pre></td><td>
Syntactic sugar for values of <code>header.record_type</code>.</td></tr>
<tr><td>
<pre>
#define NULLSLACKOFFSET 0xA00
</pre></td><td>
Magic constant used to indicate NULL in the <code>flood_begins</code>
and <code>ebb_begins</code> fields of the tide record.</td></tr>
<tr><td>
<pre>
#define AMPLITUDE_EPSILON 0.00005
</pre></td><td>
This is the level below which an amplitude rounds to zero.
</td></tr>
<tr><td><pre>
void dump_tide_record (TIDE_RECORD *rec);
</pre></td><td>
Prints a low-level dump of the tide record to stderr.</td></tr>
<tr><td><pre>
NV_CHAR *get_country (NV_INT32 num);
NV_CHAR *get_tzfile (NV_INT32 num);
NV_CHAR *get_level_units (NV_INT32 num);
NV_CHAR *get_dir_units (NV_INT32 num);
NV_CHAR *get_restriction (NV_INT32 num);
NV_CHAR *get_datum (NV_INT32 num);
NV_CHAR *get_legalese (NV_INT32 num);
</pre></td><td>For fields in the tide record that are indices into
tables of character string values, these functions are used to retrieve the
character string value corresponding to a particular index.&nbsp;
The value "Unknown" is returned when no translation exists.&nbsp; The
return value is a pointer into static memory.</td></tr>
<tr><td><pre>
NV_CHAR *get_constituent (NV_INT32 num);
</pre></td><td>Get the name of the constituent corresponding to index
<code>num</code> [0,<code>constituents</code>-1].&nbsp; The
return value is a pointer into static memory.</td></tr>
<tr><td><pre>
NV_CHAR *get_station (NV_INT32 num);
</pre></td><td>
Get the name of the station whose <code>record_number</code> is <code>num</code> [0,<code>number_of_records</code>-1].&nbsp; The
return value is a pointer into static memory.
</td></tr>
<tr><td><pre>
NV_FLOAT64 get_speed (NV_INT32 num);
</pre></td><td>
Returns the speed of the constituent indicated by <code>num</code>
[0,<code>constituents</code>-1].</td></tr>
<tr><td><pre>
NV_FLOAT32 get_equilibrium (NV_INT32 num, NV_INT32 year);
NV_FLOAT32 get_node_factor (NV_INT32 num, NV_INT32 year);
</pre></td><td>
Get the equilibrium argument and node factor for the constituent indicated by <code>num</code>
[0,<code>constituents</code>-1], for the year <code>start_year</code>+<code>year</code>.</td></tr>
<tr><td><pre>
NV_FLOAT32 *get_equilibriums (NV_INT32 num);
NV_FLOAT32 *get_node_factors (NV_INT32 num);
</pre></td><td>
Get all available equilibrium arguments and node factors for the constituent indicated by <code>num</code>
[0,<code>constituents</code>-1].&nbsp; The return value is a pointer into
static memory which is an array of <code>number_of_years</code> floats,
corresponding to the years <code>start_year</code> through <code>start_year</code>+<code>number_of_years</code>-1.</td></tr>
<tr><td><pre>
NV_INT32 get_time (NV_CHAR *string);
NV_CHAR *ret_time (NV_INT32 time);
NV_CHAR *ret_time_neat (NV_INT32 time);
</pre></td><td>
Convert between character strings of the form "[+-]HH:MM" and the
encoding Hours * 100 + Minutes.&nbsp; <code>ret_time</code> pads the hours with a leading zero when less than 10; <code>ret_time_neat</code> omits the leading zero and omits the sign when the value is 0:00.&nbsp; Returned pointers
point into static memory.
</td></tr>
<tr><td><pre>
NV_CHAR *ret_date (NV_U_INT32 date);
</pre></td><td>
Convert the encoding Year * 10000 + Month [1, 12] * 100 + Day [1, 31]
to a character string of the form "YYYY-MM-DD", or "NULL" if the value is
zero.&nbsp; The returned pointer points into static memory.&nbsp; (The compact form,
without hyphens, is obtainable just by printing the integer.)
</td></tr>
<tr><td><pre>
NV_INT32 search_station (NV_CHAR *string);
</pre></td><td>
When invoked multiple times with the same <code>string</code>,
returns record numbers of all stations
that have that string anywhere in the station
name.&nbsp; This search is case insensitive.&nbsp; When no more
records are found it returns -1.</td></tr>
<tr><td><pre>
NV_INT32 find_station (NV_CHAR *name);
NV_INT32 find_tzfile (NV_CHAR *name);
NV_INT32 find_country (NV_CHAR *name);
NV_INT32 find_level_units (NV_CHAR *name);
NV_INT32 find_dir_units (NV_CHAR *name);
NV_INT32 find_restriction (NV_CHAR *name);
NV_INT32 find_datum (NV_CHAR *name);
NV_INT32 find_constituent (NV_CHAR *name);
NV_INT32 find_legalese (NV_CHAR *name);
</pre></td><td>
Inverses of the corresponding <code>get_</code> operations.&nbsp;
Return -1 for not found.
</td></tr>
<tr><td><pre>
NV_INT32 add_restriction (NV_CHAR *name, DB_HEADER_PUBLIC *db);
NV_INT32 add_tzfile (NV_CHAR *name, DB_HEADER_PUBLIC *db);
NV_INT32 add_country (NV_CHAR *name, DB_HEADER_PUBLIC *db);
NV_INT32 add_datum (NV_CHAR *name, DB_HEADER_PUBLIC *db);
NV_INT32 add_legalese (NV_CHAR *name, DB_HEADER_PUBLIC *db);
</pre></td><td>
Add the value of <code>name</code> to the corresponding lookup table and
return the index of the new value.&nbsp; If <code>db</code> is not NULL,
the database header
   struct pointed to will be updated to reflect the changes.&nbsp;
The maximum length of <code>name</code> is restricted by the corresponding
size definition in <code>tide_db_default.h</code> (restriction 30,
tzfile 30, country 50, datum 70, legalese 70, including the terminating
null).</td></tr>
<tr><td>
<pre>
NV_INT32 find_or_add_restriction (NV_CHAR *name, DB_HEADER_PUBLIC *db);
NV_INT32 find_or_add_tzfile (NV_CHAR *name, DB_HEADER_PUBLIC *db);
NV_INT32 find_or_add_country (NV_CHAR *name, DB_HEADER_PUBLIC *db);
NV_INT32 find_or_add_datum (NV_CHAR *name, DB_HEADER_PUBLIC *db);
NV_INT32 find_or_add_legalese (NV_CHAR *name, DB_HEADER_PUBLIC *db);
</pre></td><td>
Add the value of <code>name</code> to the corresponding lookup table if
and only if it is not already present.&nbsp; Return the index of the
value.&nbsp; If <code>db</code> is not NULL,
the database header
   struct pointed to will be updated to reflect the changes.&nbsp; The maximum length of <code>name</code> is restricted by the corresponding
size definition in <code>tide_db_default.h</code> (restriction 30,
tzfile 30, country 50, datum 70, legalese 70, including the terminating
null).</td></tr>
<tr><td><pre>
void set_speed (NV_INT32 num, NV_FLOAT64 value);
</pre></td><td>
Set the speed for the
constituent corresponding to index
<code>num</code> [0,<code>constituents</code>-1].
</td></tr>
<tr><td><pre>
void set_equilibrium (NV_INT32 num, NV_INT32 year, NV_FLOAT32 value);
void set_node_factor (NV_INT32 num, NV_INT32 year, NV_FLOAT32 value);
</pre></td><td>
Set the equilibrium argument and node factor for the
constituent corresponding to index
<code>num</code> [0,<code>constituents</code>-1], for the year
<code>start_year</code>+<code>year</code>.
</td></tr>
<tr><td><pre>
NV_BOOL open_tide_db (NV_CHAR *file);
</pre></td><td>
Opens the specified TCD file.&nbsp; If a different database is already open, it
will be closed.&nbsp; libtcd maintains considerable internal state and can
only handle one open database at a time.&nbsp; Returns false if the open
failed.
</td></tr>
<tr><td><pre>
void close_tide_db ();
</pre></td><td>
Closes the open database.
</td></tr>
<tr><td><pre>
NV_BOOL create_tide_db (NV_CHAR *file, NV_U_INT32 constituents,
    NV_CHAR *constituent[], NV_FLOAT64 *speed, NV_INT32 start_year,
    NV_U_INT32 num_years, NV_FLOAT32 *equilibrium[],
    NV_FLOAT32 *node_factor[]);
</pre></td><td>
Creates a TCD file with the supplied constituents and no tide stations.&nbsp;
Returns false if creation failed.&nbsp; The database is left in an open
state.
</td></tr>
<tr><td><pre>
DB_HEADER_PUBLIC get_tide_db_header ();
</pre></td><td>Returns a copy of the database header for the open database.</td></tr>
<tr><td><pre>
NV_BOOL get_partial_tide_record (NV_INT32 num, TIDE_STATION_HEADER *rec);
</pre></td><td>Gets "header" portion of tide record for the station whose
<code>record_number</code> is <code>num</code> [0,<code>number_of_records</code>-1] and writes it into
<code>rec</code>.&nbsp; Returns false if <code>num</code> is out of
range.&nbsp; <code>num</code> is preserved in the static variable
<code>current_index</code>.
</td></tr>
<tr><td><pre>
NV_INT32 get_next_partial_tide_record (TIDE_STATION_HEADER *rec);
</pre></td><td>
Invokes <code>get_partial_tide_record</code> for <code>current_index</code>+1.&nbsp; Returns the record number or -1 for failure.
</td></tr>
<tr><td><pre>
NV_INT32 get_nearest_partial_tide_record (NV_FLOAT64 lat, NV_FLOAT64 lon,
                                          TIDE_STATION_HEADER *rec);
</pre></td><td>
Invokes <code>get_partial_tide_record</code> for a station that appears closest
to the specified <code>lat</code> and <code>lon</code> in the
Cylindrical Equidistant projection.&nbsp; Returns the record number or
-1 for failure.
</td></tr>
<tr><td><pre>
NV_INT32 read_tide_record (NV_INT32 num, TIDE_RECORD *rec);
</pre></td><td>
Gets tide record for the station whose
<code>record_number</code> is <code>num</code> [0,<code>number_of_records</code>-1] and writes it into
<code>rec</code>.&nbsp; <code>num</code> is preserved in the static variable
<code>current_record</code>.&nbsp; Returns <code>num</code>, or -1
if <code>num</code> is out of range.</td></tr>
<tr><td><pre>
NV_INT32 read_next_tide_record (TIDE_RECORD *rec);
</pre></td><td>
Invokes <code>read_tide_record</code> for <code>current_record</code>+1.&nbsp; Returns the record number or -1 for failure.
</td></tr>
<tr><td><pre>
NV_BOOL add_tide_record (TIDE_RECORD *rec, DB_HEADER_PUBLIC *db);
#ifdef COMPAT114
/* Omission of db parameter was a bug. */
NV_BOOL update_tide_record (NV_INT32 num, TIDE_RECORD *rec);
#else
NV_BOOL update_tide_record (NV_INT32 num, TIDE_RECORD *rec, DB_HEADER_PUBLIC *db);
#endif
NV_BOOL delete_tide_record (NV_INT32 num, DB_HEADER_PUBLIC *db);
</pre></td><td>
Add a new record, update an existing record, or delete an existing
   record.&nbsp; If the deleted record is a reference station, all
   dependent subordinate stations will also be deleted.&nbsp; Add and
   update return false if the new record is invalid; delete and update return
   false if the specified <code>num</code> is invalid or there is
   insufficient memory available.&nbsp; If <code>db</code> is not NULL,
the database header
   struct pointed to will be updated to reflect the changes.</td></tr>
<tr><td><pre>
NV_BOOL infer_constituents (TIDE_RECORD *rec);
</pre></td><td>
Computes inferred constituents when
M2, S2, K1, and O1 are given and fills in the
remaining unfilled constituents.&nbsp; The inferred constituents
are developed or decided based on Article 230 of
"Manual of Harmonic Analysis and Prediction of Tides,"
Paul Schureman, C&GS Special Publication No. 98,
October 1971.&nbsp; Returns false if M2, S2, K1, or O1 is missing.</td></tr>
</table>

<P>For more information, see <a
href="ftp://ftp.flaterco.com/xtide/TCD.pdf">Jan Depner, "Format for
the Oceanographic and Atmospheric Master Library (OAML) Tide
Constituent Database," rev. 2003-06-06</a>.</P>


<h2 id="migration">6. V1 to V2 migration guide</h2>

<h3>libtcd vs. TCD file version compatibilities</h3>

<P>libtcd 1 can only read and modify files written by v1.&nbsp;
Versions of libtcd prior to 1.99 will fail inelegantly when attempting
to read v2 files and will corrupt the database if a modification is
attempted.&nbsp; Versions 1.99 and 1.100 will detect and report the
version mismatch.</P>

<P>libtcd 2 can read files written by v1 or v2, but it can only create
or modify v2 files.&nbsp; If it is necessary to create a v1 file, you
must compile your application using libtcd 1.&nbsp; Version 1.100 is
recommended for this purpose.&nbsp; It is available at
<a href="ftp://ftp.flaterco.com/xtide/libtcd-1.100.tar.gz">ftp://ftp.flaterco.com/xtide/libtcd-1.100.tar.gz</a>.</P>

<P>To convert a v1 TCD file to a v2 TCD file, use the application
rewrite_tide_db in the package tcd-utils, available from <a
href="http://www.flaterco.com/xtide/files.html">http://www.flaterco.com/xtide/files.html</a>.</P>

<h3>Compiling old applications without changes</h3>

<p>This option is for emergencies only, or in case of such extreme
laziness that maintaining the version-integrity of the API is less
important than avoiding minor changes to your applications.</p>

<P>Applications written against the TCD version 1 API should compile
and run without changes using libtcd 2 if libtcd 2 is built with the
<code>COMPAT114</code> option, as follows:</p>

<p><blockquote><pre>
bash-3.1$ ./configure --enable-COMPAT114
</pre></blockquote></p>

<p>This will allow old applications to read and write version 2 TCD
files.&nbsp; However, the resulting library is NOT API-compatible
with libraries built in the normal way, which will break applications that
are properly written for the v2 API.&nbsp; Therefore, any library built
with this option is for local use only.&nbsp; Only a static library will
be built.</p>

<p>Even with <code>COMPAT114</code> enabled, some fields do not
behave as they did in libtcd 1.&nbsp; Specifically,
<code>COMPAT114</code> does the following.</P>

<UL>
<li>The preprocessor defines <code>NAME_LENGTH</code>,
<code>SOURCE_LENGTH</code> and <code>COMMENTS_LENGTH</code> are
provided.</li>

<li>The tide record field <code>units</code> is provided and is copied
to and from <code>level_units</code> as needed to support reference
stations.</li>

<li>The tide record fields <code>pedigree</code>,
<code>avg_level_units</code>, <code>min_avg_level</code>, and
<code>max_avg_level</code> are provided, but they are never used.</li>

<li>The deprecated functions <code>get_pedigree</code>,
<code>find_pedigree</code> and <code>add_pedigree</code> are provided,
but their behavior is to change all pedigrees to "Unknown."</li>

<li>The function <code>update_tide_record</code> accepts only two parameters
<code>(NV_INT32 num, TIDE_RECORD *rec)</code>.</li>

<li>The function <code>check_simple</code> is provided.</li>

<li>Reading of pre-version-1.02 TCD files with old style header
checksums is supported.</li>

</ul>

<P>The following subsections detail the application changes that are
required and recommended to migrate to the version 2 API without
<code>COMPAT114</code>.</P>

<h3>Checklist of required changes when migrating applications from v1 API to v2 API</h3>

<OL>
<li>Replace all uses of the <code>units</code> field
with <code>level_units</code>.</li>
<li>Replace all uses of <code>NAME_LENGTH</code> and <code>SOURCE_LENGTH</code>
with <code>ONELINER_LENGTH</code>.</li>
<li>Replace all uses of <code>COMMENTS_LENGTH</code> with <code>MONOLOGUE_LENGTH</code>.</li>
<li>Provide a third parameter, <code>DB_HEADER_PUBLIC *db</code>, on each
invocation of <code>update_tide_record</code>.&nbsp; It can be NULL if
the calling function has no need of the db header.</li>
<li>Stop using the tide record fields <code>pedigree</code>, <code>units</code>, <code>avg_level_units</code>, <code>min_avg_level</code>, and <code>max_avg_level</code>.</li>
<li>Stop using the functions <code>get_pedigree</code>, <code>find_pedigree</code>, <code>add_pedigree</code>, and <code>check_simple</code>.</li>
</ol>

<h3>Checklist of optional changes when migrating applications from v1 API to v2 API</h3>

<OL>
<li>Ensure that the <code>max_direction</code> and <code>min_direction</code>
fields are initialized to 361 (the value indicating null) and/or set to
their correct values when writing reference stations.&nbsp; Ensure that
<code>direction_units</code> is set properly.</li>
<li>Change variable data types and casts to match the new type of
<code>months_on_station</code>.&nbsp; This was changed from unsigned
char (uint8) to unsigned short (uint16) in libtcd 1.100.</li>
<li>Change variable data types and casts to match the new types of
the <code>constituents</code> and <code>num_years</code> parameters
to <code>create_tide_db</code> and
any fields in <code>DB_HEADER_PUBLIC</code> that were changed
from signed to unsigned in libtcd 1.99.</li>
<li>Make use of the new fields <code>notes</code>, <code>legalese</code>,
<code>xfields</code>, <code>station_id_context</code>, <code>station_id</code>,
and <code>date_imported</code> as appropriate.</li>
<li>Make use of the new functions <code>find_or_add...</code>, <code>...legalese</code>,
<code>get_equilibriums</code>, <code>get_node_factors</code>,
<code>ret_time_neat</code>, and <code>ret_date</code> as appropriate.</li>
<li>When invoking functions that accept a <code>DB_HEADER_PUBLIC *db</code> parameter,
if the db header has no use in the calling function except to fill in this
parameter, a NULL can be passed instead.</li>
</ol>


<h2 id="changelog">7. Changelog</h2>


<h3>libtcd 2.2.2 (libtcd.so.0.0.2)</h3>
<p>David Flater<br>
2007-01-22</p>

<p>(Bugs, tide_db.c) Corrected more off-by-one errors in the use of
calculate_bits by the function create_tide_db.&nbsp; The function
write_tide_db_header was rounding values when it encoded them but
create_tide_db was truncating them when it was calculating the number
of bits.&nbsp; So if the maximum speed, equilibrium argument, or node
factor value as encoded in integer form happened to round up to an
exact power of 2, an invalid TCD file could have been produced.</p>

<p>(Nit, bit_pack.c) Eliminated the special case in calculate_bits to
return 1 on the input 0.&nbsp; A field whose only possible value is 0
consumes no space in the TCD file but is correctly decoded as 0.</p>


<h3>libtcd 2.2.1 (libtcd.so.0.0.1)</h3>
<p>David Flater<br>
2007-01-21</p>

<p>(Bug, bit_pack.c) Rewrote calculate_bits to eliminate ambiguous
interpretation of "range" argument (now called "value"), eliminate the
possibility of roundoff error, return the correct result in the case
where the input value is 1, and, as a special case, return 1 if the
input value is 0.</P>

<p>(Bugs, tide_db.c) Corrected off-by-one errors in the use of
calculate_bits by the function create_tide_db.</p>

<p>The preceding issues could have resulted in the production of an
invalid TCD file if the number of constituents, the number of distinct
speed values, the number of distinct equilibrium argument values, or
the number of distinct node factor values that were required by a
particular database happened to be exactly a power of 2 (including
1).&nbsp; These conditions are unlikely to have occurred in normal
use.&nbsp; I discovered the problems while attempting to generate a test
case for XTide using exactly one constituent.</p>


<h3>libtcd 2.2 (libtcd.so.0.0.0) release 2</h3>
<p>David Flater<br>
2006-11-26</p>

<p>Added warranty disclaimer to COPYING and libtcd.html.&nbsp; No
source code changes.</p>


<h3>libtcd 2.2 (libtcd.so.0.0.0)</h3>
<p>David Flater<br>
2006-11-22</p>

<p>Split out libtcd into a separate tarball (was previously bundled with
XTide).</p>

<p>Started using automake and libtool; started building shared lib.</p>

<p>Resolved preprocessor define clash between automake and libtcd on
VERSION.</p>

<p>Merged nvtypes.h and tide_db_version.h into tcd.h to reduce
pollution of public include directories.</p>

<p>Removed extraneous #includes from tcd.h.</p>

<p>Changed COMPAT114 into a configure option (--enable-COMPAT114).</p>

<p>Caused tcd.h to be generated at configure time with the following substitutions.
<ul>
<li>COMPAT114 is defined or undefined per --enable-COMPAT114.</li>
<li>Version string reflects enabling of COMPAT114.</li>
<li>NV_ data type definitions and any pertinent #includes are synchronized
    with what configure found.</li>
</ul>
</p>


<h3>libtcd 2.1.3</h3>
<p>David Flater<br>
2006-10-05</p>

<p>Changed asserts with side-effects to use require macro instead.</p>

<p>Minor clean-ups.</p>


<h3>libtcd 2.1.2</h3>
<p>David Flater<br>
2006-08-03</p>

<p>Improved configure script.</p>


<h3>libtcd 2.1.1</h3>
<p>David Flater<br>
2006-08-02</p>

<p>Added configure script.</p>

<p>Cleaned up nvtypes.h.</p>

<p>Corrected date on version 2.1 (said 2005, was 2006).</p>


<h3>libtcd 2.1</h3>
<p>David Flater<br>
2006-05-28</p>

<p>Fixed bug in delete_tide_record:&nbsp; linkage from subordinate
stations to reference stations other than the one being deleted was
getting broken.&nbsp; Bug report credit:&nbsp; August Hahn.</p>

<p>Fixed a memory management error in update_tide_record.&nbsp;
Bug report credit:&nbsp; August Hahn.</p>

<p>Added enum for TIDE_RECORD_TYPE.</p>

<p>In update or delete_tide_record, if cannot allocate memory, return
failure to the caller instead of exiting.</p>

<p>Added more error traps for invalid input.</p>

<p>Miscellaneous cleanups and nit fixes.</p>


<h3>libtcd 2.0</h3>
<p>David Flater<br>
2004-10-15</p>

<P>Lots of changes to implement the revised database schema.</P>

<P><code>COMPAT114</code> is no longer defined by default in tcd.h
(i.e. the v2 API is now effective).</P>

<P>Removed the unused define <code>DEFAULT_NUMBER_OF_OBSOLETE_RECORDS</code>
from tide_db_default.h.</P>

<P>Fixed repeated reporting of amplitude/epoch errors.</P>

<P>Fixed wasted byte bug on speeds, equilibrium arguments, and node
factors.</P>


<h3>libtcd 1.100 "enhanced edition"</h3>
<p>David Flater<br>
2004-10-13</p>

<P>With luck, this will be the final release in the version 1 series
and the last release that will write files that can be read by libtcd
1.02 through 1.14.&nbsp; It includes an additional round of cleanups
that will enhance upward compatibilty but should not break backward
compatibility.</P>

<P><code>check_simple</code> was placed inside <code>#ifdef
COMPAT114</code> (which is still defined in tcd.h).&nbsp; AFAIK, this
function is now of interest only in restore_tide_db, which uses it to
determine which XML format to output.</P>

<P>Copied the libtcd.html descriptions of operations into the header
file.</P>

<P>Made the <code>db</code> parameters to <code>add_</code> and
<code>find_or_add_</code> operations optional (can be null).</P>

<P>Gave <code>delete_tide_record</code> a return value.&nbsp; Gave
<code>update_tide_record</code> a <code>db</code> parameter,
conditional on <code>COMPAT114</code> not being defined.</P>

<P>In <code>delete_tide_record</code>, assert that the record is found.</P>

<P>Deleted unused <code>systemtime</code> var in
<code>write_tide_db_header</code>.</P>

<P>Deleted unnecessary <code>file</code> parameter to
<code>read_tide_db_header</code>.</P>

<P>Added static function <code>check_tide_record</code> to do sanity
checks on records before committing them to the database and called it
from <code>add_tide_record</code> and
<code>update_tide_record</code>.</P>

<P>Initialized an uninitialized var in <code>search_station</code>.</P>

<P>Added <code>clip_string</code> calls in
<code>find_tzfile</code> and <code>find_constituent</code>.</P>

<P>Replaced some weird code in <code>find_constituent</code> with
more normal code.</P>

<P>Got rid of unnecessary copy operations in <code>add_</code> functions.</P>

<P>Renamed <code>tname</code> parms to <code>name</code>.</P>

<P>Added checks to ensure that database is open when expected to be open and
closed when expected to be closed.</P>

<P>Added <code>AMPLITUDE_EPSILON</code> and used it where applicable.</P>

<P>Moved duplicate record-size-figuring code into a new function,
<code>figure_size</code>.&nbsp; Fixed wasted byte when record size is
even multiple of 8 bits.&nbsp; (The same bug exists for calculating the
size of the speeds, equilibrium arguments, and node factors, but fixing
that would break compatibility, and the wastage is at most 3 bytes.)</P>

<P>Changed <code>months_on_station</code> to uint16.</P>

<P>Fixed bounds checking on input to <code>add_</code> functions --
they all have their own static limits.&nbsp; Added similar bounds checking
in <code>write_tide_db_header</code>.</P>

<P>Lots of documentation nits fixed.</P>



<h3>libtcd 1.99 "collector's edition"</h3>
<p>David Flater<br>
2004-10-05</p>

<P>With luck, this will be the final release in the version 1 series
and the last release that will write files that can be read by libtcd
1.02 through 1.14.&nbsp; It includes a gigantic round of cleanups to
libtcd code that were made in anticipation of the upcoming major
revision.&nbsp; Except for the bug fixes, the changes <I>should</I> be
invisible to all wholesome client applications.&nbsp; Unwholesome
client code or the use of unwholesome databases built by old, buggy
versions of libtcd may now trigger errors or warnings that did not
appear with libtcd 1.14.</P>

<P>In the process of testing the cleanups, I discovered some unrelated,
really horrible bugs.</P>

<P>(Bug) Constituents with zero phases were not being
encoded by <code>write_tide_record</code>.&nbsp; The affected
reference stations in harmonics-dwf-2004-09-14 are Delfzijl,
Netherlands; The Battery, New York Harbor, NY; and South Pass, LA.&nbsp;
<code>write_tide_record</code> now encodes any constituent with a
positive amplitude.&nbsp; Constituents with zero amplitude but nonzero
phase are dropped, and negative amplitudes yield assertion failures.&nbsp;
Made analogous changes to <code>update_tide_record</code> (the counts
were supposed to match).</P>

<P>(Bug) <code>update_tide_record</code> was counting constituents for
subordinate stations.</P>

<P>(Bug) Backed out the change to the
control flow in <code>delete_tide_record</code> made in 1.14, which
broke the deletion of reference stations with sub stations.</P>

<P>(Bug) Fixed logic error in <code>delete_tide_record</code> that
corrupted the database when reference stations with sub stations were
deleted.</P>

<P>(Bug) Made <code>read_next_tide_record</code> return -1 if at end
of file, instead of going off the deep end.</P>

<P>Following are details of the gigantic cleanup.</P>

<P>The defines <code>NAME_LENGTH</code>, <code>SOURCE_LENGTH</code>
and <code>COMMENTS_LENGTH</code> were generalized to
<code>ONELINER_LENGTH</code> to set the length of one-line character
string buffers and <code>MONOLOGUE_LENGTH</code> to set the length of
verbose character string buffers.</P>

<P><code>COMPAT114</code> is defined in tcd.h.&nbsp; The effects are:</P>

<UL>
<li><code>NAME_LENGTH</code>, <code>SOURCE_LENGTH</code> and
<code>COMMENTS_LENGTH</code> get defined.</li>
<li>Pre-version-1.02 TCD files using the old checksum code are accepted.</li>
</UL>

<P>Added commentary on the commentary about OS-specific defines in
nvtypes.h and tide_db_header.h, noting that they are not actually
used.</P>

<P>Cleaned up all warnings and errors compiling under g++ -Wall:</P>

<ul>
<li>(Errors) Corrected casts in union initializers in <code>tide_db_header.h</code>.</li>
<li>(Warnings) Added casts or changed data types, as appropriate, to resolve
flagged signed-unsigned conversions.</li>
<li>(Errors) Added casts where <code>NV_U_BYTE</code> buffers were
passed to functions from <code>string.h</code> (invalid conversion
from <code>unsigned char*</code> to <code>const char*</code>).</li>
</ul>

<P>Added asserts everywhere that I came across potentially dangerous
assumptions.</p>

<p>Changed <code>-O2</code> to <code>-O</code> in Makefile (Forte doesn't like <code>O2</code>).</p>

<P>Moved changelogs from source files to libtcd.html.</P>

<P>Made <code>write_tide_record</code> static; no applications should be
calling this directly.</P>

<P>In <code>clip_string</code>, expanded static buffer to
<code>MONOLOGUE_LENGTH</code>, fixed a bug that was previously masked,
simplified code and added bounds checking.</P>

<P>Eliminated repeated calls to <code>clip_string</code> in find
functions.</P>

<P>Eliminated <code>clip_string</code> duplication in <code>write_tide_record</code>.</P>

<P>Changed 256-char static buffers in <code>add_pedigree</code>,
<code>add_tzfile</code>, <code>add_country</code>,
<code>add_datum</code>, and <code>add_restriction</code> to
<code>ONELINER_LENGTH</code> and added bounds checking.</P>

<P>Made filename <code>MONOLOGUE_LENGTH</code>.</P>

<P>Made version and last_modified <code>ONELINER_LENGTH</code>.</P>

<P>Handled overlong strings in TCD file by complaining and truncating
instead of returning garbarge.</P>

<P>Eliminated <code>get_string</code>.</P>

<P>Eliminated repeated parsing of input in
<code>read_tide_db_header</code>.&nbsp; Changed input buffer size to
<code>ONELINER_LENGTH</code>.</P>

<P>Made support for pre-version-1.02 header checksums conditional
on <code>COMPAT114</code>.</p>

<P>Changed many int32 fields in the DB header that had no reason ever
to be negative to uint32.&nbsp; Exceptions include
<code>start_year</code> (in case some crazy goes B.C.) and the offsets
for speeds, equilibrium arguments, and node factors (required to read
pre-version-1.11 TCD files).&nbsp; Also changed
<code>record_size</code> in the tide record header, but left alone the
rest of the tide record because the risk of breaking client code is
too great:&nbsp; build_tide_db temporarily stores negative results
from find functions in these fields, and tideEditor puts -1 in the
<code>record_number</code> field to flag a new record.&nbsp; -1 in
<code>reference_station</code> means no reference, though normally
it's only -1 for reference stations.</P>

<P>In signature of <code>create_tide_db</code>,
<code>constituents</code> and <code>num_years</code> became
unsigned.</P>

<P>Changed many looper, index, size, etc. variables from signed to
unsigned.</P>

<P>Corrected unsigned scan formats in <code>read_tide_db_header</code>.</P>

<P>Removed unused fields, unused data types and overcomplicated
infrastructure in <code>tide_db_header.h</code> and
<code>read_tide_db_header</code>.</P>

<P>Corrected signature of <code>bit_unpack</code> in <code>tide_db.c</code>.</P>

<P>Made <code>get_speed</code>, <code>get_equilibrium</code> and
<code>get_node_factor</code> give assertion failures instead of
returning -1.0 when invoker attempts to retrieve nonsense values.&nbsp;
Added a missing bounds check.</P>

<P>Made <code>set_speed</code>, <code>set_equilibrium</code> and
<code>set_node_factor</code> give assertion failures instead of
doing nothing when invoker attempts to set nonsense values.&nbsp;
Added a missing bounds check.</P>

<P>Made <code>record_size</code> in <code>TIDE_INDEX</code> struct 32 bits.</P>

<P>Added a warning about TCD files with negative offsets for speeds, eq args
or node factors (caused by the bug fixed in 1.11).</P>

<P>Changed format of date string in [LAST MODIFIED] to XTide style.&nbsp;
Changed identifer in [VERSION] to libtcd.</P>

<P>Added these functions:</P>

<UL>
<li><code>NV_FLOAT32 *get_equilibriums (NV_INT32 num);</code></li>
<li><code>NV_FLOAT32 *get_node_factors (NV_INT32 num);</code></li>
<li><code>NV_INT32 find_or_add_restriction (NV_CHAR *tname, DB_HEADER_PUBLIC *db);</code></li>
<li><code>NV_INT32 find_or_add_tzfile (NV_CHAR *tname, DB_HEADER_PUBLIC *db);</code></li>
<li><code>NV_INT32 find_or_add_country (NV_CHAR *tname, DB_HEADER_PUBLIC *db);</code></li>
<li><code>NV_INT32 find_or_add_datum (NV_CHAR *tname, DB_HEADER_PUBLIC *db);</code></li>
<li><code>NV_CHAR *ret_time_neat (NV_INT32 time);</code></li>
<li><code>NV_CHAR *ret_date (NV_U_INT32 date);</code></li>
</ul>

<P>Added header fields for major and minor revision number.&nbsp; Added
check for attempt to read higher major revision than supported.</P>



<h3>libtcd 1.14</h3>
<p>David Flater<br>
2004-09-03</p>

<P>
    (tide_db.c)  Certain database update operations that ended with
    closing and re-opening the database were broken by nulling out the
    filename in close_tide_db.&nbsp; Incorporated patch from Jan Depner
    that rectifies this and tweaks the control flow in
    delete_tide_record.
</P>

<h3>libtcd 1.13</h3>
<p>David Flater<br>
2004-08-15</p>

<P>
    (create_tide_db)  Write a valid end_of_file record even if someone
    creates a database with no tide records.
</p><p>
    (tide_db_default)  Removed a superfluous space character in table of
    default datum types (after Lowest Astronomical Tide).
</p>


<h3>libtcd 1.12</h3>
<p>David Flater<br>
2003-12-04</p>

<P>(tcd.h) Deleted #include &lt;malloc.h&gt;.</P>


<h3>libtcd 1.11</h3>
<p>David Flater<br>
2003-11-16</p>

<P>
    (create_tide_db)  Fixed <b>horrible bug</b>:&nbsp; offsets for speeds,
    equilibrium args, and node factors were sign-reversed with respect
    to their usage in read_tide_db_header and write_tide_db_header,
    resulting in possible overflows.
</P><P>
    (read_tide_db_header)  Added handling for zero tide records, which
    happens on new database create.
</p><P>
    (open_tide_db)  Added check of modified flag to 2003-10-14 code.
</p><P>
    (close_tide_db)  Deleted repeat free of tindex introduced
2003-10-14.&nbsp; Cleared modified flag on close.
</P>


<h3>libtcd 1.10</h3>
<p>David Flater<br>
2003-10-14</p>

<P>
    Incorporated patch from Phil Thornton that closes a memory leak
    and improves performance on repeat calls to open_tide_db.&nbsp;
    See <a href="http://www.flaterco.com/xtide/tcd_notes.html">http://www.flaterco.com/xtide/tcd_notes.html</a>
</P>


<h3>libtcd 1.09</h3>
<p>Jan C. Depner<br>
2003-09-04</p>

<P>
    Bug fix - modifying last record in file and changing size of record caused
    a big problem.&nbsp; Duh, I should have seen that one coming.
</P>


<h3>libtcd 1.08</h3>
<p>Jan C. Depner<br>
2003-07-23</p>

<P>Bug fix - deleting last record in file caused a big problem.</P>


<h3>libtcd 1.07</h3>
<p>David Flater<br>
2003-03-27</p>

<P>
    (This version was also released briefly without an updated version
    string.)
</P><P>
    Check_simple altered per resolution to abolish "simplified"
    type 2 records.&nbsp; See <a href="http://www.flaterco.com/xtide/tcd_notes.html">http://www.flaterco.com/xtide/tcd_notes.html</a>
</P>


<h3>libtcd 1.06</h3>
<p>David Flater<br>
2003-03-18</p>

<P>
    (This version failed to report an updated version string.)
</P><P>
    Added NULLSLACKOFFSET to tcd.h per 2003-03-18 change to slack
    offsets.&nbsp; See <a href="http://www.flaterco.com/xtide/tcd_notes.html">http://www.flaterco.com/xtide/tcd_notes.html</a>
</P>


<h3>libtcd 1.05</h3>
<p>David Flater<br>
2003-03-11</p>

<P>
    Incorporated nit fixes from August Hahn:&nbsp; added clip_string(tname)
    to take care of cases when the country name had leading/trailing
    spaces, and removed a couple of unused variables.
</P>


<h3>libtcd 1.04</h3>
<p>David Flater<br>
2002-12-13</p>

<P>While integrating 1.03, deleted an unused variable (whoop de do).</P>


<h3>libtcd 1.03</h3>
<p>Wade Ladner/Jan C. Depner<br>
2002-12-09</p>

<P>
    Small fix for Micro$oft Windoze.&nbsp; We don't need to add CR's to strings
    (apparently).
</P>


<h3>libtcd 1.02</h3>
<p>August Hahn<br>
2002-11-15</p>

<P>
    Replaced the simplistic checksum with a real CRC.&nbsp; If the first 
    checksum fails it will check for an old-style checksum.
</P>


<h3>libtcd 1.01</h3>
<p>David Flater<br>
2002-10-01</p>

<P>
    Many changes since 2002-08-01.&nbsp; Bumped version upon integration
    with XTide distribution.&nbsp; Renamed resulting library to libtcd.
</P>


<h3>tidelib 1.00</h3>
<p>Jan C. Depner<br>
2002-08-01</p>

<P>First tested release.</P>


<h3>tidelib 0.99</h3>
<p>Jan C. Depner<br>
2002-07-15</p>

<P>Prototype for testing.</P>

<H2 id="references">8. References</H2>

<P><a href="ftp://ftp.flaterco.com/xtide/TCD.pdf">Jan Depner, "Format
for the Oceanographic and Atmospheric Master Library (OAML) Tide
Constituent Database," rev. 2003-06-06</a>.</p>

<P><a href="ftp://ftp.flaterco.com/xtide/libtcd-1.100.tar.gz">libtcd version 1.100</A>.</p>

<P><a href="http://www.flaterco.com/xtide/">XTide</a>.</P>

<P><a href="http://en.wikipedia.org/wiki/ISO_8859-1">ISO-8859-1</a>.</P>

<P>
    Manual of Harmonic Analysis and Prediction of Tides.&nbsp;  Special
    Publication No. 98, Revised (1940) Edition (reprinted 1958 with
    corrections; reprinted again 1994).&nbsp;  United States Government
    Printing Office, 1994.
</P>

  </body>
</html>