# Requirements: rtl_power, heatmap.py, PanoJS, imgcnv, ImageMagick
#	You have to manually edit and set the directory variables at the top of rtlpower2web.sh
# 	A copy of heatmap.py should be in $LOGDIR.

# The visualizations are from keenerd's rtl_power using FFT mode and his heatmap.py
# https://github.com/keenerd/rtl-sdr
# https://raw.githubusercontent.com/keenerd/rtl-sdr-misc/master/heatmap/heatmap.py

# Just an example.
# ./rtl_power -f 86M:1.1G:25k -g 34 -c 28% $LOGDIR/$(date +%Y-%m-%d_%H-%M --utc)_86-1000_25k.csv

# This script automates processing of heatmap.py's ~50,000px wide png into 3600 smaller image
# tiles using imgcnv that PanoJS can load and present with javascript on a static website. 
# http://www.dimin.net/software/panojs/
#
# The assumption is that within th $LOGDIR the most recent csv file is what to process.
# Or you can pass the cvs filepath as an argument. The filename determines the final directory name.
#
# It first calls heatmap.py to make the wide png. Then imgcnv slices that into a few sets of
# zoom levels of png tiles; about 3600 of them. The original and resulting filenames fill a 
# barebones PanoJS html template to generate an index.html. This is deposited in the $OUTPUTDIR
# with png tiles and PanoJS js libs to run it. 
# 
# Then that independent PanoJS implementation dir is transfer to the webserver. The whole 
# process takes about ~30 minutes for a 2.5 GB csv file on my i5-3750 w/16GB ram.

# PanoJS is great. The one change I had to make was  in ./panojs/pyramid_imgcnv.js, line 94,
#    return "" + l + "_" + x + "_" + y + ".jpg";//?"+level;    
# to
#    return "" + l + "_" + x + "_" + y + ".png";//?"+level; 
# Because PanoJS kept trying to use .jpg extension to load tiles and I didn't know how else to
# configure it. imgcnv can not output jpg in this mode. 
#
# imgcnv of BioImageConvert is required for slicing. There is a Debian repository for it at:
# https://bitbucket.org/dimin/bioimageconvert/wiki/Appending%20debian%20repositories 
# To fulfill dependencies I had to install libopenjpeg5 1.5.2 from utopic. 
# http://packages.ubuntu.com/pt/utopic/amd64/libopenjpeg5/download
# identify from ImageMagick is required to get x and y pixel sizes.

(superkuh)-(jobs:0)-(~/tests)
(! 2008)-> ./rtlpower2web.sh 
Copying logfile to temp.

real	0m29.425s
user	0m0.016s
sys	0m4.076s
Running heatmap.py
loading
x: 46779, y: 10081, z: (-47.420000, 22.300000)
drawing
labeling
saving

real	54m28.312s
user	54m23.508s
sys	0m2.336s
Checking if output [/home/superkuh/bob/radio/www/2014-07-18_07-24_86-1100_25k] exists already...
Dir already exists. Deleting old [/home/superkuh/bob/radio/www/2014-07-18_07-24_86-1100_25k].
Slicing images...

real	2m41.295s
user	2m40.650s
sys	0m1.368s
Done, [/home/superkuh/bob/radio/www/2014-07-18_07-24_86-1100_25k] full of tiles.
Copying PanoJS stuff...
Generating HTML index ... [/home/superkuh/bob/radio/www/2014-07-18_07-24_86-1100_25k/index.html]
Copying everything to [/home/superkuh/limbo/www/radio/] ...

real	11m48.486s
user	0m0.152s
sys	0m2.768s
Deleting tmp image [spectrum_2014-07-18_07-24_86-1100_25k.png]: Disabled.
Done

# The 11 minute cp time is because it's ~10,000 files over NFS.