I'm trying to use PHP to find the most recent images/loop of a weather model that dynamically updates.
I'm using this script within a looper to show some static images, and the third object in the slideshow/looper is the problematic model loop within an iframe.
Here's the page: http://greenandtheblue.com/weather/thunderstorms.php
It looks like cURL is working some of the time, but for some reason, when there is a new model run, even though the page refreshes, it does not respond and can't "find" the new run to show.
You can see I basically use cURL to detect if an image exists. If it doesn't, it goes back one hour to see if that "model run" exists. It does this until it finds a run that exists and then plugs this time into the URL for the weather model, trying to display it in an iframe.
Here's the php (I'm not good at coding, and you can tell I'm sure):
//begin process to find date format...then run through past 6 hours to get formats for each possible HRRR run in order to find most recent HRRR run available
$hrrr_date = date('Ymd');
$cur_hr = date('H');
$cur_hr = strtotime("now")."
";
$m1_hr = strtotime("-1 hour");
$m2_hr = strtotime("-2 hours");
$m3_hr = strtotime("-3 hours");
$m4_hr = strtotime("-4 hours");
$m5_hr = strtotime("-5 hours");
$m6_hr = strtotime("-6 hours");
$time_cur = date("YmdH",$cur_hr);
$time_m1 = date("YmdH",$m1_hr);
$time_m2 = date("YmdH",$m2_hr);
$time_m3 = date("YmdH",$m3_hr);
$time_m4 = date("YmdH",$m4_hr);
$time_m5 = date("YmdH",$m5_hr);
$hrrr_cur = "rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_cur."/t1/cref_t1sfc_f05.png";
$hrrr_m1 = "rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m1."/t1/cref_t1sfc_f05.png";
$hrrr_m2 = "rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m2."/t1/cref_t1sfc_f05.png";
$hrrr_m3 = "rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m3."/t1/cref_t1sfc_f05.png";
$hrrr_m4 = "rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m4."/t1/cref_t1sfc_f05.png";
$hrrr_m5 = "rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m5."/t1/cref_t1sfc_f05.png";
echo $time_cur;
//use curl to find if various HRRR runs result in 404...or broken link
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_cur."/t1/cref_t1sfc_f05.png");
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_exec($ch);
$is404 = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404;
curl_close($ch);
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL, "http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m1."/t1/cref_t1sfc_f05.png");
curl_setopt($ch2, CURLOPT_NOBODY, 1);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch2, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_exec($ch2);
$is404_2 = curl_getinfo($ch2, CURLINFO_HTTP_CODE) == 404;
curl_close($ch2);
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, "http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m2."/t1/cref_t1sfc_f05.png");
curl_setopt($ch3, CURLOPT_NOBODY, 1);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch3, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_exec($ch3);
$is404_3 = curl_getinfo($ch3, CURLINFO_HTTP_CODE) == 404;
curl_close($ch3);
$ch4 = curl_init();
curl_setopt($ch4, CURLOPT_URL, "http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m3."/t1/cref_t1sfc_f05.png");
curl_setopt($ch4, CURLOPT_NOBODY, 1);
curl_setopt($ch4, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch4, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_exec($ch4);
$is404_4 = curl_getinfo($ch4, CURLINFO_HTTP_CODE) == 404;
curl_close($ch4);
$ch5 = curl_init();
curl_setopt($ch5, CURLOPT_URL, "http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m4."/t1/cref_t1sfc_f05.png");
curl_setopt($ch5, CURLOPT_NOBODY, 1);
curl_setopt($ch5, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch5, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_exec($ch5);
$is404_5 = curl_getinfo($ch5, CURLINFO_HTTP_CODE) == 404;
curl_close($ch5);
if ($is404 == 0) {
$good_time = $time_cur;
echo "latest HRRR run: $good_time"; //current http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_cur."/t1/cref_t1sfc_f05.png = $is404 <br>";
}
else if ($is404_2 == 0) {
$good_time = $time_m1;
echo "latest HRRR run: $good_time"; //http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m1."/t1/cref_t1sfc_f05.png = $is404_2 <br>";
}
else if ($is404_3 == 0) {
$good_time = $time_m2;
echo "latest HRRR run $good_time"; //http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m2."/t1/cref_t1sfc_f05.png = $is404_3 <br>";
}
else if ($is404_4 == 0) {
$good_time = $time_m3;
echo "latest HRRR run $good_time"; //http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m3."/t1/cref_t1sfc_f05.png = $is404_4 <br>";
}
else if ($is404_5 == 0) {
$good_time = $time_m4;
echo "latest HRRR run $good_time"; //http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/".$time_m4."/t1/cref_t1sfc_f05.png = $is404_5 <br>";
}
?>
It's hard to explain, but the script doesn't work or refresh correctly. For instance, this image exists right now: http://rapidrefresh.noaa.gov/HRRR/for_web/hrrr_ncep_jet/2015050417/t1/cref_t1sfc_f05.png
The script should key in on the time of 2015050417 as the most recent valid run time. It then plugs it into this http://rapidrefresh.noaa.gov/HRRR/jsloopLocalDiskDateDomainZipZ.cgi?dsKeys=hrrr_ncep_jet:&runTime=2015050417&plotName=cref_sfc&fcstInc=60&numFcsts=16&model=hrrr&ptitle=HRRR%20Model%20Fields%20-%20Experimental&maxFcstLen=15&fcstStrLen=-1&resizePlot=1&domain=full URL to display in an iframe. This URL exists right now and shows properly in a window itself, but inside the looper as an iframe, it does not display.
Sorry for any confusion in explaining, but I find this problem to be difficult to put into words. If you have any suggestions for streamlining/fixing this script to work more consistently, I'm happy to listen.
Thanks for any help.