/**********************************************************************
  NOTE TO ALL USERS:

  This file contains 6 main() programs and 12 C functions. 
  Before compiling with a C compiler, this file should be split into
  6 files; each having one main program and two C functions.  The six
  files are separated by equal signs (========== name =============).

 **********************************************************************/

/*============================= GPS_YD.C ===============================*/
#include <stdio.h>
#include <math.h>
#define JAN61980 44244
#define JAN11901 15385
#define SEC_PER_DAY 86400.0
void gps_to_ydhms(long gps_week, double sec_of_week,
              long *year, long *yday, long *hour, long *minute, double *second);
void ydhms_to_gps(long year, long yday, long hour, long minute, double second,
              long *gps_week, double *sec_of_week);
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void main()
{
long gps_week, year, yday, hour, minute;
double sec_of_week, second;

gps_week = 633; sec_of_week = 241000.0;

gps_to_ydhms(gps_week, sec_of_week, &year, &yday, &hour, &minute, &second);
printf("%ld %lf %ld %ld %ld %ld %lf\n",
       gps_week, sec_of_week, year, yday, hour, minute, second);
ydhms_to_gps(year, yday, hour, minute, second, &gps_week, &sec_of_week);
printf("%ld %lf %ld %ld %ld %ld %lf\n",
       gps_week, sec_of_week, year, yday, hour, minute, second);
}
/*------------------------------------------------------------------------
 * gps_to_ydhms                                                          *
 *                                                                       *
 * Input: gps_week, sec_of_week                                          *
 *                                                                       *
 * Output: year, yday, hour, minute, second                              *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void gps_to_ydhms(long gps_week, double sec_of_week,
	      long *year, long *yday, long *hour, long *minute, double *second)
{
long mjd, days_fr_jan1_1901;
double fmjd;
long delta_yrs, num_four_yrs, years_so_far, days_left;

mjd = gps_week*7 + sec_of_week/SEC_PER_DAY + JAN61980;
fmjd = fmod(sec_of_week, SEC_PER_DAY)/SEC_PER_DAY;

days_fr_jan1_1901 = mjd - JAN11901;
num_four_yrs = days_fr_jan1_1901/1461;
years_so_far = 1901 + 4*num_four_yrs;
days_left = days_fr_jan1_1901 - 1461*num_four_yrs;
delta_yrs = days_left/365 - days_left/1460;

*year = years_so_far + delta_yrs;
*yday = days_left - 365*delta_yrs + 1;
*hour = fmjd*24.0;
*minute = fmjd*1440.0 - *hour*60.0;
*second = fmjd*86400.0 - *hour*3600.0 - *minute*60.0;
}
/*------------------------------------------------------------------------
 * ydhms_to_gps                                                          *
 *                                                                       *
 * Input: year, yday, hour, minute, second                               *
 *                                                                       *
 * Output: gps_week, sec_of_week                                         *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void ydhms_to_gps(long year, long yday, long hour, long minute, double second,
                  long *gps_week, double *sec_of_week)
{
long mjd;
double fmjd;

mjd = ((year - 1901)/4)*1461 + ((year - 1901)%4)*365 + yday - 1 + JAN11901;
fmjd = ((second/60.0 + minute)/60.0 + hour)/24.0;

*gps_week = (mjd - JAN61980)/7;
*sec_of_week = ( (mjd - JAN61980) - *gps_week*7 + fmjd )*SEC_PER_DAY;
}
/*============================= GPS_YMD.C ===============================*/
#include <stdio.h>
#include <math.h>

#define JAN61980 44244
#define JAN11901 15385
#define SEC_PER_DAY 86400.0

void gps_to_ymdhms(long gps_week, double sec_of_week, long *year,
      long *month, long *mday, long *hour, long *minute, double *second);
