<?php

/*
   ###############################################
   TEST/DEMO FUNCTION:  Loc_Sid_Time()

   This program requires an Internet connection to
   access the NASA/JPL Horizons API.

   AUTHOR   : Jay Tanner - 2024
   LANGUAGE : PHP v7.4.9
   LICENSE  : Public Domain
   ###############################################
*/

// -------------------------------------------------------------
// Define randomized date variables for this ST ephemeris demo.

   
$Year  Random_Int(16002100);
   
$Month substr('JanFebMarAprMayJunJulAugSepOctNovDec'3*Random_Int(0,11), 3);
   
$DayA  Random_Int(116);
   
$DayB  $DayA Random_Int(512);

   
$DayA SPrintF("%02d"$DayA);
   
$DayB SPrintF("%02d"$DayB);

   
$TZHH SPrintF("%02d"Random_Int(011));
   
$TZHH = ((MT_Rand() % <> 0)? '-' '+').$TZHH;

// ------------------------------------------------
// Define date and time variables for ST ephemeris.

   
$TimeScale     'UT';         // 'UT|TT'
   
$TimeZone      "$TZHH:00";

   
$StartDateTime "AD $Year-$Month-$DayA 00:00:00";
   
$StopDateTime  "AD $Year-$Month-$DayB 00:00:00";
   
$StepSize      '1 day';      // 'Minutes|Hours|Days|Months|Years'

   
$LonDeg        '-76.862737'// East = +Positive

   
$DaySumYN      'No';         // 'Yes|No'

// ---------------------
// Define TT scale note.

   
$TTScaleNoteText = (substr(StrToUpper($TimeScale),0,1) == 'T')?
                     
"(Time Zone is ignored when using TT scale)" '';

// ------------------------------------------------------
// Set output angle mode for decimal or sexagesimal mode.

   
$DEGorHMS  'HMS'// 'DEG|HMS'

// -------------------------------------------------------
// Call function to get the local sidereal time CSV table.

   
$RawEphem Loc_Sid_Time ($TimeScale,$StartDateTime,$StopDateTime,
                             
$StepSize,$TimeZone,$DaySumYN,$LonDeg,
                             
$DEGorHMS);

// --------------------------------------------------------
// Generate a client web page to display the returned text.

   
print <<< HTML_WEB_PAGE

<!DOCTYPE HTML>

<!-- Top yellow source code view link. --->
<br>
<table width="420" align="bottom" cellspacing="1" cellpadding="3">
<tr>
<td colspan="1" style="font-size:10pt; color:black; background:white;
                       text-align:left;">
<b><a href='View-Source-Code.php' target='_blank'
     style='font-family:Verdana; color:black; background:yellow;
            text-decoration:none; border:1px solid black; padding:4px;'>
&nbsp;View Source Code&nbsp;</a></b>
</td>
</tr>
</table>

<b><pre style='font-size:11pt;'>
TEST/DEMO FUNCTION:  Loc_Sid_Time()

RawEphem = Loc_Sid_Time (TimeScale,StartDateTime,StopDateTime,StepSize,
                         TimeZone,DaySumYN,LonDeg,DEGorHMS)

This function returns a local sidereal time table ephemeris for a single date
and time or for a range of times at any given convenient intervals.

For Greenwich sidereal time, set:
TimeZone = '+00:00'  and  LonDeg = 0  and  DaySumYN = 'No'


REFRESH page for a new random date/time span selection.

##############################################################################
##############################################################################
Time Scale      =  
$TimeScale
Time Zone       =  UT
$TimeZone   $TTScaleNoteText
Day/Sum Time ?  =  
$DaySumYN
DEG or HMS   ?  =  
$DEGorHMS
Reference Frame =  ICRF

Start Date/Time =  
$StartDateTime
Stop  Date/Time =  
$StopDateTime
Step Size       =  
$StepSize

Longitude       =  
$LonDeg &deg;  (+Positive = East)
Latitude        =  Not needed or used for these computations.

##############################################################################
##############################################################################
$RawEphem
</pre></b>

<!-- For extra scroll space at bottom of page --->
<br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br>

HTML_WEB_PAGE;



