Jump to content

Manipulating data [need some help]


php2010

Recommended Posts

What I'm basically doing is grabbing data from a source and I want to display it in a different way. A preview of the original data is below:

 

 
CODED SURFACE FRONTAL POSITIONS
NWS HYDROMETEOROLOGICAL PREDICTION CENTER CAMP SPRINGS MD
626 PM EDT WED NOV 03 2010

VALID 110321Z
HIGHS 1022 4120673 1028 6431205 1037 5121174 1022 7090423 1038 4801098 1030
7571450 1022 3471278 1034 4051133 
LOWS 993 5320344 966 5391454 997 6230330 999 5240802 1010 3420760 983 6500819
1005 4610855 1006 2940904 
OCFNT 2940905 2960901 2980896 2990888 
STNRY 3420761 3320775 3250786 3120797 3060803 2990809 2960827 2960845 2980865
3000875 
WARM 2990890 3000884 3000874 
COLD 2990889 2920886 2860886 2740887 2550890 2300898 2050915 1900925 1790928 
TROF 3370890 3230894 3150899 3030905 2960905 
OCFNT 5350359 5450351 5510329 5500297 5430258 
OCFNT 5381446 5311448 5301459 5351468 5441476 5551479 5631473 5681464 
WARM 5681463 5741440 5751407 5641362 
COLD 5681464 5651448 5571426 5411395 5101369 4621361 4181372 3911400 
WARM 5240803 5260801 5300797 
WARM 3420760 3370753 3220744 3110732 
COLD 6310816 6070824 5710847 5390887 5260940 5331010 5471037 5701058 
STNRY 5691057 5791069 5931103 6061141 6101175 6101228 6211269 6261311 6171372 
OCFNT 6480822 6540806 6550778 6500744 6400726 6320724 
COLD 6320724 6190730 6010743 5650771 5470785 5300798 
WARM 6330724 6200704 5830682 
COLD 5240803 5090814 4840835 4740842 4660850 
WARM 4600857 4630853 4670849 
TROF 5400425 5190425 5030426 
COLD 4590856 4510867 4390888 4330920 
TROF 4490863 4290871 4110884 
TROF 4280784 4190795 4140809 4080821 4050830 3980846 3870859 3780871 
TROF 6190558 5990522 5680487 5520465 5480454 
TROF 6790829 7110827 7470810 7810750 
TROF 4511195 4621185 4751161 4831138 
TROF 5891623 6091652 6331660 6571662 6781674 6961718 

$$

 

I will take a few lines from that and explain what I'd like to do.

 

HIGHS 1022 4120673 1028 6431205 1037 5121174 1022 7090423 1038 4801098 1030
7571450 1022 3471278 1034 4051133

 

I would like to pull the "1022" (MB) and "4120673" (LAT/LON), then "1028" (MB) and "6431205" (LAT/LON), etc. and display them like this:

 

Icon: 41.2, -67.3,000,1,1,1022
Icon: 64.3, -120.5,000,1,1,1028

 

Basically:

Icon: LAT, LON,000,1,1,MB

 

"LOWS" would be displayed the same way as "HIGHS" above.

 

 

 

"WARM", "COLD", "STNRY", "OCFNT", and "TROF" would be displayed differently as you can see the data for these is different. Those would be displayed like this:

 

From:

COLD 5681464 5651448 5571426 5411395 5101369 4621361 4181372 3911400 

 

To:

Line: 3,0,"Cold Front"
56.8, -146.4
56.5, -144.8
55.7, -142.6
54.1, -139.5
51.0, -136.9
46.2, -136.1
41.8, -137.2
39.1, -140.0
End:

 

For a better understanding of what this data represents you can visit this website. They explain everything nicely. I'm playing around with "explode" at the moment but I'm clueless as the direction to go to achieve all these different things. Any help will be greatly appreciated.

Link to comment
Share on other sites

You could try it like this:

 

if (strpos($line, "HIGHS") === 0) {
  list($highs, $mb1, $latlon1, $mb2, $latlon2) = explode(" ", $line);
}

 

