Skip to content

Commit

Permalink
Add sunrise sunset time and a pretty tides json to help git comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
Jbithell committed Jun 9, 2024
1 parent d6202dd commit b50b795
Show file tree
Hide file tree
Showing 4 changed files with 63,346 additions and 32 deletions.
63 changes: 32 additions & 31 deletions data/php/dataParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,40 @@



if (date("I",strtotime($date)) == 1) $daylightAdjust = 3600; //Add an hour for BST
if (date("I", strtotime($date)) == 1) $daylightAdjust = 3600; //Add an hour for BST
else $daylightAdjust = 0;

//For some reason, known only to the UKHO - some of them do high/low tide in the wrong order....
if ($array[10] == "X") $tides[strtotime($date . " " . substr_replace($array[7], ":", 2, 0 ) . ":00")+$daylightAdjust] = $array[9];
else $tides[strtotime($date . " " . substr_replace($array[10], ":", 2, 0 ) . ":00")+$daylightAdjust] = $array[12];
if ($array[10] == "X") $tides[strtotime($date . " " . substr_replace($array[7], ":", 2, 0) . ":00") + $daylightAdjust] = $array[9];
else $tides[strtotime($date . " " . substr_replace($array[10], ":", 2, 0) . ":00") + $daylightAdjust] = $array[12];

