\chapter{GUI}
\label{sec:gui}

In this chapter the usage of the developed GUI application is explained. It was developed to easily download new data, as well as try various algorithms for flare detection and the usage of various functions of the lightkurve python package (\cite{lightkurve}).

\section{Data download}

During the first startup the GUI window (e.g. in figure \ref{fig:full_gui_normal_selection}) is nearly completely empty. An empty local database will be created with SQLite3 (\cite{sqlite}). To show lightcurves, first data needs to be downloaded. For that, the "Add" button has to be clicked. A new dialog window will show. It can be seen in figure \ref{fig:download_new_star_data_gui}. This GUI uses the astroquery (\cite{astroquery}) python package to fetch metadata and download fits files from MAST (Mikulski Archive for Space Telescopes).\\
The first step is to enter the identifier of the star(s). In case of multiple, they have to be seperated by a semicolon. Then the missions from which the data should be fetched can be selected. The supported missions are TESS and Kepler/K2. For Kepler and K2 short and/or long cadence can be selected. The differeces are described in section \ref{sec:intro:space_missions}. After the selection has been made, the fetch button needs to be pressed. The program will then request metadata matching the star identifier(s) and selected missions from the MAST archive. The found mission data is then displayed in the preview field. It can then be downloaded by pressing the "Ok" button. The program the proceeds to download the according fits-files. The filename and TESS sector or Kepler/K2 target table ID is appended to the previously fetched metadata and handed back to the main GUI.\\
There a request to Simbad is made to fetch additional metadata. First the alternative identifiers are fetched from Simbad, which also has a MAIN\_ID attribute. This is to make sure we only have one entry in the local database per star and no duplicates. Afterwards additional data is fetched from Simbad (\cite{simbad}) like spectral type, radial velocity and distance. After the data is gathered, the main identifier, the alternative identifiers, spectral type, rotational velocity and distance (and their respective units) as well as the file path, source (TESS, Kepler or K2), and sector/target table id are saved into a local database using SQLite3 (\cite{sqlite}).

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/download_star_data_gui.png}
  \caption{GUI window for downloading new fit files with astroquery designed using the Qt5 designer shipped with PyQt5 (\cite{pyqt5}). The star identifier field supports one or more (separated by a semicolon) identifiers. There are checkboxes to select TESS, Kepler (long and/or short cadence) and K2 (long and/or short cadence) data. The preview field shows metadata (Identifier, TESS quarter or Kepler/K2 target table ID) to check the data which was found and which can be downloaded. The "Ok" button then proceeds to download the data and hands information back to the main GUI.}
  \label{fig:download_new_star_data_gui}
\end{figure}

\section{Data display and manipulation}

Afterwards the list of stars seen in figure \ref{fig:full_gui_normal_selection} will be refreshed. It shows the via Simbad determined main identifier alphabetically sorted.\\
When one of the stars in the list is selected, the program will show the additional saved informations. This includes all known IDs (in the Alt. IDs list), the spectral type, its rotational velocity and its distance. Additionally a fit type for the folded lightcurves which are described in section \ref{sec:data:data_reduction}. The default for this is "sine". The list to the right of the information section shows the available fit files. The formating for the list entries is "TESS - <sequence>" and "Kepler/K2 - <target table id>". The combine button allows to show multiple fit files at once if multiple are selected. Select one or combining multiple fit files, enables the plot options.\\
The main plot options allow the user to change the type of flux thats plotted. This is either "SAP\_FLUX" (simple aperture photometry) or "PDCSAP\_FLUX" (presearch data conditioning simple aperture photometry) (\cite{lightkurve}). The PDCSAP\_FLUX has parts of its data removed depending on the quality flags (e.g. indicating systematic errors like an attitude tweak or a cosmic ray (\cite{tess_science_data_products}, \cite{kepler_archive_manual})) as well as corrected long-term brightness changes (\cite{lightkurve}). An example of a PDCSAP\_FLUX is shown in figure \ref{fig:full_gui_normal_selection}.\\
The lightkurve python package also supports normalizing (to unscaled, percent, parts per thousands (ppt) or parts per million (ppm)) which can be seen in figure \ref{fig:full_gui_normal_selection_normalize_options}. Flattened lightcurves (see figure \ref{fig:full_gui_normal_selection_flattened} as an example for $BD-08\ 995$) is used to identify flares. The "remove outliers", "remove nans/infs" and "bin" features are exposed in the GUI, but are not actively used in this thesis.\\
The checkboxes to flatten the lightcurve (figure \ref{fig:full_gui_normal_selection_flattened}), fold the lighhtcurve (figure \ref{fig:full_gui_normal_selection_folded}) and showing the periodogram (figures \ref{fig:full_gui_normal_selection_periodogram_lombscargle} and \ref{fig:full_gui_normal_selection_periodogram_boxleastsquares}) are exclusive of each other. Only one can be used at a time.

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/full_gui_normal_selection.png}
  \caption{Main GUI window. This window was designed with the Qt5 designer shipped with PyQz5 (\cite{pyqt5}). It shows a list of all available stars with their main identifier (taken from SIMBAD (\cite{simbad})) sorted alphabetically. The "Star infos" box contains additional information on the star gathered from SIMBAD. There is a list of alternative IDs, the spectral type, the roational velocity, and the distance. It also shows the prefered fit type for folding lightcurves. On the right of the information box is a list of all available lightcurves, with an option to combine multiple to plot at once. To the right of the list of available lightcurves are multiple different plot options, which utilize the different capabilities of the lightkurve python package (\cite{lightkurve}). The values for "Period" and "Epoch Time" are automatically calculated and used as default for the selected lightcurve. In this image the TESS Sector 32 lightcurve of $BD-08\ 995$ is shown. The red crosses show the peaks of the found flares, while the red overall over the lightcurve indicates the duration of the flare.}
  \label{fig:full_gui_normal_selection}
