Script to mark stars by spectral type

All about writing scripts for Celestia in Lua and the .cel system
Topic author
chris
Site Admin
Posts: 4211
Joined: 28.01.2002
With us: 22 years 9 months
Location: Seattle, Washington, USA

Script to mark stars by spectral type

Post #1by chris » 12.10.2007, 03:11

I modified a script that Vincent wrote to mark all stars within 100 ly of the current selection. This variation uses the size of the marker to indicate absolute magnitude and the color to indicate spectral type. The usual color conventions for spectral type are used, though white dwarfs are highlighted in magenta and brown dwarfs in blue. Enjoy!

Code: Select all

-- Title: Mark stars by spectral type

-- Mark all stars in a sphere centered on the current selection.
-- Original script by Vincent Gian, modified by Chris Laurel

starcolors = {
    S = "#ff0000";
    M = "#ff4400";
    K = "#ffaa22";
    G = "#cccc33";
    F = "#ffffaa";
    A = "#dddddd";
    B = "#88aaff";
    O = "#4444ff";
    D = "#ff00ff";
    L = "#00ff00";
    T = "#00ff00";
    W = "#0000ff";
}
   
function mark_star_dist(obj, radius)
   for star in celestia:stars() do
      if star:getposition():distanceto(obj:getposition()) <= radius then
    local size = 10.0 - star:absmag()
    local color = starcolors[string.sub(star:spectraltype(), 1, 1)]
    if color == nil then
             star:mark("#404040", "square", size, 0.5)
    else
             star:mark(color, "circle", size, 0.5)
         end
      end
   end
end

celestia:unmarkall()
objname = ""
obj = celestia:getselection()

-- Radius of the sphere (in km):
radius = 1e15

while true do
   obj = celestia:getselection()
   if obj and objname ~= obj:name() then
      objname = obj:name()
      celestia:unmarkall()
      mark_star_dist(obj, radius)
   end
   wait(0)
end


--Chris

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

Post #2by Cham » 12.10.2007, 19:10

Thanks Chris. This is a good one.
"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!"

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

Post #3by Vincent » 14.10.2007, 11:49

Chris,

What about replacing marktype.celx - that is currently included in the distribution script folder - with your script ?

Then, if you're for keeping marktype.celx, here's a revised version that uses the new star iterator:

Code: Select all

-- Title: Mark all stars of a specific spectral type

function mark_spectraltype(x)
    for star in celestia:stars() do
        first, last = string.find(star:spectraltype(), x, 1, true)
        if first == 1 then
            star:mark("#ff99ff", "circle", 5)
        end
    end
end

spectral = "O"
celestia:flash("Marking all " .. spectral .. " stars.")
celestia:unmarkall()
celestia:setrenderflags{markers = true}
mark_spectraltype(spectral)
@+
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

ElChristou
Developer
Posts: 3776
Joined: 04.02.2005
With us: 19 years 9 months

Post #4by ElChristou » 14.10.2007, 12:23

Off topic, sorry, and for sure a noob question: why a copy/paste of a script posted as code always result (for me) in a script that don't run at all in Celestia?
Image

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

Post #5by Vincent » 14.10.2007, 12:26

ElChristou wrote:Off topic, sorry, and for sure a noob question: why a copy/paste of a script posted as code always result (for me) in a script that don't run at all in Celestia?

Christophe,

Try to add a blank line at the end of your script ([Enter])...
@+
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

ElChristou
Developer
Posts: 3776
Joined: 04.02.2005
With us: 19 years 9 months

Post #6by ElChristou » 14.10.2007, 13:40

I've done that, remove the spaces before each CR etc... NOTHING!
Each time I do a copy/paste of a script in code, niet! Now the same script downloaded within a celx file just works fine. I have tried to paste first in several text editor to save the file, but niet, re niet!! I have no idea of what to do...
Image

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #7by selden » 14.10.2007, 14:01

ElChristou,

What editing program do you use when you copy and paste scripts?

The usual cause of a copy-and-pasted script failure is that the final line terminator gets left out, which is why Vincent suggested adding a blank line at the end. However, my impression is that this particular limitation was fixed in v1.5.0pre3.

Another problem I recently learned about is that some people use "word processing" programs and not plain-text editors. Word processing programs often convert some standard ASCII character codes into binary "punctuation marks". Apostrophe ('), quote (") and double hyphen (--) are some that are often damaged this way. In particular, if your editor changes a double-hyphen into an "em dash", this breaks Lua programs. I know that at least one old Mac editor does this every time. (Sorry I can't find its name.)
Selden

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #8by selden » 14.10.2007, 14:08

Suggestion: on a Mac, you should be able to use standard Unix utilities in a command (terminal) window. "cat" should be available. It can be used to create a file directly from cut-and-paste text without modifying any of the character codes.

Code: Select all

cat > file.txt

type a [carriage-return] or [enter] after .txt
paste in the text which is to be put into the file
*including the final line terminator*
and then type a Ctrl-D
which is the Unix end-of-file command.
You will be typing Ctrl-D on a line by itself after the file's final line terminator.

You should be able to use any name for the file, of course, not just "file.txt".
Selden

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 7 months
Location: Hamburg, Germany

Post #9by t00fri » 14.10.2007, 14:11

I think Christophe simply forgot to hit CTRL+k (markers on) before executing the script ;-)

Right?

Cheers,
Fridger
Image

Avatar
selden
Developer
Posts: 10192
Joined: 04.09.2002
With us: 22 years 2 months
Location: NY, USA

Post #10by selden » 14.10.2007, 14:16

Fridger,

ElChristou wrote that
ElChristou wrote: why a copy/paste of a script posted as code always result (for me) in a script that don't run at all in Celestia?

so he's having the problem with other scripts,not just with this particular one.
Selden

ElChristou
Developer
Posts: 3776
Joined: 04.02.2005
With us: 19 years 9 months

Post #11by ElChristou » 14.10.2007, 14:30

t00fri wrote:I think Christophe simply forgot to hit CTRL+k (markers on) before executing the script ;-)

Right?


I'm a big noob sometimes but not at this point!! :mrgreen:

Tx, Selden, your cat command work like a charm! I'm pretty sure you are right about the change of ASCII character codes... As I knew that TextEdit (the base osSX text editor) would probably cause this kind of stuff, I've used SimpleText (which I thought was a plain-text editor), but nope, same problem... Anyway, now I have a solution. Tx again! :D

EDIT: End of off topic.

Tx Chris and Vincent!
Image


Return to “Scripting”