Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. <?php
  2.  
  3. require_once('Conexao.class.php');
  4.  
  5. class Previsao extends Conexao {
  6.  
  7. // função que interpola os dados de forma bi-dimensional de acordo com https://en.wikipedia.org/wiki/Bilinear_interpolation
  8. function interpolar($x, $y, $x1, $x2, $y1, $y2, $Q11, $Q21, $Q12, $Q22) {
  9. $ans1 = ((($x2 - $x) * ($y2 - $y)) / (($x2 - $x1) * ($y2 - $y1))) * $Q11;
  10. $ans2 = ((($x - $x1) * ($y2 - $y)) / (($x2 - $x1) * ($y2 - $y1))) * $Q21;
  11. $ans3 = ((($x2 - $x) * ($y - $y1)) / (($x2 - $x1) * ($y2 - $y1))) * $Q12;
  12. $ans4 = ((($x - $x1) * ($y - $y1)) / (($x2 - $x1) * ($y2 - $y1))) * $Q22;
  13. return $ans1 + $ans2 + $ans3 + $ans4;
  14. }
  15.  
  16. // calcula direcao do vento ugrid e vgrid - função feita por Adrinano
  17. function calcuuvvdd($uu, $vv) {
  18. if ($vv == 0) {
  19. if ($uu == 0) {
  20. $dd = 0;
  21. $ff = 0;
  22. $d32 = 0;
  23. }
  24. if ($uu > 0) {
  25. $dd = 270;
  26. $ff = $uu;
  27. $d32 = 24;
  28. } else {
  29. $dd = 90;
  30. $ff = - $uu;
  31. $d32 = 8;
  32. }
  33. }
  34.  
  35. else if ($vv < 0) {
  36. $dd = atan ( $uu / $vv ) * 180 / pi ();
  37. }
  38.  
  39. else {
  40. $dd = atan ( $uu / $vv ) * 180 / pi () + 180;
  41. }
  42.  
  43. if ($dd < 0) {
  44. $dd = $dd + 360;
  45. }
  46. return ($dd);
  47. }
  48.  
  49.  
  50. // calcula intensidade do vento ugrid e vgrid - função feita por Adrinano
  51. function calcuuvvff($uu, $vv) {
  52. return $ff = (sqrt ( $uu * $uu + $vv * $vv ));
  53. }
  54.  
  55. function calcuuvvd32($uu, $vv) {
  56. if ($vv == 0) {
  57. if ($uu == 0) {
  58. $dd = 0;
  59. $ff = 0;
  60. $d32 = 0;
  61. }
  62. if ($uu > 0) {
  63. $dd = 270;
  64. $ff = $uu;
  65. $d32 = 24;
  66. } else {
  67. $dd = 90;
  68. $ff = - $uu;
  69. $d32 = 8;
  70. }
  71. }
  72. else if ($vv < 0) {
  73. $dd = atan ( $uu / $vv ) * 180 / pi ();
  74. }
  75. else {
  76. $dd = atan ( $uu / $vv ) * 180 / pi () + 180;
  77. }
  78.  
  79. if ($dd < 0) {
  80. $dd = $dd + 360;
  81. }
  82. return ($dd / 360 * 32);
  83. }
  84.  
  85.  
  86.  
  87. public function listarPrevisao($nome, $parametro, $latitude, $longitude){
  88.  
  89. $pdo = parent::getDB();
  90.  
  91. // $listarPrevisao = $pdo->prepare("SELECT time1 as data, longitude as PQUAD_LON, latitude as PQUAD_LAT
  92.  
  93. // FROM :nome
  94. // WHERE (-35.25 + 0.25 >= longitude) AND
  95. // (-35.25 - 0.25) <= longitude) AND
  96. // (:-7.75 + 0.25 >= latitude) AND
  97. // (:-7.75 - 0.25 <= latitude)
  98. // ORDER BY data, PQUAD_LON, PQUAD_LAT");
  99.  
  100. $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
  101.  
  102. FROM Previsao_gfs_100_1_2018011800 P
  103. WHERE -35.50 + 1 >= P.longitude AND
  104. -35.50 - 1 <= P.longitude AND
  105. -8.00 + 1 >= P.latitude AND
  106. -8.00 - 1 <= P.latitude
  107. ORDER BY data, PQUAD_LON, PQUAD_LAT;");
  108. // $listarPrevisao->bindParam('nome', $nome);
  109. // $listarPrevisao->bindParam('vvv_longitude', $longitude);
  110. // $listarPrevisao->bindParam('vvv_latitude', $latitude);
  111. // $listarPrevisao->bindParam('parametro', $parametro);
  112.  
  113. $listarPrevisao->execute();
  114. // print_r(['PQUAD_LAT');
  115.  
  116. $r = $listarPrevisao->fetchAll();
  117. // echo $r['PQUAD_LON'];
  118.  
  119. // print_r($nome);
  120. // echo $r['PQUAD_LON'];
  121.  
  122. // echo $r['PQUAD_LON'];
  123. // exit();
  124.  
  125. // salva a lat,lon e data pra checar mais adiante se vieram 4 pontos (quadrante) por ponto médio
  126. // print_r($r);
  127. foreach($r as $res) {
  128. $pontosInterpolados = Array();
  129. $quadrante = Array();
  130.  
  131. $dataHora = null;
  132. $q = (object) array('longitude' => $res['PQUAD_LON'], 'latitude' => $res['PQUAD_LAT'], 'precipitacao' => $res['precipitacao'], 'rajada' => $res['rajada']
  133. , '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']);
  134.  
  135. // echo $res['data'];
  136. // echo $q->longitude;
  137. // print_r($q);
  138.  
  139. // var_dump($q);
  140.  
  141.  
  142.  
  143. // pegou os 4 pontos do quadrante
  144. $quadrante[] = $q;
  145. if(sizeof($quadrante) == 4){
  146.  
  147. // interpola todas as medidas (menos lat e lont...)
  148. foreach ($q as $key => $value) {
  149. if($key == 'longitude' OR $key == 'latitude')
  150. continue;
  151.  
  152. $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, '.', '');
  153. }
  154.  
  155. // adiciona algumas data e calcula vento
  156. $pontoInterpolado['data'] = $res['data'];
  157.  
  158. $pontoInterpolado['vento10_direcao'] = number_format((float) calcuuvvdd($pontoInterpolado['vento10U'], $pontoInterpolado['vento10V']), 2, '.', '');
  159. $pontoInterpolado['vento10_intensidade'] = number_format((float) calcuuvvff($pontoInterpolado['vento10U'], $pontoInterpolado['vento10V']), 2, '.', '');
  160.  
  161. $pontoInterpolado['vento80_direcao'] = number_format((float) calcuuvvdd($pontoInterpolado['vento80U'], $pontoInterpolado['vento80V']), 2, '.', '');
  162. $pontoInterpolado['vento80_intensidade'] = number_format((float) calcuuvvff($pontoInterpolado['vento80U'], $pontoInterpolado['vento80V']), 2, '.', '');
  163.  
  164. $pontoInterpolado['vento100_direcao'] = number_format((float) calcuuvvdd($pontoInterpolado['vento100U'], $pontoInterpolado['vento100V']), 2, '.', '');
  165. $pontoInterpolado['vento100_intensidade'] = number_format((float) calcuuvvff($pontoInterpolado['vento100U'], $pontoInterpolado['vento100V']), 2, '.', '');
  166.  
  167. // remove
  168. unset($pontoInterpolado['vento10U']);
  169. unset($pontoInterpolado['vento80U']);
  170. unset($pontoInterpolado['vento100U']);
  171. unset($pontoInterpolado['vento10V']);
  172. unset($pontoInterpolado['vento80V']);
  173. unset($pontoInterpolado['vento100V']);
  174.  
  175. // adiciona a array de resposta
  176. $pontosInterpolados[] = $pontoInterpolado;
  177.  
  178. // zera quadrante
  179. $quadrante = Array();
  180. }
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. }
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201. $instance = new Previsao();
  202.  
  203. $var = $instance->listarPrevisao('Previsao_gfs_025_5_2018011700',0.25,-35.25,-7.75);
  204. print_r($var);
  205.  
  206.  
  207.  
  208. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement