Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once('Conexao.class.php');
- class Previsao extends Conexao {
- // função que interpola os dados de forma bi-dimensional de acordo com https://en.wikipedia.org/wiki/Bilinear_interpolation
- function interpolar($x, $y, $x1, $x2, $y1, $y2, $Q11, $Q21, $Q12, $Q22) {
- $ans1 = ((($x2 - $x) * ($y2 - $y)) / (($x2 - $x1) * ($y2 - $y1))) * $Q11;
- $ans2 = ((($x - $x1) * ($y2 - $y)) / (($x2 - $x1) * ($y2 - $y1))) * $Q21;
- $ans3 = ((($x2 - $x) * ($y - $y1)) / (($x2 - $x1) * ($y2 - $y1))) * $Q12;
- $ans4 = ((($x - $x1) * ($y - $y1)) / (($x2 - $x1) * ($y2 - $y1))) * $Q22;
- return $ans1 + $ans2 + $ans3 + $ans4;
- }
- // calcula direcao do vento ugrid e vgrid - função feita por Adrinano
- function calcuuvvdd($uu, $vv) {
- if ($vv == 0) {
- if ($uu == 0) {
- $dd = 0;
- $ff = 0;
- $d32 = 0;
- }
- if ($uu > 0) {
- $dd = 270;
- $ff = $uu;
- $d32 = 24;
- } else {
- $dd = 90;
- $ff = - $uu;
- $d32 = 8;
- }
- }
- else if ($vv < 0) {
- $dd = atan ( $uu / $vv ) * 180 / pi ();
- }
- else {
- $dd = atan ( $uu / $vv ) * 180 / pi () + 180;
- }
- if ($dd < 0) {
- $dd = $dd + 360;
- }
- return ($dd);
- }
- // calcula intensidade do vento ugrid e vgrid - função feita por Adrinano
- function calcuuvvff($uu, $vv) {
- return $ff = (sqrt ( $uu * $uu + $vv * $vv ));
- }
- function calcuuvvd32($uu, $vv) {
- if ($vv == 0) {
- if ($uu == 0) {
- $dd = 0;
- $ff = 0;
- $d32 = 0;
- }
- if ($uu > 0) {
- $dd = 270;
- $ff = $uu;
- $d32 = 24;
- } else {
- $dd = 90;
- $ff = - $uu;
- $d32 = 8;
- }
- }
- else if ($vv < 0) {
- $dd = atan ( $uu / $vv ) * 180 / pi ();
- }
- else {
- $dd = atan ( $uu / $vv ) * 180 / pi () + 180;
- }
- if ($dd < 0) {
- $dd = $dd + 360;
- }
- return ($dd / 360 * 32);
- }
- public function listarPrevisao($nome, $parametro, $latitude, $longitude){
- $pdo = parent::getDB();
- // $listarPrevisao = $pdo->prepare("SELECT time1 as data, longitude as PQUAD_LON, latitude as PQUAD_LAT
- // FROM :nome
- // WHERE (-35.25 + 0.25 >= longitude) AND
- // (-35.25 - 0.25) <= longitude) AND
- // (:-7.75 + 0.25 >= latitude) AND
- // (:-7.75 - 0.25 <= latitude)
- // ORDER BY data, PQUAD_LON, PQUAD_LAT");
- $listarPrevisao = $pdo->prepare("SELECT P.time1 as data, P.longitude as PQUAD_LON, P.latitude as PQUAD_LAT, P.APCP_surface AS precipitacao, GUST_surface AS rajada, VGRD_10_m_above_ground as vento10_V, UGRD_10_m_above_ground as vento10_U, VGRD_80_m_above_ground as vento80_V, UGRD_80_m_above_ground as vento80_U, VGRD_100_m_above_ground as vento100_V, UGRD_100_m_above_ground as vento100_U, TMP_2_m_above_ground - 273.15 as temperatura, TMAX_2_m_above_ground - 273.15 as temperatura_MAX, TMIN_2_m_above_ground - 273.15 as temperatura_MIN, (PRES_surface/100) as pressao, TCDC_high_cloud_layer as nuvem_alta, TCDC_low_cloud_layer as nuvem_baixa, TCDC_middle_cloud_layer as nuvem_media, RH_2_m_above_ground as umidade, DSWRF_surface as radiacao
- FROM Previsao_gfs_100_1_2018011800 P
- WHERE -35.50 + 1 >= P.longitude AND
- -35.50 - 1 <= P.longitude AND
- -8.00 + 1 >= P.latitude AND
- -8.00 - 1 <= P.latitude
- ORDER BY data, PQUAD_LON, PQUAD_LAT;");
- // $listarPrevisao->bindParam('nome', $nome);
- // $listarPrevisao->bindParam('vvv_longitude', $longitude);
- // $listarPrevisao->bindParam('vvv_latitude', $latitude);
- // $listarPrevisao->bindParam('parametro', $parametro);
- $listarPrevisao->execute();
- // print_r(['PQUAD_LAT');
- $r = $listarPrevisao->fetchAll();
- // echo $r['PQUAD_LON'];
- // print_r($nome);
- // echo $r['PQUAD_LON'];
- // echo $r['PQUAD_LON'];
- // exit();
- // salva a lat,lon e data pra checar mais adiante se vieram 4 pontos (quadrante) por ponto médio
- // print_r($r);
- foreach($r as $res) {
- $pontosInterpolados = Array();
- $quadrante = Array();
- $dataHora = null;
- $q = (object) array('longitude' => $res['PQUAD_LON'], 'latitude' => $res['PQUAD_LAT'], 'precipitacao' => $res['precipitacao'], 'rajada' => $res['rajada']
- , 'vento10U' => $res['vento10_U'], 'vento10V' => $res['vento10_V'], 'vento80U' => $res['vento80_U'], 'vento80V' => $res['vento80_V'],'vento100U' => $res['vento100_U'], 'vento100V' => $res['vento100_V'], 'temperatura_instantanea' => $res['temperatura'], 'temperatura_MAX' => $res['temperatura_MAX'], 'temperatura_MIN' => $res['temperatura_MIN'], 'pressao' => $res['pressao'], 'nuvem_alta' => $res['nuvem_alta'], 'nuvem_baixa' => $res['nuvem_baixa'], 'nuvem_media' => $res['nuvem_media'], 'umidade' => $res['umidade'], 'radiacao' => $res['radiacao']);
- // echo $res['data'];
- // echo $q->longitude;
- // print_r($q);
- // var_dump($q);
- // pegou os 4 pontos do quadrante
- $quadrante[] = $q;
- if(sizeof($quadrante) == 4){
- // interpola todas as medidas (menos lat e lont...)
- foreach ($q as $key => $value) {
- if($key == 'longitude' OR $key == 'latitude')
- continue;
- $pontoInterpolado[$key] = number_format((float)interpolar($longitude, $latitude, $quadrante[0]->longitude, $quadrante[3]->longitude, $quadrante[0]->latitude, $quadrante[3]->latitude, $quadrante[0]->$key, $quadrante[3]->$key, $quadrante[1]->$key, $quadrante[2]->$key), 2, '.', '');
- }
- // adiciona algumas data e calcula vento
- $pontoInterpolado['data'] = $res['data'];
- $pontoInterpolado['vento10_direcao'] = number_format((float) calcuuvvdd($pontoInterpolado['vento10U'], $pontoInterpolado['vento10V']), 2, '.', '');
- $pontoInterpolado['vento10_intensidade'] = number_format((float) calcuuvvff($pontoInterpolado['vento10U'], $pontoInterpolado['vento10V']), 2, '.', '');
- $pontoInterpolado['vento80_direcao'] = number_format((float) calcuuvvdd($pontoInterpolado['vento80U'], $pontoInterpolado['vento80V']), 2, '.', '');
- $pontoInterpolado['vento80_intensidade'] = number_format((float) calcuuvvff($pontoInterpolado['vento80U'], $pontoInterpolado['vento80V']), 2, '.', '');
- $pontoInterpolado['vento100_direcao'] = number_format((float) calcuuvvdd($pontoInterpolado['vento100U'], $pontoInterpolado['vento100V']), 2, '.', '');
- $pontoInterpolado['vento100_intensidade'] = number_format((float) calcuuvvff($pontoInterpolado['vento100U'], $pontoInterpolado['vento100V']), 2, '.', '');
- // remove
- unset($pontoInterpolado['vento10U']);
- unset($pontoInterpolado['vento80U']);
- unset($pontoInterpolado['vento100U']);
- unset($pontoInterpolado['vento10V']);
- unset($pontoInterpolado['vento80V']);
- unset($pontoInterpolado['vento100V']);
- // adiciona a array de resposta
- $pontosInterpolados[] = $pontoInterpolado;
- // zera quadrante
- $quadrante = Array();
- }
- }
- }
- }
- $instance = new Previsao();
- $var = $instance->listarPrevisao('Previsao_gfs_025_5_2018011700',0.25,-35.25,-7.75);
- print_r($var);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement