url = "http://ssd.jpl.nasa.gov/horizons.cgi"; $this->curl = curl_init($this->url); $this->cookiefile = "/tmp/horizon_cookie"; $this->verbose = false; /* try to delete cruft from prior session */ unlink ( $this->cookiefile ); curl_setopt($this->curl, CURLOPT_COOKIEJAR, $this->cookiefile ); curl_setopt($this->curl, CURLOPT_HEADER, 1); curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->curl, CURLOPT_POST, 1); } public function cleanup () { curl_close($this->curl); unlink ( $this->cookiefile ); shell_exec ( "rm -f " . $this->cookiefile ); } function ck_error ( $html ) { if ( strpos ( $html, "
") != 0 ) return true; else return false; } public function show_settings () { $p1 = strpos($this->html, "

Current Settings"); $p2 = strpos($this->html, "
", $p1); $set = substr($this->html, $p1, $p2-$p1) . "\n"; echo ( $set ); } /* First call, set search to true */ public function set_target ( $thing, $search ) { if ( $search ) { $postString = "body_group=all&find_body=Search&sstr=" . $thing; } else { $postString = "body=" . $thing . "&select_body=Select Indicated body"; } curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postString); $this->html = curl_exec($this->curl); /* One of three things can happen: * 1) We get some kind of error. * 2) The object we ask for is unambiguous, and we are good to go. * 3) We are offered a choice of several targets. */ if ( $this->ck_error ( $this->html ) ) { $this->cleanup (); return "That object is unknown\n"; } else if(strpos($this->html, "

Target Body

") != 0) { $this->cleanup (); /* end up here if Horizons handed back a menu/choice */ $tmpBegin = strpos($this->html, "", $tmpBegin); return substr($this->html, $tmpBegin, $tmpEnd-$tmpBegin); } else { /* Keep curl session open */ echo "target selected: " . $thing . "\n"; return false; } } public function get_info ( $start_time, $stop_time ) { /* *** 1 *** * First form post: Set the Observer location to Mount Hopkins. * (Horizons knows this as 110:53:02.4 31:41:19.7) */ $postString = "l_str=mount_hopkins&s_lookup=Search"; curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postString); $this->html = curl_exec($this->curl); if ( $this->verbose ) $this->show_settings (); if ( $this->ck_error ( $this->html ) ) { return false; } if ( $this->verbose ) echo ( "Phase 1\n" ); /* *** 3 *** / /* Third form post: Set the output format. */ $oq_1 = "1"; $oq_4 = "1"; $extra_prec = "YES"; $time_fmt = "JD"; $ang_format = "DEG"; $set_table_settings = "1"; $obj_data = "YES"; $csv_format = "YES"; $postString = "oq_1=" . $oq_1 . "&oq_4=" . $oq_4 . "&extra_prec=" . $extra_prec . "&time_fmt=" . $time_fmt . "&ang_format=" . $ang_format . "&set_table_settings=" . $set_table_settings . "&obj_data=" . $obj_data . "&csv_format=" . $csv_format; curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postString); $this->html = curl_exec($this->curl); if ( $this->verbose ) $this->show_settings (); if ( $this->ck_error ( $this->html ) ) { return false; } if ( $this->verbose ) echo ( "Phase 2\n" ); /* *** 4 *** / /* Fourth form post: Set the Time Span. */ $step_size = "1"; $interval_mode = "m"; $set_time_span = "Use%20Specified%20Times"; $postString = "start_time=" . $start_time . "&stop_time=" . $stop_time . "&step_size=" . $step_size . "&interval_mode=" . $interval_mode . "&set_time_span=" . $set_time_span; curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postString); $this->html = curl_exec($this->curl); if ( $this->verbose ) $this->show_settings (); if ( $this->ck_error ( $this->html ) ) { return false; } if ( $this->verbose ) echo ( "Phase 3\n" ); /* *** 5 *** / /* Fifth form post: generate the Ephemeris. */ /* (hit the "Generate" button). */ $postString = "go=Generate%20Ephemeris"; curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postString); $this->html = curl_exec($this->curl); if ( $this->verbose ) $this->show_settings (); if ( $this->ck_error ( $this->html ) ) { return false; } $this->cleanup (); return $this->extract_info (); //return $this->html; } function extract_info () { $start = strpos($this->html, "
**");
	$end = strpos($this->html, "\$\$EOE");
	return substr($this->html, $start, $end-$start+5) . "\n";
    }

    function extract_header ( $info ) {
	/* extract header for display */
	$start = strpos($info, "
**");
	$end = strpos($info, "\$\$SOE");
	return substr($info, $start, $end-$start-1);
    }

    function extract_table ( $info ) {
	/* extract the table */
	$start = strpos($info, "\$\$SOE");
	$end = strpos($info, "\$\$EOE");
	return substr($info, $start, $end-$start+6);
    }

    function parseTable ( $table ) {
	$rv = array();
	$lines = split("\n", $table);

	$i = 0;
	foreach($lines as $line) {
		$words = split(",", $line);		
		if(count($words) > 3) {
			$entry = new Entry($words[0], $words[3], $words[4], $words[5], $words[6]);
			$rv[$i++] = $entry;
		}
	}

	return rv;
    }

    function misc ( $ephemeris ) {
	if ( count($ephemeris) < 1 ) {
	    return;
	}

	/* First line in table */
	$first = $ephemeris[0];

	/* XXX - really need to check against current time */
	if ( $first->alt < 6.0 ) {
	    $below_horizon = true;
	    $status = "Ephemeris generated. (starts below horizon)";
	} else {
	    $below_horizon = false;
	    $status = "Ephemeris generated. Ready to send to the telescope.";
	}
    }

}

/* ------------------------------------------------------ */
/* ------------------------------------------------------ */

if ( $argc == 2 ) {
    $target = $argv[1];
} else {
    $target = "Europa";
    $target = "Mars";
    $target = "MB:499";
    $target = "MB:52";	// fails
    $target = "Europa";
    $target = "MB:502";
    $target = "pallas";
}

// Just doesn't work via web interface.
//$target = "g:62.2,84.4,0@301";
//$search = true;

if ( strpos ( $target, ":" ) ) {
    $search = false;
    echo "Looking for " . $target . "\n";
} else {
    $search = true;
    echo "Searching for " . $target . "\n";
}

/* set defaults for first time through script,
 * the +7 hours takes us from MST to UT
 */
$start_time = date("Y-m-d H:i", strtotime("now +7 hours"));
$stop_time = date("Y-m-d H:i", strtotime("+12 hours +7 hours"));

$start_time = "2009-03-31 19:00";
$stop_time = "2009-03-31 21:00";

echo "Start: " . $start_time . "\n";
echo "Stop: " . $stop_time . "\n";

$net = new horizons ();

$msg =  $net->set_target ( $target, $search );
//$net->show_settings ();
echo ( "Target Set!\n" );

if ( $msg ) {
	echo $msg;
	//exit ();
}

$info = $net->get_info ( $start_time, $stop_time );

if ( ! $info ) {
	$net->cleanup ();
	echo ( "Sorry\n" );
	exit ();
}

echo ( $info );

//echo ( "Got it!\n" );

//$ephemeris = $net->parseTable($table);
	
?>