That's assuming there are always the same number of MB and LATLON in each line like that.  If not, then it'll be a bit more complex.

Link to comment
Share on other sites

You could try it like this:

 

if (strpos($line, "HIGHS") === 0) {
  list($highs, $mb1, $latlon1, $mb2, $latlon2) = explode(" ", $line);
}

 

That's assuming there are always the same number of MB and LATLON in each line like that.  If not, then it'll be a bit more complex.

 

Thanks for your response. Unfortunately I'm not sure if the amount of MD and LAT/LON points are static. Hopefully they are. :P I will play around with this coding now thanks. Do you have any idea how to do the others? (cold, warm, ocfnt, trof etc.) As those values fluctuate a bunch. :-\

Link to comment
Share on other sites

If the number of items is variable, here's one way you could do it:

 

$exploded = explode(" ", $line);
$exploded_length = count($exploded);
for ($i = 1; $i < $exploded_length; $i += 2) {
  print "MD: " . $exploded[$i] . " LATLON: " . $exploded[$i+1] . "\n";
}

 

That gives you access to each item in turn, which you can either print right away or store into an array.  Starting at index 1 skips the "HIGHS" at the start of the line, and the $i += 2 makes it jump 2 entries each time.

Link to comment
Share on other sites

If the number of items is variable, here's one way you could do it:

 

$exploded = explode(" ", $line);
$exploded_length = count($exploded);
for ($i = 1; $i < $exploded_length; $i += 2) {
  print "MD: " . $exploded[$i] . " LATLON: " . $exploded[$i+1] . "\n";
}

 

That gives you access to each item in turn, which you can either print right away or store into an array.  Starting at index 1 skips the "HIGHS" at the start of the line, and the $i += 2 makes it jump 2 entries each time.

 

Thanks. This may seem like a silly question, but how would I call this remote file? I'm currently using fopen and a dummy file I just made with the content I posted in the original post titled "fff.txt". I'm not sure how to setup the variables to link it together to read the data. Should it be something like:

 

$file = fopen('fff.txt','r');	
while($content = fread($file,102465)){ $line .= $content; }
fclose($file);

 

? :shrug: I'm relatively new to exploding data and calling external files.  :P

Link to comment
Share on other sites

I got it to finally explode the data with what you posted btherl but the output was kinda strange:

 

MD: 
CODED LATLON: SURFACE
MD: FRONTAL LATLON: POSITIONS
NWS
MD: HYDROMETEOROLOGICAL LATLON: PREDICTION
MD: CENTER LATLON: CAMP
MD: SPRINGS LATLON: MD
626
MD: PM LATLON: EDT
MD: WED LATLON: NOV
MD: 03 LATLON: 2010

