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() {}
310TC_Error_Code TCDS_Binary_Harmonic::LoadData(
const wxString &data_file_path) {
311 if (!open_tide_db(data_file_path.mb_str()))
return TC_TCD_FILE_CORRUPT;
317 source_ident = wxString(hdr.version, wxConvUTF8);
319 num_csts = hdr.constituents;
320 if (0 == num_csts)
return TC_GENERIC_ERROR;
322 num_nodes = hdr.number_of_years;
323 if (0 == num_nodes)
return TC_GENERIC_ERROR;
326 m_work_buffer = (
double *)malloc(num_csts *
sizeof(
double));
329 m_cst_speeds = (
double *)malloc(num_csts *
sizeof(
double));
331 for (
int a = 0; a < num_csts; a++) {
332 m_cst_speeds[a] = get_speed(a);
333 m_cst_speeds[a] *= M_PI / 648000;
337 m_first_year = hdr.start_year;
338 num_epochs = hdr.number_of_years;
340 m_cst_epochs = (
double **)malloc(num_csts *
sizeof(
double *));
341 for (
int i = 0; i < num_csts; i++)
342 m_cst_epochs[i] = (
double *)malloc(num_epochs *
sizeof(
double));
344 for (
int i = 0; i < num_csts; i++) {
345 for (
int year = 0; year < num_epochs; year++) {
346 m_cst_epochs[i][year] = get_equilibrium(i, year);
347 m_cst_epochs[i][year] *= M_PI / 180.0;
353 m_cst_nodes = (
double **)malloc(num_csts *
sizeof(
double *));
354 for (
int a = 0; a < num_csts; a++)
355 m_cst_nodes[a] = (
double *)malloc(num_nodes *
sizeof(
double));
357 for (
int a = 0; a < num_csts; a++) {
358 for (
int year = 0; year < num_nodes; year++)
359 m_cst_nodes[a][year] = get_node_factor(a, year);
365 for (
unsigned int i = 0; i < hdr.number_of_records; i++) {
366 read_tide_record(i, ptiderec);
370 pIDX->source_data_type = SOURCE_TYPE_BINARY_HARMONIC;
371 pIDX->pDataSource = NULL;
375 pIDX->pref_sta_data = NULL;
376 pIDX->IDX_Useable = 1;
377 pIDX->IDX_tzname = NULL;
379 pIDX->IDX_lon = ptiderec->header.longitude;
380 pIDX->IDX_lat = ptiderec->header.latitude;
382 const char *tz = get_tzfile(ptiderec->header.tzfile);
383 change_time_zone((
char *)tz);
384 if (tz_info) pIDX->IDX_time_zone = -tz_info->tzi.Bias;
386 strncpy(pIDX->IDX_station_name, ptiderec->header.name, MAXNAMELEN);
390 std::string name(ptiderec->header.name);
391 size_t n = name.find(
"depth");
392 if (n != std::string::npos) {
393 std::string d = name.substr(n);
394 std::string dp = d.substr(6);
395 size_t nd = dp.find_first_of(
' ');
396 std::string sval = dp.substr(0, nd);
397 int depth = std::stoi(sval);
398 pIDX->current_depth = depth;
401 pIDX->IDX_flood_dir = ptiderec->max_direction;
402 pIDX->IDX_ebb_dir = ptiderec->min_direction;
404 if (REFERENCE_STATION == ptiderec->header.record_type) {
406 wxString caplin(pIDX->IDX_station_name, wxConvUTF8);
408 if (caplin.Contains(_T(
"CURRENT")))
409 pIDX->IDX_type =
'C';
411 pIDX->IDX_type =
'T';
413 int t1 = ptiderec->zone_offset;
414 double zone_offset = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
417 pIDX->IDX_ht_time_off = pIDX->IDX_lt_time_off = 0;
418 pIDX->IDX_ht_mpy = pIDX->IDX_lt_mpy = 1.0;
419 pIDX->IDX_ht_off = pIDX->IDX_lt_off = 0.0;
420 pIDX->IDX_ref_dbIndex = ptiderec->header.reference_station;
426 psd->amplitude = (
double *)malloc(num_csts *
sizeof(
double));
427 psd->epoch = (
double *)malloc(num_csts *
sizeof(
double));
428 psd->station_name = (
char *)malloc(ONELINER_LENGTH);
430 strncpy(psd->station_name, ptiderec->header.name, MAXNAMELEN);
431 psd->station_type = pIDX->IDX_type;
435 psd->meridian = -(tz_info->tzi.Bias * 60);
436 psd->zone_offset = zone_offset;
439 strncpy(psd->unit, get_level_units(ptiderec->level_units), 40 - 1);
440 psd->unit[40 - 1] =
'\0';
442 psd->have_BOGUS = (findunit(psd->unit) != -1) &&
443 (known_units[findunit(psd->unit)].type == BOGUS);
447 unit_c = findunit(
"knots");
449 unit_c = findunit(psd->unit);
452 strncpy(psd->units_conv, known_units[unit_c].name,
453 sizeof(psd->units_conv) - 1);
454 strncpy(psd->units_abbrv, known_units[unit_c].abbrv,
455 sizeof(psd->units_abbrv) - 1);
457 strncpy(psd->units_conv, psd->unit, 40 - 1);
458 psd->units_conv[40 - 1] =
'\0';
459 strncpy(psd->units_abbrv, psd->unit, 20 - 1);
460 psd->units_abbrv[20 - 1] =
'\0';
464 for (
int a = 0; a < num_csts; a++) {
465 psd->amplitude[a] = ptiderec->amplitude[a];
466 psd->epoch[a] = ptiderec->epoch[a] * M_PI / 180.;
469 psd->DATUM = ptiderec->datum_offset;
471 m_msd_array.Add(psd);
472 pIDX->pref_sta_data = psd;
473 pIDX->IDX_ref_dbIndex = i;
474 pIDX->have_offsets = 0;
475 }
else if (SUBORDINATE_STATION == ptiderec->header.record_type) {
477 wxString caplin(pIDX->IDX_station_name, wxConvUTF8);
479 if (caplin.Contains(_T(
"CURRENT")))
480 pIDX->IDX_type =
'c';
482 pIDX->IDX_type =
't';
484 int t1 = ptiderec->max_time_add;
485 double t1a = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
487 pIDX->IDX_ht_time_off = t1a;
488 pIDX->IDX_ht_mpy = ptiderec->max_level_multiply;
489 if (0. == pIDX->IDX_ht_mpy) pIDX->IDX_ht_mpy = 1.0;
490 pIDX->IDX_ht_off = ptiderec->max_level_add;
492 t1 = ptiderec->min_time_add;
493 t1a = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
495 pIDX->IDX_lt_time_off = t1a;
496 pIDX->IDX_lt_mpy = ptiderec->min_level_multiply;
497 if (0. == pIDX->IDX_lt_mpy) pIDX->IDX_lt_mpy = 1.0;
498 pIDX->IDX_lt_off = ptiderec->min_level_add;
500 pIDX->IDX_ref_dbIndex = ptiderec->header.reference_station;
504 if (pIDX->IDX_ht_time_off || pIDX->IDX_ht_off != 0.0 ||
505 pIDX->IDX_lt_off != 0.0 || pIDX->IDX_ht_mpy != 1.0 ||
506 pIDX->IDX_lt_mpy != 1.0)
507 pIDX->have_offsets = 1;
510 m_IDX_array.Add(pIDX);
514 unsigned int max_index = GetMaxIndex();
515 for (
unsigned int i = 0; i < max_index; i++) {
518 pIDX->num_nodes = num_nodes;
519 pIDX->num_csts = num_csts;
520 pIDX->num_epochs = num_epochs;
521 pIDX->m_cst_speeds = m_cst_speeds;
522 pIDX->m_cst_nodes = m_cst_nodes;
523 pIDX->m_cst_epochs = m_cst_epochs;
524 pIDX->first_year = m_first_year;
525 pIDX->m_work_buffer = m_work_buffer;
533IDX_entry *TCDS_Binary_Harmonic::GetIndexEntry(
int n_index) {
534 return &m_IDX_array[n_index];
537TC_Error_Code TCDS_Binary_Harmonic::LoadHarmonicData(
IDX_entry *pIDX) {
539 if (!strlen(pIDX->IDX_reference_name)) {
540 strncpy(pIDX->IDX_reference_name, get_station(pIDX->IDX_ref_dbIndex),
542 pIDX->IDX_reference_name[MAXNAMELEN - 1] =
'\0';
548 IDX_entry *pIDX_Ref = &m_IDX_array.Item(pIDX->IDX_ref_dbIndex);
550 pIDX->pref_sta_data = pRefSta;
551 pIDX->station_tz_offset =
552 -pRefSta->meridian + (pRefSta->zone_offset * 3600);