Access data files & convert to csv

John - Below is a PHP program to grab data from a phpfina file and send it to a csv file.

You’ll need to change these two lines to meet your needs:

$dir = "/home/pi/data/phpfina/";
$outputDir = "/home/pi/data/";

to run it, type in the command with the feed number:

$ php export_phpfina_feed_to_csv_v4.php nnn

FYI - a 2MB phpfina .dat file will become a 16MB csv file.

Download the csv file via putty and then open with MS Excel.
 

#!/usr/bin/php
<?php
$time_start = microtime(true);

// Directory of phpfina feeds, see: settings.php
$dir = "/home/pi/data/phpfina/";	// must end with '/'
$outputDir = "/home/pi/data/";		// must end with '/'
;
// Feed id to read:
if ($argc > 1) {
	$feedid = $argv[1];
} else {
  echo "no argument passed.  Need feed number\n";
  exit;
}
 
//----------------------------------------------------

// read meta data
$meta = new stdClass();
$metafile = fopen($dir.$feedid.".meta", 'rb');
fseek($metafile,8);
$tmp = unpack("I",fread($metafile,4)); 
$meta->interval = $tmp[1];
$tmp = unpack("I",fread($metafile,4)); 
$meta->start_time = $tmp[1];
fclose($metafile);

$fh = fopen($dir."$feedid.dat", 'rb');
$filesize = filesize($dir."$feedid.dat");

print "meta: " .$dir.$feedid.".meta\n";
print "feedDB: " .$dir.$feedid. ".dat\n\n";

$npoints = floor($filesize / 4.0);

$csv_file = $outputDir."feed_" .$feedid. ".csv";
$fp = fopen($csv_file, 'w');
    
    for ($i=0; $i<$npoints; $i++)
    {
        $val = unpack("f",fread($fh,4));
        $time = $meta->start_time + $i * $meta->interval;
        $value = $val[1];
        $array = array($time, $value); 
        if (!is_nan($value))
        {
            fputcsv($fp, $array);
            //print $time."\t".$value."\n";
        }
        
        //print $time."\t".$value."\n";
    }
fclose($fh);
fclose($fp);

print "csv_file: " .$csv_file."\n";
print "filesize: " .$filesize."\n";
print "npoints: " .$npoints."\n\n";

$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Ran in $time seconds\n";

?>

PS - I believe the main part of the above PHP script came from Trystan Lea (but I could be wrong). Sorry I can’t properly credit this!