# Path: rtlsdr_scanner/constants.py
# APP_NAME = 'RTLSDR Scanner'
# 
# Path: rtlsdr_scanner/misc.py
# def format_iso_time(timeStamp):
#     dt = datetime.datetime.utcfromtimestamp(timeStamp)
#     return dt.isoformat() + 'Z'
# 
# Path: rtlsdr_scanner/spectrum.py
# def create_mesh(spectrum, mplTime):
#     total = len(spectrum)
#     width = len(spectrum[min(spectrum)])
#     x = numpy.empty((width, total + 1)) * numpy.nan
#     y = numpy.empty((width, total + 1)) * numpy.nan
#     z = numpy.empty((width, total + 1)) * numpy.nan
# 
#     j = 1
#     for ys in spectrum:
#         time = utc_to_mpl(ys) if mplTime else ys
#         xs, zs = split_spectrum(spectrum[ys])
#         for i in range(len(xs)):
#             x[i, j] = xs[i]
#             y[i, j] = time
#             z[i, j] = zs[i]
#         j += 1
# 
#     x[:, 0] = x[:, 1]
#     if mplTime:
#         y[:, 0] = y[:, 1] - seconds(1)
#     else:
#         y[:, 0] = y[:, 1] - 1
#     z[:, 0] = z[:, 1]
# 
#     return x, y, z
# 
# def sort_spectrum(spectrum):
#     newSpectrum = OrderedDict()
#     for timeStamp in sorted(spectrum):
#         newPoints = OrderedDict()
#         points = sorted(spectrum[timeStamp].items())
#         for point in points:
#             newPoints[point[0]] = point[1]
#         newSpectrum[timeStamp] = newPoints
# 
#     return newSpectrum


import cPickle
import datetime
import glob
import json
import os
import subprocess
import sys
import tempfile
import threading
import uuid
import zipfile
import matplotlib
import wx
from collections import OrderedDict
from PIL import Image
from matplotlib.backends.backend_agg import FigureCanvasAgg
from rtlsdr_scanner.constants import APP_NAME
from rtlsdr_scanner.misc import format_iso_time
from rtlsdr_scanner.spectrum import create_mesh, sort_spectrum
        i += 1

        values = ''
        for scan in sweep[1].items():
            values += '\t\t\t<v>{:.1f}</v>\n'.format(scan[1])
        handle.write(values)

        handle.write('\t\t</data_set>\n')

    handle.write('\t</data_sets>\n')
    handle.write('\t<markers/>\n')
    handle.write('</scan_data_source>\n')


def export_kmz(filename, bounds, image):
    tempPath = tempfile.mkdtemp()

    name = os.path.splitext(os.path.basename(filename))[0]
    filePng = name + '.png'
    fileKml = name + '.kml'

    image.save('{}/{}'.format(tempPath, filePng))

    handle = open('{}/{}'.format(tempPath, fileKml), 'wb')
    handle.write('<?xml version="1.0" encoding="UTF-8"?>\n')
    handle.write('<kml xmlns="http://www.opengis.net/kml/2.2" '
                 'xmlns:gx="http://www.google.com/kml/ext/2.2" '
                 'xmlns:kml="http://www.opengis.net/kml/2.2" '
                 'xmlns:atom="http://www.w3.org/2005/Atom">\n')
    handle.write('<GroundOverlay>\n')

# Path: rtlsdr_scanner/constants.py
# APP_NAME = 'RTLSDR Scanner'
# 
# Path: rtlsdr_scanner/misc.py
# def format_iso_time(timeStamp):
#     dt = datetime.datetime.utcfromtimestamp(timeStamp)
#     return dt.isoformat() + 'Z'
# 
# Path: rtlsdr_scanner/spectrum.py
# def create_mesh(spectrum, mplTime):
#     total = len(spectrum)
#     width = len(spectrum[min(spectrum)])
#     x = numpy.empty((width, total + 1)) * numpy.nan
#     y = numpy.empty((width, total + 1)) * numpy.nan
#     z = numpy.empty((width, total + 1)) * numpy.nan
# 
#     j = 1
#     for ys in spectrum:
#         time = utc_to_mpl(ys) if mplTime else ys
#         xs, zs = split_spectrum(spectrum[ys])
#         for i in range(len(xs)):
#             x[i, j] = xs[i]
#             y[i, j] = time
#             z[i, j] = zs[i]
#         j += 1
# 
#     x[:, 0] = x[:, 1]
#     if mplTime:
#         y[:, 0] = y[:, 1] - seconds(1)
#     else:
#         y[:, 0] = y[:, 1] - 1
#     z[:, 0] = z[:, 1]
# 
#     return x, y, z
# 
# def sort_spectrum(spectrum):
#     newSpectrum = OrderedDict()
#     for timeStamp in sorted(spectrum):
#         newPoints = OrderedDict()
#         points = sorted(spectrum[timeStamp].items())
#         for point in points:
#             newPoints[point[0]] = point[1]
#         newSpectrum[timeStamp] = newPoints
# 
#     return newSpectrum


# Path: rtlsdr_scanner/file.py
import cPickle
import datetime
import glob
import json
import os
import subprocess
import sys
import tempfile
import threading
import uuid
import zipfile
import matplotlib
import wx
from collections import OrderedDict
from PIL import Image
from matplotlib.backends.backend_agg import FigureCanvasAgg
from rtlsdr_scanner.constants import APP_NAME
from rtlsdr_scanner.misc import format_iso_time
from rtlsdr_scanner.spectrum import create_mesh, sort_spectrum

        i += 1

        values = ''
        for scan in sweep[1].items():
            values += '\t\t\t<v>{:.1f}</v>\n'.format(scan[1])
        handle.write(values)

        handle.write('\t\t</data_set>\n')

    handle.write('\t</data_sets>\n')
    handle.write('\t<markers/>\n')
    handle.write('</scan_data_source>\n')


def export_kmz(filename, bounds, image):
    tempPath = tempfile.mkdtemp()

    name = os.path.splitext(os.path.basename(filename))[0]
    filePng = name + '.png'
    fileKml = name + '.kml'

    image.save('{}/{}'.format(tempPath, filePng))

    handle = open('{}/{}'.format(tempPath, fileKml), 'wb')
    handle.write('<?xml version="1.0" encoding="UTF-8"?>\n')
    handle.write('<kml xmlns="http://www.opengis.net/kml/2.2" '
                 'xmlns:gx="http://www.google.com/kml/ext/2.2" '
                 'xmlns:kml="http://www.opengis.net/kml/2.2" '
                 'xmlns:atom="http://www.w3.org/2005/Atom">\n')
    handle.write('<GroundOverlay>\n')

    handle.write('\t<name>{} - {}</name>\n'.format(APP_NAME, name))