24#include "TCDS_Binary_Harmonic.h"
32static const char *tz_names[][2] = {
67typedef unsigned short WORD;
84 WCHAR StandardName[32];
87 WCHAR DaylightName[32];
107tz_info_entry tz_info_local, tz_info_remote, *tz_info = &tz_info_local;
113char *tz_time2sec(
char *psrc,
long *timesec) {
118 while (*psrc ==
' ') psrc++;
119 if (*psrc ==
'+') psrc++;
128 while (isdigit(*psrc)) temp = temp * 10 + (*(psrc++) -
'0');
130 *timesec = *timesec + temp * mpy;
136 }
while (isdigit(*psrc));
138 if (neg) *timesec = 0 - *timesec;
147static char *tz_parse_name(
char *psrc,
char *pdst,
int maxlen) {
151 while (*psrc ==
' ') psrc++;
153 while (isalpha(*psrc) && nReturn < maxlen) {
154 *(pdst++) = *(psrc++);
165static char *tz_parse_rule(
char *psrc,
SYSTEMTIME *st) {
166 int mol[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
168 if (*psrc ==
',') psrc++;
170 while (*psrc ==
' ') psrc++;
179 st->wMilliseconds = 0;
184 while (isdigit(*psrc)) temp = temp * 10 + (*(psrc++) -
'0');
186 if (temp < 1 || temp > 365)
return (0);
188 for (mo = 0; temp >= mol[mo]; mo++) temp -= mol[mo];
194 else if (*psrc ==
'M') {
198 while (isdigit(*psrc)) temp = temp * 10 + (*(psrc++) -
'0');
199 if (temp < 1 || temp > 12 || *psrc !=
'.')
return (0);
200 st->wMonth = (
unsigned short)temp;
204 while (isdigit(*psrc))
205 temp = temp * 10 + (*(psrc++) -
'0');
206 if (temp < 1 || temp > 5 || *psrc !=
'.')
return (0);
207 st->wDay = (
unsigned short)temp;
211 while (isdigit(*psrc))
212 temp = temp * 10 + (*(psrc++) -
'0');
213 if (temp < 0 || temp > 6)
return (0);
214 st->wDayOfWeek = (
unsigned short)temp;
219 psrc = tz_time2sec(psrc, &temp);
220 if (temp < 0 || temp >= 86400)
return (0);
221 st->wHour = temp / 3600;
223 st->wMinute = temp / 60;
224 st->wSecond = temp % 60;
232static void tz_load_rule(
char *prule,
tz_info_entry *tz_info_remote) {
233 prule = tz_parse_name(prule, (
char *)tz_info_remote->tzi.StandardName, 30);
234 prule = tz_time2sec(prule, &tz_info_remote->tzi.Bias);
235 tz_info_remote->tzi.Bias /= 60;
236 tz_info_remote->tzi.StandardBias = 0;
238 prule = tz_parse_name(prule, (
char *)tz_info_remote->tzi.DaylightName, 30);
239 if (*(
char *)tz_info_remote->tzi.DaylightName !=
'\0') {
240 prule = tz_time2sec(prule, &tz_info_remote->tzi.DaylightBias);
241 tz_info_remote->tzi.DaylightBias /= 60;
242 if (tz_info_remote->tzi.DaylightBias == 0)
243 tz_info_remote->tzi.DaylightBias = -60;
245 tz_info_remote->tzi.DaylightBias -= tz_info_remote->tzi.Bias;
248 prule = tz_parse_rule(prule, &tz_info_remote->tzi.DaylightDate);
249 if (prule && *prule ==
',')
250 tz_parse_rule(prule, &tz_info_remote->tzi.StandardDate);
252 tz_parse_rule((
char *)
"M10.5.0/02:00:00",
253 &tz_info_remote->tzi.StandardDate);
255 tz_parse_rule((
char *)
"M4.1.0/02:00:00",
256 &tz_info_remote->tzi.DaylightDate);
257 tz_parse_rule((
char *)
"M10.5.0/02:00:00",
258 &tz_info_remote->tzi.StandardDate);
261 tz_info_remote->tzi.DaylightDate.wMonth = 0;
262 tz_info_remote->isdst = 0;
267static void change_time_zone(
const char *tz) {
271 if (*tz ==
':') tz++;
275 if (tz_names[index][0] == NULL) {
276 tz_info = &tz_info_local;
280 if (!strcmp(tz_names[index][0], (tz))) {
282 strncpy(tz, tz_names[index][1], 39);
283 tz_load_rule(tz, &tz_info_remote);
284 tz_info = &tz_info_remote;
286 tz_info->year_beg = 0;
287 tz_info->year_end = 0;
295TCDS_Binary_Harmonic::TCDS_Binary_Harmonic() {
308TCDS_Binary_Harmonic::~TCDS_Binary_Harmonic() {
309 for (
int i = 0; i < num_csts; i++) {
310 free(m_cst_nodes[i]);
311 free(m_cst_epochs[i]);
319TC_Error_Code TCDS_Binary_Harmonic::LoadData(
const wxString &data_file_path) {
320 if (!open_tide_db(data_file_path.mb_str()))
return TC_TCD_FILE_CORRUPT;
326 source_ident = wxString(hdr.version, wxConvUTF8);
328 num_csts = hdr.constituents;
329 if (0 == num_csts)
return TC_GENERIC_ERROR;
331 num_nodes = hdr.number_of_years;
332 if (0 == num_nodes)
return TC_GENERIC_ERROR;
335 m_work_buffer = (
double *)malloc(num_csts *
sizeof(
double));
338 m_cst_speeds = (
double *)malloc(num_csts *
sizeof(
double));
340 for (
int a = 0; a < num_csts; a++) {
341 m_cst_speeds[a] = get_speed(a);
342 m_cst_speeds[a] *= M_PI / 648000;
346 m_first_year = hdr.start_year;
347 num_epochs = hdr.number_of_years;
349 m_cst_epochs = (
double **)malloc(num_csts *
sizeof(
double *));
350 for (
int i = 0; i < num_csts; i++)
351 m_cst_epochs[i] = (
double *)malloc(num_epochs *
sizeof(
double));
353 for (
int i = 0; i < num_csts; i++) {
354 for (
int year = 0; year < num_epochs; year++) {
355 m_cst_epochs[i][year] = get_equilibrium(i, year);
356 m_cst_epochs[i][year] *= M_PI / 180.0;
362 m_cst_nodes = (
double **)malloc(num_csts *
sizeof(
double *));
363 for (
int a = 0; a < num_csts; a++)
364 m_cst_nodes[a] = (
double *)malloc(num_nodes *
sizeof(
double));
366 for (
int a = 0; a < num_csts; a++) {
367 for (
int year = 0; year < num_nodes; year++)
368 m_cst_nodes[a][year] = get_node_factor(a, year);
374 for (
unsigned int i = 0; i < hdr.number_of_records; i++) {
375 read_tide_record(i, ptiderec);
388 pIDX->
IDX_lon = ptiderec->header.longitude;
389 pIDX->
IDX_lat = ptiderec->header.latitude;
391 const char *tz = get_tzfile(ptiderec->header.tzfile);
392 change_time_zone((
char *)tz);
399 std::string name(ptiderec->header.name);
400 size_t n = name.find(
"depth");
401 if (n != std::string::npos) {
402 std::string d = name.substr(n);
403 std::string dp = d.substr(6);
404 size_t nd = dp.find_first_of(
' ');
405 std::string sval = dp.substr(0, nd);
406 int depth = std::stoi(sval);
413 if (REFERENCE_STATION == ptiderec->header.record_type) {
417 if (caplin.Contains(_T(
"CURRENT")))
422 int t1 = ptiderec->zone_offset;
423 double zone_offset = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
435 psd->amplitude = (
double *)malloc(num_csts *
sizeof(
double));
436 psd->epoch = (
double *)malloc(num_csts *
sizeof(
double));
437 psd->station_name = (
char *)malloc(ONELINER_LENGTH);
439 strncpy(psd->station_name, ptiderec->header.name, MAXNAMELEN);
444 psd->meridian = -(tz_info->tzi.Bias * 60);
445 psd->zone_offset = zone_offset;
448 strncpy(psd->unit, get_level_units(ptiderec->level_units), 40 - 1);
449 psd->unit[40 - 1] =
'\0';
451 psd->have_BOGUS = (findunit(psd->unit) != -1) &&
452 (known_units[findunit(psd->unit)].type == BOGUS);
456 unit_c = findunit(
"knots");
458 unit_c = findunit(psd->unit);
461 strncpy(psd->units_conv, known_units[unit_c].name,
462 sizeof(psd->units_conv) - 1);
463 strncpy(psd->units_abbrv, known_units[unit_c].abbrv,
464 sizeof(psd->units_abbrv) - 1);
466 strncpy(psd->units_conv, psd->unit, 40 - 1);
467 psd->units_conv[40 - 1] =
'\0';
468 strncpy(psd->units_abbrv, psd->unit, 20 - 1);
469 psd->units_abbrv[20 - 1] =
'\0';
473 for (
int a = 0; a < num_csts; a++) {
474 psd->amplitude[a] = ptiderec->amplitude[a];
475 psd->epoch[a] = ptiderec->epoch[a] * M_PI / 180.;
478 psd->DATUM = ptiderec->datum_offset;
480 m_msd_array.Add(psd);
484 }
else if (SUBORDINATE_STATION == ptiderec->header.record_type) {
488 if (caplin.Contains(_T(
"CURRENT")))
493 int t1 = ptiderec->max_time_add;
494 double t1a = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
497 pIDX->
IDX_ht_mpy = ptiderec->max_level_multiply;
501 t1 = ptiderec->min_time_add;
502 t1a = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
505 pIDX->
IDX_lt_mpy = ptiderec->min_level_multiply;
519 m_IDX_array.Add(pIDX);
523 unsigned int max_index = GetMaxIndex();
524 for (
unsigned int i = 0; i < max_index; i++) {
542IDX_entry *TCDS_Binary_Harmonic::GetIndexEntry(
int n_index) {
543 return &m_IDX_array[n_index];
546TC_Error_Code TCDS_Binary_Harmonic::LoadHarmonicData(
IDX_entry *pIDX) {
561 -pRefSta->meridian + (pRefSta->zone_offset * 3600);
Represents an index entry for tidal and current data.
int have_offsets
Flag indicating presence of time/height offsets.
int IDX_time_zone
Station timezone offset from UTC (in minutes)
char IDX_type
Entry type identifier "TCtcIUu".
int num_nodes
Number of nodes in harmonic analysis.
float IDX_lt_off
Low tide height offset.
TCDataSource * pDataSource
Pointer to the associated data source.
float IDX_lt_mpy
Low tide height multiplier.
char IDX_reference_name[MAXNAMELEN]
Name of the reference station.
int Valid15
Validity flag for 15-minute interval data.
int IDX_flood_dir
Flood current direction (in degrees)
int IDX_ref_dbIndex
Database index of the reference station.
int IDX_Useable
Flag indicating if the entry is usable.
char IDX_station_name[MAXNAMELEN]
Name of the tidal or current station.
int station_tz_offset
Offset in seconds to convert from harmonic data (epochs) to the station time zone.
int first_year
First year of valid data.
double * m_work_buffer
Work buffer for calculations.
int IDX_ht_time_off
High tide time offset (in minutes)
source_data_t source_data_type
Format of the source data (ASCII or binary)
double ** m_cst_nodes
2D array of constituent nodes
int IDX_ebb_dir
Ebb current direction (in degrees)
double ** m_cst_epochs
2D array of constituent epochs
float IDX_ht_mpy
High tide height multiplier.
int current_depth
Depth for current stations.
double IDX_lat
Latitude of the station (in degrees, +North)
double IDX_lon
Longitude of the station (in degrees, +East)
int num_epochs
Number of epochs in harmonic data.
double * m_cst_speeds
Array of constituent speeds.
Station_Data * pref_sta_data
Pointer to the reference station data.
int IDX_lt_time_off
Low tide time offset (in minutes)
int num_csts
Number of harmonic constituents.
char * IDX_tzname
Timezone name (dynamically allocated)
float IDX_ht_off
High tide height offset.