Completed
Push — master ( 7b0a03...ef2efa )
by Yannick
33:49
created

Weather::oscar_wave()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 6
nop 0
dl 0
loc 25
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * This class is part of FlightAirmap. It's used to get weather data and create clouds
4
 *
5
 * Copyright (c) Ycarus (Yannick Chabanois) <[email protected]>
6
 * Licensed under AGPL license.
7
 * For more information see: https://www.flightairmap.com/
8
*/
9
require_once(dirname(__FILE__).'/class.Common.php');
10
class Weather {
11
	public function buildcloudlayer($metar) {
12
		//print_r($metar);
13
		$result = array();
14
		foreach($metar['cloud'] as $key => $data) {
15
			$alt_m = $metar['cloud'][$key]['level'];
16
			$alt_ft = $alt_m*3.28084;
17
			$pressure = $metar['QNH'];
18
			$cumulus_base = 122.0 * ($metar['temperature'] - $metar['dew']);
19
			$stratus_base = 100.0 * (100.0 * $metar['rh'])*0.3048;
20
			$coverage_norm = 0.0;
21
			if ($metar['cloud'][$key]['type'] == 'Few') {
22
				$coverage_norm = 2.0/8.0;
23
			} elseif ($metar['cloud'][$key]['type'] == 'Scattered') {
24
				$coverage_norm = 4.0/8.0;
25
			} elseif ($metar['cloud'][$key]['type'] == 'Broken') {
26
				$coverage_norm = 6.0/8.0;
27
			} elseif ($metar['cloud'][$key]['type'] == 'Overcast/Full cloud coverage') {
28
				$coverage_norm = 8.0/8.0;
29
			}
30
			$layer_type = 'nn';
0 ignored issues
show
Unused Code introduced by
$layer_type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
			if ($metar['cloud'][$key]['significant'] == 'cirrus') {
32
				$layer_type = 'ci';
33
			} elseif ($alt_ft > 16500) {
34
				$layer_type = 'ci';
35
			} elseif ($alt_ft > 6500) {
36
				$layer_type = 'ac';
37
				if ($pressure < 1005.0 && $coverage_norm >= 0.5) {
38
					$layer_type = 'ns';
39
				}
40
			} else {
41
				if ($cumulus_base * 0.80 < $alt_m && $cumulus_base * 1.20 > $alt_m) {
42
					$layer_type = 'cu';
43
				} elseif ($stratus_base * 0.80 < $alt_m && $stratus_base * 1.40 > $alt_m) {
44
					$layer_type = 'st';
45
				} else {
46
					if ($alt_ft < 2000) {
47
						$layer_type = 'st';
48
					} elseif ($alt_ft < 4500) {
49
						$layer_type = 'cu';
50
					} else {
51
						$layer_type = 'sc';
52
					}
53
				}
54
			}
55
			//echo 'coverage norm : '.$coverage_norm.' - layer_type: '.$layer_type."\n";
56
			$result[] = array('cov' => $coverage_norm, 'type' => $layer_type,'alt' => $alt_m,'rh' => $metar['rh']);
57
		}
58
		if (count($result) < 2 && $metar['rh'] > 60) {
59
			$result[] = array('cov' => 0.75, 'type' => 'ci','alt' => 4000,'rh' => $metar['rh']);
60
		}
61
		return $result;
62
	}
63
	
64
	public function openweathermap($latitude,$longitude) {
65
		global $globalOpenWeatherMapKey;
66
		if ($globalOpenWeatherMapKey == '') return array();
67
		$Common = new Common();
68
		$url = 'http://api.openweathermap.org/data/2.5/weather?lat='.$latitude.'&lon='.$longitude.'&units=metric&appid='.$globalOpenWeatherMapKey;
69
		//echo $url."\n";
70
		$weatherdata = json_decode($Common->getData($url),true);
71
		if (!isset($weatherdata['main']['temp']) || !isset($weatherdata['weather'][0]['id'])) return array();
72
		$dew = $weatherdata['main']['temp'] - ((100-$weatherdata['main']['humidity'])/5);
73
		$cumulus_base = 122.0 * ($weatherdata['main']['temp'] - $dew);
74
		$stratus_base = 100.0 * (100.0 * $weatherdata['main']['humidity'])*0.3048;
75
		$coverage_norm = 0.0;
76
		if ($weatherdata['weather'][0]['id'] == 801) {
77
			// few clouds
78
			$coverage_norm = 2.0/8.0;
79
		} elseif ($weatherdata['weather'][0]['id'] == 802) {
80
			//scattered cloud
81
			$coverage_norm = 4.0/8.0;
82
		} elseif ($weatherdata['weather'][0]['id'] == 803) {
83
			// broken clouds
84
			$coverage_norm = 6.0/8.0;
85
		} elseif ($weatherdata['weather'][0]['id'] == 804) {
86
			// overcast clouds
87
			$coverage_norm = 8.0/8.0;
88
		}
89
		$layer_type = 'nn';
0 ignored issues
show
Unused Code introduced by
$layer_type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
90
		$alt_m = 1000;
91
		if ($cumulus_base * 0.80 < $alt_m && $cumulus_base * 1.20 > $alt_m) {
92
			$layer_type = 'cu';
93
		} elseif ($stratus_base * 0.80 < $alt_m && $stratus_base * 1.40 > $alt_m) {
94
			$layer_type = 'st';
95
		} else {
96
			$layer_type = 'st';
97
		}
98
		$result[] = array('cov' => $coverage_norm, 'type' => $layer_type,'alt' => $alt_m,'rh' => $weatherdata['main']['humidity']);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$result was never initialized. Although not strictly required by PHP, it is generally a good practice to add $result = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
99
		if ($weatherdata['main']['humidity'] > 60) {
100
			$result[] = array('cov' => 0.75, 'type' => 'ci','alt' => 4000,'rh' => $weatherdata['main']['humidity']);
101
		}
102
		return $result;
103
	}
104
	
105
	public function nomad_wind($hour = '') {
106
		global $globalWindsPath;
107
		if ($hour == '') $hour = date('G');
108
		if (isset($globalWindsPath) && $globalWindsPath != '') {
109
			$grib2json = $globalWindsPath['grib2json'];
110
			$windpathsrc = $globalWindsPath['source'];
111
			$windpathdest = $globalWindsPath['destination'];
112
		} else {
113
			$grib2json = dirname(__FILE__).'/libs/grib2json/bin/grib2json';
114
			$windpathsrc = dirname(__FILE__).'/../data/winds.gb2';
115
			$windpathdest = dirname(__FILE__).'/../data/winds.json';
116
		}
117
		
118
		// http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p50.pl?file=gfs.t05z.pgrb2full.0p50.f000&lev_10_m_above_ground=on&lev_surface=on&var_TMP=on&var_UGRD=on&var_VGRD=on&leftlon=0&rightlon=360&toplat=90&bottomlat=-90&dir=/gfs.2017111717
119
		$resolution = '0.5';
120
		$baseurl = 'http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_'.($resolution === '1' ? '1p00':'0p50').'.pl';
121
		$get = array('file' => 'gfs.t'.sprintf('%02d',(6*floor($hour/6))).($resolution === '1' ? 'z.pgrb2.1p00.f000' : 'z.pgrb2full.0p50.f000'),
122
			'lev_10_m_above_ground' => 'on',
123
			'lev_surface' => 'on',
124
			'var_TMP' => 'on',
125
			'var_UGRD' => 'on',
126
			'var_VGRD' => 'on',
127
			'leftlon' => 0,
128
			'rightlon' => 360,
129
			'toplat' => 90,
130
			'bottomlat' => -90,
131
			'dir' => '/gfs.'.date('Ymd').sprintf('%02d',(6*floor($hour/6)))
132
		);
133
		$url = $baseurl.'?'.http_build_query($get);
134
		//echo $url;
135
		$Common = new Common();
136
		$Common->download($url,$windpathsrc);
137
		// Check if the downloaded file is in GRIB format
138
		$file = fopen($windpathsrc,"r");
139
		$firc = fgetc($file);
140
		fclose($file);
141
		if ($firc == 'G') {
142
			system($grib2json.' --data --output '.$windpathdest.' --names --compact '.$windpathsrc);
143
		} else {
144
			// if not try previous run
145
			if ($hour == date('G')) $this->nomad_wind(date('G')-6);
146
		}
147
	}
148
149
	public function oscar_wave() {
150
		global $globalWavesPath;
151
		if (isset($globalWavesPath) && $globalWavesPath != '') {
152
			$grib2json = $globalWavesPath['grib2json'];
153
			$wavepathsrc = $globalWavesPath['source'];
154
			$wavepathdest = $globalWavesPath['destination'];
155
		} else {
156
			$grib2json = dirname(__FILE__).'/libs/grib2json/bin/grib2json';
157
			$wavepathsrc = dirname(__FILE__).'/../data/waves.nc';
158
			$wavepathdest = dirname(__FILE__).'/../data/waves.json';
159
		}
160
		
161
		$url = 'https://podaac.jpl.nasa.gov/ws/search/granule/?datasetId=PODAAC-OSCAR-03D01&itemsPerPage=1&sortBy=timeDesc&format=atom&pretty=false';
162
		$Common = new Common();
163
		$oscarlst = $Common->getData($url);
164
		$oscarlst_xml = json_decode(json_encode(simplexml_load_string($oscarlst)),true);
165
		foreach ($oscarlst_xml['entry']['link'] as $oscarlnk) {
166
			if ($oscarlnk['@attributes']['type'] == 'application/x-netcdf') {
167
				$Common->download($oscarlnk['@attributes']['href'],$wavepathsrc.'.gz');
168
				break;
169
			}
170
		}
171
		$Common->gunzip($wavepathsrc.'.gz');
172
		system($grib2json.' --data --output '.$wavepathdest.' --names --compact '.$wavepathsrc);
173
	}
174
}
175
/*
176
require_once('class.METAR.php');
177
$METAR = new METAR();
178
*/
179
/*
180
$themetar = $METAR->getMETAR('LFLL');
181
print_r($themetar);
182
$result = $METAR->parse($themetar[0]['metar']);
183
*/
184
/*
185
$result = $METAR->parse('LFLL 081330Z 01006KT 340V050 9999 FEW020 BKN080 07/01 Q1018 NOSIG');
186
print_r($result);
187
$Weather = new Weather();
188
//print_r($Weather->buildcloudlayer($result));
189
//print_r($Weather->buildcloud('46.3870','5.2941','2000','0.25'));
190
print_r($Weather->generateRandomPoint('46.3870','5.2941','2000'));
191
*/
192
/*
193
$Weather = new Weather();
194
$Weather->nomad_wind();
195
*/
196
/*
197
$Weather = new Weather();
198
$Weather->oscar_wave();
199
*/
200
/*
201
$Weather = new Weather();
202
print_r($Weather->openweathermap(0.0,-10.0));
203
*/
204
?>