3

I'm trying to port a script from R into PHP but not sure what lines 3 and 4 (taken from the larger function discussed here) are doing. Looks like logical operations and array definition at the same time. Can someone please give me the equivalent in PHP?

cosAzPos <- (0 <= sin(dec) - sin(el) * sin(lat))
sinAzNeg <- (sin(az) < 0)
az[cosAzPos & sinAzNeg] <- az[cosAzPos & sinAzNeg] + twopi
az[!cosAzPos] <- pi - az[!cosAzPos]
Community
  • 1
  • 1
Owen
  • 7,347
  • 12
  • 54
  • 73
  • pls provide some possible values for variables. we need to know their type – Electronick May 16 '12 at 19:38
  • @Electronick -- To provide context, I just added a link to the source of the code to the post above. – Josh O'Brien May 16 '12 at 19:43
  • @Electronick -- IIRC, the variables `dec` (declination), `el` (elevation), `lat` (latitude), and `az` (azimuth) will all be real numbers between -2*pi and 2*pi. – Josh O'Brien May 16 '12 at 19:50
  • @Josh - would you mind explaining the logic in those last two lines?? – Owen May 16 '12 at 20:22
  • 1
    @Owen -- Sure. `az` is a vector of real numbers representing angles in radians. The last line, says, essentially, "take those values of `az` whose cosines are not positive (the left-hand side); and replace them (the `<-`); with `pi` minus that same set of values". As an example, it will take a vector of numbers like `1.3 1.4 1.5 1.6 1.7 1.8` and return `1.3 1.4 1.5 1.541593 1.441593 1.341593`. – Josh O'Brien May 16 '12 at 20:37
  • Thanks Josh. Will finish this tomorrow. Looking forward to testing it out. I'm hoping it compares well with the calculator at http://aa.usno.navy.mil/data/docs/AltAz.php – Owen May 16 '12 at 21:28
  • @Owen -- No problem. If you do get it up and running, you could update your post with a brief report on the results of the comparison. I for one would be interested to learn more about how it performs. – Josh O'Brien May 17 '12 at 14:40
  • @JoshO'Brien - I ended up installing R on my XP machine and printing out vars from your script one by one till I got it exactly the same! I'm about to put it through some test coords/dates, but initial results look excellent. – Owen May 17 '12 at 18:42

1 Answers1

2

I think it looks something like:

if (0 < sin($dec) - sin($el) * sin($lat)) {
  if(sin($az) < 0)
    $az = $az + $twopi;
} 
else {
  $az = $pi - $az;
}

Only for lines 3-4:

if ($cosAzPos && $sinAzNeg) {
  $az = $az + $twopi;
}
elseif (!$cosAzPos) {
  $az = $pi - $az;
}
else {
  // leave $az value
}

according to commet I found in referenced post. But I not sure about accessing indexes in float

Electronick
  • 1,122
  • 8
  • 15