MD: 
VALID LATLON: 110321Z
HIGHS
MD: 1022 LATLON: 4120673
MD: 1028 LATLON: 6431205
MD: 1037 LATLON: 5121174
MD: 1022 LATLON: 7090423
MD: 1038 LATLON: 4801098
MD: 1030
7571450 LATLON: 1022
MD: 3471278 LATLON: 1034
MD: 4051133 LATLON: 
LOWS
MD: 993 LATLON: 5320344
MD: 966 LATLON: 5391454
MD: 997 LATLON: 6230330
MD: 999 LATLON: 5240802
MD: 1010 LATLON: 3420760
MD: 983 LATLON: 6500819
1005
MD: 4610855 LATLON: 1006
MD: 2940904 LATLON: 
OCFNT
MD: 2940905 LATLON: 2960901
MD: 2980896 LATLON: 2990888
MD: 
STNRY LATLON: 3420761
MD: 3320775 LATLON: 3250786
MD: 3120797 LATLON: 3060803
MD: 2990809 LATLON: 2960827
MD: 2960845 LATLON: 2980865
3000875
MD: 
WARM LATLON: 2990890
MD: 3000884 LATLON: 3000874
MD: 
COLD LATLON: 2990889
MD: 2920886 LATLON: 2860886
MD: 2740887 LATLON: 2550890
MD: 2300898 LATLON: 2050915
MD: 1900925 LATLON: 1790928
MD: 
TROF LATLON: 3370890
MD: 3230894 LATLON: 3150899
MD: 3030905 LATLON: 2960905
MD: 
OCFNT LATLON: 5350359
MD: 5450351 LATLON: 5510329
MD: 5500297 LATLON: 5430258
MD: 
OCFNT LATLON: 5381446
MD: 5311448 LATLON: 5301459
MD: 5351468 LATLON: 5441476
MD: 5551479 LATLON: 5631473
MD: 5681464 LATLON: 
WARM
MD: 5681463 LATLON: 5741440
MD: 5751407 LATLON: 5641362
MD: 
COLD LATLON: 5681464
MD: 5651448 LATLON: 5571426
MD: 5411395 LATLON: 5101369
MD: 4621361 LATLON: 4181372
MD: 3911400 LATLON: 
WARM
MD: 5240803 LATLON: 5260801
MD: 5300797 LATLON: 
WARM
MD: 3420760 LATLON: 3370753
MD: 3220744 LATLON: 3110732
MD: 
COLD LATLON: 6310816
MD: 6070824 LATLON: 5710847
MD: 5390887 LATLON: 5260940
MD: 5331010 LATLON: 5471037
MD: 5701058 LATLON: 
STNRY
MD: 5691057 LATLON: 5791069
MD: 5931103 LATLON: 6061141
MD: 6101175 LATLON: 6101228
MD: 6211269 LATLON: 6261311
MD: 6171372 LATLON: 
OCFNT
MD: 6480822 LATLON: 6540806
MD: 6550778 LATLON: 6500744
MD: 6400726 LATLON: 6320724
MD: 
COLD LATLON: 6320724
MD: 6190730 LATLON: 6010743
MD: 5650771 LATLON: 5470785
MD: 5300798 LATLON: 
WARM
MD: 6330724 LATLON: 6200704
MD: 5830682 LATLON: 
COLD
MD: 5240803 LATLON: 5090814
MD: 4840835 LATLON: 4740842
MD: 4660850 LATLON: 
WARM
MD: 4600857 LATLON: 4630853
MD: 4670849 LATLON: 
TROF
MD: 5400425 LATLON: 5190425
MD: 5030426 LATLON: 
COLD
MD: 4590856 LATLON: 4510867
MD: 4390888 LATLON: 4330920
MD: 
TROF LATLON: 4490863
MD: 4290871 LATLON: 4110884
MD: 
TROF LATLON: 4280784
MD: 4190795 LATLON: 4140809
MD: 4080821 LATLON: 4050830
MD: 3980846 LATLON: 3870859
MD: 3780871 LATLON: 
TROF
MD: 6190558 LATLON: 5990522
MD: 5680487 LATLON: 5520465
MD: 5480454 LATLON: 
TROF
MD: 6790829 LATLON: 7110827
MD: 7470810 LATLON: 7810750
MD: 
TROF LATLON: 4511195
MD: 4621185 LATLON: 4751161
MD: 4831138 LATLON: 
TROF
MD: 5891623 LATLON: 6091652
MD: 6331660 LATLON: 6571662
MD: 6781674 LATLON: 6961718
MD: 
LATLON: 
$$


 

Something is wrong here. :o

Link to comment
Share on other sites

Add this line back in, from my first post:

 

if (strpos($line, "HIGHS") === 0) {
   .... explode and other stuff
}

 

That will make it so it processes only lines starting with "HIGHS"

 

As for reading the file, line by line is best:

 

$file = fopen('fff.txt','r');	
while($line = fgets($file)) {
            if (strpos($line, "HIGHS") === 0) {
                $line = chop($line); # Remove newline from end of line
                $exploded = explode(" ", $line);
                $exploded_length = count($exploded);
                for ($i = 1; $i < $exploded_length; $i += 2) {
                    print "MD: " . $exploded[$i] . " LATLON: " . $exploded[$i+1] . "\n";
                }
            }
        }
