[PHP] Re: Extracting data from exec() call
On 06/14/2011 12:35 PM, Ashley M. Kirchner wrote: I'm trying to extract data coming from an exec() call. The exec() call is performing an ncdump on a netCDF file: ?php $filename = mlab.20110101.cdf; exec(/usr/bin/ncdump -l 2048 -v wmax .$filename, $output); echo ---\n; print_r($output); echo ---\n; ? This is all fine, however the data comes in with extra information I don't need, and the data I do need is in a long string format: Array ( [0] = netcdf mlab.20110101 { [1] = dimensions: [2] = time = UNLIMITED ; // (288 currently) [3] = variables: [4] = int base_time ; [5] = int samp_secs ; [6] = float lat ; [7] = float lon ; [8] = float alt ; [9] = int station ; [10] = float time_offset(time) ; [11] = float tdry(time) ; [12] = float rh(time) ; [13] = float pres(time) ; [14] = float cpres0(time) ; [15] = float dp(time) ; [16] = float wdir(time) ; [17] = float wspd(time) ; [18] = float wmax(time) ; [19] = float wsdev(time) ; [20] = float wchill(time) ; [21] = float raina(time) ; [22] = float raina24(time) ; [23] = float bat(time) ; [24] = data: [25] = [26] = wmax = 5, 5, 5, 5.4, 5.7, 5.1, 7.1, 6.1, 4.4, 9.5, 13.5, 14.4, 11.1, 9.6, 10.5, 10.6, 11.1, 10, 15.3, 17.2, 16.9, 16, 13.9, 16.7, 15.3, 18.3, 17.2, 16.3, 15.9, 18.9, 17.7, 19.7, 19.7, 16.6, 16.4, 16, 14.6, 14.9, 14, 16.7, 18.3, 16.2, 18.4, 15.4, 15.3, 13.3, 14.8, 15.6, 15.1, 14.7, 13.8, 14.2, 18, 16.7, 16.6, 15.6, 15.8, 17.6, 17.4, 20.3, 17.4, 21.3, 18.7, 16.7, 15.4, 17.5, 17.3, 17.7, 20.4, 17.5, 16.8, 18.1, 15.9, 17.8, 17.3, 13.9, 16.2, 17.4, 19.8, 17.5, 19, 20, 20.3, 20.4, 20.8, 21.6, 26.4, 23.1, 21.3, 19.9, 20.1, 21.1, 22.1, 22.5, 21.2, 20.2, 22.2, 21.6, 19.6, 21.1, 21.7, 21, 20.8, 23.7, 26.6, 21.2, 23.8, 23.3, 23.5, 23.9, 21.4, 22.1, 23.5, 22.8, 23, 21.8, 22.2, 25.6, 21.9, 22.8, 26.2, 24.2, 23.6, 25.5, 26.8, 25.6, 27.4, 24.5, 23.5, 22.1, 23.1, 25.5, 22.5, 22.5, 23.8, 21.8, 22.6, 23.5, 24, 23, 22.3, 21.6, 23.6, 20.5, 21, 19.4, 23.6, 19.2, 18, 19.2, 20.5, 19.3, 19.1, 20.4, 18, 17.8, 18.4, 19.4, 21, 18.3, 20.4, 21.2, 22.1, 20.1, 17.3, 17.2, 17.6, 17.2, 19.9, 21, 20.6, 21.5, 23.8, 25, 24, 23.9, 23.5, 27.7, 21.1, 20.8, 20.1, 18.6, 21, 18.7, 19.9, 16.4, 16.3, 17.3, 11.8, 11.4, 15.2, 12.4, 14.2, 16.3, 21.6, 25, 19, 7.7, 9.7, 15.7, 7, 11.1, 10.3, 11.2, 14.8, 11.7, 5.5, 5.5, 5.5, 6.9, 7.6, 6.5, 6.4, 6.7, 6.7, 4.9, 5.1, 4.7, 5.7, 5.5, 9.2, 6.2, 7.1, 7.7, 6.5, 5.5, 6, 6.4, 7, 6.2, 6.7, 4.7, 3.8, 9.2, 8.1, 7.6, 6.8, 6.3, 7.1, 10.5, 8, 9.9, 10.2, 12, 9.4, 14, 18, 11.9, 17.7, 20.9, 18.8, 17.4, 13.5, 13.8, 12.6, 5.5, 6.1, 6.6, 6.3, 6.3, 5.1, 7.7, 5.8, 5.4, 4.9, 4.5, 4.9, 4.7, 5, 4.8, 4.5, 5.6, 5.9, 4.6, 5.7, 7.2, 6.1, 6.4, 5.4, 6.1, 5.6, 5.9, 6.4, 9.5, 11.2, 15.8, 15, 13.6 ; [27] = } ) ncdump doesn't provide an option to suppress the header information, and as you can hopefully tell, the data I'm looking for is in one long string at key position 26. How can I extract what I need from that (wmax) and have it as an array: wmax = array(5, 5, ...) This is only one variable. I will be pulling more variables out, and they all come out the same way as above, as strings. The headers might change, so I can't simply say ignore the first 0-25 keys and expect what I need to be at position 26. I need a reliable way of detecting where the data section starts. And I need to convert the strings into arrays for each data set. Suggestions? Assuming that it is always the second to the last item, then: $wmax = explode(', ', $output[count($output)-1]); array_shift($wmax); print_r($wmax); --or to search for wmax = if($array = preg_grep('/^ wmax = $/', $output)) { $wmax = explode(', ', $array[0]); } array_shift($wmax); print_r($wmax); May need some more error checking. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Extracting data from exec() call
On 06/14/2011 01:03 PM, Shawn McKenzie wrote: $wmax = explode(', ', $output[count($output)-1]); Should be -2 :( $wmax = explode(', ', $output[count($output)-2]); -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Extracting data from exec() call
On 6/14/2011 12:03 PM, Shawn McKenzie wrote: --or to search for wmax = if($array = preg_grep('/^ wmax = $/', $output)) { $wmax = explode(', ', $array[0]); } array_shift($wmax); print_r($wmax); May need some more error checking. Yeah, error checking ... Can't search for '/^ wmax $/' because it just returns nothing. Dropping the '$' at the end works. The array_shift() also drops the first variable because the explode() call breaks it up as: wmax = 5, 5, 5.4 ... etc. First value = 'wmax = 5' And the explode also fails because data is not in $array[0], it's in $array[30]. Inserting an array_values() to reindex helps. So as of right now, I'm looking at: if ($array = preg_grep('/^ wmax = /', $output)) { $array = array_values($array); $wmax = explode(', ', $array[0]); } array_shift($wmax); print_r($wmax); The array_shift() call needs to shift out the 'wmax = ', but leave the first value. And, I still need to convert the whole thing into '$wmax = {5, 5, 5.4, ...}'. Right now it's an array of strings: Array ( [0] = wmax = 5 [1] = 5 [2] = 5 [3] = 5.4 ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Extracting data from exec() call
On 06/14/2011 01:30 PM, Ashley M. Kirchner wrote: if ($array = preg_grep('/^ wmax = /', $output)) { $array = array_values($array); $wmax = explode(', ', $array[0]); } Sorry, didn't have a way to test when I posted. So you've posted two different expected outputs and I misread the first. You want a string of only the coma separated numbers? If so: if ($array = preg_grep('/^ wmax = /', $output)) { $wmax = array_shift($array); } If you want to get rid of the wmax = or add the { } or whatever use str_replace() and/or concat then on. -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Extracting data from exec() call
On Jun 14, 2011, at 1:30 PM, Ashley M. Kirchner wrote: On 6/14/2011 12:03 PM, Shawn McKenzie wrote: --or to search for wmax = if($array = preg_grep('/^ wmax = $/', $output)) { $wmax = explode(', ', $array[0]); } array_shift($wmax); print_r($wmax); May need some more error checking. Yeah, error checking ... Can't search for '/^ wmax $/' because it just returns nothing. Dropping the '$' at the end works. The array_shift() also drops the first variable because the explode() call breaks it up as: wmax = 5, 5, 5.4 ... etc. First value = 'wmax = 5' And the explode also fails because data is not in $array[0], it's in $array[30]. Inserting an array_values() to reindex helps. So as of right now, I'm looking at: if ($array = preg_grep('/^ wmax = /', $output)) { $array = array_values($array); $wmax = explode(', ', $array[0]); } array_shift($wmax); print_r($wmax); The array_shift() call needs to shift out the 'wmax = ', but leave the first value. And, I still need to convert the whole thing into '$wmax = {5, 5, 5.4, ...}'. Right now it's an array of strings: Array ( [0] = wmax = 5 [1] = 5 [2] = 5 [3] = 5.4 ) Seems what's best here is to do two explodes: $matches = preg_grep('/^\s*wmax/',$inputdata); if (count($matches) != 1) die(More or less than one wmax in data set); $wmaxline = end($matches); list($var,$data) = explode(' = ',$wmaxline); $data = substr($data, 0, -2); $wmax = explode(', ',$data); print_r($wmax); which gives me the result of an array of data values. Array ( [0] = 5 [1] = 5 [2] = 5 [3] = 5.4 [4] = 5.7 [5] = 5.1 [6] = 7.1 [7] = 6.1 [8] = 4.4 [9] = 9.5 --%- [278] = 5.4 [279] = 6.1 [280] = 5.6 [281] = 5.9 [282] = 6.4 [283] = 9.5 [284] = 11.2 [285] = 15.8 [286] = 15 [287] = 13.6 ) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php