if (($array[13] != "X" and count($array) == 17) or count($array) == 20) {
if ($array[13] != "X") $tides[strtotime($date . " " . substr_replace($array[13], ":", 2, 0 ) . ":00")+$daylightAdjust] = $array[15]; //Two tides on that day
else $tides[strtotime($date . " " . substr_replace($array[16], ":", 2, 0 ) . ":00")+$daylightAdjust] = $array[18]; //Two tides on that day
if ($array[13] != "X") $tides[strtotime($date . " " . substr_replace($array[13], ":", 2, 0) . ":00") + $daylightAdjust] = $array[15]; //Two tides on that day
else $tides[strtotime($date . " " . substr_replace($array[16], ":", 2, 0) . ":00") + $daylightAdjust] = $array[18]; //Two tides on that day
}
}
}
ksort ($tides); //Put the tides in ascending order
ksort($tides); //Put the tides in ascending order
$tidesDays = [];
foreach ($tides as $time=>$height) {
foreach ($tides as $time => $height) {
if (!isset($tidesDays[date("d M Y", $time)])) $tidesDays[date("d M Y", $time)] = [];

$tidesDays[date("d M Y", $time)][] = ["time" => date("H:i:s", $time), "height" => $height];
}
//Generate timings in the format that the app likes
$tidesApp = [];
foreach ($tidesDays as $time=>$day) {
$tidesApp[] = ["date" => date("Y-m-d", strtotime($time)), "groups" => $day];
foreach ($tidesDays as $time => $day) {
$tidesApp[] = ["date" => date("Y-m-d", strtotime($time)), "groups" => $day, "sunrise" => date_sunrise(strtotime($time), SUNFUNCS_RET_STRING, 52.92, -4.13, ini_get("date.sunrise_zenith"), date("I", strtotime($time))), "sunset" => date_sunset(strtotime($time), SUNFUNCS_RET_STRING, 52.92, -4.13, ini_get("date.sunrise_zenith"), date("I", strtotime($time)))];
}

//Generate tide tables as PDFs that look nice
$tidesMonths = [];
foreach ($tidesDays as $time=>$day) {
foreach ($tidesDays as $time => $day) {
if (!isset($tidesMonths[date("M Y", strtotime($time))])) $tidesMonths[date("M Y", strtotime($time))] = [];
$tidesMonths[date("M Y", strtotime($time))][$time] = $day;
}
$pdfs = [];
foreach ($tidesMonths as $month=>$data) {
foreach ($tidesMonths as $month => $data) {
//Generate a PDF
$pdf = ["name" => date("F Y", strtotime($month)), "date" => date("Y-m-d", strtotime($month)), "filename" => strtolower(date("Y/m", strtotime($month))) . '.pdf', "htmlfilename" => strtolower(date("Y/m", strtotime($month))) . '.html', "url" => strtolower(date("Y/m", strtotime($month)))];

Expand All @@ -92,7 +92,7 @@
<table style="width: 99%; border: none;">
<tr style="width: 99%;">
<td colspan="5" style="width: 50%; border: none;">
<h2>' . date("F",strtotime($month)) . '&nbsp;' . date("Y",strtotime($month)) . '</h2>
<h2>' . date("F", strtotime($month)) . '&nbsp;' . date("Y", strtotime($month)) . '</h2>
</td>
<td colspan="3" style="text-align: right; width: 50%;">
<h2>Porthmadog</h2>
Expand Down Expand Up @@ -140,24 +140,25 @@
</center>
</td>
</tr>';
foreach ($data as $dayDate=>$day) {
$output .= '<tr style="width: 99%; border: 1px solid black;">
foreach ($data as $dayDate => $day) {
$output .= '<tr style="width: 99%; border: 1px solid black;">
<td style="border: 1px solid black; text-align: right;">&nbsp;' . date('l', strtotime($dayDate)) . '&nbsp;</td>
<td style="border: 1px solid black; text-align: left;">&nbsp;' . date('d', strtotime($dayDate)) . '&nbsp;</td>
<td style="border: 1px solid black; text-align: center">' . date_sunrise(strtotime($dayDate), SUNFUNCS_RET_STRING,52.92,-4.13,ini_get("date.sunrise_zenith"),date("I",strtotime($dayDate))) . '</td>
<td style="border: 1px solid black; text-align: center">' . date_sunrise(strtotime($dayDate), SUNFUNCS_RET_STRING, 52.92, -4.13, ini_get("date.sunrise_zenith"), date("I", strtotime($dayDate))) . '</td>
<td style="border: 1px solid black; text-align: center;">&nbsp;' . date('H:i', strtotime($day[0]['time'])) . '&nbsp;</td>
<td style="border: 1px solid black; text-align: center;">&nbsp;' . $day[0]['height'] . 'm &nbsp;</td>';
if (count($day)>1) {
$output .= '<td style="border: 1px solid black; text-align: center;">&nbsp;' . date('H:i', strtotime($day[1]['time'])) . '&nbsp;</td>
if (count($day) > 1) {
$output .= '<td style="border: 1px solid black; text-align: center;">&nbsp;' . date('H:i', strtotime($day[1]['time'])) . '&nbsp;</td>
<td style="border: 1px solid black; text-align: center;">&nbsp;' . $day[1]['height'] . 'm &nbsp;</td>';
} else {
$output .= '<td style="border: 1px solid black; text-align: center;" colspan="2">&nbsp;&nbsp;</td>';
}
$output .= '<td style="border: 1px solid black; text-align:center">' . date_sunset (strtotime($dayDate), SUNFUNCS_RET_STRING,52.92,-4.13,ini_get("date.sunrise_zenith"),date("I",strtotime($dayDate))) . '</td></tr>';
}
} else {
$output .= '<td style="border: 1px solid black; text-align: center;" colspan="2">&nbsp;&nbsp;</td>';
}
$output .= '<td style="border: 1px solid black; text-align:center">' . date_sunset(strtotime($dayDate), SUNFUNCS_RET_STRING, 52.92, -4.13, ini_get("date.sunrise_zenith"), date("I", strtotime($dayDate))) . '</td></tr>';
}
$output .= '</table>';
$footer = '<span style="font-weight: bold;">Copyright Information:</span> All Tidal Data is &copy;Crown Copyright. Reproduced by permission of the Controller of Her Majesty\'s Stationery Office and the UK Hydrographic Office (www.ukho.gov.uk). No tidal data may be reproduced without the expressed permission of the ukho licencing department.<br/><span style="font-weight: bold;">Disclaimer:</span> Tidal Predictions are provided for use by all water users though the providers of this table can not be held accountable for the accuracy of this data or any accidents that result from the use of this data. <span style="font-weight: bold;">Time Zone Information: </span>All Tidal Predictions above are displayed in GMT/BST<br/><div align="right" style="font-style: italic;">Table provided by port-tides.com</div>';
$mpdf=new \Mpdf\Mpdf(['format' => 'A4',
$mpdf = new \Mpdf\Mpdf([
'format' => 'A4',
'margin_left' => 10,
'margin_right' => 10,
'margin_top' => 9,
Expand All @@ -166,13 +167,13 @@
'margin_footer' => 9
]);
$mpdf->SetHTMLFooter($footer);
$mpdf->SetTitle('Porthmadog Tide Times | ' . date("F",strtotime($month)));
$mpdf->SetAuthor ('port-tides.com');
$mpdf->SetTitle('Porthmadog Tide Times | ' . date("F", strtotime($month)));
$mpdf->SetAuthor('port-tides.com');
$mpdf->SetProtection(array('print'), '');
$mpdf->WriteHTML($output);
if (!is_dir(__DIR__ . '/../../static/tide-tables/' . date("Y",strtotime($month)))) mkdir(__DIR__ . '/../../static/tide-tables/' . date("Y",strtotime($month)));
$mpdf->Output(__DIR__ . '/../../static/tide-tables/' . $pdf['filename'],'F');
file_put_contents(__DIR__ . '/../../static/tide-tables/' . $pdf['htmlfilename'],($output.$footer));
if (!is_dir(__DIR__ . '/../../static/tide-tables/' . date("Y", strtotime($month)))) mkdir(__DIR__ . '/../../static/tide-tables/' . date("Y", strtotime($month)));
$mpdf->Output(__DIR__ . '/../../static/tide-tables/' . $pdf['filename'], 'F');
file_put_contents(__DIR__ . '/../../static/tide-tables/' . $pdf['htmlfilename'], ($output . $footer));
echo "Generated PDF for " . $pdf['filename'] . "\n";
$pdfs[] = $pdf;
}
Expand All @@ -190,7 +191,7 @@
$fp = fopen(__DIR__ . '/../tides.json', 'w');
fwrite($fp, json_encode($output));
fclose($fp);

$fp = fopen(__DIR__ . '/../tides-pretty.json', 'w');
fwrite($fp, json_encode($output, JSON_PRETTY_PRINT));
fclose($fp);
echo "Finished";
?>

Loading

0 comments on commit b50b795

Please sign in to comment.