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

In this chapter the usage of the newly developed GUI application is explained. It was developed to easily download new data, as well as to try various algorithms for flare detection and the usage of various functions of the $lightkurve$ python package \citep{lightkurve}. The GUI is written in python 3 \citep{10.5555/1593511} and uses the PyQt5 \citep{pyqt5} framework. 

\section{Data download}
\label{sec:gui: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 \citep{sqlite}. To show lightcurves, data need to be downloaded first. For that, the "Add" button has to be clicked and a new dialog window appears. This can be seen in figure \ref{fig:download_new_star_data_gui}. This GUI uses the astroquery \citep{astroquery} python package to fetch metadata and download flexible image transport system (fits) files from the Mikulski Archive for Space Telescopes (MAST).

The first step is to enter the identifier of the star(s). In case of multiple identifiers, 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 differences 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 then proceeds to download the corresponding "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 have 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 \citep{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 as an unique identifier into a local database using SQLite3 \citep{sqlite}. For Kepler/K2 the target table ID was chosen instead of the quarter, as the target table ID is a unique number, whereas there can be multiple "fits" files for the same star and the same quarter.

\begin{figure}[pt!]
  \centering
  \includegraphics[width=.9\linewidth]{gui/download_star_data_gui.png}
  \caption{GUI window for downloading new "fits" files with astroquery designed using the Qt5 designer shipped with PyQt5 \citep{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 sector 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}
\label{sec:gui:data_display}

Afterwards the list of stars seen in figure \ref{fig:full_gui_normal_selection} will be refreshed. It shows the alphabetically sorted main identifier determined from SIMBAD.

When one of the stars in the list is selected, the program will show the additionally saved information. This includes all known identifiers for the star (categorized as alternative identifiers in the Alt. IDs list seen in the "Star infos" section of the GUI), the spectral type, its rotational velocity and its distance (from SIMBAD). Additionally a preferred fitting type for the folded lightcurves (which are described in section \ref{sec:data:data_reduction}) can be selected. The default value for this is "sine". The list to the right of the information section shows the available "fits" files. The formating for the list entries is "TESS - <sequence>" and "Kepler/K2 - <target table id>". The "combine" button combines temporary multiple "fits" files into one, which is then plotted. Selecting one or combining multiple "fits" files, enables the plot options.

The main plot options allow the user to change the type of flux that is plotted. This is either "SAP\_FLUX" (simple aperture photometry) or "PDCSAP\_FLUX" (presearch data conditioning simple aperture photometry) \citep{lightkurve} with the latter being the default option. 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 \citep{tess_science_data_products,kepler_archive_manual}) as well as corrected long-term brightness changes \citep{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$) are used to identify flares. The "remove outliers", "remove nans/infs" and "bin" features are available in the GUI, but are not actively used in this thesis.

The checkboxes to flatten the lightcurve, meaning removing all longterm trends like spot/rotational modulation but leaving short term events intact (figure \ref{fig:full_gui_normal_selection_flattened}), folding the lightcurve (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. The values for "Period" and "Epoch Time" are automatically calculated and used as default for the selected lightcurve. If "Optimize" in the "Fold" options is unchecked, the "Epoch Time" represents the first found minimum in the lightcurve, and the "Period" is set to the period of the highest peak found in the Lomb-Scargle periodogram. If "Optimize" is checked, it will use the found rotational period of the optimize fold algorithm described in \ref{sec:data:data_reduction:lightcurve_folding}. If "Show Spot Modulation" is selected, it will use the found spot modulation period instead of the rotational period (see section \ref{sec:data:data_reduction:lightcurve_folding}).

\begin{landscape}
\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 PyQt5 \citep{pyqt5}. It shows a list of all available stars with their main identifier (taken from SIMBAD) sorted alphabetically. The "Star infos" box contains additional information on the star gathered from SIMBAD. The "Sequences/Target Table ID" box shows the list of all available lightcurves for the selected star, with an option to combine multiple "fits" files to plot at once. To the right of the list of available lightcurves are different plot options, which utilize the different capabilities of the lightkurve python package \citep{lightkurve}. In this image the TESS Sector 32 lightcurve of $BD-08\ 995$ is shown. The red crosses show the peaks of the detected flares, while the red solid lines the lightcurve indicate the duration estimate of the flare.}
  \label{fig:full_gui_normal_selection}
\end{figure}
\end{landscape}

\begin{landscape}
\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 (black solid line) is also shown.}
  \label{fig:full_gui_normal_selection_show_quality}
\end{figure}
\end{landscape}

\begin{landscape}
\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 \citep{lightkurve} supports.}
  \label{fig:full_gui_normal_selection_normalize_options}
\end{figure}
\end{landscape}

\begin{landscape}
\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 lines show a fit of the flares. This is described in chapter \ref{sec:data:data_reduction}.}
  \label{fig:full_gui_normal_selection_flattened}
\end{figure}
\end{landscape}

\begin{landscape}
\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 a period of ~2.697 days.}
  \label{fig:full_gui_normal_selection_folded}
\end{figure}
\end{landscape}

\begin{landscape}
\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 Lomb-Scargle algorithm. Furthermore it shows the four highest peaks (which are at least separated by 100 datapoints). The method is explained in chapter \ref{sec:data:data_reduction}.}
  \label{fig:full_gui_normal_selection_periodogram_lombscargle}
\end{figure}
\end{landscape}

\begin{landscape}
\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}
\end{landscape}

\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 described in section \ref{sec:data:data_reduction} to the lightcurves, 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 data provides:

\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 save the folded lightcurves and related data for the stars period as well.

After this process is finished, a new window will open. It allows to show multiple different debugging statistics like flares per file, flares per star (total, or normalized to a 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 by clicking "File" and then "Save As", which uses the pandas \citep{pandas} "to\_pickle" function. Similarly it can be loaded again using the "read\_pickle" function which is called either by "File" and then "Open" in the debugging statistics window, or by clicking "Open" in the main window next to "New FC".

\begin{figure}[pt!]
  \includegraphics[width=\linewidth]{gui/statistics_summary_window.png}
  \caption{Debugging statistics window. It has the option to filter data by sources and spectral type for Kepler/K2/TESS pdcsap flux data. It has the ability to show various statistics like flares per "fits" file, flares per star (total or normalized to a period of 7 days), or the mean period detected. The figure shows statistics generated by pressing the "Show Flares/File" button.}
  \label{fig:summary_statistics_window}
\end{figure}