fclose($file);

 

 

Link to comment
Share on other sites

Thanks! That separates the HIGHS and LOWS but how would i convert the lat/lon to the format I need?

 

from:

5121174 to 51.2, -117.4

4120673 to 41.2, -67.3

 

etc.

 

I'm going to play around with this code to try to figure out the COLD, WARM, OCFNT, etc. to see if I understand this any. :shy:

 

Edit: I just noticed a problem. The HIGHS and LOWS have line breaks in them:

 

HIGHS 1022 4120673 1028 6431205 1037 5121174 1022 7090423 1038 4801098 1030
7571450 1022 3471278 1034 4051133 

 

After 1030 so it calls the MB but doesnt pick up the 7571450 latlon and everything after it.  :-\

Link to comment
Share on other sites

Oh dear.. those line breaks complicate things.  There's 2 approaches I would use in that situation:

 

1.  Pre-process the file, sticking together broken lines into full lines, so all the HIGHS are on a single line.  Then process as before.

2.  Use a state based approach - Once you see the "HIGHS" line, remember that you are currently processing HIGHS.  Then when you get a new line you can continue reading more HIGHS data in.  Given that the line break occurs between a MD and a LATLON this approach is going to be difficult, so I would go with approach 1.

 

For approach 1, using the rule "If we have seen the VALID line and the current line starts with a digit, append it to the previous line", the code is:

 

$lines_arr = array();
$cur_line = null;
$seen_valid = false;
while ($line = fgets($file)) {
  if (!$seen_valid) {
    if (strpos($line, 'VALID') === 0) {
      $seen_valid = true;
    }
    continue; # Jumps up to the top of the "while"
  }

  if (!$seen_valid) continue; # Skip lines before we see the "VALID" line

  if (ctype_alpha($line{0})) {
    # Line starts with letter
    if ($cur_line !== null) {
      $lines_arr[] = $cur_line;
    }
    $cur_line = chop($line); # Making sure to remove newline with chop()
  } elseif (ctype_digit($line{0})) {
    # Line start with digit.  Append to previous line
    $cur_line .= ' ' . chop($line); # Making sure to remove newline with chop().  And adding a space to separate the values.
  }
}

foreach ($lines_arr as $line) {
   # In here you can do processing on full lines, no need to worry about line breaks.
}

Link to comment
Share on other sites

  • 2 weeks later...

I ran into a little problem. For some reason it isn't catching the last line of the source. Like right now this is the data:

 

 
CODED SURFACE FRONTAL POSITIONS
NWS HYDROMETEOROLOGICAL PREDICTION CENTER CAMP SPRINGS MD
825 PM EST SAT NOV 13 2010

VALID 111400Z
HIGHS 1021 3340813 1022 3840775 1033 4291214 1028 5070752 1022 3350963 
LOWS 1004 4450907 1002 6481006 998 6591355 1000 6111579 1016 3531047 984
7300598 987 6590579 994 6070493 1003 5391451 
COLD 2950919 2830927 2690940 2580948 2480957 2360963 2280969 2190976 
OCFNT 4450907 4470903 4480897 4490889 4470881 4450875 4420869 4380865 4310862 
COLD 4310862 4230860 4150859 4040860 3890864 3650874 3340890 3120904 2950917 
COLD 5570390 5260437 5040484 4930534 4880553 4780574 4660596 4550615 4450632
4400646 4350662 4320675 4300708 4290724 4300743 4300758 
WARM 6471004 6430977 6360941 6290920 6210904 6090890 
COLD 6461009 6361021 6261036 6161056 6061083 6001111 5981134 5981156 6011184
6111211 
WARM 6581351 6531316 6411275 6301247 6111212 
STNRY 6581358 6581389 6531419 6441439 6381463 6361484 6331516 6261548 6161570
6111578 
COLD 6081577 5961575 5801578 5581592 
TROF 5651150 5511133 5291112 5061099 4851092 4531083 
TROF 4151241 3991234 3821225 3641214 3471201 3281183 
TROF 4061025 3891029 3691036 3531047 3431059 3361075 3341097 
TROF 6580581 6880580 7160588 7420610 7610643 7840636 
OCFNT 6070495 5950457 5840429 5690405 5580390 
TROF 6110506 6300523 6490544 6590570 
TROF 6480589 6040572 5760556 5480538 
WARM 5331359 5081315 4801288 
COLD 5901588 5901588 
WARM 5570390 5380369 5250357 5150351 
COLD 5331359 5231356 5111363 5011378 4891400 4801417 4691439 
OCFNT 5391450 5431427 5421393 5341360 
TROF 6861739 6821697 6721653 6601628 6471610 
STNRY 4300759 4270782 4200808 4160820 4170829 4180834 4210838 4260842 4290845
4300850 4300854 4310858 4310861 

