Completed
Push — master ( 235efc...44f214 )
by Yannick
29:36
created

Weather::nomad_wind()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 43
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 35
nc 32
nop 1
dl 0
loc 43
rs 6.7272
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 nomad_wind($hour = '') {
65
		global $globalWindsPath;
66
		if ($hour == '') $hour = date('G');
67
		if (isset($globalWindsPath) && $globalWindsPath != '') {
68
			$grib2json = $globalWindsPath['grib2json'];
69
			$windpathsrc = $globalWindsPath['source'];
70
			$windpathdest = $globalWindsPath['destination'];
71
		} else {
72
			$grib2json = dirname(__FILE__).'/libs/grib2json/bin/grib2json';
73
			$windpathsrc = dirname(__FILE__).'/../data/winds.gb2';
74
			$windpathdest = dirname(__FILE__).'/../data/winds.json';
75
		}
76
		
77
		// 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
78
		$resolution = '0.5';
79
		$baseurl = 'http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_'.($resolution === '1' ? '1p00':'0p50').'.pl';
80
		$get = array('file' => 'gfs.t'.sprintf('%02d',(6*floor($hour/6))).($resolution === '1' ? 'z.pgrb2.1p00.f000' : 'z.pgrb2full.0p50.f000'),
81
			'lev_10_m_above_ground' => 'on',
82
			'lev_surface' => 'on',
83
			'var_TMP' => 'on',
84
			'var_UGRD' => 'on',
85
			'var_VGRD' => 'on',
86
			'leftlon' => 0,
87
			'rightlon' => 360,
88
			'toplat' => 90,
89
			'bottomlat' => -90,
90
			'dir' => '/gfs.'.date('Ymd').sprintf('%02d',(6*floor($hour/6)))
91
		);
92
		$url = $baseurl.'?'.http_build_query($get);
93
		echo $url;
94
		$Common = new Common();
95
		$Common->download($url,$windpathsrc);
96
		// Check if the downloaded file is in GRIB format
97
		$file = fopen($windpathsrc,"r");
98
		$firc = fgetc($file);
99
		fclose($file);
100
		if ($firc == 'G') {
101
			system($grib2json.' --data --output '.$windpathdest.' --names --compact '.$windpathsrc);
102
		} else {
103
			// if not try previous run
104
			$this->nomad_wind(date('G')-6);
105
		}
106
	}
107
108
	public function oscar_wave() {
109
		global $globalWavesPath;
110
		if (isset($globalWavesPath) && $globalWavesPath != '') {
111
			$grib2json = $globalWavesPath['grib2json'];
112
			$wavepathsrc = $globalWavesPath['source'];
113
			$wavepathdest = $globalWavesPath['destination'];
114
		} else {
115
			$grib2json = dirname(__FILE__).'/libs/grib2json/bin/grib2json';
116
			$wavepathsrc = dirname(__FILE__).'/../data/waves.nc';
117
			$wavepathdest = dirname(__FILE__).'/../data/waves.json';
118
		}
119
		
120
		$url = 'https://podaac.jpl.nasa.gov/ws/search/granule/?datasetId=PODAAC-OSCAR-03D01&itemsPerPage=1&sortBy=timeDesc&format=atom&pretty=false';
121
		$Common = new Common();
122
		$oscarlst = $Common->getData($url);
123
		$oscarlst_xml = json_decode(json_encode(simplexml_load_string($oscarlst)),true);
124
		foreach ($oscarlst_xml['entry']['link'] as $oscarlnk) {
125
			if ($oscarlnk['@attributes']['type'] == 'application/x-netcdf') {
126
				$Common->download($oscarlnk['@attributes']['href'],$wavepathsrc.'.gz');
127
				break;
128
			}
129
		}
130
		$Common->gunzip($wavepathsrc.'.gz');
131
		system($grib2json.' --data --output '.$wavepathdest.' --names --compact '.$wavepathsrc);
132
	}
133
}
134
/*
135
require_once('class.METAR.php');
136
$METAR = new METAR();
137
*/
138
/*
139
$themetar = $METAR->getMETAR('LFLL');
140
print_r($themetar);
141
$result = $METAR->parse($themetar[0]['metar']);
142
*/
143
/*
144
$result = $METAR->parse('LFLL 081330Z 01006KT 340V050 9999 FEW020 BKN080 07/01 Q1018 NOSIG');
145
print_r($result);
146
$Weather = new Weather();
147
//print_r($Weather->buildcloudlayer($result));
148
//print_r($Weather->buildcloud('46.3870','5.2941','2000','0.25'));
149
print_r($Weather->generateRandomPoint('46.3870','5.2941','2000'));
150
*/
151
/*
152
$Weather = new Weather();
153
$Weather->nomad_wind();
154
*/
155
/*
156
$Weather = new Weather();
157
$Weather->oscar_wave();
158
*/
159
?>