home *** CD-ROM | disk | FTP | other *** search
- /* Tide TTY client for XTide
- Last modified 1/4/96
-
- This program uses the harmonic method to compute tide levels.
- All of the data and constants are read in from the harmonics file.
- Please refer to ReadMe Mac and README for more information.
-
-
- Copiright (C) 1996 Mikhail Fridberg
- Copyright (C) 1995 David Flater.
- Also starring: Jef Poskanzer.
-
- 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 2 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, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-
- The tide prediction algorithm used in this program was developed
- with United States Government funding, so no proprietary rights
- can be attached to it. For more information, refer to the
- following publications:
-
- Manual of Harmonic Analysis and Prediction of Tides. Special
- Publication No. 98, Revised (1940) Edition. United States
- Government Printing Office, 1941.
-
- Computer Applications to Tides in the National Ocean Survey.
- Supplement to Manual of Harmonic Analysis and Prediction of Tides
- (Special Publication No. 98). National Ocean Service, National
- Oceanic and Atmospheric Administration, U.S. Department of
- Commerce, January 1982.
-
- */
-
- #include "config.h"
- #include <stdio.h>
- #include <math.h>
- #include <stdlib.h>
- #include <signal.h>
- #include "unistd.h"
- #include <unix.h>
- #include <time.h>
- #include <assert.h>
- #include <string.h>
- #include "sys/time.h"
- #include <console.h>
- #include "tidelib.h"
- #include "arglib.h"
-
-
- /* External variables */
-
- extern char hfile_name[MAXARGLEN+1], location[MAXARGLEN+1], *ppm,
- tzfile[MAXARGLEN+1], *geometry;
- extern char *fgrise_color_arg, *fgfall_color_arg,
- *bg_color_arg, *fgtext_color_arg, *fgmark_color_arg,
- *fgmllw_color_arg, *fgmiddle_color_arg;
- extern int tadjust, utc, list, checkyear, text, now, graphmode, httimeoff,
- lttimeoff, tstep, mark, middle, mllw, lines, skinny, PPMWIDTH,
- PPMHEIGHT, hinc, tinc, loctz, iscurrent, curonly, toplines, hincmagic,
- calendar, banner;
- extern time_t faketime;
- extern double htleveloff, ltleveloff, marklev;
- extern Cursor waitCursor;
- extern CursHandle hCurs;
- extern int gHasFPU;
- extern int gTimezone;
- #define getarg if (!(arg = poparg())) barf (WANTMOREARGS)
- #define getint(a) if (sscanf (arg, "%d", &a) != 1) barf (BADINTEGER)
- #define getfloat(a) if (sscanf (arg, "%lf", &a) != 1) barf (BADFLOAT)
-
- int
- main(int argc, char **argv)
- {
- char *arg;
- int argl;
-
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MaxApplZone();
-
- argc=ccommand(&argv);
- cshow(stdout);
-
- InitGraf((Ptr) &qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- InitCursor();
- FindIfHasFPU(gHasFPU);
-
- /* Exit without core dump on assertion failures. */
- signal (SIGABRT, sigabort);
-
- /* From config.h */
- assert ((int)(strlen (deflocation)) <= MAXARGLEN);
- strcpy (location, deflocation);
- assert ((int)(strlen (hfile)) <= MAXARGLEN);
- strcpy (hfile_name, hfile);
- /* Check environment */
- if ((arg = getenv ("LOCATION"))) {
- assert ((int)(strlen (arg)) <= MAXARGLEN);
- strcpy (location, arg);
- }
- if ((arg = getenv ("HFILE"))) {
- assert ((int)(strlen (arg)) <= MAXARGLEN);
- strcpy (hfile_name, arg);
- }
- FindTimeZone();
- text = -1;
- push_command_line (argc, argv);
- /* Read default config file only if none is explicitly specified */
- for (argl=1;argl<argc;argl++) {
- if (!strcmp (argv[argl], "-config")) {
- argl = 0;
- break;
- }
- }
- /* Try to read file specified by TIDERC */
- if (argl) {
- if ((arg = getenv ("TIDERC"))) {
- if (push_config_file (arg))
- argl = 0;
- }
- }
- /* Try to read ~/.tiderc */
- if (argl) {
- if ((arg = getenv ("HOME"))) {
- char tfile[MAXARGLEN+1];
- assert ((int)(strlen (arg)) + 8 <= MAXARGLEN);
- sprintf (tfile, "%s/.tiderc", arg);
- if (push_config_file (tfile))
- argl = 0;
- }
- }
- /* If failed, try to read system config file */
- if (argl)
- push_config_file (sysconfig);
-
- while ((arg = poparg ())) {
- if (!strcmp (arg, "-hfile")) {
- getarg;
- strcpy (hfile_name, arg);
- } else if (!strcmp (arg, "-check")) {
- getarg;
- getint (checkyear);
- } else if (!strcmp (arg, "-ppm")) {
- getarg;
- if (ppm)
- free (ppm);
- ppm = (char *)strdup (arg);
- } else if (!strcmp (arg, "-config")) {
- getarg;
- if (!(push_config_file (arg)))
- barf (CONFIGFAIL);
- } else if (!strcmp (arg, "-gstart")) {
- getarg;
- faketime = parse_time_string (arg);
- } else if (!strcmp (arg, "-location")) {
- getarg;
- strcpy (location, arg);
- } else if (!strcmp (arg, "-text")) {
- if (dataarg ()) {
- getarg;
- getint (text);
- }
- } else if (!strcmp (arg, "-hinc")) {
- if (dataarg ()) {
- getarg;
- getint (hinc);
- }
- if (hinc < 1)
- hincmagic = 1;
- } else if (!strcmp (arg, "-tinc")) {
- if (dataarg ()) {
- getarg;
- getint (tinc);
- }
- if (tinc < 1)
- tinc = 1;
- } else if (!strcmp (arg, "-tz")) {
- getarg;
- tadjust = hhmm2seconds (arg);
- if (!tadjust)
- utc = 1;
- } else if (!strcmp (arg, "-list")) {
- list = 1;
- } else if (!strcmp (arg, "-listtz")) {
- list = 2;
- } else if (!strcmp (arg, "-loctz")) {
- loctz = 1;
- } else if (!strcmp (arg, "-graph")) {
- graphmode = 1;
- } else if (!strcmp (arg, "-gstretch")) {
- double gstretch;
- getarg;
- getfloat (gstretch);
- if (gstretch <= 0.0)
- barf (STRETCHTOOSMALL);
- tstep = (int)(180.0 / gstretch);
- if (tstep <= 0)
- barf (STRETCHTOOBIG);
- } else if (!strcmp (arg, "-utc")) {
- utc = 1;
- } else if (!strcmp (arg, "-mark")) {
- mark = 1;
- getarg;
- getfloat (marklev);
- } else if (!strcmp (arg, "-middle")) {
- middle = 1;
- } else if (!strcmp (arg, "-mllw")) {
- mllw = 1;
- } else if (!strcmp (arg, "-nolines")) {
- lines = 0;
- } else if (!strcmp (arg, "-now")) {
- now = 1;
- } else if (!strcmp (arg, "-calen")) {
- calendar = 1;
- } else if ((!strcmp (arg, "-geometry"))||(!strcmp (arg, "-ppmgeom"))) {
- getarg;
- if (geometry)
- free (geometry);
- geometry = (char *)strdup (arg);
- } else if (!strcmp (arg, "-cur")) {
- curonly = 1;
- } else if (!strcmp (arg, "-toplines")) {
- toplines = 1;
- } else if (!strcmp (arg, "-skinny")) {
- skinny = 1;
- } else if (!strcmp (arg, "-banner")) {
- banner = 1;
- } else if (!strcmp (arg, "-hloff")) {
- getarg;
- getfloat (htleveloff);
- } else if (!strcmp (arg, "-lloff")) {
- getarg;
- getfloat (ltleveloff);
- } else if (!strcmp (arg, "-htoff")) {
- getarg;
- httimeoff = hhmm2seconds (arg);
- } else if (!strcmp (arg, "-ltoff")) {
- getarg;
- lttimeoff = hhmm2seconds (arg);
- } else if (!strcmp (arg, "-bg")) {
- getarg;
- if (bg_color_arg)
- free (bg_color_arg);
- bg_color_arg = (char *)strdup(arg);
- } else if (!strcmp (arg, "-fgrise")) {
- getarg;
- if (fgrise_color_arg)
- free (fgrise_color_arg);
- fgrise_color_arg = (char *)strdup(arg);
- } else if (!strcmp (arg, "-fgfall")) {
- getarg;
- if (fgfall_color_arg)
- free (fgfall_color_arg);
- fgfall_color_arg = (char *)strdup(arg);
- } else if (!strcmp (arg, "-fgtext")) {
- getarg;
- if (fgtext_color_arg)
- free (fgtext_color_arg);
- fgtext_color_arg = (char *)strdup(arg);
- } else if (!strcmp (arg, "-fgmark")) {
- getarg;
- if (fgmark_color_arg)
- free (fgmark_color_arg);
- fgmark_color_arg = (char *)strdup(arg);
- } else if (!strcmp (arg, "-fgmllw")) {
- getarg;
- if (fgmllw_color_arg)
- free (fgmllw_color_arg);
- fgmllw_color_arg = (char *)strdup(arg);
- } else if (!strcmp (arg, "-fgmiddle")) {
- getarg;
- if (fgmiddle_color_arg)
- free (fgmiddle_color_arg);
- fgmiddle_color_arg = (char *)strdup(arg);
- } else if (!strcmp (arg, "-version")) {
- if (PATCHLEVEL)
- printf ("Tide %s.%d\n", VERSION, PATCHLEVEL);
- else
- printf ("Tide %s\n", VERSION);
- exit (0);
- } else {
- fprintf (stderr,
- "Usage: tide [-banner] Sideways ASCII tide graph\n"
- " [-bg color] Background color\n"
- " [-calen] Make 1 month tide calendar (slow)\n"
- " [-check YYYY] Check for errors in equilibrium arguments\n"
- " [-config {filename | -}] Read configuration file / stdin\n"
- " [-cur] Restrict -location search to currents\n");
- fprintf (stderr,
- " [-fgfall color] Color of ebb current\n"
- " [-fgmark color] Color of mark line\n"
- " [-fgmiddle color] Color of middle line\n"
- " [-fgmllw color] Color of mllw line\n"
- " [-fgrise color] Color of normal graph / flood current\n"
- " [-fgtext color] Color of text and lines\n");
- fprintf (stderr,
- " [-geometry NxN] PPM geometry\n"
- " [-graph] ASCII graph mode\n"
- " [-gstart YYYY:MM:DD:HH:MM] Time at which to start\n"
- " [-gstretch N.NN] Adjust graph detail vs. time span\n"
- " [-hfile ...] Location and name of harmonics file\n"
- " [-hinc [N]] Label depth marks in increments of N\n");
- fprintf (stderr,
- " [-hloff [-]N.NN] (Text) Tide level offset for high tide\n"
- " [-htoff [-]HH:MM] (Text) Time offset for high tide\n"
- " [-list] List all locations in harmonics file\n"
- " [-listtz] List with meridians and time zone info\n"
- " [-lloff [-]N.NN] (Text) Tide level offset for low tide\n"
- " [-location \"Name\"] Location to show tides for\n");
- fprintf (stderr,
- " [-loctz] Try to use time zone of location (Moof!)\n"
- " [-ltoff [-]HH:MM] (Text) Time offset for low tide\n"
- " [-mark [-]N.NN] Draw line at specified tide level\n"
- " [-middle] Draw line near mean tide level\n"
- " [-mllw] Draw line at mean lower low water level\n"
- " [-nolines] Suppress tick marks / depth lines\n");
- fprintf (stderr,
- " [-now] Mark current time on graph\n"
- " [-ppm {filename | -}] Write graph as PPM to file / stdout\n"
- " [-skinny] Abbreviate timestamps\n"
- " [-text N] List N tides to standard output\n"
- " [-tinc [N]] Label each N hours\n"
- " [-toplines] (PPM) Depth lines on top of graph\n");
- fprintf (stderr,
- " [-tz [-]HH:MM] Fixed time zone offset for timestamps\n"
- " [-utc] Show timestamps in UTC\n"
- " [-version] Print Tide version\n");
- fprintf (stderr,
- "\n"
- "Time format example -- half past midnight, June 1, 1995: 1995:06:01:00:30\n"
- "Colors must be specified as rgb:hh/hh/hh (where hh is a 2-digit hexadecimal\n"
- "number)\n");
- exit (0);
- }
- }
-
- /* Remove glaring inconsistencies in args */
- if (!lines)
- hinc = tinc = 0;
- if (banner) {
- graphmode = 0;
- ppm = 0x00;
- }
- if (graphmode && ppm)
- graphmode = 0;
- if (graphmode || ppm) {
- calendar = 0;
- if (!hinc)
- hincmagic = 1;
- }
- if (calendar || graphmode)
- skinny = 1;
- if ((httimeoff || lttimeoff || htleveloff != 0.0 || ltleveloff != 0.0)
- && (graphmode || ppm || banner || mark))
- barf (OFFSETSTEXTONLY);
-
- /* Adjust tstep for ASCII graph mode */
- if (graphmode)
- tstep *= AGTSTEP;
- /* Adjust tstep for banner mode */
- if (banner)
- tstep *= BANTSTEP;
-
- /* Parse geometry for PPM mode (it's needed here because of -now) */
- if (geometry && ppm) {
- if (sscanf (geometry, "%dx%d", &PPMWIDTH, &PPMHEIGHT) != 2)
- barf (BADGEOMETRY);
- if ((PPMWIDTH <= 20)||(PPMHEIGHT <= 20))
- barf (BADGEOMETRY);
- PPMHEIGHT -= 12; /* We take 12 extra, so compensate. */
- }
-
- if (!faketime) {
- faketime = time (0x00);
- /* Center graph around current time if -now */
- if (graphmode && now)
- faketime -= tstep * (TEXTWIDTH>>1);
- else if (ppm && now)
- faketime -= tstep * (PPMWIDTH>>1);
- }
-
- load_data ();
- if (checkyear) {
- check_epoch ();
- exit (0);
- }
- fprintf(stdout,"Using time offset of %d from UTC\n",gTimezone/3600);
- if (loctz)
- change_time_zone (tzfile); /* Moof! */
- if (iscurrent) {
- mark = 1;
- marklev = 0.0;
- }
- if (ppm) {
- tide2ppm (ppm);
- exit (0);
- }
- if (banner) {
- do_banner ();
- exit (0);
- }
- if (graphmode) {
- tide2ascii ();
- exit (0);
- }
- if (calendar) {
- do_calendar ();
- exit (0);
- }
- hCurs = GetCursor(watchCursor);
- waitCursor = **hCurs;
- list_tides ();
- SetCursor(&arrow);
- exit (0);
- }
-