/* co_mag_time.f -- translated by f2c (version 20100827).
   You must link the resulting object file with libf2c:
	on Microsoft Windows system, link with libf2c.lib;
	on Linux or Unix systems, link with .../path/to/libf2c.a -lm
	or, if you install libf2c.a in a standard place, with -lf2c -lm
	-- in that order, at the end of the command line, as in
		cc *.o -lf2c -lm
	Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,

		http://www.netlib.org/f2c/libf2c.zip
*/

#include "f2c.h"

/* Table of constant values */

static integer c__1 = 1;
static integer c__4 = 4;
static real c_b17 = 1.f;

/* Main program */ int MAIN__(void)
{
    /* Format strings */
    static char fmt_100[] = "(a,3f12.5)";
    static char fmt_400[] = "(a,3i5)";
    static char fmt_300[] = "(a,4(f10.6),2f10.4)";
    static char fmt_200[] = "(a,f10.5,a,1x,i2.2,\002:\002,i2.2,\002:\002,i2."
	    "2)";

    /* System generated locals */
    real r__1, r__2;

    /* Builtin functions */
    integer s_wsle(cilist *), e_wsle(void), s_wsfe(cilist *), do_fio(integer *
	    , char *, ftnlen), e_wsfe(void), s_rsle(cilist *), do_lio(integer 
	    *, integer *, char *, ftnlen), e_rsle(void);
    /* Subroutine */ int s_stop(char *, ftnlen);

    /* Local variables */
    extern /* Subroutine */ int g_gei_geo_sun_dir__(real *, real *, real *, 
	    real *, real *, real *);
    static integer ih, im;
    static real dr;
    static integer is;
    static real pr, sr, psd, pis2, dphi;
    static integer iday;
    static real dtet, pphi, rlat, sphi, rlon, ptet, rtet, stet;
    extern /* Subroutine */ int t_geo_to_mag__(real *, real *, real *, real *,
	     real *, real *), g_gei_geo_dipole_dir__(real *, real *, real *, 
	    real *, real *, real *), t_car_to_sph__(real *, real *, real *, 
	    real *, real *, real *), t_sph_to_car__(real *, real *, real *, 
	    real *, real *, real *);
    static integer ihmag, immag;
    static real hmagp, dxgei, dygei, dzgei;
    static integer ismag;
    static real dxgeo, dygeo, dzgeo;
    static integer iyear;
    static real pxmag, pymag, pzmag, sxgei, sygei, szgei, pxgeo, pygeo, pzgeo,
	     sxgeo, sygeo, szgeo, sxmag, symag, szmag;
    extern /* Subroutine */ int cp_time_param__(integer *, integer *, integer 
	    *, integer *, integer *, integer *), r_date__(integer *, integer *
	    , integer *), r_time__(integer *, integer *, integer *);
    static integer imonth;

    /* Fortran I/O blocks */
    static cilist io___1 = { 0, 6, 0, 0, 0 };
    static cilist io___2 = { 0, 6, 0, fmt_100, 0 };
    static cilist io___3 = { 0, 6, 0, 0, 0 };
    static cilist io___4 = { 0, 6, 0, fmt_100, 0 };
    static cilist io___5 = { 0, 6, 0, 0, 0 };
    static cilist io___12 = { 0, 6, 0, fmt_100, 0 };
    static cilist io___13 = { 0, 5, 0, 0, 0 };
    static cilist io___16 = { 0, 6, 0, fmt_100, 0 };
    static cilist io___54 = { 0, 6, 0, 0, 0 };
    static cilist io___55 = { 0, 6, 0, fmt_400, 0 };
    static cilist io___56 = { 0, 6, 0, fmt_400, 0 };
    static cilist io___57 = { 0, 6, 0, 0, 0 };
    static cilist io___58 = { 0, 6, 0, fmt_300, 0 };
    static cilist io___59 = { 0, 6, 0, fmt_300, 0 };
    static cilist io___60 = { 0, 6, 0, fmt_300, 0 };
    static cilist io___61 = { 0, 6, 0, fmt_300, 0 };
    static cilist io___62 = { 0, 6, 0, 0, 0 };
    static cilist io___63 = { 0, 6, 0, fmt_200, 0 };
    static cilist io___64 = { 0, 6, 0, 0, 0 };
    static cilist io___65 = { 0, 6, 0, fmt_100, 0 };
    static cilist io___66 = { 0, 6, 0, 0, 0 };



/*     ***************************************************************0** */
/* *   Compute_Magnetic_Time ; use cmagtim module */
/*     P. Robert, Jan 2002 */
/*     ***************************************************************0** */

    s_wsle(&io___1);
    e_wsle();
    s_wsfe(&io___2);
    do_fio(&c__1, "****************************************************", (
	    ftnlen)52);
    e_wsfe();
    s_wsle(&io___3);
    e_wsle();
    s_wsfe(&io___4);
    do_fio(&c__1, "Test of cmagtim:", (ftnlen)16);
    e_wsfe();
    s_wsle(&io___5);
    e_wsle();

    r_date__(&iyear, &imonth, &iday);
    r_time__(&ih, &im, &is);
    cp_time_param__(&iyear, &imonth, &iday, &ih, &im, &is);

    s_wsfe(&io___12);
    do_fio(&c__1, "lat et long en degre?", (ftnlen)21);
    e_wsfe();
    s_rsle(&io___13);
    do_lio(&c__4, &c__1, (char *)&rlat, (ftnlen)sizeof(real));
    do_lio(&c__4, &c__1, (char *)&rlon, (ftnlen)sizeof(real));
    e_rsle();
    s_wsfe(&io___16);
    do_fio(&c__1, "lat, long=", (ftnlen)10);
    do_fio(&c__1, (char *)&rlat, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&rlon, (ftnlen)sizeof(real));
    e_wsfe();

    psd = .017453292777777778f;
    pis2 = 1.57079635f;
    rlat *= psd;
    rlon *= psd;
    rtet = pis2 - rlat;

    g_gei_geo_sun_dir__(&sxgei, &sygei, &szgei, &sxgeo, &sygeo, &szgeo);
    g_gei_geo_dipole_dir__(&dxgei, &dygei, &dzgei, &dxgeo, &dygeo, &dzgeo);

    t_car_to_sph__(&dxgeo, &dygeo, &dzgeo, &dr, &dtet, &dphi);
    t_sph_to_car__(&c_b17, &rtet, &rlon, &pxgeo, &pygeo, &pzgeo);

    t_geo_to_mag__(&pxgeo, &pygeo, &pzgeo, &pxmag, &pymag, &pzmag);
    t_geo_to_mag__(&sxgeo, &sygeo, &szgeo, &sxmag, &symag, &szmag);

    t_car_to_sph__(&pxmag, &pymag, &pzmag, &pr, &ptet, &pphi);
    t_car_to_sph__(&sxmag, &symag, &szmag, &sr, &stet, &sphi);

    hmagp = (sphi - pphi) * 12.f / 3.1415927f + 12.f;
    if (hmagp < 0.f) {
	hmagp += 24.f;
    }

    ihmag = (integer) hmagp;
    immag = (integer) (hmagp * 60.f) - ihmag * 60;
    ismag = (integer) (hmagp * 3600.f) - ihmag * 3600 - immag * 60;

    s_wsle(&io___54);
    e_wsle();
    s_wsfe(&io___55);
    do_fio(&c__1, "date: YY/MM/JJ=", (ftnlen)15);
    do_fio(&c__1, (char *)&iyear, (ftnlen)sizeof(integer));
    do_fio(&c__1, (char *)&imonth, (ftnlen)sizeof(integer));
    do_fio(&c__1, (char *)&iday, (ftnlen)sizeof(integer));
    e_wsfe();
    s_wsfe(&io___56);
    do_fio(&c__1, "time: HH/MM/SS=", (ftnlen)15);
    do_fio(&c__1, (char *)&ih, (ftnlen)sizeof(integer));
    do_fio(&c__1, (char *)&im, (ftnlen)sizeof(integer));
    do_fio(&c__1, (char *)&is, (ftnlen)sizeof(integer));
    e_wsfe();
    s_wsle(&io___57);
    e_wsle();
    s_wsfe(&io___58);
    do_fio(&c__1, "Dipole in GEO:", (ftnlen)14);
    do_fio(&c__1, (char *)&dxgeo, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&dygeo, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&dzgeo, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&dr, (ftnlen)sizeof(real));
    r__1 = dtet / psd;
    do_fio(&c__1, (char *)&r__1, (ftnlen)sizeof(real));
    r__2 = dphi / psd;
    do_fio(&c__1, (char *)&r__2, (ftnlen)sizeof(real));
    e_wsfe();
    s_wsfe(&io___59);
    do_fio(&c__1, "Point  in GEO:", (ftnlen)14);
    do_fio(&c__1, (char *)&pxgeo, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&pygeo, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&pzgeo, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&c_b17, (ftnlen)sizeof(real));
    r__1 = rtet / psd;
    do_fio(&c__1, (char *)&r__1, (ftnlen)sizeof(real));
    r__2 = rlon / psd;
    do_fio(&c__1, (char *)&r__2, (ftnlen)sizeof(real));
    e_wsfe();
    s_wsfe(&io___60);
    do_fio(&c__1, "Point  in MAG:", (ftnlen)14);
    do_fio(&c__1, (char *)&pxmag, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&pymag, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&pzmag, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&pr, (ftnlen)sizeof(real));
    r__1 = ptet / psd;
    do_fio(&c__1, (char *)&r__1, (ftnlen)sizeof(real));
    r__2 = pphi / psd;
    do_fio(&c__1, (char *)&r__2, (ftnlen)sizeof(real));
    e_wsfe();
    s_wsfe(&io___61);
    do_fio(&c__1, "Sun    in MAG:", (ftnlen)14);
    do_fio(&c__1, (char *)&sxmag, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&symag, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&szmag, (ftnlen)sizeof(real));
    do_fio(&c__1, (char *)&sr, (ftnlen)sizeof(real));
    r__1 = stet / psd;
    do_fio(&c__1, (char *)&r__1, (ftnlen)sizeof(real));
    r__2 = sphi / psd;
    do_fio(&c__1, (char *)&r__2, (ftnlen)sizeof(real));
    e_wsfe();
    s_wsle(&io___62);
    e_wsle();
    s_wsfe(&io___63);
    do_fio(&c__1, "Mag. Local Time:", (ftnlen)16);
    do_fio(&c__1, (char *)&hmagp, (ftnlen)sizeof(real));
    do_fio(&c__1, " soit ", (ftnlen)6);
    do_fio(&c__1, (char *)&ihmag, (ftnlen)sizeof(integer));
    do_fio(&c__1, (char *)&immag, (ftnlen)sizeof(integer));
    do_fio(&c__1, (char *)&ismag, (ftnlen)sizeof(integer));
    e_wsfe();
    s_wsle(&io___64);
    e_wsle();
    s_wsfe(&io___65);
    do_fio(&c__1, "****************************************************", (
	    ftnlen)52);
    e_wsfe();
    s_wsle(&io___66);
    e_wsle();

    s_stop("co_mag_time: normal termination", (ftnlen)31);
    return 0;
} /* MAIN__ */

/* Main program alias */ int co_mag_time__ () { MAIN__ (); return 0; }
