Page 1 of 1

help for scripting needed

Posted: 05.03.2010, 08:39
by lucaspazio
Dear All, I'm new to Celestia world.
I'm sorry if this topic has already been treated, but I could not find in previosu discussion.

I'm trying to print in Celestia the content of a txt file

I wanted to use the io.open function in order to read the txt file. This is the basic code I use

Code: Select all

propagationFile = assert(io.open("propagation.txt", "r"))
propagation= propagationFile:read()
io.close()


but anything I get is an error saying
with I put assert that no such file or directory (the error is [String "...path..."]:4: propagation.txt: No such file or directory)
when I do not put assert something like attempt to call field 'open' (a nil value)

Is there any special grammar to be used to open the file or to declare where the file resides that I did not get? The file is in the same repository than the script, which is nested into the extra folder of Celestia.

Thanks to anybody willing to provide me a clue.

Re: help for scripting needed

Posted: 05.03.2010, 08:57
by Vincent
Hi,

Lua uses the celestia base directory (= package.path) to look for files.
So you need to add the path of your running script to the text filename:

Code: Select all

scriptpath = celestia:getscriptpath()
filename = scriptpath.."/../".."propagation.txt"

propagationFile = assert(io.open(filename, "r"))
propagation= propagationFile:read()
io.close(propagationFile)

Re: help for scripting needed

Posted: 05.03.2010, 09:50
by lucaspazio
Thanks for lua hints, now it seems to work properly. I think some of the issues were coming from the path and from the fat I've to use thecelestia:requestsystemaccess() function

Re: help for scripting needed

Posted: 05.03.2010, 15:43
by lucaspazio
Ok the work has continued, but I still find some difficulties.

I ask the script to read the content of a file through the f:read("*n") if it is numeric and through the f:read("*l") functions.
Some time due the fact I don't know the number of lines contained in the file I try some recursive figures through the for do end loop
The first time to insert in a vector part of the content of a file
The second time to print the content of a file to Celestia

Both times the grammar I use is

Code: Select all

celestia:print(file:read("*l"),duration)

both times nothing happen, Celestia does not print anything.

in case I replace as

Code: Select all

celestia:print(tostring(file:read("*l")),duration)

Celestia print

Code: Select all

nil


Here the code

Code: Select all

celestia:requestsystemaccess()
wait(0)

--start determining the scripth path to deduct the path to other files
scriptpath = celestia:getscriptpath()
--determination of the path to the propagation.txt file where some data are contained
filename=string.gsub(scriptpath,"test.celx","save\\propagation.txt")
propagationFile = assert(io.open(filename, "r"))

--the first 3 rows contain the begin date, the duration of the script, the time step
begin=propagationFile:read("*n")
duration=propagationFile:read("*n")
step=propagationFile:read("*n")
--the fourth contains the number of targets
target_ns=propagationFile:read("*n")
--the fifth onwards contain the name of the targets
--I try to create a table containing in each row the target name
--reading the table through the command f:read("*l") due the non numeric format
target_t={}
for j = 1,target_ns do
   target_t[j]=propagationFile:read("*l")
   celestia:print(target_t[j],5)
   wait(5)   
end

propagationFile:close()
--in the infomission.txt the spacecraft info is contained
--the first row contains the name of the s/c, the second the object around which the s/c orbits
filename=string.gsub(filename,"propagation","infomission")
INFO=assert(io.open(filename,"r"))
name=INFO:read("*l")
planet=INFO:read("*l")
INFO:close()

target=celestia:find(planet.."/"..name)
celestia:select(target)
obs = celestia:getobserver()
rad_targ=target:radius()*61

obs:gotodistance(target, rad_targ, 2.0)
wait(10)
scale=duration/180

obs:center(target)
obs:track(target)
wait(10)

--the file Mission_Timeline contains in each row a text that shall appear at each timestep in Celestia
filename=string.gsub(filename,"INFO","Mission_Timeline")
MissionTimeline=assert(io.open(filename,"r"))

celestia:settimescale(scale)
celestia:settime(begin)

--this part extracts from MissionTimeline the text in each row and show it in celestia
for line in MissionTimeline:lines() do

guidance=MissionTimeline:read("*l")
celestia:print(guidance,step/scale)
wait(step/scale)

end

MissionTimeline:close()


Again thanks to anyone willing to debugme :)

Re: help for scripting needed

Posted: 06.03.2010, 09:33
by Vincent
You may want to try using the Lua line iterator:

Code: Select all

myFile = assert(io.open(filename, "r"))
for line in myFile:lines() do
    celestia:print(line)
    wait(5)
end

io.close(myFile)

Re: help for scripting needed

Posted: 08.03.2010, 15:01
by lucaspazio
Thanks.

now it works pretty well. I think I solved all issues related to teh i/o side.

Re: help for scripting needed

Posted: 08.03.2010, 16:24
by lucaspazio
I enter now in troubles related to Celestia itself.
I was wondering what is the celx command analogue to the "rightclick"+ Reference Vectors/Show Sun Direction.

Thanks

Re: help for scripting needed

Posted: 08.03.2010, 17:07
by Vincent
lucaspazio wrote:I enter now in troubles related to Celestia itself.
I was wondering what is the celx command analogue to the "rightclick"+ Reference Vectors/Show Sun Direction.
You'll find some help on the Celestia wiki Celx/Lua scripting page:
http://en.wikibooks.org/wiki/Celestia/C ... erencemark