array("red" => 0, "green" => 0, "blue" => 0), // Apparently negative-indexed arrays are possible in PHP now. -110 => array("red" => 23, "green" => 0, "blue" => 0), // (weren't available at first writing of this script.) -100 => array("red" => 46, "green" => 0, "blue" => 0), -90 => array("red" => 69, "green" => 0, "blue" => 0), -80 => array("red" => 92, "green" => 0, "blue" => 0), -70 => array("red" => 115, "green" => 0, "blue" => 0), -60 => array("red" => 138, "green" => 0, "blue" => 0), -50 => array("red" => 161, "green" => 0, "blue" => 0), -40 => array("red" => 184, "green" => 0, "blue" => 0), -30 => array("red" => 207, "green" => 0, "blue" => 0), -20 => array("red" => 230, "green" => 0, "blue" => 0), -10 => array("red" => 255, "green" => 0, "blue" => 0) ); $range = min(array_keys($colors)) - max(array_keys($colors)); if (in_array('--low', $argv)) { $g_datadir = "rawdata-low/*.dat"; $URL_margin = 24; $file_prefix = 'low'; $k = array_search('--high', $argv); unset($argv[$k]); } if (in_array('--high', $argv)) { $g_datadir = "rawdata-high/*.dat"; $URL_margin = 5; $file_prefix = 'high'; $k = array_search('--low', $argv); unset($argv[$k]); } /* // As of PHP 4.3.5, I can just glob($g_datadir) $g_filestart = 11287; $g_filestop = 11478; */ $g_datadir_files = glob($g_datadir); $g_numfiles = count($g_datadir_files); // this also serves as the im_height. echo "Number of data files to process: $g_numfiles.\n"; $max_bwslice = 0.0; $min_bwslice = 10000; $y_scale = 2; // y-direction stretch. used in --merge. function ReadFileHeader() { global $fp, $nbands, $nsamples; $comment = fgets($fp); // first line of every file == comment. $nsamples = fgets($fp); // number of samples per band. $nbands = fgets($fp); // this is the number of images to produce and should not change. $step = fgets($fp); // not sure what this number controls. } function ReadDataHeader($band) { global $fp, $data; // $data's the global array holding the samples. $timestamp = fgets($fp); // ignore this, assuming contiguity of sample datafile set. $data[$band]["fstart"] = (float) fgets($fp); $data[$band]["fstop"] = (float) fgets($fp); $dBadjust = fgets($fp); $dummy2 = fgets($fp); $dummy3 = fgets($fp); $dummy4 = fgets($fp); $dummy5 = fgets($fp); } function ReadBandData($band, $filename) { global $fp, $data, $nsamples, $maximum; for ($j = 0; $j < $nsamples; $j++) { $in = (float) fgets($fp); if ($in > $maximum[$band][$j]) $maximum[$band][$j] = $in; $in = (int) ceil($in/10)*10; $data[$band][$filename][] = $in; // Look at this old and madly obfuscated dynamic array assignment! Simplification will lead to great speed improvements. // $data[$filename]["band"][$i]["data"][(string) (($bwslice * $j) + $data[$filename]["band"][$i]["fstart"])] = (float) fgets($fp); } // echo "$filename: band $band read.\n"; } function DetermineScale() { global $data, $nbands, $nsamples, $bwslice, $max_bwslice, $scalefactor; for ($band = 0; $band < $nbands; $band++) { $bandwidth = $data[$band]["fstop"] - $data[$band]["fstart"]; $bwslice[$band] = $bandwidth / ($nsamples - 1); $max_bwslice = max($max_bwslice, $bwslice[$band]); } for ($band = 0; $band < $nbands; $band++) { $scalefactor[$band] = round(($nsamples - 1) * ($bwslice[$band] / $max_bwslice)); } echo "MHz/pixel resolution == $max_bwslice | kHz/pixel resolution == " . $max_bwslice * 1000 . "\n"; } /**************************** Main program loops *****************************/ if (in_array('--all', $argv)) { unset($argv); $argv[] = '--chart'; $argv[] = '--merge'; $argv[] = '--markers'; $argv[] = '--composite'; } if (in_array('--chart', $argv)) { $fp = fopen($g_datadir_files[0], "r"); ReadFileHeader(); for ($i = 0; $i < $nbands; $i++) { $maximum[$i] = array_fill(0, $nsamples, -10000); // array of nsamples, all initially set to -10000. max() compare. } fclose($fp); foreach ($g_datadir_files as $g_filename) { $fp = fopen($g_filename, "r"); ReadFileHeader(); for ($i = 0; $i < $nbands; $i++) { ReadDataHeader($i); ReadBandData($i, $g_filename); } print "$g_filename processed.\t"; fclose($fp); print getmicrotime() - $time_last . " (seconds)\n"; $time_last = getmicrotime(); } for ($band = 0; $band < $nbands; $band++) { // global $chart; // this puppy holds the GD library graphics resources as an array of resource handles. $chart_width = $nsamples - 1; $chart_height = $g_numfiles; echo "-- creating chart -- " . $data[$band]["fstart"] . " -- " . $data[$band]["fstop"] . " MHz image == $chart_width x $chart_height unscaled. "; $chart[$band] = imagecreatetruecolor($chart_width, $chart_height) or die ("Cannot initialize new GD image stream!\n"); foreach ($colors as $k => $v) { $chart_colors[$k] = imagecolorallocate($chart[$band], $v["red"], $v["green"], $v["blue"]); } $yellow = imagecolorallocate($chart[$band], 255, 255, 64); print getmicrotime() - $time_last . " (seconds)\n"; $time_last = getmicrotime(); } for ($band = 0; $band < $nbands; $band++) { echo "-- plotting data -- "; $x_px = 0; $y_px = 0; foreach ($g_datadir_files as $g_filename) { array_pop($data[$band][$g_filename]); // pop the last element off the array 501 x 1 --> 500 x 1. // echo "$band:$g_filename: " . count($data[$band][$g_filename]) . "\n"; foreach($data[$band][$g_filename] as $k) { imagesetpixel($chart[$band], $x_px++, $y_px, $chart_colors[$k]); } $x_px = 0; $y_px++; } $x_px = 0; array_pop($maximum[$band]); foreach($maximum[$band] as $dmax) { imagesetpixel($chart[$band], $x_px++, (int) ($g_numfiles * ($dmax / $range)), $yellow); } ($band < 10) ? $band_pad = "0" . $band : $band_pad = $band; imagepng($chart[$band], "sda$version.$file_prefix.band-$band_pad.png"); print getmicrotime() - $time_last . " (seconds)\n"; $time_last = getmicrotime(); } } if (in_array('--merge', $argv)) { if (@!$data) { $fp = fopen($g_datadir_files[0], "r"); ReadFileHeader(); for ($i = 0; $i < $nbands; $i++) { $maximum[$i] = array_fill(0, $nsamples, -10000); // array of nsamples, all initially set to -10000. max() compare. ReadDataHeader($i); ReadBandData($i, $g_datadir_files[0]); } fclose($fp); } DetermineScale(); $combined = imagecreatetruecolor((int) array_sum($scalefactor), $g_numfiles*$y_scale); $combined_x = 0; for ($band = 0; $band < $nbands; $band++) { ($band < 10) ? $band_pad = "0" . $band : $band_pad = $band; $chart[$band] = imagecreatefrompng("sda$version.$file_prefix.band-$band_pad.png"); list($chart_width, $chart_height) = getimagesize("sda$version.$file_prefix.band-$band_pad.png"); $scaled[$band] = imagecreatetruecolor((int) $scalefactor[$band], $g_numfiles*$y_scale); if ($scaled[$band] && $chart[$band]) { imagecopyresized($scaled[$band], $chart[$band], 0, 0, 0, 0, (int) $scalefactor[$band], $g_numfiles*$y_scale, $chart_width, $chart_height); imagepng($scaled[$band], "sda$version.$file_prefix.scaled-$band_pad.png"); imagecopy($combined, $scaled[$band], $combined_x, 0, 0, 0, (int) $scalefactor[$band], $g_numfiles*$y_scale); $combined_x += (int) $scalefactor[$band]; } print "-- scaling and merging -- "; print getmicrotime() - $time_last . " (seconds)\n"; $time_last = getmicrotime(); } print "-- saving combined spectrum image -- "; imagepng($combined, "sda$version.$file_prefix.combined.png"); print getmicrotime() - $time_last . " (seconds)\n"; $time_last = getmicrotime(); } if (in_array('--markers', $argv)) { print "-- adding dBm scale and freq markers --\n"; $dbm_ref_width = 50; $dbm_ref_height = $g_numfiles * $y_scale; $dbm_ref_colors = count($colors); $dbm_ref = imagecreatetruecolor($dbm_ref_width, $dbm_ref_height); $dbm_ref_scaley = (int) ($dbm_ref_height / $dbm_ref_colors); $dbm_ref_marginr = 4; $dbm_ref_y = 0; $white = imagecolorallocate($dbm_ref, 255, 255, 255); foreach (array_reverse($colors, true) as $k => $v) { $chart_colors[$k] = imagecolorallocate($dbm_ref, $v["red"], $v["green"], $v["blue"]); imagefilledrectangle($dbm_ref, 0, $dbm_ref_y, $dbm_ref_width, $dbm_ref_y + $dbm_ref_scaley, $chart_colors[$k]); $dbm_ref_bbox = imagettfbbox(12, 0, 'Vera.ttf', $k); imagettftext ($dbm_ref, 12, 0, $dbm_ref_width - $dbm_ref_marginr - ($dbm_ref_bbox[2] - $dbm_ref_bbox[0]), $dbm_ref_y + $dbm_ref_scaley - 5, $white, 'Vera.ttf', $k); $dbm_ref_y += $dbm_ref_scaley; } imagepng($dbm_ref, "sda$version.$file_prefix.dbmref.png"); if (@!data) { $fp = fopen($g_datadir_files[0], "r"); ReadFileHeader(); for ($i = 0; $i < $nbands; $i++) { $maximum[$i] = array_fill(0, $nsamples, -10000); // array of nsamples, all initially set to -10000. max() compare. ReadDataHeader($i); ReadBandData($i, $g_datadir_files[0]); } fclose($fp); DetermineScale(); } list($combined_width, $combined_height) = getimagesize("sda$version.$file_prefix.combined.png") or die("ERROR: sda$version.$file_prefix.combined.png is missing. Run 'php sda$version.php --merge'.\n"); $div_line = 1; $freq_ref_height = 40; $freq_ref_width = $combined_width + $dbm_ref_width + $div_line; $freq_ref = imagecreatetruecolor($freq_ref_width, $freq_ref_height); $freq_ref_x = 0; $freq_ref_margin = 3; $white_line = imagecolorallocatealpha($freq_ref, 255, 255, 255, 64); $white = imagecolorallocate($freq_ref, 255, 255, 255); for ($band = 0; $band < $nbands; $band++) { $freq_ref_bbox = imagettfbbox(12, 0, 'Vera.ttf', $data[$band]["fstart"]); imagettftext($freq_ref, 12, 0, $freq_ref_x + $freq_ref_margin, $freq_ref_height - 5, $white, 'Vera.ttf', $data[$band]["fstart"]); imageline($freq_ref, $freq_ref_x, 0, $freq_ref_x, $freq_ref_height, $white_line); $freq_ref_x += $scalefactor[$band]; } imageline($freq_ref, 0, 0, $freq_ref_width, 0, $white_line); imagepng($freq_ref, "sda$version.$file_prefix.freqref.png"); $dbm_legend_bbox = imagettfbbox(12, 0, 'Vera.ttf', 'dBm'); $freq_legend_bbox = imagettfbbox(12, 0, 'Vera.ttf', 'MHz'); $legend_ref = imagecreatetruecolor($dbm_ref_width, $freq_ref_height); $white = imagecolorallocate($legend_ref, 255, 255, 255); imagettftext($legend_ref, 10, 0, 2, 14, $white, 'Vera.ttf', 'dBm'); imagettftext($legend_ref, 10, 0, $dbm_ref_width - 29, $freq_ref_height - 5, $white, 'Vera.ttf', 'MHz'); imagesetthickness($legend_ref, 2); imageline($legend_ref, $dbm_ref_width, 0, 0, $freq_ref_height, $white); imagepng($legend_ref, "sda$version.$file_prefix.legendref.png"); } if (in_array('--composite', $argv)) { if (@!$data) { $fp = fopen($g_datadir_files[0], "r"); ReadFileHeader(); for ($i = 0; $i < $nbands; $i++) { $maximum[$i] = array_fill(0, $nsamples, -10000); // array of nsamples, all initially set to -10000. max() compare. ReadDataHeader($i); ReadBandData($i, $g_datadir_files[0]); } fclose($fp); } DetermineScale(); $combined_ref_x = 0; list( $dbm_ref_width, $dbm_ref_height) = getimagesize("sda$version.$file_prefix.dbmref.png") or die("ERROR: sda$version.$file_prefix.dbmref.png is missing. Run 'php sda$version.php --markers'.\n"); list($combined_width, $combined_height) = getimagesize("sda$version.$file_prefix.combined.png") or die("ERROR: sda$version.$file_prefix.combined.png is missing. Run 'php sda$version.php --merge'.\n"); list($freq_ref_width, $freq_ref_height) = getimagesize("sda$version.$file_prefix.freqref.png") or die("ERROR: sda$version.$file_prefix.freqref.png is missing. Run 'php sda$version.php --markers'.\n"); list($legend_ref_width, $legend_ref_height) = getimagesize("sda$version.$file_prefix.legendref.png") or die ("ERROR: sda$version.$file_prefix.legendref.png is missing. Run 'php sda$version.php --markers.\n"); if (@!$dbm_ref) $dbm_ref = imagecreatefrompng("sda$version.$file_prefix.dbmref.png"); if (@!$combined) $combined = imagecreatefrompng("sda$version.$file_prefix.combined.png"); if (@!$freq_ref) $freq_ref = imagecreatefrompng("sda$version.$file_prefix.freqref.png"); if (@!$legend_ref) $legend_ref = imagecreatefrompng("sda$version.$file_prefix.legendref.png"); $white = imagecolorallocatealpha($combined, 255, 255, 255, 64); // use alpha overlay on spectrum measurement data. for ($band = 0; $band < $nbands; $band++) { imageline($combined, $combined_ref_x, 0, $combined_ref_x, $combined_height, $white); $combined_ref_x += $scalefactor[$band]; } $time_ref_width = 105; $time_ref_height = $dbm_ref_height + $combined_height; $time_scale_height = $combined_height; $time_ref = imagecreatetruecolor($time_ref_width, $time_ref_height); $white = imagecolorallocate($time_ref, 255, 255, 255); $white_line = imagecolorallocatealpha($time_ref, 255, 255, 255, 64); // use alpha overlay on spectrum measurement data. imageline($time_ref, 0, 0, 0, $time_scale_height, $white_line); $skip = (count($g_datadir_files) - 1) / 3; for ($a = 0; $a < 4; $a++) { $fp = fopen($g_datadir_files[(int) ($a * $skip)], "r"); ReadFileHeader(); $timestamp = fgets($fp); $timestamp = substr($timestamp, 28, 11); print "adding timestamp ref -- " . $timestamp . " --\n"; if ($a == 0) imagettftext($time_ref, 12, 0, 5, $time_scale_height * ($a / 3) + 15, $white, 'Vera.ttf', $timestamp); if ($a == 3) imagettftext($time_ref, 12, 0, 5, $time_scale_height * ($a / 3) - 5, $white, 'Vera.ttf', $timestamp); if ($a > 0 && $a < 3) imagettftext($time_ref, 12, 0, 5, ($time_scale_height - 30) * ($a / 3) + 15, $white, 'Vera.ttf', $timestamp); fclose($fp); } imagepng($time_ref, "sda$version.$file_prefix.timeref.png"); $composite_width = $dbm_ref_width + $combined_width + $time_ref_width; $composite_height = $freq_ref_height + $combined_height; $composite = imagecreatetruecolor($composite_width, $composite_height); imagecopy($composite, $dbm_ref, 0, 0, 0, 0, $dbm_ref_width, $dbm_ref_height); imagecopy($composite, $combined, $dbm_ref_width, 0, 0, 0, $combined_width, $combined_height); imagecopy($composite, $legend_ref, 0, $dbm_ref_height, 0, 0, $legend_ref_width, $legend_ref_height); imagecopy($composite, $freq_ref, $dbm_ref_width, $combined_height, 0, 0, $freq_ref_width, $freq_ref_height); imagecopy($composite, $time_ref, $dbm_ref_width + $combined_width, 0, 0, 0, $time_ref_width, $time_ref_height); /* finishing touches -- URL */ $url_color = imagecolorallocate($composite, 255, 190, 33); imagettftext($composite, 12, 0, $composite_width - 265, $composite_height - $URL_margin, $url_color, 'Vera.ttf', 'http://vilimpoc.org/research/sda/'); imagepng($composite, "sda$version.$file_prefix.composite.png"); } print "Processing took " . (getmicrotime() - $time_start) . " seconds.\n"; ?>