$$

 

and it's not pulling and and making the "STNRY" line. :shrug:

Link to comment
Share on other sites

Sorry, I forgot to mention one thing - at the end of the loop you will have one unprocessed line in $cur_line, so you need to add that final line into the array.  I often forget to do that myself when using this programming pattern.  So at the end:

 

if ($cur_line !== null) $lines_arr[] = $cur_line;

Link to comment
Share on other sites

This is where preg_match can show its power :)

 

Not exactly the output format you wanted, but that can always be changed. as the hard part is done :)

 


<?php

preg_match_all('@^([A-Z]{4,5})(?:\s+(?:\d{3,4}\s+\d{7,7}|\d{7,7}))+@m',
	file_get_contents('sample.data'), /* change sample.data to your data file */
	$matches,PREG_SET_ORDER);
foreach($matches as $val)
{
	$val[0]=explode(' ',str_replace(array('  ',"\r","\n"),' ',$val[0]));
	array_shift($val[0]);
	$step=($val[1]=='HIGHS' || $val[1]=='LOWS')?2:1;
	echo "{$val[1]}: ";
	for($i=0;$i<count($val[0]);$i+=$step)
	{
		$lonlan=intval($val[0][$i+($step-1)]);
		$lon=intval(substr($lonlan,0,3))/10;
		$lan=intval(substr($lonlan,3))/10;
		echo "{$lon}, {$lan}". ($step==2?",000,1,1,{$val[0][$i]}":'') . "\r\n ";
	}
}

Link to comment
Share on other sites

Sorry, I forgot to mention one thing - at the end of the loop you will have one unprocessed line in $cur_line, so you need to add that final line into the array.  I often forget to do that myself when using this programming pattern.  So at the end:

 

if ($cur_line !== null) $lines_arr[] = $cur_line;

 

I've placed this code at the end and no errors have been reported. I will post back if anything else goes wrong. Thanks a bunch!

 

This is where preg_match can show its power :)

 

Not exactly the output format you wanted, but that can always be changed. as the hard part is done :)

 


<?php

preg_match_all('@^([A-Z]{4,5})(?:\s+(?:\d{3,4}\s+\d{7,7}|\d{7,7}))+@m',
	file_get_contents('sample.data'), /* change sample.data to your data file */
	$matches,PREG_SET_ORDER);
foreach($matches as $val)
{
	$val[0]=explode(' ',str_replace(array('  ',"\r","\n"),' ',$val[0]));
	array_shift($val[0]);
	$step=($val[1]=='HIGHS' || $val[1]=='LOWS')?2:1;
	echo "{$val[1]}: ";
	for($i=0;$i<count($val[0]);$i+=$step)
	{
		$lonlan=intval($val[0][$i+($step-1)]);
		$lon=intval(substr($lonlan,0,3))/10;
		$lan=intval(substr($lonlan,3))/10;
		echo "{$lon}, {$lan}". ($step==2?",000,1,1,{$val[0][$i]}":'') . "\r\n ";
	}
}

 

Thanks for this code! I will def play around with this. What I'm currently doing is this:

 