void ymdhms_to_gps(long year, long month, long mday, long hour,
      long minute, double second, long *gps_week, double *sec_of_week);
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void main()
{
long gps_week;
long year, month, mday, hour, minute;
double sec_of_week, second;

gps_week = 842; sec_of_week = 475200.0;

gps_to_ymdhms(gps_week, sec_of_week, &year, &month, &mday, &hour, &minute, &second);
printf("%ld %lf %ld %ld %ld %ld %ld %lf\n",
       gps_week, sec_of_week, year, month, mday, hour, minute, second);

gps_week = 0; sec_of_week = 0.0;
ymdhms_to_gps(year, month, mday, hour, minute, second, &gps_week, &sec_of_week);
printf("%ld %lf %ld %ld %ld %ld %ld %lf\n",
       gps_week, sec_of_week, year, month, mday, hour, minute, second);
}
/*------------------------------------------------------------------------
 * gps_to_ymdhms                                                         *
 *                                                                       *
 * Input: gps_week, sec_of_week                                          *
 *                                                                       *
 * Output: year, month, mday, hour, minute, second                       *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void gps_to_ymdhms(long gps_week, double sec_of_week, long *year,
      long *month, long *mday, long *hour, long *minute, double *second)
{
static long month_day[2][13] = {
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
   {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
};
long leap, guess, more;
long yday, mjd, days_fr_jan1_1901;
long delta_yrs, num_four_yrs, years_so_far, days_left;
double fmjd;

mjd = gps_week*7 + sec_of_week/SEC_PER_DAY + JAN61980;
fmjd = fmod(sec_of_week, SEC_PER_DAY)/SEC_PER_DAY;

printf(" mjd = %ld   fmjd = %16.14lf \n",mjd,fmjd);

days_fr_jan1_1901 = mjd - JAN11901;
num_four_yrs = days_fr_jan1_1901/1461;
years_so_far = 1901 + 4*num_four_yrs;
days_left = days_fr_jan1_1901 - 1461*num_four_yrs;
delta_yrs = days_left/365 - days_left/1460;

*year = years_so_far + delta_yrs;
yday = days_left - 365*delta_yrs + 1;
*hour = fmjd*24.0;
*minute = fmjd*1440.0 - *hour*60.0;
*second = fmjd*86400.0 - *hour*3600.0 - *minute*60.0;

leap = ( *year%4 == 0 );
guess = yday*0.032;
more = (  ( yday - month_day[leap][guess+1] )  > 0  );
*month = guess + more + 1;
*mday = yday - month_day[leap][guess+more];
}
/*------------------------------------------------------------------------
 * ymdhms_to_gps                                                         *
 *                                                                       *
 * Input: year, month, mday, hour, minute, second                        *
 *                                                                       *
 * Output: gps_week, sec_of_week                                         *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void ymdhms_to_gps(long year, long month, long mday, long hour,
      long minute, double second, long *gps_week, double *sec_of_week)
{
static long month_day[2][12] = {
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
   {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
};
long yday, mjd, leap;
double fmjd;

leap = (year%4 == 0);
yday = month_day[leap][month-1] + mday;

mjd = ((year - 1901)/4)*1461 + ((year - 1901)%4)*365 + yday - 1 + JAN11901;
fmjd = ((second/60.0 + minute)/60.0 + hour)/24.0;

*gps_week = (mjd - JAN61980)/7;
*sec_of_week = ( (mjd - JAN61980) - *gps_week*7 + fmjd )*SEC_PER_DAY;
}
/*============================= MJD_GPS.C ===============================*/
#include <stdio.h>
#include <math.h>

#define JAN61980 44244
#define JAN11901 15385
#define SEC_PER_DAY 86400.0

