<?php
/**
 * juyo to csv
 * juyo2csv.php
 *
 * @author Hiroaki Sakuma <sakuma@medicalsystems.jp>
 * @version $Id$
 *
 * Copyright (C) @sqm.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of the nor the names of its contributors may be used to
 *   endorse or promote products derived from this software without specific
 *   prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

ini_set('date.timezone''Asia/Tokyo');

$juyoURL 'http://www.tepco.co.jp/forecast/html/images/juyo-j.gif';
$juyoCSVURL 'http://www.tepco.co.jp/forecast/html/images/juyo-j.csv';
$ch curl_init();
curl_setopt($chCURLOPT_URL$juyoURL);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
$juyo curl_exec($ch);
//header('Content-type: image/gif');
//print($juyo);
$im imagecreatefromstring($juyo);
$height imagesy($im);
curl_setopt($chCURLOPT_URL$juyoCSVURL);
$juyocsv explode("\n"curl_exec($ch));
for(
$i 0$i 24$i++) {

  
$f1[$i] = false;
  
$f2[$i] = false;
  
$f3[$i] = false;
  
$output['noblackout'][$i] = null;
  
$output['blackout'][$i] = null;

  for(
$j $height$j 0$j--) {

    
$rgb imagecolorat($im68 22 $i$j);
    
$colors imagecolorsforindex($im$rgb);

    
// No Blackout
    
if ($f1[$i] == false && $f3[$i] == false && $colors[red] == 64 && $colors[green] == 255 && $colors[blue] == 255) {
      
$f1[$i] = true;
    }
    if(
$f1[$i] == true) {
      
$output['noblackout'][$i]++;
    }
    if (
$f1[$i] == true && $colors[red] == 255 && $colors[green] == 255 && $colors[blue] == 191) {
      
$f1[$i] = false;
      
$f3[$i] = true;
    }

    
// Blackout
    
if ($f2[$i] == false && $f3[$i] == false && $colors[red] == 255 && $colors[green] == 128 && $colors[blue] == 0) {
      
$f2[$i] = true;
    }
    if (
$f2[$i] == true) {
      
$output['blackout'][$i]++;
    }
    if (
$f2[$i] == true && $colors[red] == 255 && $colors[green] == 255 && $colors[blue] == 191) {
      
$f2[$i] = false;
      
$f3[$i] = true;
    }

    
// last year
    
if ($colors[red] == && $colors[green] == && $colors[blue] == 255) {
      
$output['lastyear'][$i] = $height $j 64
    }

    
// last day
    
if ($colors[red] == 255 && $colors[green] == && $colors[blue] == 255) {
      
$output['lastday'][$i] = $height $j 64;
    }

    
// Blank
    
if ($f1[$i] == false && $f2[$i] == false && $colors[red] == 255 && $colors[green] == 255 && $colors[blue] == 191) {
      
$f1[$i] = false;
      
$f2[$i] = false;
      
$f3[$i] = true;
      
$zero[$i] = $j;
    }
  } 
// $j

  
if (!is_null($output['noblackout'][$i])) {
    
//$output['noblackout'][$i] = round(($output['noblackout'][$i] + 1) * 1000 / 39, 0);
    
$tmp explode(","$juyocsv[$i 2]);
    
$output['noblackout'][$i] = $tmp[2];
  }

  if (!
is_null($output['blackout'][$i])) {
    
$output['blackout'][$i] = round(($output['blackout'][$i] + 1) * 1000 390);
  }

  if (!
is_null($output['lastyear'][$i])) {
    
$output['lastyear'][$i] = round(($output['lastyear'][$i] + 1) * 1000 390);
  }

  if (!
is_null($output['lastday'][$i])) {
    
//$output['lastday'][$i] = round(($output['lastday'][$i] + 1) * 1000 / 39, 0);
    
$tmp explode(","$juyocsv[$i 2]);
    
$output['lastday'][$i] = $tmp[3];
  }

// $i


if (!empty($_GET['jsonp'])) {

  
header('Content-type: text/javascript');
  print 
"{$_GET['jsonp']}(" json_encode($output) . ");";

} else {

  
header('Content-type: text/csv');
  
header('Content-disposition: attachment; filename="juyo-' date('YmdHis') . '.csv"');
  print 
'""';
  for(
$i 0$i 24$i++) {
    print 
',"' $i '"';
  }
  print 
"\n";

  print 
'"NoBlackout"';
  for(
$i 0$i 24$i++) {
    print 
',"' number_format($output['noblackout'][$i]) . '"';
  }
  print 
"\n";

  print 
'"Blackout"';
  for(
$i 0$i 24$i++) {
    print 
',"' number_format($output['blackout'][$i]) . '"';
  }
  print 
"\n";

  print 
'"LastYear"';
  for(
$i 0$i 24$i++) {
    print 
',"' number_format($output['lastyear'][$i]) . '"';
  }
  print 
"\n";

  print 
'"LastDay"';
  for(
$i 0$i 24$i++) {
    print 
',"' number_format($output['lastday'][$i]) . '"';
  }
  print 
"\n";

}

?>