dockapps/wmsun/wmSun.c

377 lines
8.3 KiB
C
Raw Normal View History

/*
*
* wmSun-1.03 (C) 1999 Mike Henderson (mghenderson@lanl.gov)
2015-06-14 22:04:43 +00:00
*
* - Shows Sun Rise/Set Times....
2015-06-14 22:04:43 +00:00
*
*
*
*
*
* 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, 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 (see the file COPYING); if not, write to the
2015-06-14 22:04:43 +00:00
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA
*
* Things TODO:
2015-06-14 22:04:43 +00:00
* - clean up code!
* - support for 8-bit displays.
* - more detailed documentation.
* - eclipses?
* - add buttons to play will date and lat lon...
* Could be something like this;
* First click brings up buttons to change date.
* Second click brings up buttons to change lat/lon.
2015-06-14 22:04:43 +00:00
* Third goes back to display
* Set time delay to go back to display if user doesnt do it...
*
*
2015-06-14 22:04:43 +00:00
*
*
*
* Changes:
*
* Version 1.03 - released February 4, 1999.
* cosmetic for AfterStep users. removed spurious black line at RHS edge an mask.
*
*
* Version 1.02 - released January 12, 1999.
* Added support for User-specified date and Time difference so that
* you can have the display be correct in local time even for remote
* lat/lons. (I am in Hawaii right now. I dont want to reset the time on
* my laptop, so with these new options I can still get the correct
* local times of rise/set. Sunset is awesome here in Kona!!! And the calcs
* seem to be quite good -- it's a good test here because the Sun sets over
* the Pacific (no mountains are in the way)).
*
* Version 1.01 - released January 6, 1999.
* Fixed stupid bug in Date change montior.
*
* Version 1.0 - released January 5, 1999.
*
*
*/
2015-06-14 22:04:43 +00:00
/*
* Includes
*/
#include <stdio.h>
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <X11/X.h>
#include <X11/xpm.h>
#include "wmgeneral/wmgeneral.h"
#include "wmSun_master.xpm"
#include "wmSun_mask.xbm"
2015-06-14 22:04:43 +00:00
/*
* Delay between refreshes (in microseconds)
*/
#define DELAY 10000L
#define WMSUN_VERSION "1.03"
#define DegPerRad 57.29577951308232087680
#define RadPerDeg 0.01745329251994329576
void ParseCMDLine(int argc, char *argv[]);
void pressEvent(XButtonEvent *xev);
2015-06-14 22:04:40 +00:00
void SunRise(int year, int month, int day, double LocalHour, double *UTRise,
double *UTSet);
int ToggleWindow = 0;
int nMAX = 1;
2015-06-14 22:04:43 +00:00
int Flag = 1;
int UseUserTimeDiff = 0;
int UseUserDate = 0;
long UserDate;
double Glat, Glon, SinGlat, CosGlat, TimeZone, UserTimeDiff;
int xDigit[11] = {8, 18, 27, 37, 46, 55, 64, 74, 83, 92, 102};
2015-06-14 22:04:43 +00:00
/*
* main
*/
int main(int argc, char *argv[]) {
struct tm *GMTTime, *LocalTime;
XEvent event;
2015-06-14 22:04:39 +00:00
int n;
int Year, Month, OldLocalDayOfMonth;
int LocalDayOfMonth, DayOfMonth;
long CurrentLocalTime, CurrentGMTTime, date;
2015-06-14 22:04:39 +00:00
double UT, val, LTRise, LTSet, LocalHour, hour24();
int H, M;
2015-06-14 22:04:43 +00:00
/*
* Parse any command line arguments.
*/
Glat = Glon = 0.0;
ParseCMDLine(argc, argv);
Glat *= RadPerDeg; SinGlat = sin( Glat ); CosGlat = cos( Glat );
2015-06-14 22:04:43 +00:00
openXwindow(argc, argv, wmSun_master, (char *)wmSun_mask_bits, wmSun_mask_width, wmSun_mask_height);
2015-06-14 22:04:43 +00:00
/*
* Loop until we die
*/
n = 32000;
OldLocalDayOfMonth = -999;
while(1) {
if (Flag) {
n = 32000;
Flag = 0;
}
/*
* The Moon Ephemeris calculations are somewhat costly (the Moon is one of the most
* difficult objects to compute position for). So only process every nMAXth cycle of this
* loop. We run outer loop it faster to catch expose events, button presses, etc...
*
*/
if (n>nMAX){
n = 0;
nMAX = 1000;
2015-06-14 22:04:43 +00:00
CurrentGMTTime = time(CurrentTime); GMTTime = gmtime(&CurrentGMTTime);
DayOfMonth = GMTTime->tm_mday;
UT = GMTTime->tm_hour + GMTTime->tm_min/60.0 + GMTTime->tm_sec/3600.0;
Year = GMTTime->tm_year+1900;
Month = GMTTime->tm_mon+1;
CurrentLocalTime = CurrentGMTTime; LocalTime = localtime(&CurrentLocalTime);
LocalDayOfMonth = LocalTime->tm_mday;
if ((OldLocalDayOfMonth != LocalDayOfMonth)||(Flag)){
Flag = 0;
if (UseUserDate){
date = UserDate;
Year = date/10000;
date -= Year*10000;
Month = date/100;
date -= Month*100;
DayOfMonth = date;
date = UserDate;
} else {
date = Year*10000 + Month*100 + DayOfMonth;
}
LocalHour = LocalTime->tm_hour + LocalTime->tm_min/60.0 + LocalTime->tm_sec/3600.0;
TimeZone = (UseUserTimeDiff) ? UserTimeDiff : UT - LocalHour;
/*
* Clear Plotting area
*/
copyXPMArea(65, 5, 54, 54, 5, 5);
/*
* Compute Sun Rise/Set Times in Local Time
*/
SunRise(Year, Month, DayOfMonth, LocalHour, &LTRise, &LTSet);
if (LTRise > 0.0){
val = LTRise;
H = (int)val; val = (val-H)*60.0;
M = (int)val;
copyXPMArea(xDigit[H/10], 73, 7, 9, 17, 13);
copyXPMArea(xDigit[H%10], 73, 7, 9, 17+7, 13);
copyXPMArea(xDigit[10], 75, 3, 6, 17+15, 15);
copyXPMArea(xDigit[M/10], 73, 7, 9, 17+19, 13);
copyXPMArea(xDigit[M%10], 73, 7, 9, 17+26, 13);
} else {
copyXPMArea(10, 84, 28, 7, 19, 15);
}
2015-06-14 22:04:43 +00:00
if (LTSet > 0.0){
val = LTSet;
H = (int)val; val = (val-H)*60.0;
M = (int)val;
copyXPMArea(xDigit[H/10], 73, 7, 9, 17, 40);
copyXPMArea(xDigit[H%10], 73, 7, 9, 17+7, 40);
copyXPMArea(xDigit[10], 75, 3, 6, 17+15, 42);
copyXPMArea(xDigit[M/10], 73, 7, 9, 17+19, 40);
copyXPMArea(xDigit[M%10], 73, 7, 9, 17+26, 40);
} else {
copyXPMArea(10, 84, 28, 7, 19, 40);
}
}
OldLocalDayOfMonth = LocalDayOfMonth;
} else {
/*
2015-06-14 22:04:43 +00:00
* Update the counter.
*/
++n;
}
2015-06-14 22:04:43 +00:00
/*
* Process any pending X events.
*/
while(XPending(display)){
XNextEvent(display, &event);
switch(event.type){
case Expose:
RedrawWindow();
break;
case ButtonPress:
pressEvent(&event.xbutton);
break;
case ButtonRelease:
break;
}
}
2015-06-14 22:04:43 +00:00
/*
* Redraw and wait for next update
*/
RedrawWindow();
usleep(DELAY);
}
}
2015-06-14 22:04:43 +00:00
/*
* ParseCMDLine()
*/
void ParseCMDLine(int argc, char *argv[]) {
int i;
2015-06-14 22:04:43 +00:00
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-display")){
++i;
} else if (!strcmp(argv[i], "-lat")){
Glat = atof(argv[++i]);
} else if (!strcmp(argv[i], "-lon")){
Glon = atof(argv[++i]);
} else if (!strcmp(argv[i], "-td")){
UseUserTimeDiff = 1;
UserTimeDiff = atof(argv[++i]);
} else if (!strcmp(argv[i], "-date")){
UseUserDate = 1;
UserDate = atoi(argv[++i]);
} else {
printf("\nwmSun version: %s\n", WMSUN_VERSION);
printf("\nusage: wmSun [-display <Display>] [-lat <Latitude>] [-lon <Longitude>] [-h]\n\n");
printf("\t-display <Display>\tUse alternate X display.\n");
printf("\t-lat <Latitude>\t\tObservers Latitude. Positive to the west.\n");
printf("\t-lon <Longitude>\tObservers Longitude.\n");
printf("\t-td <Delta Time>\tUser defined difference between UT an LT (hours).\n");
printf("\t-h\t\t\tDisplay help screen.\n\n");
exit(1);
}
}
}
/*
* This routine handles button presses. Clicking in the window
* toggles the display.
*
*/
void pressEvent(XButtonEvent *xev){
(void) xev;
++ToggleWindow;
if (ToggleWindow > 4) ToggleWindow = 0;
Flag = 1;
return;
}