\end{figure}

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/full_gui_normal_selection_show_quality.png}
  \caption{Main GUI window. Same as in figure \ref{fig:full_gui_normal_selection}, with the difference that the data quality flag is also shown.}
  \label{fig:full_gui_normal_selection_show_quality}
\end{figure}

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/full_gui_normal_selection_normalize_options.png}
  \caption{Main GUI window. Same as in figure \ref{fig:full_gui_normal_selection}, with the difference that the lightcurve was normalized. It also shows all possible normalize options the lightkurve python package (\cite{lightkurve}) supports.}
  \label{fig:full_gui_normal_selection_normalize_options}
\end{figure}

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/full_gui_normal_selection_flattened.png}
  \caption{Main GUI window. Showing the same lightcurve as figure \ref{fig:full_gui_normal_selection}, with the difference that it is now flattened. The green line shows a fit of the flare. This is described in chapter \ref{sec:data:data_reduction}.}
  \label{fig:full_gui_normal_selection_flattened}
\end{figure}

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/full_gui_normal_selection_folded.png}
  \caption{Main GUI window. Showing the same lightcurve as figure \ref{fig:full_gui_normal_selection}, with the difference that it is now folded with an period of ~2.697 days. Additionally vertical lines are shown which indicate the region around the minima and maxima of the folded lightcurve respectively.}
  \label{fig:full_gui_normal_selection_folded}
\end{figure}

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/full_gui_normal_selection_periodogram_lombscargle.png}
  \caption{Main GUI window. Showing the periodogram of the lightcurve in figure \ref{fig:full_gui_normal_selection} using the lombscargle algorithm. Furthermore it shows the four highest peaks (which are atleast 100 datapoints separated). The method is explained in chapter \ref{sec:data:data_reduction}.}
  \label{fig:full_gui_normal_selection_periodogram_lombscargle}
\end{figure}

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/full_gui_normal_selection_periodogram_boxleastsquares.png}
  \caption{Main GUI window. Showing the periodogram of the lightcurve in figure \ref{fig:full_gui_normal_selection} using the box least square algorithm. Similarly to figure \ref{fig:full_gui_normal_selection_periodogram_lombscargle} the four highest peaks are marked with a red "x".}
  \label{fig:full_gui_normal_selection_periodogram_boxleastsquares}
\end{figure}

\FloatBarrier

\section{Data processing \label{sec:gui:data_processing}}

Pressing the button "New FC" will start the processing of all fits files in the current loaded database. It will sequentially go through all files and apply the flare detection algorithms to the lightcurve, as well as generate the periodogram and optimized folded lightcurves. This is done for the PDCSAP\_FLUX. Additionally plots for each individual lightcurve and processing step are generated. The included data includes:

\begin{itemize}
  \item Flares (peak, timestamp, datapoint index in the lightcurve, TESS/Kepler data quality flags)
  \item Number of flares
  \item Fits for the flares (fit, timestamps, indices in the lightcurve, TESS/Kepler data quality flags)
  \item Uninterrupted timespans in the data
  \item Total length of timespans in seconds
  \item Rotational period
  \item Spot modulation period
  \item Folded lightcurve phase range
  \item Folded lightcurve flare positions (phase, peak)
  \item Period which was used to fold the lightcurve
  \item Additional boundaries around minima and maxima
\end{itemize}

And in the case the period and spot modulation of the star differs, it will seperately safe the folded lightcurves and related data for the stars period as well.

After this process is done, a new window will open. It allows to show multiple different statistics like flares per file, flares per star (total, or normalized to per 7 day period) or mean periods for each star. This window can be seen in figure \ref{fig:summary_statistics_window}.
The data can then be saved using the pandas (\cite{pandas}) "to\_pickle" functions and also loaded again using the "read\_pickle" function.
