Scripting with GoQat

In the first example, we saw how to create a task list that can call a script. We also looked in detail at the required structure of the script, so that it could communicate with GoQat. Here we will look at another example (possibly more useful than the first one!), but will consider only the interesting parts of the script.

Reading telescope pointing coordinates and exposure times from a text file

Suppose we want to do an automatic survey of the sky, slewing to selected locations and then making a CCD camera exposure for a given length of time, depending on the objects we're studying. One way to do this would be to construct a task list with all the pointing coordinates in 'GoTo' tasks and all the exposure times specified in 'Expose' tasks. But this isn't so straightforward if we regularly want to change some of the pointing coordinates, or add or remove objects from the list. Perhaps we might even want to use a standard list of objects, but select only those that are more than 30 degrees above the horizon at the the current time.

Using scripts with GoQat makes this much easier, because we can store all our pointing coordinates and exposure times in a text file or database, and then retrieve them and pass them to GoQat as required. Suppose we have our coordinates and exposure times in a text file called Database.txt, like this:

07:03:15 -08:24:11 150
08:14:17 -05:47:19 180
08:40:37 +19:37:52 120

These are tab-separated columns containing the RA coordinates, the Dec coordinates and the exposure times in seconds.

Now we need a script that will open the file and return the first record the first time the script is called, the second record the second time the script is called and so on. The script must also indicate to GoQat when all the records have been read. To start with, let's suppose that the filename containing the pointing coordinates is passed to the script in parameter 9. This Perl code will open the file, read the records into an array, and chop the terminating new-line character from each record:

my @data;
open (DATA, $task_params[9]);
@data = < DATA > ;
chomp (@data);
close (DATA);

(It is a bit risky not checking for an error opening the file; ideally the script should return parameter 0 as zero to GoQat, and a message saying that the 'open' statement has failed. This is left as an exercise for the reader!).

Now suppose that the task list passes us the required record number as parameter 0, starting at '1' for the first record. We need to check that the requested record number is less than or equal to the total number of records in the file. If it is, we store the corresponding coordinates and exposure time. Otherwise, we need to indicate to GoQat that there are no more records:

my @fields;
my $record_num = $task_params[0];
if ($record_num <= $#data + 1) {
$_ = $data[$record_num - 1];
@fields = split (/\t+/);
$record_num++;
} else {
$record_num = 0;
}

Remember that Perl arrays begin at element zero, so for record "$record_num" the above code stores the contents of element "$record_num - 1" of the @data array in the $_ variable, and then splits that record at tab characters, storing the RA, Dec and exposure time in elements 0 to 2 of the @fields array. If there are more records to read, $record_num is incremented by one; otherwise we set it to zero, to tell GoQat that there are no more records.

Now all we need to do is pass the results back using these commands inside the 'if ($sync)' block of the script:

open (RESULTS, '>', $results_file) or die "Can't open $results_file";
print (RESULTS "%0 $record_num\n");
if ($record_num) {
print (RESULTS "%1 $fields[0]\n");
print (RESULTS "%2 $fields[1]\n");
print (RESULTS "%3 $fields[2]\n");
print (RESULTS "#Returned record: $fields[0] $fields[1] $fields[2]\n");
}
close (RESULTS);

We return $record_num in parameter 0, and if we've got some values for the RA, Dec and exposure time (i.e. if $record_num isn't zero), we return them as parameters 1, 2, and 3. Finally we return a record (beginning with a hash) for GoQat to write to the log window.

So that's all the Perl code we need; but remember also to include the commands at the beginning of the Perl script to get all the variables from GoQat, and to close the 'if ($sync)' block by creating the file to tell GoQat that the script is done. You can see the example DemoScript.pl in the data folder of GoQat's installation tree for more details.

Suppose we saved this Perl script as ReadData.pl; now we need a task list to call it from GoQat. Here it is:

SetParam 0 1
SetParam 9 /my_file_path/Database.txt
Exec /my_file_path/ReadData.pl
While %0
GoTo %1 %2
Expose TARGET V %3 1 1 1000 1000 1 1 -15
Exec /my_file_path/ReadData.pl
EndWhile

Parameter 1 (the starting record number) is set to 1, and parameter 9 is set to the full file path and name of the database file. Then the script is called for the first time. This returns the RA, Dec and exposure time for the first record in parameters 1, 2, and 3. Parameter 0 (the record number) is incremented to a value of 2, and since that is non-zero, the 'While' loop is executed. This instructs the telescope to go to the given coordinates and make an exposure of the requested length (through a 'V' filter, exposing a 1000-pixel square at -15C). Then the script is called again. This returns the next record and increments the record number (parameter 0) again, resulting in another iteration of the 'While' loop. If there are no more records, the script returns a value of zero for parameter 0: the 'While' loop is no longer executed and task execution stops.

So it really is that simple! If you want to change your selection of objects, just edit Database.txt. You don't need to change anything else. Or maybe you have various target lists saved in different files, in which case you can easily edit the task list using GoQat's in-built task editing options to set parameter 9 to the target list that you want.

Scripting in GoQat is straightforward, immensely flexible and very powerful, enabling you to achieve camera and telescope control and data reduction tasks with ease.