34static const char *tz_names[][2] = {
69typedef unsigned short WORD;
86 WCHAR StandardName[32];
89 WCHAR DaylightName[32];
109tz_info_entry tz_info_local, tz_info_remote, *tz_info = &tz_info_local;
115char *tz_time2sec(
char *psrc,
long *timesec) {
120 while (*psrc ==
' ') psrc++;
121 if (*psrc ==
'+') psrc++;
130 while (isdigit(*psrc)) temp = temp * 10 + (*(psrc++) -
'0');
132 *timesec = *timesec + temp * mpy;
138 }
while (isdigit(*psrc));
140 if (neg) *timesec = 0 - *timesec;
149static char *tz_parse_name(
char *psrc,
char *pdst,
int maxlen) {
153 while (*psrc ==
' ') psrc++;
155 while (isalpha(*psrc) && nReturn < maxlen) {
156 *(pdst++) = *(psrc++);
167static char *tz_parse_rule(
char *psrc,
SYSTEMTIME *st) {
168 int mol[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
170 if (*psrc ==
',') psrc++;
172 while (*psrc ==
' ') psrc++;
181 st->wMilliseconds = 0;
186 while (isdigit(*psrc)) temp = temp * 10 + (*(psrc++) -
'0');
188 if (temp < 1 || temp > 365)
return (0);
190 for (mo = 0; temp >= mol[mo]; mo++) temp -= mol[mo];
196 else if (*psrc ==
'M') {
200 while (isdigit(*psrc)) temp = temp * 10 + (*(psrc++) -
'0');
201 if (temp < 1 || temp > 12 || *psrc !=
'.')
return (0);
202 st->wMonth = (
unsigned short)temp;
206 while (isdigit(*psrc))
207 temp = temp * 10 + (*(psrc++) -
'0');
208 if (temp < 1 || temp > 5 || *psrc !=
'.')
return (0);
209 st->wDay = (
unsigned short)temp;
213 while (isdigit(*psrc))
214 temp = temp * 10 + (*(psrc++) -
'0');
215 if (temp < 0 || temp > 6)
return (0);
216 st->wDayOfWeek = (
unsigned short)temp;
221 psrc = tz_time2sec(psrc, &temp);
222 if (temp < 0 || temp >= 86400)
return (0);
223 st->wHour = temp / 3600;
225 st->wMinute = temp / 60;
226 st->wSecond = temp % 60;
234static void tz_load_rule(
char *prule,
tz_info_entry *tz_info_remote) {
235 prule = tz_parse_name(prule, (
char *)tz_info_remote->tzi.StandardName, 30);
236 prule = tz_time2sec(prule, &tz_info_remote->tzi.Bias);
237 tz_info_remote->tzi.Bias /= 60;
238 tz_info_remote->tzi.StandardBias = 0;
240 prule = tz_parse_name(prule, (
char *)tz_info_remote->tzi.DaylightName, 30);
241 if (*(
char *)tz_info_remote->tzi.DaylightName !=
'\0') {
242 prule = tz_time2sec(prule, &tz_info_remote->tzi.DaylightBias);
243 tz_info_remote->tzi.DaylightBias /= 60;
244 if (tz_info_remote->tzi.DaylightBias == 0)
245 tz_info_remote->tzi.DaylightBias = -60;
247 tz_info_remote->tzi.DaylightBias -= tz_info_remote->tzi.Bias;
250 prule = tz_parse_rule(prule, &tz_info_remote->tzi.DaylightDate);
251 if (prule && *prule ==
',')
252 tz_parse_rule(prule, &tz_info_remote->tzi.StandardDate);
254 tz_parse_rule((
char *)
"M10.5.0/02:00:00",
255 &tz_info_remote->tzi.StandardDate);
257 tz_parse_rule((
char *)
"M4.1.0/02:00:00",
258 &tz_info_remote->tzi.DaylightDate);
259 tz_parse_rule((
char *)
"M10.5.0/02:00:00",
260 &tz_info_remote->tzi.StandardDate);
263 tz_info_remote->tzi.DaylightDate.wMonth = 0;
264 tz_info_remote->isdst = 0;
269static void change_time_zone(
const char *tz) {
273 if (*tz ==
':') tz++;
277 if (tz_names[index][0] == NULL) {
278 tz_info = &tz_info_local;
282 if (!strcmp(tz_names[index][0], (tz))) {
284 strncpy(tz, tz_names[index][1], 39);
285 tz_load_rule(tz, &tz_info_remote);
286 tz_info = &tz_info_remote;
288 tz_info->year_beg = 0;
289 tz_info->year_end = 0;
297TCDS_Binary_Harmonic::TCDS_Binary_Harmonic() {
307 m_work_buffer =
nullptr;
308 m_cst_epochs =
nullptr;
309 m_cst_nodes =
nullptr;
310 m_cst_speeds =
nullptr;
313TCDS_Binary_Harmonic::~TCDS_Binary_Harmonic() {
314 for (
int i = 0; i < num_csts; i++) {
315 free(m_cst_nodes[i]);
316 free(m_cst_epochs[i]);
324TC_Error_Code TCDS_Binary_Harmonic::LoadData(
const wxString &data_file_path) {
326 if (!open_tide_db(data_file_path.mb_str()))
return TC_TCD_FILE_CORRUPT;
328 return TC_TCD_FILE_CORRUPT;
335 source_ident = wxString(hdr.version, wxConvUTF8);
337 num_csts = hdr.constituents;
338 if (0 == num_csts)
return TC_GENERIC_ERROR;
340 num_nodes = hdr.number_of_years;
341 if (0 == num_nodes)
return TC_GENERIC_ERROR;
344 m_work_buffer = (
double *)malloc(num_csts *
sizeof(
double));
347 m_cst_speeds = (
double *)malloc(num_csts *
sizeof(
double));
349 for (
int a = 0; a < num_csts; a++) {
350 m_cst_speeds[a] = get_speed(a);
351 m_cst_speeds[a] *= M_PI / 648000;
355 m_first_year = hdr.start_year;
356 num_epochs = hdr.number_of_years;
358 m_cst_epochs = (
double **)malloc(num_csts *
sizeof(
double *));
359 for (
int i = 0; i < num_csts; i++)
360 m_cst_epochs[i] = (
double *)malloc(num_epochs *
sizeof(
double));
362 for (
int i = 0; i < num_csts; i++) {
363 for (
int year = 0; year < num_epochs; year++) {
364 m_cst_epochs[i][year] = get_equilibrium(i, year);
365 m_cst_epochs[i][year] *= M_PI / 180.0;
371 m_cst_nodes = (
double **)malloc(num_csts *
sizeof(
double *));
372 for (
int a = 0; a < num_csts; a++)
373 m_cst_nodes[a] = (
double *)malloc(num_nodes *
sizeof(
double));
375 for (
int a = 0; a < num_csts; a++) {
376 for (
int year = 0; year < num_nodes; year++)
377 m_cst_nodes[a][year] = get_node_factor(a, year);
383 for (
unsigned int i = 0; i < hdr.number_of_records; i++) {
384 read_tide_record(i, ptiderec);
397 pIDX->
IDX_lon = ptiderec->header.longitude;
398 pIDX->
IDX_lat = ptiderec->header.latitude;
400 const char *tz = get_tzfile(ptiderec->header.tzfile);
401 change_time_zone((
char *)tz);
408 std::string name(ptiderec->header.name);
409 size_t n = name.find(
"depth");
410 if (n != std::string::npos) {
411 std::string d = name.substr(n);
412 std::string dp = d.substr(6);
413 size_t nd = dp.find_first_of(
' ');
414 std::string sval = dp.substr(0, nd);
415 int depth = std::stoi(sval);
422 if (REFERENCE_STATION == ptiderec->header.record_type) {
426 if (caplin.Contains(
"CURRENT"))
431 int t1 = ptiderec->zone_offset;
432 double zone_offset = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
444 psd->amplitude = (
double *)malloc(num_csts *
sizeof(
double));
445 psd->epoch = (
double *)malloc(num_csts *
sizeof(
double));
446 psd->station_name = (
char *)malloc(ONELINER_LENGTH);
448 strncpy(psd->station_name, ptiderec->header.name, MAXNAMELEN);
453 psd->meridian = -(tz_info->tzi.Bias * 60);
454 psd->zone_offset = zone_offset;
457 strncpy(psd->unit, get_level_units(ptiderec->level_units), 40 - 1);
458 psd->unit[40 - 1] =
'\0';
460 psd->have_BOGUS = (findunit(psd->unit) != -1) &&
461 (known_units[findunit(psd->unit)].type == BOGUS);
465 unit_c = findunit(
"knots");
467 unit_c = findunit(psd->unit);
470 strncpy(psd->units_conv, known_units[unit_c].name,
471 sizeof(psd->units_conv) - 1);
472 strncpy(psd->units_abbrv, known_units[unit_c].abbrv,
473 sizeof(psd->units_abbrv) - 1);
475 strncpy(psd->units_conv, psd->unit, 40 - 1);
476 psd->units_conv[40 - 1] =
'\0';
477 strncpy(psd->units_abbrv, psd->unit, 20 - 1);
478 psd->units_abbrv[20 - 1] =
'\0';
482 for (
int a = 0; a < num_csts; a++) {
483 psd->amplitude[a] = ptiderec->amplitude[a];
484 psd->epoch[a] = ptiderec->epoch[a] * M_PI / 180.;
487 psd->DATUM = ptiderec->datum_offset;
489 m_msd_array.Add(psd);
493 }
else if (SUBORDINATE_STATION == ptiderec->header.record_type) {
497 if (caplin.Contains(
"CURRENT"))
502 int t1 = ptiderec->max_time_add;
503 double t1a = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
506 pIDX->
IDX_ht_mpy = ptiderec->max_level_multiply;
510 t1 = ptiderec->min_time_add;
511 t1a = (double)(t1 / 100) + ((double)(t1 % 100)) / 60.;
514 pIDX->
IDX_lt_mpy = ptiderec->min_level_multiply;
528 m_IDX_array.Add(pIDX);
532 unsigned int max_index = GetMaxIndex();
533 for (
unsigned int i = 0; i < max_index; i++) {
551IDX_entry *TCDS_Binary_Harmonic::GetIndexEntry(
int n_index) {
552 return &m_IDX_array[n_index];
555TC_Error_Code TCDS_Binary_Harmonic::LoadHarmonicData(
IDX_entry *pIDX) {
570 -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.
@ SOURCE_TYPE_BINARY_HARMONIC
Binary harmonic source type.
TCDataFactory loading data from binary file.
Tide and Current Manager @TODO Add original author copyright.