Page 1 of 2

Celx script that displays RA/Dec for observer

Posted: 25.03.2008, 12:12
by Vincent
The following script displays geocentric coordinates for observer.
(RA in hour/min/sec, Dec in deg/arc min/arc sec)

Code: Select all

-- Title: Display current RA/Dec for observer

KM_PER_LY = 9460730472580.8
KM_PER_AU = 149597870.7
PI = math.pi
degToRad = PI / 180;
J2000Obliquity = 23.4392911 * degToRad

LOOK  = celestia:newvector(0, 0, -1)
earth = celestia:find("Sol/Earth")

-- Convert coordinates from cartesian to polar:
xyz2rtp = function (x, y, z)
   local r = math.sqrt(x * x + y * y + z * z)
   local phi = math.atan2(y, x)
   local theta = math.atan2(math.sqrt(x * x + y * y), z)

   return r, theta, phi
end

-- Return current distance of observer from Earth:
getR = function (obs)
   return earth:getposition():distanceto(obs:getposition())
end

-- Return current RA, Dec for observer:
getRADec = function (obs)
   local base_rot = celestia:newrotation(celestia:newvector(1, 0, 0), -J2000Obliquity)
   local rot = earth:getposition():orientationto(obs:getposition(), LOOK) * base_rot
   local look = rot:transform(LOOK):normalize()
   local r, theta, phi = xyz2rtp(look.x, look.z, look.y)
   local phi = math.mod(720 - math.deg(phi), 360)
   local theta = math.deg(theta)
   if theta > 0 then
      theta = 90 - theta
   else
      theta = (-90 - theta)
   end
   return phi, theta
end

km2Unit =
   function(km)
      local sign, value, units
      if km < 0 then sign = -1 else sign = 1 end
      km = math.abs(km)
      if km > 1e12 then
         value = km / KM_PER_LY
         units = "ly"
      elseif km >= 1e8 then
         value = km/KM_PER_AU
         units = "AU"
      else
         value = km
         units = "km"
      end;
      return string.format("%.2f", sign * value).." "..units
   end

deg2dms = function(deg)
   local   a = math.abs(deg)
  local   d = math.floor(a)
   local   r = (a - d) * 60
   local   m = math.floor(r)
   local   s = (r - m) * 60
   if deg < 0 then d = -d end
   return string.format("%0.0fd %02.0f' %2.0f''",d,m,s)
end

deg2hms =   function(deg)
   local   a = math.abs(deg / 15)
  local   d = math.floor(a)
   local   r = (a - d) * 60
   local   m = math.floor(r)
   local   s = (r - m) * 60
   return string.format("%0.0fh %02.0fm %2.0fs", d, m, s)
end

while true do
   obs = celestia:getobserver()
   obsR = getR(obs)
   obsRA, obsDec = getRADec(obs)
   celestia:print(obsR..' '..obsRA..' '..obsDec)
   if obsR >= 0 then
      -- Display geocentric coordinates for observer:
      obsRStr = km2Unit(obsR)
      obsRAStr = deg2hms(obsRA)
      obsDecStr = deg2dms(obsDec)
      celestia:print("Geocentric coordinates for observer:\nR: "..obsRStr.."\nRA: "..obsRAStr.."\nDec: "..obsDecStr, 1, -1, -1, 1, 7)
   end
   wait(0)
end

Posted: 25.03.2008, 13:21
by Cham
I didn't tested it yet, but this script is a very good idea.

Why not the distance from Earth too ?

Thanks a lot, Vincent ! :D

Posted: 25.03.2008, 16:56
by Vincent
Cham wrote:Why not the distance from Earth too ?
Although I didn't mention it in the title, the script displays the distance from Earth too.

Cham wrote:Thanks a lot, Vincent ! :D

You're welcome, Martin.

Posted: 25.03.2008, 17:07
by chris
Vincent,

One observation: do you know about the atan2 function? It's ideal for converting rectangular to polar coordinates, and can make your xyz2rtp function simpler:

http://lua-users.org/wiki/MathLibraryTutorial

(What you have now works fine, though.)

--Chris

Posted: 25.03.2008, 17:35
by the_aphid
Hello All

Celestia 'Newbie' here. I've had a great time playing around with this amazing program for a little over a year now, but figured that I should try to start learning the scripting, etc. I enjoy writing sci-fi as a hobby, and figured it'd be great to try and create some fictional worlds of my own in Celestia.

So, a script that would tell me my location in space would be great, if I could get it to work! I'm sure that my ignorance is the problem here, and not the celx script you've provided, so perhaps you could tell me what I've done wrong.

I simply copied the script above and created a celx script titled "location.celx" in the scripts folder. When I try to run this script, Celestia crashes.

Thanks in advance! Keep up the great work!