void mjd_to_gps(long mjd, double fmjd, long *gps_week, double *sec_of_week);
void gps_to_mjd(long gps_week, double sec_of_week, long *mjd, double *fmjd);
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void main()
{
long mjd=0, gps_week=0;
double fmjd=0.0, sec_of_week=0.0;

mjd = 48522; fmjd = 0.75;

printf("%ld %lf %ld %lf\n", mjd, fmjd, gps_week, sec_of_week);

mjd_to_gps(mjd, fmjd, &gps_week, &sec_of_week);
printf("%ld %lf %ld %lf\n", mjd, fmjd, gps_week, sec_of_week);

gps_week = 736; sec_of_week = 410000.0;

gps_to_mjd(gps_week, sec_of_week, &mjd, &fmjd);
printf("%ld %lf %ld %lf\n", mjd, fmjd, gps_week, sec_of_week);
}
/*------------------------------------------------------------------------
 * mjd_to_gps                                                            *
 *                                                                       *
 * Input: mjd, fmjd                                                      *
 *                                                                       *
 * Output: gps_week, sec_of_week                                         *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void mjd_to_gps(long mjd, double fmjd, long *gps_week, double *sec_of_week)
{
*gps_week = (mjd - JAN61980)/7;
*sec_of_week = ( (mjd - JAN61980) - *gps_week*7 + fmjd )*SEC_PER_DAY;
}
/*------------------------------------------------------------------------
 * gps_to_mjd                                                            *
 *                                                                       *
 * Input: gps_week, sec_of_week                                          *
 *                                                                       *
 * Output: mjd, fmjd                                                     *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void gps_to_mjd(long gps_week, double sec_of_week, long *mjd, double *fmjd)
{
*mjd = gps_week*7 + sec_of_week/SEC_PER_DAY + JAN61980;
*fmjd = fmod(sec_of_week, SEC_PER_DAY)/SEC_PER_DAY;
}
/*============================= MJD_YD.C ===============================*/
#include <stdio.h>
#include <math.h>

#define JAN61980 44244
#define JAN11901 15385
#define SEC_PER_DAY 86400.0

void mjd_to_ydhms(long mjd, double fmjd, long *year, long *yday,
                  long *hour, long *minute, double *second);
void ydhms_to_mjd(long year, long yday, long hour, long minute,
                  double second, long *mjd, double *fmjd);
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void main()
{
long mjd, year, yday, hour, minute;
double fmjd, second;

mjd = 49400; fmjd = 0.65;
mjd_to_ydhms(mjd, fmjd, &year, &yday, &hour, &minute, &second);
printf("%ld %lf %ld %ld %ld %ld %lf\n",
	    mjd, fmjd, year, yday, hour, minute, second);
ydhms_to_mjd(year, yday, hour, minute, second, &mjd, &fmjd);
printf("%ld %lf %ld %ld %ld %ld %lf\n",
	    mjd, fmjd, year, yday, hour, minute, second);

}
/*------------------------------------------------------------------------
 * mjd_to_ydhms                                                          *
 *                                                                       *
 * Input: mjd, fmjd                                                      *
 *                                                                       *
 * Output: year, yday, hour, minute, second                              *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void mjd_to_ydhms(long mjd, double fmjd, long *year, long *yday,
		  long *hour, long *minute, double *second)
{
long days_fr_jan1_1901, delta_yrs, num_four_yrs, years_so_far, days_left;

days_fr_jan1_1901 = mjd - JAN11901;
num_four_yrs = days_fr_jan1_1901/1461;
years_so_far = 1901 + 4*num_four_yrs;
days_left = days_fr_jan1_1901 - 1461*num_four_yrs;
delta_yrs = days_left/365 - days_left/1460;

*year = years_so_far + delta_yrs;
*yday = days_left - 365*delta_yrs + 1;
*hour = fmjd*24.0;
*minute = fmjd*1440.0 - *hour*60.0;
*second = fmjd*86400.0 - *hour*3600.0 - *minute*60.0;
}
/*------------------------------------------------------------------------
 * ydhms_to_mjd                                                          *
 *                                                                       *
 * Input: year, yday, hour, minute, second                              *
 *                                                                       *
 * Output: mjd, fmjd                                                     *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void ydhms_to_mjd(long year, long yday, long hour, long minute,
		  double second, long *mjd, double *fmjd)
{
*mjd = ((year - 1901)/4)*1461 + ((year - 1901)%4)*365 + yday - 1 + JAN11901;
*fmjd = ((second/60.0 + minute)/60.0 + hour)/24.0;
}
/*============================= MJD_YMD.C ===============================*/
#include <stdio.h>
#include <math.h>

#define JAN61980 44244
#define JAN11901 15385
#define SEC_PER_DAY 86400.0

void mjd_to_ymdhms(long mjd, double fmjd, long *year, long *month,
                   long *mday, long *hour, long *minute, double *second);
