Astro tools, Software

Converting DSLR RAW images into scientific FITS format. Part 1: UI

FITS – is an open standard defining a digital file format useful for storage, transmission and processing of scientific and other images. FITS is the most commonly used digital file format in astronomy. Unlike many image formats, FITS is designed specifically for scientific data and hence includes many provisions for describing photometric and spatial calibration information, together with image origin metadata.  (Wikipedia (c)).
Most of the astronomical software and hardware produces and working only with FITS files .
But there is a big amount of astrophotographers and amateurs astronomers (actually not all is really amateurs 🙂 ) who using only modern DSLR cameras and shooting in RAW format.
Modern DSLR cameras can be used for serious science, like searching for variable stars or astrometry of some objects. But only way to do that – convert RAW files to the FITS format for future processing in a astronomical software.
I wrote a little program that solves this problem and allow  easily convert files in a batch mode.

In this article i want to describe in details how it works.

Part 2: working with LIBRAW

Program is written in pure C with GTK 3 user interface.
All external dependencies is only LibRaw (latest stable versions from official site is much preferable than versions in distros) and cfitsio.
LibRaw is used for reading images and metada from the RAW files and supports a great amount of cameras vendors and models.
Cfits is a popular C library for working FITS files.

Source code is available here: Github Raw2Fits

Mainly designed for Linux, but i believe that it can be compiled and successfully run on OS X and even Windows OS’s. All question is in build procedure and dependencies resolution.

Program is under active development and improvements so some of the points at this article may be deprecated, sorry.

Here is user interface.

Very simple.

All you need is just set input and output directories, fill up some text fields and press “Start” button.
All entered data will be written into FITS headers according to FITS file standard.

All conversions is doing in background threads, number of this threads depends on count of your cpu or cores.

User interface

Is made using Glade designed. It’s simple and intuitive (mostly 🙂 ) Here is how it looks in designer.

Click on the image to see full resolution.

If you want learn about UI development with Glade start with this articles: https://prognotes.net/2016/03/gtk-3-c-code-hello-world-tutorial-using-glade-3/
This is a good start point.

Here i will say only that Glade doesn’t produce any code. Only one generated output is a custom XML file with description of all your windows and UI components.

This file is loading dynamically in your program with GTK function gtk_builder_add_from_file ()

Very quick example.

#include <stdio.h>
#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GError *err = NULL;
    GtkWidget *window;
    GtkBuilder *builder;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();

    if (!gtk_builder_add_from_file (builder, "ui.glade", &err)) {
        perror(err->message);
        g_object_unref(builder);
        return -1;
    }

    window = GTK_WIDGET(gtk_builder_get_object(builder, "windowname"));

    gtk_widget_show(window);
    gtk_main();

    g_object_unref(builder);

    return 0;
}

In this example you got a GtkBuilder object with all UI schema loaded using gtk_builder_add_from_file ()
Now you can get any element from this schema by it name using gtk_builder_get_object (), for example some widget named “windowname”.

Please check out file src/main.c in my source tree to discover how i loading and accessing different types of components – buttons, text inputs and so on.

Another interesting note about GTK UI is that only one (main) thread is allowed to update UI.

In my case i has a multiple threads which reporting about conversion progress and printing some messages that should be shown somewhere in UI.
But how to do UI updates from this background threads without ruining GTK runtime?
Good solution is use GTK idle source with attached callbacks.
Conception is very simple.
Somewhere in your threaded function you should create GSource object using g_idle_source_new () the with functiong_source_set_callback(gsource, functionname, arg, NULL) you can create callback to function functioname with your custom argument arg.
Then call g_source_attach(gsource, context) where context is a global GMainContext object you should initialize somewhere in your main thread with g_main_context_default()
After that you functionname with argument arg will be called in the main thread.
This function may implement some UI updates and arg may be some data, that should be displayed.

GMainContext *main_context;

/* this function is "called" by my_threaded_function but really executed in the main thread */
static gboolean main_thread_func(void *arg)
{
    // do_some_ui_stuff_with_arg(arg)

    return G_SOURCE_REMOVE;  // don't forget to return this value!
}

/* this function is executed in another thread */
void my_threaded_function(void *data)
{
    GSource *source = g_idle_source_new();

    g_source_set_callback(source, main_thread_func, arg, NULL);
    g_source_attach(source, main_context);
    g_source_unref(source);
}

int main()
{
    ...

    main_context = g_main_context_default();
}

Please check out logger_msg_preformat () or progress_update () in src/main.c to see how it works.

Will be continued…

 

Tagged , ,

1 thought on “Converting DSLR RAW images into scientific FITS format. Part 1: UI

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.