Page 1 of 1

Script to mark stars by spectral type

Posted: 12.10.2007, 03:11
by chris
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

Posted: 12.10.2007, 19:10
by Cham
Thanks Chris. This is a good one.

Posted: 14.10.2007, 11:49
by Vincent
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)

Posted: 14.10.2007, 12:23
by ElChristou
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?

Posted: 14.10.2007, 12:26
by Vincent
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])...

Posted: 14.10.2007, 13:40
by ElChristou
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...

Posted: 14.10.2007, 14:01
by selden
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.)

Posted: 14.10.2007, 14:08
by selden
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".

Posted: 14.10.2007, 14:11
by t00fri
I think Christophe simply forgot to hit CTRL+k (markers on) before executing the script ;-)

Right?

Cheers,
Fridger

Posted: 14.10.2007, 14:16
by selden
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.

Posted: 14.10.2007, 14:30
by ElChristou
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!