Completed
Push — master ( da8f44...fff8a0 )
by Yannick
34:08
created

Weather::buildcloudlayer()   C

Complexity

Conditions 19
Paths 92

Size

Total Lines 52
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 42
nc 92
nop 1
dl 0
loc 52
rs 5.8807
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
require_once(dirname(__FILE__).'/class.Common.php');
3
class Weather {
4
	public function buildcloudlayer($metar) {
5
		//print_r($metar);
6
		$result = array();
7
		foreach($metar['cloud'] as $key => $data) {
8
			$alt_m = $metar['cloud'][$key]['level'];
9
			$alt_ft = $alt_m*3.28084;
10
			$pressure = $metar['QNH'];
11
			$cumulus_base = 122.0 * ($metar['temperature'] - $metar['dew']);
12
			$stratus_base = 100.0 * (100.0 * $metar['rh'])*0.3048;
13
			$coverage_norm = 0.0;
14
			if ($metar['cloud'][$key]['type'] == 'Few') {
15
				$coverage_norm = 2.0/8.0;
16
			} elseif ($metar['cloud'][$key]['type'] == 'Scattered') {
17
				$coverage_norm = 4.0/8.0;
18
			} elseif ($metar['cloud'][$key]['type'] == 'Broken') {
19
				$coverage_norm = 6.0/8.0;
20
			} elseif ($metar['cloud'][$key]['type'] == 'Overcast/Full cloud coverage') {
21
				$coverage_norm = 8.0/8.0;
22
			}
23
			$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...
24
			if ($metar['cloud'][$key]['significant'] == 'cirrus') {
25
				$layer_type = 'ci';
26
			} elseif ($alt_ft > 16500) {
27
				$layer_type = 'ci';
28
			} elseif ($alt_ft > 6500) {
29
				$layer_type = 'ac';
30
				if ($pressure < 1005.0 && $coverage_norm >= 0.5) {
31
					$layer_type = 'ns';
32
				}
33
			} else {
34
				if ($cumulus_base * 0.80 < $alt_m && $cumulus_base * 1.20 > $alt_m) {
35
					$layer_type = 'cu';
36
				} elseif ($stratus_base * 0.80 < $alt_m && $stratus_base * 1.40 > $alt_m) {
37
					$layer_type = 'st';
38
				} else {
39
					if ($alt_ft < 2000) {
40
						$layer_type = 'st';
41
					} elseif ($alt_ft < 4500) {
42
						$layer_type = 'cu';
43
					} else {
44
						$layer_type = 'sc';
45
					}
46
				}
47
			}
48
			//echo 'coverage norm : '.$coverage_norm.' - layer_type: '.$layer_type."\n";
49
			$result[] = array('cov' => $coverage_norm, 'type' => $layer_type,'alt' => $alt_m,'rh' => $metar['rh']);
50
		}
51
		if (count($result) < 2 && $metar['rh'] > 60) {
52
			$result[] = array('cov' => 0.75, 'type' => 'ci','alt' => 4000,'rh' => $metar['rh']);
53
		}
54
		return $result;
55
	}
56
	
57
	public function nomad_wind() {
58
		global $globalWindsPath;
59
		if (isset($globalWindsPath) && $globalWindsPath != '') {
60
			$grib2json = $globalWindsPath['grib2json'];
61
			$windpathsrc = $globalWindsPath['source'];
62
			$windpathdest = $globalWindsPath['destination'];
63
		} else {
64
			$grib2json = dirname(__FILE__).'/libs/grib2json/bin/grib2json';
65
			$windpathsrc = dirname(__FILE__).'/../data/winds.gb2';
66
			$windpathdest = dirname(__FILE__).'/../data/winds.json';
67
		}
68
		
69
		// 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
70
		$resolution = '0.5';
71
		$baseurl = 'http://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_'.($resolution === '1' ? '1p00':'0p50').'.pl';
72
		$get = array('file' => 'gfs.t'.sprintf('%02d',(6*floor(date('G')/6))).($resolution === '1' ? 'z.pgrb2.1p00.f000' : 'z.pgrb2full.0p50.f000'),
73
			'lev_10_m_above_ground' => 'on',
74
			'lev_surface' => 'on',
75
			'var_TMP' => 'on',
76
			'var_UGRD' => 'on',
77
			'var_VGRD' => 'on',
78
			'leftlon' => 0,
79
			'rightlon' => 360,
80
			'toplat' => 90,
81
			'bottomlat' => -90,
82
			'dir' => '/gfs.'.date('Ymd').sprintf('%02d',(6*floor(date('G')/6)))
83
		);
84
		$url = $baseurl.'?'.http_build_query($get);
85
		echo $url;
86
		$Common = new Common();
87
		$Common->download($url,$windpathsrc);
88
		system($grib2json.' --data --output '.$windpathdest.' --names --compact '.$windpathsrc);
89
	}
90
91
	public function oscar_wave() {
92
		global $globalWavesPath;
93
		if (isset($globalWavesPath) && $globalWavesPath != '') {
94
			$grib2json = $globalWavesPath['grib2json'];
95
			$wavepathsrc = $globalWavesPath['source'];
96
			$wavepathdest = $globalWavesPath['destination'];
97
		} else {
98
			$grib2json = dirname(__FILE__).'/libs/grib2json/bin/grib2json';
99
			$wavepathsrc = dirname(__FILE__).'/../data/waves.nc';
100
			$wavepathdest = dirname(__FILE__).'/../data/waves.json';
101
		}
102
		
103
		$url = 'https://podaac.jpl.nasa.gov/ws/search/granule/?datasetId=PODAAC-OSCAR-03D01&itemsPerPage=1&sortBy=timeDesc&format=atom&pretty=false';
104
		$Common = new Common();
105
		$oscarlst = $Common->getData($url);
106
		$oscarlst_xml = json_decode(json_encode(simplexml_load_string($oscarlst)),true);
107
		foreach ($oscarlst_xml['entry']['link'] as $oscarlnk) {
108
			if ($oscarlnk['@attributes']['type'] == 'application/x-netcdf') {
109
				$Common->download($oscarlnk['@attributes']['href'],$wavepathsrc.'.gz');
110
				break;
111
			}
112
		}
113
		$Common->gunzip($wavepathsrc.'.gz');
114
		system($grib2json.' --data --output '.$wavepathdest.' --names --compact '.$wavepathsrc);
115
	}
116
}
117
/*
118
require_once('class.METAR.php');
119
$METAR = new METAR();
120
*/
121
/*
122
$themetar = $METAR->getMETAR('LFLL');
123
print_r($themetar);
124
$result = $METAR->parse($themetar[0]['metar']);
125
*/
126
/*
127
$result = $METAR->parse('LFLL 081330Z 01006KT 340V050 9999 FEW020 BKN080 07/01 Q1018 NOSIG');
128
print_r($result);
129
$Weather = new Weather();
130
//print_r($Weather->buildcloudlayer($result));
131
//print_r($Weather->buildcloud('46.3870','5.2941','2000','0.25'));
132
print_r($Weather->generateRandomPoint('46.3870','5.2941','2000'));
133
*/
134
/*
135
$Weather = new Weather();
136
$Weather->nomad_wind();
137
*/
138
/*
139
$Weather = new Weather();
140
$Weather->oscar_wave();
141
*/
142
?>