if (strpos($line, "HIGHS") === 0) {
			$line = chop($line);
                $explodedhp = explode(" ", $line);
                $explodedhp_length = count($explodedhp);
                for ($i = 1; $i < $explodedhp_length; $i += 2) {
					$lathp[$i] = substr($explodedhp[$i+1], -7, 3);
					$lonhp[$i] = substr($explodedhp[$i+1], -4, 4);
					$lathp2[$i] = substr($explodedhp[$i+1], -7, 3) - 1.5;
					$lonhp2[$i] = substr($explodedhp[$i+1], -4, 4);
                    echo "Object: ".($lathp[$i]/10).", -".($lonhp[$i]/10)."\nThreshold:999\nColor: 255 100 000\n";
				echo "Icon: 0,0, 0, 2, 1, ",$explodedhp[$i]."MB\n";
				echo "Text: 0, -30, 1, \"".$explodedhp[$i]."MB\"\nEnd:\n\n";
                }echo "\n\n";
            }

 

The same basically for Lows except with a different color, etc.

 

The only other somewhat weird problem I'm having is alternating the colors of the stationary front lines. What I would like to do is like have this:

 

lat1,lon1 to lat2,lon2 - RED LINE
lat2,lon2 to lat3,lon3 - BLUE LINE

 

On and on until it reaches no more lat lon points. So 4 lat points would make: --- --- ---

 

and an output of something like:

 

Color: 255 000 000
Line: 6,0,"Stationary Front at 9 PM"
31.1, -87.1
30.4, -88.2
End:
Color: 000 000 255
Line: 6,0,"Stationary Front at 9 PM"
30.4, -88.2
30.1, -88.9
End:
Color: 255 000 000
Line: 6,0,"Stationary Front at 9 PM"
31.1, -87.1
30.4, -88.2
End:

 

I'm currently achieving this but with a few errors below:

 

