Generator.py 7.74 KB
Newer Older
Dorian's avatar
Dorian committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
import csv
import re

def getData(container, value, type):
    result = container[value]
    if (result == "M" or result == "T") :
        result = None

    if (type == "string"):
        return result
    elif (type == "float") :
        if (result != None):
            result = float(result)
        else :
            return -8000
        return result
    elif (type == "date") :
        if (result != None) :
            dateparser = re.compile("(?P<year>\d+)-(?P<month>\d+)-(?P<day>\d+) (?P<hour>\d+):(?P<minute>\d+)")
            match = dateparser.match(container[value])
            if not match :
                return "continue"
            data = match.groupdict()
            result = (
                int(data["year"]),
                int(data["month"]),
                int(data["day"]),
                int(data["hour"]),
                int(data["minute"]),
            )
            return result
    else :
        return None


def loadata(filename):
    with open(filename) as f:
        for r in csv.DictReader(f):
            timestamp = getData(r, "valid", "date")
            if (timestamp == "continue" or timestamp == None):
                continue
            data = {}
            data["valid"] = timestamp

            data["station"] = getData(r, "station", "string")
            data["lon"] = getData(r, "lon", "float")
            data["lat"] = getData(r, "lat", "float")
import csv
import re

def getData(container, value, type):
    result = container[value]
    if (result == "M" or result == "T") :
        result = None

    if (type == "string"):
        return result
    elif (type == "float") :
        if (result != None):
            result = float(result)
        return result
    elif (type == "date") :
        if (result != None) :
            dateparser = re.compile("(?P<year>\d+)-(?P<month>\d+)-(?P<day>\d+) (?P<hour>\d+):(?P<minute>\d+)")
            match = dateparser.match(container[value])
            if not match :
                return "continue"
            data = match.groupdict()
            result = (
                int(data["year"]),
                int(data["month"]),
                int(data["day"]),
                int(data["hour"]),
                int(data["minute"]),
            )
            return result
    else :
        return None

def loadata(filename):
    with open(filename) as f:
        for r in csv.DictReader(f):
            timestamp = getData(r, "valid", "date")
            if (timestamp == "continue" or timestamp == None):
                continue
            data = {}
            data["valid"] = timestamp

            data["station"] = getData(r, "station", "string")
            data["lon"] = getData(r, "lon", "float")
            data["lat"] = getData(r, "lat", "float")

            data["tmpf"] = getData(r, "tmpf", "float")
            data["dwpf"] = getData(r, "dwpf", "float")
            data["relh"] = getData(r, "relh", "float")
            data["drct"] = getData(r, "drct", "float")
            data["sknt"] = getData(r, "sknt", "float")
            data["p01i"] = getData(r, "p01i", "float")
            data["alti"] = getData(r, "alti", "float")
            data["mslp"] = getData(r, "mslp", "float")
            data["vsby"] = getData(r, "vsby", "float")
            data["gust"] = getData(r, "gust", "float")
            data["skyc1"] = getData(r, "skyc1", "string")
            data["skyc2"] = getData(r, "skyc2", "string")
            data["skyc3"] = getData(r, "skyc3", "string")
            data["skyc4"] = getData(r, "skyc4", "string")
            data["skyl1"] = getData(r, "skyl1", "float")
            data["skyl2"] = getData(r, "skyl2", "float")
            data["skyl3"] = getData(r, "skyl3", "float")
            data["skyl4"] = getData(r, "skyl4", "float")
            data["wxcodes"] = getData(r, "wxcodes", "string")
            data["ice_accretion_1hr"] = getData(r, "ice_accretion_1hr", "float")
            data["ice_accretion_3hr"] = getData(r, "ice_accretion_3hr", "float")
            data["ice_accretion_6hr"] = getData(r, "ice_accretion_6hr", "float")
            data["peak_wind_gust"] = getData(r, "peak_wind_gust", "float")
            data["peak_wind_drct"] = getData(r, "peak_wind_drct", "float")
            data["peak_wind_time"] = getData(r, "peak_wind_time", "float")
            data["feel"] = getData(r, "feel", "float")
            data["metar"] = getData(r, "metar", "string")

            yield data


import json

from cassandra.cluster import Cluster

cluster = Cluster(['localhost'])
session = cluster.connect('dbermond_projet')

query = '''
    DROP TABLE IF EXISTS weatherByTime ;
    '''

session.execute(query)

query = '''
    CREATE TABLE weatherByTime(

    station text,

    valid_year varint,
    valid_month varint,
    valid_day varint,
    valid_hour varint,
    valid_minute varint,

    lon float,
    lat float,

    tmpf float,
    dwpf float,
    relh float,
    drct float,
    sknt float,
    p01i float,
    alti float,
    mslp float,
    vsby float,
    gust float,

    skyc1 text,
    skyc2 text,
    skyc3 text,
    skyc4 text,

    skyl1 float,
    skyl2 float,
    skyl3 float,
    skyl4 float,

    wxcodes text,

    ice_accretion_1hr float,
    ice_accretion_3hr float,
    ice_accretion_6hr float,

    peak_wind_gust float,
	peak_wind_drct float,
	peak_wind_time float,

    feel float,

    metar text,

    PRIMARY KEY ((valid_year, valid_month, valid_day),  valid_hour, valid_minute, metar)
    );
'''

session.execute(query)

def getWeatherByTime(csvfilename, session, limit):
    data = loadata(csvfilename)
    n = 0 ;
    for r in data:
        t = (
            r["station"],

            r["valid"][0],
            r["valid"][1],
            r["valid"][2],
            r["valid"][3],
            r["valid"][4],

            r["lon"],
            r["lat"],

            r["tmpf"],
            r["dwpf"],
            r["relh"],
            r["drct"],
            r["sknt"],
            r["alti"],
            r["mslp"],
            r["p01i"],
            r["vsby"],
            r["gust"],

            r["skyc1"],
            r["skyc2"],
            r["skyc3"],
            r["skyc4"],

            r["skyl1"],
            r["skyl2"],
            r["skyl3"],
            r["skyl4"],

            r["wxcodes"],

            r["ice_accretion_1hr"],
            r["ice_accretion_3hr"],
            r["ice_accretion_6hr"],
            r["peak_wind_gust"],
            r["peak_wind_drct"],
            r["peak_wind_time"],

            r["feel"],
            r["metar"],
            )
        query = """
        INSERT INTO weatherByTime(

                station,

                valid_year,
                valid_month,
                valid_day,
                valid_hour,
                valid_minute,
                lon,
                lat,

                tmpf,
                dwpf,
                relh,
                drct,
                sknt,
                p01i,
                alti,
                mslp,
                vsby,
                gust,

                skyc1,
                skyc2,
                skyc3,
                skyc4,

                skyl1,
                skyl2,
                skyl3,
                skyl4,

                wxcodes,

                ice_accretion_1hr,
                ice_accretion_3hr,
                ice_accretion_6hr,
                peak_wind_gust,
                peak_wind_drct,
                peak_wind_time,

                feel,
                metar)
        VALUES (%s, %s, %s ,%s ,%s, %s, %s, %s ,%s ,%s, %s, %s, %s ,%s ,%s, %s, %s, %s ,%s ,%s, %s, %s, %s ,%s ,%s, %s, %s, %s ,%s ,%s, %s, %s, %s ,%s ,%s)
        """
        #(%s, %d, %d, %d, %d, %d, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %s, %s, %s, %s, %f, %f, %f, %f, %s, )
        session.execute(query, t)
        n += 1
        if (limit != 0 and n >= limit) :
            break