/*
   ###########################################################################
   This function returns a table of local sidereal times for a given location
   or for Greenwich.  For Greenwich sidereal time, set the local longitude to
   0 degrees and the Time Zone to [UT+00:00].

   IMPORTANT:
   Make sure that the given location coordinates are actually within the given
   time zone or the computed times may not be accurate.

   To create a sidereal time ephemeris, the body ID used = '10' = The Sun.
   Apparently, we can't do it without some body ID and the sun ephemeris
   goes farther back and farther forward in time than any other body, from
   BC 9999 to AD 9999, which give us a range of almost 20,000 years.

   I'm not sure how accurate the sidereal time is for ancient or far future
   dates, but the ephemeris produces results for such dates.

   ============================================================
   COLUMN   CONTENT
   ------   ---------------------------------------------------
     01     Date and Time
     02     Sp (Solar Presence Symbol)
     03     Lp (Lunar Presence Symbol)
     04     Local/Greenwich Apparent Sidereal Time (DEG or HMS)

   ###########################################################################
*/

   
function Loc_Sid_Time ($TimeScale,$StartDateTime,$StopDateTime,$StepSize,
                          
$TimeZone,$DaySumYN,$LonDeg,$DEGorHMS)
{
// ---------------
// Read arguments.

   
$Command    '10';
   
$TimeScale  = (StrToUpper(substr(trim($TimeScale),0,1)) == 'T')? 'TT':'UT';
   
$DEGorHMS   = (StrToUpper(substr(trim($DEGorHMS),0,1)) == 'D')? 'DEG':'HMS';

/* -----------------------------------------------------------
   Adjust for Daylight/Summer Time, if indicated. This assumes
   that the Time Zone string is given in the standard +-HH:mm
   format or an error may occur.
*/
   
$DaySumYN  substr(StrToUpper(trim($DaySumYN)),0,1);
   
$DSSTAdj = ($DaySumYN == 'N')? 0:1;
   list(
$TZHH$TZmm) = PReg_Split("[\:]"$TimeZone);
   
$TZSign substr($TZHH,0,1);
   
$TZHours = (($TZSign == '-')? -1:1)*(abs($TZHH) + $TZmm/60) + $DSSTAdj;
   
$i StrPos($TZHours'.');  if ($i == FALSE) {$TZHours .= '.00';}
   
$i StrPos($TZHours'.');
   
$TZHH $TZSign.SPrintF("%02d"abs(substr($TZHours,0,$i)));
   
$TimeZone "$TZHH:$TZmm";

// --------------------------------------------------
// Construct query URL for the NASA/JPL Horizons API.

   
$From_Horizons_API =
   
"https://ssd.jpl.nasa.gov/api/horizons.api?format=text" .
   
"&COMMAND='$Command'"                     .
   
"&OBJ_DATA='NO'"                          .
   
"&MAKE_EPHEM='YES'"                       .
   
"&EPHEM_TYPE='OBSERVER'"                  .
   
"&CAL_FORMAT='CAL'"                       .
   
"&CAL_TYPE='GREGORIAN'"                   .
   
"&REF_SYSTEM='ICRF'"                      .
   
"&ANG_FORMAT='$DEGorHMS'"                 .
   
"&CENTER='COORD@399'"                     .
   
"&COORD_TYPE='GEODETIC'"                  .
   
"&SITE_COORD='$LonDeg,0,0'"               .
   
"&TIME_DIGITS='SECONDS'"                  .
   
"&TIME_ZONE='$TimeZone'"                  .
   
"&START_TIME='$StartDateTime $TimeScale'" .
   
"&STOP_TIME='$StopDateTime'"              .
   
"&STEP_SIZE='$StepSize'"                  .
   
"&EXTRA_PREC='YES'"                       .
   
"&QUANTITIES='7'"                         .
   
"&CSV_FORMAT='YES'"                       ;
// ===========================================


/* ----------------------------------------------------------------------
// Send query to Horizons API to obtain the apparent sidereal time table.
*/
   
$ST =  Str_Replace(",\n"" \n"File_Get_Contents($From_Horizons_API));

// -----------------------------------------------------------
// Return an empty string ('') if no ephemeris table is found.

   
if (StrPos($ST'$$SOE') === FALSE) {return HTMLEntities($ST);}

// -----------------------------------
// Otherwise, return the raw ST table.

   
return HTMLEntities($ST);

// End of  Loc_Sid_Time(...)



?>