Cheers

Posted: 25.03.2008, 17:40
by Vincent
chris wrote:One observation: do you know about the atan2 function? It's ideal for converting rectangular to polar coordinates, and can make your xyz2rtp function simpler

Chris,

Yes, I know the atan2 function and I used it when coding the C++ compass. Actually, I copied most of the code of the above script from the Lua Tools code, and I wasn't familiar with the atan2 function when I wrote my first lines of code for the Lua Tools. Time goes fast... :wink:

Anyway, thanks for reminding me this. I've edited the script, and the xyz2rtp function looks indeed simplier now.

Posted: 25.03.2008, 17:59
by Vincent
the_aphid wrote:I simply copied the script above and created a celx script titled "location.celx" in the scripts folder. When I try to run this script, Celestia crashes.

the_aphid,

Try downloading the script from this link:
http://vincent.gian.club.fr/celestia/di ... A_Dec.celx

Posted: 25.03.2008, 18:02
by Reiko
Thank you very much! :D :D

Posted: 25.03.2008, 18:13
by Vincent
Reiko wrote:Thank you very much! :D :D

You're welcome!

Posted: 25.03.2008, 18:55
by the_aphid
Vincent wrote:the_aphid,

Try downloading the script from this link:
http://vincent.gian.club.fr/celestia/di ... A_Dec.celx
Thanks Vincent...however Celestia is still crashing on me :?

Posted: 25.03.2008, 19:06
by Vincent
the_aphid wrote:Thanks Vincent...however Celestia is still crashing on me :?

What version of Celestia are you running? On what OS?

Posted: 25.03.2008, 19:40
by the_aphid
Vincent wrote:What version of Celestia are you running? On what OS?
1.5.0 on Mac OS X. Actually I have it on my PC as well, I guess I could try it there. I just never use Celestia on my PC.

Posted: 25.03.2008, 19:45
by Reiko
Works like a charm for me. I really like how you have the distance displayed in light years instead of kiloparsecs. :)

Posted: 25.03.2008, 20:06
by Cham
Vincent,

your script is crashing my Celestia (latest SVN version, on OS X) !

:cry:

Posted: 25.03.2008, 20:14
by Vincent
Cham wrote:Vincent,

your script is crashing my Celestia (latest SVN version, on OS X) !

:cry:

Martin, the_aphid,

This might be due to the use of the UTF-8 degree symbol '?°' in the deg2dms function.
I've edited the script above and replaced this symbol with 'd'.

Let me know if this solves your problem on Mac OSX.

Posted: 25.03.2008, 20:21
by Cham
Vincent wrote:Let me know if this solves your problem on Mac OSX.


Yes, it works now, thanks !

Posted: 25.03.2008, 20:31
by chris
Vincent wrote:
Cham wrote:Vincent,

your script is crashing my Celestia (latest SVN version, on OS X) !

:cry:
Martin, the_aphid,

This might be due to the use of the UTF-8 degree symbol '?°' in the deg2dms function.
I've edited the script above and replaced this symbol with 'd'.

Let me know if this solves your problem on Mac OSX.


Vincent,

Any idea what's causing this problem? Is it a bug in Lua or the Celestia print functions? It seems unlikely that there's a problem in Lua, yet Celestia routinely uses the degree symbol in strings that don't originate form scripts.

--Chris

Re: Celx script that displays RA/Dec for observer

Posted: 26.03.2008, 05:45
by the_aphid
Excellent Vincent! Works great now.
Thanks

Re:

Posted: 26.03.2008, 06:38
by Vincent
chris wrote:Any idea what's causing this problem? Is it a bug in Lua or the Celestia print functions? It seems unlikely that there's a problem in Lua, yet Celestia routinely uses the degree symbol in strings that don't originate form scripts.
Actually, the script was not using UTF-8 encoding, but simply ANSI.
In this case, it looks like the UTF-8 chars are simply skipped on the Windows
(and Linux?) version(s) of Celestia, whereas they make Celestia crash on Mac.

The following version of the script uses the degree symbol and is UTF-8 encoded:
http://vincent.gian.club.fr/celestia/di ... A_Dec.celx
It should work correctly on Mac OS-X, and the degree symbol should be displayed.

-------------------------------------------------------------
Martin, the_aphid, could you please confirm that?
-------------------------------------------------------------

Then, it should be a good thing to have a celx option to print UTF-8 characters, without having to save the whole file in UTF-8 encoding.

Re: Celx script that displays RA/Dec for observer

Posted: 26.03.2008, 13:23
by Cham
Vincent,

yes, it works.

I edited the script to translate it in French, but the UTF8 encoding is really a pain in the butt. Each time I open the file, I'm getting weird characters and have to change them. At least, now it's working.