#!/usr/bin/python

## Copyright 2013-2015 A Mennucc1
##
##  This file is part of wfrog
##  It converts CSV recordings of meteo data to Sqlite3
##
##
##  wfrog is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.


import csv, time, string, os, sys, sqlite3
import os.path
from datetime import datetime
from distutils import util

if len(sys.argv) != 3:
	print """
Usage: csv2sql input.csv output.sql

This program reads CSV recordings of meteo data, and adds them to a Sqlite3
database. The database must be initialized to contain the table 'METEO',
see database/db-sqlite3.sql in the source code or under /usr/lib/wfrog .
Note that the SQL table may contain more fields per record than the CSV;
this script will selfadapt.
"""
	sys.exit(0)


### read schema from CSV (if any)
columns = [ 'timestamp', 'localtime', 'temp', 'hum', 'wind', 'wind_dir', 'wind_gust', 'wind_gust_dir', 'dew_point', 'rain', 'rain_rate', 'pressure', 'uv_index' ]

skip_first=False
c = [z.lower() for z in csv.reader(open(sys.argv[1])).next()]
if 'timestamp' in c or 'localtime' in c or 'temp' in c:
    skip_first=True
    sys.stderr.write("Using first line of CSV as input schema.\n")
    if c != columns:
	if c[0] != 'timestamp' or  c[1] != 'localtime': 
	    sys.stdout.write("First line of CSV is too different from the standard. This is unsupported.\n")
	    sys.exit(1)
	else:
	    sys.stdout.write("First line of CSV is somewhat different than the standard. This is poorly supported.\n")
	    sys.stdout.write('Do you want to stop here ? ')
	    if util.strtobool(raw_input()):
		sys.exit(1)
	    columns = c
    


def _get_table_fields(db, tablename='METEO'):
        sql = "PRAGMA table_info(%s);" % tablename
        fields = []

        c=db.cursor()
        c.execute(sql)
        for row in c:
            fields.append(row[1].lower())

        return fields

reader = csv.reader(open(sys.argv[1]))
if skip_first:
    reader.next()
writer = sqlite3.connect(sys.argv[2],  detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)

table_fields = _get_table_fields(writer)
# Verify Mandatory fields
assert 'timestamp_utc'   == table_fields[0]
assert 'timestamp_local' == table_fields[1]

inserter='?,'*len(table_fields)
inserter="insert into METEO  values ( %s ) ;" % inserter[:-1]

themap=[]
for i in range(len(table_fields)):
    try:
        j=columns.index(table_fields[i])
        themap.append(j)
    except ValueError:
        themap.append(None)
themap[0]=0 # will remap this to iso UTC
themap[1]=1

c=writer.cursor()
for l in reader:
    if not l:
        continue
    w=[]
    for i in range(len(table_fields)):
	if i == 0:
	    t=long(l[0]) # convert timestamp to UTC
	    a=str(datetime.utcfromtimestamp(t))
	    w.append(a)
	elif themap[i] != None:
            v=l[themap[i]]
            if len(v)==0:
                w.append(None)
            else:
                w.append(v)
	else:
            w.append(None)
    c.execute(inserter, w)
writer.commit()
c.close()
