home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / MacTide 1.3.3 / tide.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-04  |  13.3 KB  |  433 lines  |  [TEXT/MPS ]

  1. /*  Tide  TTY client for XTide
  2.     Last modified 1/4/96
  3.  
  4.     This program uses the harmonic method to compute tide levels.
  5.     All of the data and constants are read in from the harmonics file.
  6.     Please refer to ReadMe Mac and README for more information.
  7.  
  8.  
  9.     Copiright (C) 1996 Mikhail Fridberg
  10.     Copyright (C) 1995  David Flater.
  11.     Also starring:  Jef Poskanzer.
  12.  
  13.     This program is free software; you can redistribute it and/or modify
  14.     it under the terms of the GNU General Public License as published by
  15.     the Free Software Foundation; either version 2 of the License, or
  16.     (at your option) any later version.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program; if not, write to the Free Software
  25.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27.  
  28.     The tide prediction algorithm used in this program was developed
  29.     with United States Government funding, so no proprietary rights
  30.     can be attached to it.  For more information, refer to the
  31.     following publications:
  32.  
  33.     Manual of Harmonic Analysis and Prediction of Tides.  Special
  34.     Publication No. 98, Revised (1940) Edition.  United States
  35.     Government Printing Office, 1941.
  36.  
  37.     Computer Applications to Tides in the National Ocean Survey.
  38.     Supplement to Manual of Harmonic Analysis and Prediction of Tides
  39.     (Special Publication No. 98).  National Ocean Service, National
  40.     Oceanic and Atmospheric Administration, U.S. Department of
  41.     Commerce, January 1982.
  42.  
  43. */
  44.  
  45. #include "config.h"
  46. #include <stdio.h>
  47. #include <math.h>
  48. #include <stdlib.h>
  49. #include <signal.h>
  50. #include "unistd.h"
  51. #include <unix.h>
  52. #include <time.h>
  53. #include <assert.h>
  54. #include <string.h>
  55. #include "sys/time.h"
  56. #include <console.h>
  57. #include "tidelib.h"
  58. #include "arglib.h"
  59.  
  60.  
  61. /* External variables */
  62.  
  63. extern char hfile_name[MAXARGLEN+1], location[MAXARGLEN+1], *ppm,
  64.   tzfile[MAXARGLEN+1], *geometry;
  65. extern char *fgrise_color_arg, *fgfall_color_arg,
  66.   *bg_color_arg, *fgtext_color_arg, *fgmark_color_arg,
  67.   *fgmllw_color_arg, *fgmiddle_color_arg;
  68. extern int tadjust, utc, list, checkyear, text, now, graphmode, httimeoff,
  69.   lttimeoff, tstep, mark, middle, mllw, lines, skinny, PPMWIDTH,
  70.   PPMHEIGHT, hinc, tinc, loctz, iscurrent, curonly, toplines, hincmagic,
  71.   calendar, banner;
  72. extern time_t faketime;
  73. extern double htleveloff, ltleveloff, marklev;
  74. extern  Cursor            waitCursor;
  75. extern  CursHandle        hCurs;                
  76. extern    int                gHasFPU;
  77. extern    int                gTimezone;
  78. #define getarg if (!(arg = poparg())) barf (WANTMOREARGS)
  79. #define getint(a) if (sscanf (arg, "%d", &a) != 1) barf (BADINTEGER)
  80. #define getfloat(a) if (sscanf (arg, "%lf", &a) != 1) barf (BADFLOAT)
  81.  
  82. int
  83. main(int argc, char **argv)
  84. {
  85.   char *arg;
  86.   int argl;
  87.  
  88.       MoreMasters();
  89.       MoreMasters();
  90.       MoreMasters();
  91.     MaxApplZone();
  92.  
  93.   argc=ccommand(&argv);
  94.   cshow(stdout);
  95.  
  96.     InitGraf((Ptr) &qd.thePort);
  97.       InitFonts();
  98.       InitWindows();
  99.       InitMenus();
  100.       TEInit();
  101.       InitDialogs(NULL);
  102.       InitCursor();
  103.     FindIfHasFPU(gHasFPU);
  104.  
  105.   /* Exit without core dump on assertion failures. */
  106.   signal (SIGABRT, sigabort);
  107.  
  108.   /* From config.h */
  109.   assert ((int)(strlen (deflocation)) <= MAXARGLEN);
  110.   strcpy (location, deflocation);
  111.   assert ((int)(strlen (hfile)) <= MAXARGLEN);
  112.   strcpy (hfile_name, hfile);
  113.   /* Check environment */
  114.   if ((arg = getenv ("LOCATION"))) {
  115.     assert ((int)(strlen (arg)) <= MAXARGLEN);
  116.     strcpy (location, arg);
  117.   }
  118.   if ((arg = getenv ("HFILE"))) {
  119.     assert ((int)(strlen (arg)) <= MAXARGLEN);
  120.     strcpy (hfile_name, arg);
  121.   }
  122.   FindTimeZone();
  123.   text = -1;
  124.   push_command_line (argc, argv);
  125.   /* Read default config file only if none is explicitly specified */
  126.   for (argl=1;argl<argc;argl++) {
  127.     if (!strcmp (argv[argl], "-config")) {
  128.       argl = 0;
  129.       break;
  130.     }
  131.   }
  132.   /* Try to read file specified by TIDERC */
  133.   if (argl) {
  134.     if ((arg = getenv ("TIDERC"))) {
  135.       if (push_config_file (arg))
  136.         argl = 0;
  137.     }
  138.   }
  139.   /* Try to read ~/.tiderc */
  140.   if (argl) {
  141.     if ((arg = getenv ("HOME"))) {
  142.       char tfile[MAXARGLEN+1];
  143.       assert ((int)(strlen (arg)) + 8 <= MAXARGLEN);
  144.       sprintf (tfile, "%s/.tiderc", arg);
  145.       if (push_config_file (tfile))
  146.         argl = 0;
  147.     }
  148.   }
  149.   /* If failed, try to read system config file */
  150.   if (argl)
  151.     push_config_file (sysconfig);
  152.  
  153.   while ((arg = poparg ())) {
  154.     if (!strcmp (arg, "-hfile")) {
  155.       getarg;
  156.       strcpy (hfile_name, arg);
  157.     } else if (!strcmp (arg, "-check")) {
  158.       getarg;
  159.       getint (checkyear);
  160.     } else if (!strcmp (arg, "-ppm")) {
  161.       getarg;
  162.       if (ppm)
  163.         free (ppm);
  164.       ppm = (char *)strdup (arg);
  165.     } else if (!strcmp (arg, "-config")) {
  166.       getarg;
  167.       if (!(push_config_file (arg)))
  168.         barf (CONFIGFAIL);
  169.     } else if (!strcmp (arg, "-gstart")) {
  170.       getarg;
  171.       faketime = parse_time_string (arg);
  172.     } else if (!strcmp (arg, "-location")) {
  173.       getarg;
  174.       strcpy (location, arg);
  175.     } else if (!strcmp (arg, "-text")) {
  176.       if (dataarg ()) {
  177.         getarg;
  178.         getint (text);
  179.       }
  180.     } else if (!strcmp (arg, "-hinc")) {
  181.       if (dataarg ()) {
  182.         getarg;
  183.         getint (hinc);
  184.       }
  185.       if (hinc < 1)
  186.         hincmagic = 1;
  187.     } else if (!strcmp (arg, "-tinc")) {
  188.       if (dataarg ()) {
  189.         getarg;
  190.         getint (tinc);
  191.       }
  192.       if (tinc < 1)
  193.         tinc = 1;
  194.     } else if (!strcmp (arg, "-tz")) {
  195.       getarg;
  196.       tadjust = hhmm2seconds (arg);
  197.       if (!tadjust)
  198.         utc = 1;
  199.     } else if (!strcmp (arg, "-list")) {
  200.       list = 1;
  201.     } else if (!strcmp (arg, "-listtz")) {
  202.       list = 2;
  203.     } else if (!strcmp (arg, "-loctz")) {
  204.       loctz = 1;
  205.     } else if (!strcmp (arg, "-graph")) {
  206.       graphmode = 1;
  207.     } else if (!strcmp (arg, "-gstretch")) {
  208.       double gstretch;
  209.       getarg;
  210.       getfloat (gstretch);
  211.       if (gstretch <= 0.0)
  212.         barf (STRETCHTOOSMALL);
  213.       tstep = (int)(180.0 / gstretch);
  214.       if (tstep <= 0)
  215.         barf (STRETCHTOOBIG);
  216.     } else if (!strcmp (arg, "-utc")) {
  217.       utc = 1;
  218.     } else if (!strcmp (arg, "-mark")) {
  219.       mark = 1;
  220.       getarg;
  221.       getfloat (marklev);
  222.     } else if (!strcmp (arg, "-middle")) {
  223.       middle = 1;
  224.     } else if (!strcmp (arg, "-mllw")) {
  225.       mllw = 1;
  226.     } else if (!strcmp (arg, "-nolines")) {
  227.       lines = 0;
  228.     } else if (!strcmp (arg, "-now")) {
  229.       now = 1;
  230.     } else if (!strcmp (arg, "-calen")) {
  231.       calendar = 1;
  232.     } else if ((!strcmp (arg, "-geometry"))||(!strcmp (arg, "-ppmgeom"))) {
  233.       getarg;
  234.       if (geometry)
  235.         free (geometry);
  236.       geometry = (char *)strdup (arg);
  237.     } else if (!strcmp (arg, "-cur")) {
  238.       curonly = 1;
  239.     } else if (!strcmp (arg, "-toplines")) {
  240.       toplines = 1;
  241.     } else if (!strcmp (arg, "-skinny")) {
  242.       skinny = 1;
  243.     } else if (!strcmp (arg, "-banner")) {
  244.       banner = 1;
  245.     } else if (!strcmp (arg, "-hloff")) {
  246.       getarg;
  247.       getfloat (htleveloff);
  248.     } else if (!strcmp (arg, "-lloff")) {
  249.       getarg;
  250.       getfloat (ltleveloff);
  251.     } else if (!strcmp (arg, "-htoff")) {
  252.       getarg;
  253.       httimeoff = hhmm2seconds (arg);
  254.     } else if (!strcmp (arg, "-ltoff")) {
  255.       getarg;
  256.       lttimeoff = hhmm2seconds (arg);
  257.     } else if (!strcmp (arg, "-bg")) {
  258.       getarg;
  259.       if (bg_color_arg)
  260.         free (bg_color_arg);
  261.       bg_color_arg = (char *)strdup(arg);
  262.     } else if (!strcmp (arg, "-fgrise")) {
  263.       getarg;
  264.       if (fgrise_color_arg)
  265.         free (fgrise_color_arg);
  266.       fgrise_color_arg = (char *)strdup(arg);
  267.     } else if (!strcmp (arg, "-fgfall")) {
  268.       getarg;
  269.       if (fgfall_color_arg)
  270.         free (fgfall_color_arg);
  271.       fgfall_color_arg = (char *)strdup(arg);
  272.     } else if (!strcmp (arg, "-fgtext")) {
  273.       getarg;
  274.       if (fgtext_color_arg)
  275.         free (fgtext_color_arg);
  276.       fgtext_color_arg = (char *)strdup(arg);
  277.     } else if (!strcmp (arg, "-fgmark")) {
  278.       getarg;
  279.       if (fgmark_color_arg)
  280.         free (fgmark_color_arg);
  281.       fgmark_color_arg = (char *)strdup(arg);
  282.     } else if (!strcmp (arg, "-fgmllw")) {
  283.       getarg;
  284.       if (fgmllw_color_arg)
  285.         free (fgmllw_color_arg);
  286.       fgmllw_color_arg = (char *)strdup(arg);
  287.     } else if (!strcmp (arg, "-fgmiddle")) {
  288.       getarg;
  289.       if (fgmiddle_color_arg)
  290.         free (fgmiddle_color_arg);
  291.       fgmiddle_color_arg = (char *)strdup(arg);
  292.     } else if (!strcmp (arg, "-version")) {
  293.       if (PATCHLEVEL)
  294.         printf ("Tide %s.%d\n", VERSION, PATCHLEVEL);
  295.       else
  296.         printf ("Tide %s\n", VERSION);
  297.       exit (0);
  298.     } else {
  299.       fprintf (stderr,
  300. "Usage: tide [-banner]              Sideways ASCII tide graph\n"
  301. "            [-bg color]            Background color\n"
  302. "            [-calen]               Make 1 month tide calendar (slow)\n"
  303. "            [-check YYYY]          Check for errors in equilibrium arguments\n"
  304. "            [-config {filename | -}]  Read configuration file / stdin\n"
  305. "            [-cur]                 Restrict -location search to currents\n");
  306.       fprintf (stderr,
  307. "            [-fgfall color]        Color of ebb current\n"
  308. "            [-fgmark color]        Color of mark line\n"
  309. "            [-fgmiddle color]      Color of middle line\n"
  310. "            [-fgmllw color]        Color of mllw line\n"
  311. "            [-fgrise color]        Color of normal graph / flood current\n"
  312. "            [-fgtext color]        Color of text and lines\n");
  313.       fprintf (stderr,
  314. "            [-geometry NxN]        PPM geometry\n"
  315. "            [-graph]               ASCII graph mode\n"
  316. "            [-gstart YYYY:MM:DD:HH:MM]  Time at which to start\n"
  317. "            [-gstretch N.NN]       Adjust graph detail vs. time span\n"
  318. "            [-hfile ...]           Location and name of harmonics file\n"
  319. "            [-hinc [N]]            Label depth marks in increments of N\n");
  320.       fprintf (stderr,
  321. "            [-hloff [-]N.NN]       (Text)  Tide level offset for high tide\n"
  322. "            [-htoff [-]HH:MM]      (Text)  Time offset for high tide\n"
  323. "            [-list]                List all locations in harmonics file\n"
  324. "            [-listtz]              List with meridians and time zone info\n"
  325. "            [-lloff [-]N.NN]       (Text)  Tide level offset for low tide\n"
  326. "            [-location \"Name\"]     Location to show tides for\n");
  327.       fprintf (stderr,
  328. "            [-loctz]               Try to use time zone of location (Moof!)\n"
  329. "            [-ltoff [-]HH:MM]      (Text)  Time offset for low tide\n"
  330. "            [-mark [-]N.NN]        Draw line at specified tide level\n"
  331. "            [-middle]              Draw line near mean tide level\n"
  332. "            [-mllw]                Draw line at mean lower low water level\n"
  333. "            [-nolines]             Suppress tick marks / depth lines\n");
  334.      fprintf (stderr,
  335. "            [-now]                 Mark current time on graph\n"
  336. "            [-ppm {filename | -}]  Write graph as PPM to file / stdout\n"
  337. "            [-skinny]              Abbreviate timestamps\n"
  338. "            [-text N]              List N tides to standard output\n"
  339. "            [-tinc [N]]            Label each N hours\n"
  340. "            [-toplines]            (PPM)  Depth lines on top of graph\n");
  341.      fprintf (stderr,
  342. "            [-tz [-]HH:MM]         Fixed time zone offset for timestamps\n"
  343. "            [-utc]                 Show timestamps in UTC\n"
  344. "            [-version]             Print Tide version\n");
  345.      fprintf (stderr,
  346.              "\n"
  347. "Time format example -- half past midnight, June 1, 1995:  1995:06:01:00:30\n"
  348. "Colors must be specified as rgb:hh/hh/hh (where hh is a 2-digit hexadecimal\n"
  349. "number)\n");
  350.       exit (0);
  351.     }
  352.   }
  353.  
  354.   /* Remove glaring inconsistencies in args */
  355.   if (!lines)
  356.     hinc = tinc = 0;
  357.   if (banner) {
  358.     graphmode = 0;
  359.     ppm = 0x00;
  360.   }
  361.   if (graphmode && ppm)
  362.     graphmode = 0;
  363.   if (graphmode || ppm) {
  364.     calendar = 0;
  365.     if (!hinc)
  366.       hincmagic = 1;
  367.   }
  368.   if (calendar || graphmode)
  369.     skinny = 1;
  370.   if ((httimeoff || lttimeoff || htleveloff != 0.0 || ltleveloff != 0.0)
  371.       && (graphmode || ppm || banner || mark))
  372.     barf (OFFSETSTEXTONLY);
  373.  
  374.   /* Adjust tstep for ASCII graph mode */
  375.   if (graphmode)
  376.     tstep *= AGTSTEP;
  377.   /* Adjust tstep for banner mode */
  378.   if (banner)
  379.     tstep *= BANTSTEP;
  380.  
  381.   /* Parse geometry for PPM mode (it's needed here because of -now) */
  382.   if (geometry && ppm) {
  383.     if (sscanf (geometry, "%dx%d", &PPMWIDTH, &PPMHEIGHT) != 2)
  384.       barf (BADGEOMETRY);
  385.     if ((PPMWIDTH <= 20)||(PPMHEIGHT <= 20))
  386.       barf (BADGEOMETRY);
  387.     PPMHEIGHT -= 12; /* We take 12 extra, so compensate. */
  388.   }
  389.  
  390.   if (!faketime) {
  391.     faketime = time (0x00);
  392.     /* Center graph around current time if -now */
  393.     if (graphmode && now)
  394.       faketime -= tstep * (TEXTWIDTH>>1);
  395.     else if (ppm && now)
  396.       faketime -= tstep * (PPMWIDTH>>1);
  397.   }
  398.  
  399.   load_data ();
  400.   if (checkyear) {
  401.     check_epoch ();
  402.     exit (0);
  403.   }
  404. fprintf(stdout,"Using time offset of %d from UTC\n",gTimezone/3600);
  405.   if (loctz)
  406.     change_time_zone (tzfile); /* Moof! */
  407.   if (iscurrent) {
  408.     mark = 1;
  409.     marklev = 0.0;
  410.   }
  411.   if (ppm) {
  412.     tide2ppm (ppm);
  413.     exit (0);
  414.   }
  415.   if (banner) {
  416.     do_banner ();
  417.     exit (0);
  418.   }
  419.   if (graphmode) {
  420.     tide2ascii ();
  421.     exit (0);
  422.   }
  423.   if (calendar) {
  424.     do_calendar ();
  425.     exit (0);
  426.   }
  427.     hCurs = GetCursor(watchCursor);
  428.     waitCursor = **hCurs;
  429.     list_tides ();
  430.     SetCursor(&arrow);
  431.     exit (0);
  432. }
  433.