\documentclass[../../book.tex]{subfiles}
\graphicspath{{\subfix{.}}}


\begin{document}

\chapter{Input/Output}

It is going to be extremely rare that a Python program is going to be completely self-contained. Most of the time, you are going to be wanting to work with data, and this means that we are going to need to \textit{input} this data into our program.  Also, once you have worked up the data as you wish, you will need to \textit{output} the results to where you want it, in a format in which you want it. 

This desire for input and output is not unique to science, and so it should come as no surprise that there are many different ways to handle this in Python.  In this chapter we will look at a few of the xxx that we believe will be most common. This chapter is not intended to be exhaustive, but to introduce the basic ideas and point to resources for implementing them.

\subsection{I/O involving dynamic human interaction}
Imagine you had a UV-vis spectrum, and you wrote a program that would report the absorption value at a particular wavelength.  If this program is to be general, it would not have the wavelength built in.  Instead, it would ask the user for the wavelength of interest, at the time the program was run. 

There are many ways this could be done:
\begin{itemize}
    \item One could make a GUI interface, where there is a text box that a user could input this value.  See chapter X for more information on using GUIS.

    \item One could use the command line interface, to ask the user for this input.  For instance, you could output text that says `What is the wavelength of interest?' and then wait for the user to supply the number. This can be using using the built-in Python function \lstinline{input()}. To see this in action, see Chapter X.
\end{itemize}

\subsubsection{File IO}

\subsubsection{File input}
There are times that you need to pull information from an existing file. For instance, you might need to read in spectral data that you collected or perhaps you want to access the color data contained in an image. Though not quite as dynamic as discussed above, a program can also for input on how to run using a file, thus files provide a means of human interaction with a program. 

When reading data from a file, the type of file will dictate how you want to read it.  There are number of specialized functions and libraries that have been written for different types of data. For instance, if you have a file that is largely tabulated data of the same length (i.e., a spectrum), then functions like Numpy's \lstinline{genfromtxt()} is a quick and easy way to read the file and assign the data to Numpy arrays. Chapter X introduces this use. If you have an image file, then libraries like Pillow provide functions to read them, as we show in Chapter X. Even instruments that produce proprietary file formats will sometimes have Python functions that allow you to read the file. The easiest way to find out if this is the case is to perform a google search for the name of the file type, or the file extension.  For instance, the company Bruker will save files in the `BEST3T' format. If you perform a search for how to read Bruker BEST3T files in Python, you will find code that people have created to do so. Perhaps the most general way to open a file is to the the \lstinline{with open(<file>, 'r') as <alias>:} construction.  This is introduced in Chapter X. 

Once a program has run, you might want a persistent record of the processes, any errors encountered, or the final results. Perhaps you normalized spectroscopic data and want to send this to someone.  Perhaps you plotted a spectrum, and want to save this as an image file. Again, libraries like Python and Pillow provide means to easily write files from data and produces images from data.  These are shown in Chapter X and X, respectively.  However, the most general approach is again going to be using \lstinline{with open(<file>, 'w') as <alias>:}, where we can use the \lstinline{write()} method of the file to output text we want. 




\subsubsection{Web IO}

A significant amount of information is contained on the web---often just written up and included on webpages. When you access the webpage, you often have access to these files and the information in them.  Thus, you can pull this information directly from the file---a process known as `web scraping.' You can write your own script for this, parsing out the html file that is the default for webpages, or you can use one of the many libraries that exist in Pthon. We are not going to go into depth on this, but simply note this is possible.  In this way, you can obtain information directly from websites like the ACS Journals, Aldrich company, etc. 

You can also write information to an html file, for instance, if you wanted to share data on a webpage you maintain.  Again, we are not going to go into the details, but there are many libraries that exist for this. 



\subsection{Database}

Sometimes there is information you want online, but it is not actually on a page.  Instead, the page queries a database, and then displays the results. In these cases, it is more efficient to simple directly interact with the database. There are a number of different database formats and softwares, so the first step is understanding what you are dealing with. But once you do, there are likely libraries that exist in Python for read and writing to a database. An advantage of these libraries is that they can also be used to \textit{create} databases.  So, if you wanted to, you could create a fully querriable database that has all the data you collected for your research, searchable by data type, project, molecule, etc. 

\subsection{Instruments}

Many (but not all) scientific instruments can be interfaced with using Python.  This is because many (but not all) instruments are now controlled by computers and many (but not all) of these instruments allow for 'low level' control---meaning control that occurs outside of a GUI interface. 

In most cases, there will not be a Python library for this interface, instead, the instrument company will provide application programming interfaces (APIs), which allow for interaction with the instrument. So, if you wish to try to control an instrument, the first place we would start is to search the name of the instrument, the name of the company, or the name of the GUI software interface, plus API.  If you do not find a result there, then you might try the same with `Python library' to see if such a library does exist. If this does not return results, then controlling the program via Python may not be possible.  

\subsection{Email <-- part of web?}

Sometimes programs take a very long time to run.  Maybe you have a complex fitting routine that will take several hours, or days, to run. Perhaps you have a code that is controlling an instrument that is running a multi-day experiment. You can also output a notification for when this is done. The simplest way is through email. 

https://realpython.com/python-send-email/


\end{document}