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
|
// $Id: TideEvent.cc 2833 2007-12-01 01:27:02Z flaterco $
// TideEvent Generic representation for tide events.
/*
Copyright (C) 2004 David Flater.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "common.hh"
// For VC++, getpid is in process.h.
#ifdef HAVE_PROCESS_H
#include <process.h>
#endif
const bool TideEvent::isSunMoonEvent () const {
switch (eventType) {
case TideEvent::sunrise:
case TideEvent::sunset:
case TideEvent::moonrise:
case TideEvent::moonset:
case TideEvent::newmoon:
case TideEvent::firstquarter:
case TideEvent::fullmoon:
case TideEvent::lastquarter:
return true;
default:
return false;
}
}
const bool TideEvent::isMaxMinEvent () const {
switch (eventType) {
case TideEvent::max:
case TideEvent::min:
return true;
default:
return false;
}
}
const bool TideEvent::isMinCurrentEvent () const {
switch (eventType) {
case TideEvent::max:
return (isCurrent && (eventLevel.val() < 0.0));
case TideEvent::min:
return (isCurrent && (eventLevel.val() > 0.0));
default:
return false;
}
}
constString TideEvent::longDescription () const {
switch (eventType) {
case TideEvent::max:
return (isCurrent ? (eventLevel.val() >= 0.0 ? "Max Flood"
: "Min Ebb") : "High Tide");
case TideEvent::min:
return (isCurrent ? (eventLevel.val() <= 0.0 ? "Max Ebb"
: "Min Flood") : "Low Tide");
case TideEvent::slackrise:
return "Slack, Flood Begins";
case TideEvent::slackfall:
return "Slack, Ebb Begins";
case TideEvent::markrise:
if (isCurrent) {
if (eventLevel.val() < 0.0)
return "Mark, Ebb Decreasing";
if (eventLevel.val() > 0.0)
return "Mark, Flood Increasing";
return "Mark, Flood Begins";
}
return "Mark Rising";
case TideEvent::markfall:
if (isCurrent) {
if (eventLevel.val() < 0.0)
return "Mark, Ebb Increasing";
if (eventLevel.val() > 0.0)
return "Mark, Flood Decreasing";
return "Mark, Ebb Begins";
}
return "Mark Falling";
case TideEvent::sunrise:
return "Sunrise";
case TideEvent::sunset:
return "Sunset";
case TideEvent::moonrise:
return "Moonrise";
case TideEvent::moonset:
return "Moonset";
case TideEvent::newmoon:
return "New Moon";
case TideEvent::firstquarter:
return "First Quarter";
case TideEvent::fullmoon:
return "Full Moon";
case TideEvent::lastquarter:
return "Last Quarter";
case TideEvent::rawreading:
default:
assert (false);
}
return NULL; // Silence bogus "control reaches end of non-void function"
}
// This is only used for the bottom blurb line in graphs.
// Max length 5 characters.
constString TideEvent::shortDescription () const {
switch (eventType) {
case TideEvent::slackrise:
case TideEvent::slackfall:
return "Slack";
case TideEvent::markrise:
case TideEvent::markfall:
return "Mark";
case TideEvent::moonrise:
return "Mrise";
case TideEvent::moonset:
return "Mset";
case TideEvent::newmoon:
return "New";
case TideEvent::firstquarter:
return "1st/4";
case TideEvent::fullmoon:
return "Full";
case TideEvent::lastquarter:
return "3rd/4";
default:
assert (false);
}
return NULL; // Silence bogus "control reaches end of non-void function"
}
void TideEvent::print (Dstr &text_out,
Mode::Mode mode,
Format::Format form,
const Station &station) const {
Dstr timePrint, levelPrint;
switch (mode) {
case Mode::raw:
switch (form) {
case Format::CSV:
text_out = station.name;
text_out.repchar (',', Global::CSV_repchar);
text_out += ',';
text_out += eventTime.timet();
text_out += ',';
text_out += eventLevel.val();
return;
case Format::text:
text_out = eventTime.timet();
text_out += ' ';
text_out += eventLevel.val();
return;
default:
assert (false);
}
case Mode::mediumRare:
switch (form) {
case Format::CSV:
text_out = station.name;
text_out.repchar (',', Global::CSV_repchar);
text_out += ',';
eventTime.printDate (timePrint, station.timezone);
text_out += timePrint;
text_out += ',';
eventTime.printTime (timePrint, station.timezone);
text_out += timePrint;
text_out += ',';
text_out += eventLevel.val();
return;
case Format::text:
eventTime.print (text_out, station.timezone);
text_out += ' ';
text_out += eventLevel.val();
return;
default:
assert (false);
}
case Mode::plain:
switch (form) {
case Format::text:
if (!isSunMoonEvent())
eventLevel.print (levelPrint);
eventTime.print (timePrint, station.timezone);
text_out = timePrint;
text_out += ' ';
text_out += levelPrint;
text_out += " ";
text_out += longDescription ();
return;
case Format::CSV:
if (!isSunMoonEvent())
eventLevel.printnp (levelPrint);
text_out = station.name;
text_out.repchar (',', Global::CSV_repchar);
text_out += ',';
eventTime.printDate (timePrint, station.timezone);
text_out += timePrint;
text_out += ',';
eventTime.printTime (timePrint, station.timezone);
text_out += timePrint;
text_out += ',';
text_out += levelPrint;
text_out += ',';
{
Dstr mangle (longDescription());
mangle.repchar (',', Global::CSV_repchar);
text_out += mangle;
}
return;
case Format::iCalendar:
{
if (!isSunMoonEvent())
eventLevel.print (levelPrint);
static unsigned long snum = 0;
char temp[80];
// Fields required by RFC 2446 for PUBLISH method on VEVENT:
// DTSTAMP
// DTSTART
// ORGANIZER
// SUMMARY (can be null)
// UID
// Optional fields of interest:
// COMMENT
// DESCRIPTION
// GEO
// LOCATION
// TRANSP
// RFC 2445 p. 52:
//
// For cases where a "VEVENT" calendar component specifies a
// "DTSTART" property with a DATE-TIME data type but no "DTEND"
// property, the event ends on the same calendar date and time of
// day specified by the "DTSTART" property.
// TRANSP should be redundant for an event that takes up no time,
// but being explicit may help less enlightened clients to do the
// right thing.
Timestamp now ((time_t)(time(NULL)));
Dstr nowPrint;
now.print_iCalendar (nowPrint, Timestamp::keepSecs);
text_out = "BEGIN:VEVENT\r\nDTSTAMP:";
text_out += nowPrint;
text_out += "\r\nDTSTART:";
eventTime.print_iCalendar (timePrint, Timestamp::zeroOutSecs);
text_out += timePrint;
text_out += "\r\nTRANSP:TRANSPARENT\r\nORGANIZER;CN=XTide:MAILTO:nobody@localhost\r\nSUMMARY:";
text_out += longDescription();
if (!levelPrint.isNull()) {
text_out += ' ';
text_out += levelPrint;
}
text_out += "\r\nUID:XTide-";
if (!station.coordinates.isNull()) {
sprintf (temp, "%ld-%ld-", (long)(station.coordinates.lat()*10000),
(long)(station.coordinates.lng()*10000));
text_out += temp;
}
eventTime.print_iCalendar (timePrint, Timestamp::keepSecs);
text_out += timePrint;
// Latitude + longitude + event time should be nearly unique, but
// "you can never have too much overkill."
text_out += "-\r\n ";
text_out += nowPrint;
text_out += '-';
text_out += getpid();
text_out += '-';
text_out += ++snum;
text_out += '-';
text_out += rand();
text_out += "@localhost\r\n";
if (!station.coordinates.isNull()) {
sprintf (temp, "GEO:%6.4f;%6.4f\r\n", station.coordinates.lat(),
station.coordinates.lng());
text_out += temp;
}
text_out += "LOCATION:";
{
Dstr mangle (station.name);
mangle.rfc2445_mangle();
text_out += mangle;
}
text_out += "\r\nEND:VEVENT\r";
}
return;
default:
assert (false);
}
default:
assert (false);
}
}
// Cleanup2006 Done
|