if (strpos($line, "STNRY") === 0) {

			$line = chop($line);
                $explodedstnry = explode(" ", $line);
                $explodedstnry_length = count($explodedstnry);
                for ($i = 1; $i < $explodedstnry_length; $i++) {
				$latstnry[$i] = substr($explodedstnry[$i], -7, 3);
				$lonstnry[$i] = substr($explodedstnry[$i], -4, 4);}


				if ($explodedstnry % 2 == 1)
					{for ($i = 1; $i < $explodedstnry_length - 1; $i+= 2) {
					if ($lonstnry[$i+2] != null){
				echo "Color: 255 000 000\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
                    echo ($latstnry[$i]/10) . ', -' . ($lonstnry[$i]/10) . "\n";
				echo ($latstnry[$i+1]/10) . ', -' . ($lonstnry[$i+1]/10) . "\n";
				echo "End:\n";
				echo "Color: 000 000 255\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
				echo ($latstnry[$i+1]/10) . ', -' . ($lonstnry[$i+1]/10) . "\n";
				echo ($latstnry[$i+2]/10) . ', -' . ($lonstnry[$i+2]/10) . "\n";
				echo "End:\n";}
					if ($latstnry[$i+2] == null && $lonstnry[$i+1] != null){
				echo "Color: 255 000 000\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
                    echo ($latstnry[$i]/10) . ', -' . ($lonstnry[$i]/10) . "\n";
				echo ($latstnry[$i+1]/10) . ', -' . ($lonstnry[$i+1]/10) . "\n";
				echo "End:\n";

				}else{

				for ($i = 1; $i < $explodedstnry_length - 2; $i+= 2) {
					if ($lonstnry[$i+2] != null){
				echo "Color: 255 000 000\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
                    echo ($latstnry[$i]/10) . ', -' . ($lonstnry[$i]/10) . "\n";
				echo ($latstnry[$i+1]/10) . ', -' . ($lonstnry[$i+1]/10) . "\n";
				echo "End:\n";
				echo "Color: 000 000 255\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
				echo ($latstnry[$i+1]/10) . ', -' . ($lonstnry[$i+1]/10) . "\n";
				echo ($latstnry[$i+2]/10) . ', -' . ($lonstnry[$i+2]/10) . "\n";
				echo "End:\n";}
					if ($latstnry[$i+2] == null && $lonstnry[$i+1] != null){
				echo "Color: 255 000 000\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
                    echo ($latstnry[$i]/10) . ', -' . ($lonstnry[$i]/10) . "\n";
				echo ($latstnry[$i+1]/10) . ', -' . ($lonstnry[$i+1]/10) . "\n";
				echo "End:\n";}}

				}}
                }echo "\n\n";

 

Every now and then there will be more than 1 stationary front and if it's an odd amount of lat/lon points it will connect the ODD stationary front to the following stationary front. I tried to stop this with an if statement of:

 

if ($explodedstnry % 2 == 1)

 

Basically saying IF ODD then "$explodedstnry_length - 1" ELSE "$explodedstnry_length - 2" which makes sense right? :P The math of this is kind of confusing for me looking at all this code combined together.

 

I don't know if there's an easier way to achieve what I want to do or not but so far this has been a pain and I have to keep changing it to keep it working.  :shrug:

Link to comment
Share on other sites

Ok so the last line is solved .. I'm a bit lost in that latest code you posted.  Can I recommend using some functions?  For example:

 

function format_latlong($lat1, $long1, $lat2, $long2, $timect, $colour) {
    $str = '';
    $str .= "Color: $colour\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
    $str .= ($lat1/10) . ', -' . ($lon1/10) . "\n";
    $str .= ($lat2/10) . ', -' . ($lon2/10) . "\n";
    $str .= "End:\n";

    return $str;
}

print format_latlong($latstnry[$i+1], $lonstnry[$i+1], $latstnry[$i+2], $lonstnry[$i+2], $timect, $colour);

 

Alternatively you could do the division by 10 outside the function.  Either way, I think it will make the code more readable, which means it's easier to understand, and therefore easier to debug :)

 

I'm also a bit lost in the logic flow because of the indenting.  If you increase indent every time you enter an "if", "else" or "for", then it makes the logic much clearer.

Link to comment
Share on other sites

Alright it looks like I only have one more problem left with this. I need to be able to count how many lat lon points there are for EACH STNRY and make $difference = 2 if it's odd and $difference = 1 if it's even that way I can use $difference to subtract from $explodedstnry_length so it outputs the exact amount of lines I need. Like this:

 

for ($i = 1; $i < $explodedstnry_length - $difference; $i+= 2) {

					if ($lonstnry[$i+2] != null){
				echo "Color: 255 000 000\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
                    echo ($latstnry[$i]/10) . ', -' . ($lonstnry[$i]/10) . "\n";
				echo ($latstnry[$i+1]/10) . ', -' . ($lonstnry[$i+1]/10) . "\n";
				echo "End:\n";
				echo "Color: 000 000 255\nLine: 6,0,\"Stationary Front at ".$timect."\"\n";
				echo ($latstnry[$i+1]/10) . ', -' . ($lonstnry[$i+1]/10) . "\n";
				echo ($latstnry[$i+2]/10) . ', -' . ($lonstnry[$i+2]/10) . "\n";
				echo "End:\n";}

 

Is this possible?

 

Edit I didn't notice you had replied sorry. I haven't had any experience at all with functions but I will take it into consideration and play with it a bit. :)

 

Edit 2: I just tried to run the functions and I get this:

 

<b>Fatal error</b>:  Cannot redeclare format_latlong() (previously declared in fronts_fuctions.php:59) in <b>fronts_fuctions.php</b> on line <b>59</b><br />

 

:shrug:

Link to comment
Share on other sites

I have the code for the odd/even but the problem is, it takes the answer from the FIRST STNRY points (for $difference) and uses that same value for the second and so on. So when the first STNRY line is even, it says that the second line is even as well which is not a desirable output.

 

if ($explodedstnry_length & 1) {$difference = 1;}
if ($explodedstnry_length & 0) {$difference = 2;}

 

That is the code I'm using before echoing the Line statements. :-\

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.