void ymdhms_to_mjd(long year, long month, long mday, long hour, long minute,
                  double second, long *mjd, double *fmjd);
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void main()
{
long mjd, year, month, mday, hour, minute;
double fmjd, second;

mjd = 48257; fmjd = 0.65;
mjd_to_ymdhms(mjd, fmjd, &year, &month, &mday, &hour, &minute, &second);
printf("%ld %lf %ld %ld %ld %ld %ld %lf\n",
	    mjd, fmjd, year, month, mday, hour, minute, second);

ymdhms_to_mjd(year, month, mday, hour, minute, second, &mjd, &fmjd);
printf("%ld %lf %ld %ld %ld %ld %ld %lf\n",
            mjd, fmjd, year, month, mday, hour, minute, second);
}
/*------------------------------------------------------------------------
 * mjd_to_ymdhms                                                         *
 *                                                                       *
 * Input: mjd, fmjd                                                      *
 *                                                                       *
 * Output: year, month, mday, hour, minute, second                       *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void mjd_to_ymdhms(long mjd, double fmjd, long *year, long *month,
                   long *mday, long *hour, long *minute, double *second)
{
static long month_day[2][13] = {
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
   {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
};
long days_fr_jan1_1901, delta_yrs, num_four_yrs, years_so_far, days_left;
long yday, leap, guess, more;

days_fr_jan1_1901 = mjd - JAN11901;
num_four_yrs = days_fr_jan1_1901/1461;
years_so_far = 1901 + 4*num_four_yrs;
days_left = days_fr_jan1_1901 - 1461*num_four_yrs;
delta_yrs = days_left/365 - days_left/1460;

*year = years_so_far + delta_yrs;
yday = days_left - 365*delta_yrs + 1;
*hour = fmjd*24.0;
*minute = fmjd*1440.0 - *hour*60.0;
*second = fmjd*86400.0 - *hour*3600.0 - *minute*60.0;
leap = ( *year%4 == 0 );
guess = yday*0.032;
more = (( yday - month_day[leap][guess+1] ) > 0);
*month = guess + more + 1;
*mday = yday - month_day[leap][guess+more];
}
/*------------------------------------------------------------------------
 * ymdhms_to_mjd                                                         *
 *                                                                       *
 * Input: year, month, mday, hour, minute, second                        *
 *                                                                       *
 * Output: mjd, fmjd                                                     *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void ymdhms_to_mjd(long year, long month, long mday, long hour, long minute,
                  double second, long *mjd, double *fmjd)
{
static long month_day[2][12] = {
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
   {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
};
long yday, leap;

leap = (year%4 == 0);
yday = (month_day[leap][month-1] + mday);
*mjd = ((year - 1901)/4)*1461 + ((year - 1901)%4)*365 + yday - 1 + JAN11901;
*fmjd = ((second/60.0 + minute)/60.0 + hour)/24.0;
}
/*============================= YD_MJD.C ===============================*/
#include <stdio.h>
#include <math.h>

void yday_to_mday(long year, long yday, long *month, long *mday);
void mday_to_yday(long year, long month, long mday, long *yday);
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void main()
{
long year, yday, month, mday;

year = 1993; yday = 365;

yday_to_mday(year, yday, &month, &mday);
printf("%ld %ld %ld %ld\n", year, yday, month, mday);

mday_to_yday(year, month, mday, &yday);
printf("%ld %ld %ld %ld\n", year, yday, month, mday);

}
/*------------------------------------------------------------------------
 * yday_to_mday                                                          *
 *                                                                       *
 * Input: year, yday                                                     *
 *                                                                       *
 * Output: month, mday                                                   *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void yday_to_mday(long year, long yday, long *month, long *mday)
{
static long month_day[2][13] = {
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
   {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
};
long leap, guess, more;

leap = (year%4 == 0);
guess = yday*0.032;
more = (( yday - month_day[leap][guess+1] ) > 0);
*month = guess + more + 1;
*mday = yday - month_day[leap][guess+more];
}
/*------------------------------------------------------------------------
 * mday_to_yday                                                          *
 *                                                                       *
 * Input: year, month, mday                                              *
 *                                                                       *
 * Output: yday                                                          *
 *                                                                       *
 * Author: Benjamin W. Remondi                      Date: November 1999  *
 *-----------------------------------------------------------------------*/
void mday_to_yday(long year, long month, long mday, long *yday)
{
static long month_day[2][12] = {
   {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
   {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
};
long leap;
leap = (year%4 == 0);
*yday = month_day[leap][month-1] + mday;
}
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/