get_png_from_db.py 4.05 KB
import urllib.request
import json
import os
import datetime
from imageio import imread
import cv2
import numpy as np
import loca_data
import duamel_model
import matplotlib.pyplot as plt
import sher_duam_class
import dateutil.parser
import psycopg2
import pathlib


def get_connection():
    DBConnection = psycopg2.connect(
        host="localhost",
        port = "15432",
        database="wf71",
        user="wf71",
        password="wf71")
    print("Connect is success!")
    DBConnection.autocommit = True
    return DBConnection

def write_png(filename = "test.png", px_array = None):
    if px_array is None:
        px_array = np.zeros(10,10)
    path = pathlib.Path(__file__).parent.absolute()
    path = str(path) + filename
    cv2.imwrite(path, px_array)

def get_png_data_from_db():
    DBConnection = get_connection()
    print("Connect is success!")
    cur = DBConnection.cursor()
    query = "select measuretime, additional_info from md_precipitation where measurer_uuid = '1bc2b71c-d505-4034-a939-df0252b3f7c6' and measuretime >= '2020-07-08 07:00:00' and measuretime <= '2020-07-09 07:00:00'"
    cur.execute(query)
    result = cur.fetchall()
    radar_a = 200
    radar_n = 1.6
    for_txt = []

    for var in result:
        matrix = []
        m_dt = var[0]
        mx = json.loads(var[1])
        value = 0.0

        error = mx.get("error", False)

        if error == "Radar matrix is not exist":
            value = 0.0
        else:
            matrix = mx["matrix"]
            if len(matrix) > 1:
                _dbz = matrix[143][107]
                if _dbz == 0 or _dbz > 127:
                    value = 0.0
                else:
                    step1 = (_dbz - 32) / 10
                    step2 = 10 ** step1
                    step3 = step2 / radar_a
                    step4 = step3 ** (1.0/radar_n)
                    step5 = step4 / 6.0
                    value = step5

        #delta_dt = datetime.timedelta(hours=3)
        #m_dt = datetime.datetime.strptime(str(m_dt), '%Y-%m-%d %H:%M:%S') - delta_dt
        for_txt.append([str(m_dt), value])

    cur.close()
    f = open('result_from_db.txt', 'w')
    f.write(str(for_txt))
    f.close()

def get_png_picture_from_db():
    DBConnection = get_connection()
    print("Connect is success!")
    cur = DBConnection.cursor()
    query = "select measuretime, additional_info from md_precipitation where measurer_uuid = '1bc2b71c-d505-4034-a939-df0252b3f7c6' and measuretime >= '2020-07-08 07:00:00' and measuretime <= '2020-07-09 07:00:00'"
    cur.execute(query)
    result = cur.fetchall()
    i = -1


    for var in result:
        matrix = []
        m_dt = var[0]
        td = datetime.timedelta(hours=3)
        m_dt = m_dt+td
        print(m_dt)
        mx = json.loads(var[1])
        img = np.zeros((256,256, 4),dtype=np.uint8)

        error = mx.get("error", False)

        if error == "Radar matrix is not exist":
            continue
        else:
            matrix = mx["matrix"]
            if len(matrix) > 1:
                #png_data = []
                #for line in matrix:
                #    new_line = []
                #    for column in line:
                #        new_line.append([column, column, column, 255])
                #    png_data.append(new_line)
                #png_data = np.array(png_data)
                x = -1
                for line in matrix:
                    x = x + 1
                    y = -1
                    for column in line:
                        y = y + 1
                        img[x][y] = [column, column, column, 255]

                fn_dt = '{}_{}_{}__{}_{}.png'.format(m_dt.timetuple()[0], m_dt.timetuple()[1], m_dt.timetuple()[2], m_dt.timetuple()[3], m_dt.timetuple()[4])
                #write_png(filename=r"/png_07/{}".format(fn_dt), px_array=png_data)
                write_png(filename=r"/png_07/{}".format(fn_dt), px_array=img)
    cur.close()
    #get_png_picture_from_db


#get_png_data_from_db()
get_png_picture_from_db()