Celx script that displays RA/Dec for observer

All about writing scripts for Celestia in Lua and the .cel system
Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Celx script that displays RA/Dec for observer

Post #1by Vincent » 25.03.2008, 12:12

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
Last edited by Vincent on 25.03.2008, 20:14, edited 5 times in total.
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Avatar
Cham M
Posts: 4324
Joined: 14.01.2004
Age: 60
With us: 20 years 10 months
Location: Montreal

Post #2by Cham » 25.03.2008, 13:21

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
"Well! I've often seen a cat without a grin", thought Alice; "but a grin without a cat! It's the most curious thing I ever saw in all my life!"

Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #3by Vincent » 25.03.2008, 16:56

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.
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

chris
Site Admin
Posts: 4211
Joined: 28.01.2002
With us: 22 years 9 months
Location: Seattle, Washington, USA

Post #4by chris » 25.03.2008, 17:07

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

the_aphid
Posts: 5
Joined: 25.03.2008
With us: 16 years 8 months
Location: Vancouver, Canada

Post #5by the_aphid » 25.03.2008, 17:35

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

Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #6by Vincent » 25.03.2008, 17:40

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.
Last edited by Vincent on 25.03.2008, 18:01, edited 1 time in total.
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #7by Vincent » 25.03.2008, 17:59

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
Last edited by Vincent on 25.03.2008, 18:04, edited 1 time in total.
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Reiko
Posts: 1119
Joined: 05.10.2006
Age: 41
With us: 18 years 1 month
Location: Out there...

Post #8by Reiko » 25.03.2008, 18:02

Thank you very much! :D :D

Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #9by Vincent » 25.03.2008, 18:13

Reiko wrote:Thank you very much! :D :D

You're welcome!
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

the_aphid
Posts: 5
Joined: 25.03.2008
With us: 16 years 8 months
Location: Vancouver, Canada

Post #10by the_aphid » 25.03.2008, 18:55

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 :?

Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #11by Vincent » 25.03.2008, 19:06

the_aphid wrote:Thanks Vincent...however Celestia is still crashing on me :?

What version of Celestia are you running? On what OS?
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

the_aphid
Posts: 5
Joined: 25.03.2008
With us: 16 years 8 months
Location: Vancouver, Canada

Post #12by the_aphid » 25.03.2008, 19:40

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.

Reiko
Posts: 1119
Joined: 05.10.2006
Age: 41
With us: 18 years 1 month
Location: Out there...

Post #13by Reiko » 25.03.2008, 19:45

Works like a charm for me. I really like how you have the distance displayed in light years instead of kiloparsecs. :)

Avatar
Cham M
Posts: 4324
Joined: 14.01.2004
Age: 60
With us: 20 years 10 months
Location: Montreal

Post #14by Cham » 25.03.2008, 20:06

Vincent,

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

:cry:
"Well! I've often seen a cat without a grin", thought Alice; "but a grin without a cat! It's the most curious thing I ever saw in all my life!"

Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Post #15by Vincent » 25.03.2008, 20:14

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

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Avatar
Cham M
Posts: 4324
Joined: 14.01.2004
Age: 60
With us: 20 years 10 months
Location: Montreal

Post #16by Cham » 25.03.2008, 20:21

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


Yes, it works now, thanks !
"Well! I've often seen a cat without a grin", thought Alice; "but a grin without a cat! It's the most curious thing I ever saw in all my life!"

chris
Site Admin
Posts: 4211
Joined: 28.01.2002
With us: 22 years 9 months
Location: Seattle, Washington, USA

Post #17by chris » 25.03.2008, 20:31

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

the_aphid
Posts: 5
Joined: 25.03.2008
With us: 16 years 8 months
Location: Vancouver, Canada

Re: Celx script that displays RA/Dec for observer

Post #18by the_aphid » 26.03.2008, 05:45

Excellent Vincent! Works great now.
Thanks

Topic author
Vincent
Developer
Posts: 1356
Joined: 07.01.2005
With us: 19 years 10 months
Location: Nancy, France

Re:

Post #19by Vincent » 26.03.2008, 06:38

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.
@+
Vincent

Celestia Qt4 SVN / Celestia 1.6.1 + Lua Edu Tools v1.2
GeForce 8600 GT 1024MB / AMD Athlon 64 Dual Core / 4Go DDR2 / XP SP3

Avatar
Cham M
Posts: 4324
Joined: 14.01.2004
Age: 60
With us: 20 years 10 months
Location: Montreal

Re: Celx script that displays RA/Dec for observer

Post #20by Cham » 26.03.2008, 13:23

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.
"Well! I've often seen a cat without a grin", thought Alice; "but a grin without a cat! It's the most curious thing I ever saw in all my life!"


Return to “Scripting”