-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputing_msc_astro.tex
More file actions
164 lines (108 loc) · 7.09 KB
/
computing_msc_astro.tex
File metadata and controls
164 lines (108 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
\documentclass{article}
%sets paragraph indent for whole document
\setlength{\parindent}{0pt}
%gap between paragraphs
\setlength{\parskip}{1em}
%line spacing (1.3 is equivalent to one and a half spacing)
\renewcommand{\baselinestretch}{1.3}
\usepackage[english]{babel}
\usepackage{xcolor}
\definecolor{light-gray}{gray}{0.95}
\newcommand{\code}[1]{\colorbox{light-gray}{\texttt{#1}}}
\usepackage{minted}
% Set page size and margins
% Replace `letterpaper' with`a4paper' for UK/EU standard size
\usepackage[a4paper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}
% Useful packages
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorlinks=true, allcolors=blue]{hyperref}
\title{Python for Astrophysics}
\author{Dr Simon P Young}
\begin{document}
\maketitle
\begin{abstract}
Notes for the LJMU MSc Astrophysics course (2022) to get you started on a Python journey of discovery. This is the very basics - there are plenty of rabbit holes to descend down.
A note of caution. You can do some real damage to your computer by using the Terminal in your computer, although you'd have to be pretty unlucky. I take no responsibility for bricking your computer. As always, back up everything.
\end{abstract}
\tableofcontents
\newpage
\section{Topics}
The aim of this document is to try and present a way to get started with these topics as quickly as possible, with relevant examples. I'm not a 'Hello World' kind of learner - I'll present you with some code here, that you can copy and modify, that actually does some useful things for us as astrophysicists.If you can download applications, and copy and paste, you can create a Python 'programme'.
There are thousands of ways of skinning a cat when it comes to setting up to write Python code - this is the way I did it!
I'll cover the following:
\begin{itemize}
\item Getting a package manager (Conda) on your computer
\item Getting Jupyter Notebooks for creating your projects
\item Using the command line interface (CLI)/Terminal
\item Setting up your first project using Jupyter Notebook
\item Astropy package and how to use it for some simple things
\end{itemize}
\section{Package Manager - Conda}
The one I use is called Conda - it's FREE, as in no monetary cost. It is a collection of packages, e.g. scientific computation, workbooks (like Excel), graphics, that you can use as required in any programme that you write.
If you're happy using the command line interface (CLI)/Terminal, you can get it here - \url{https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html}. If you do it this way, don't forget to close the Terminal then re-open it to allow you to start using the version of Conda you downloaded (Anaconda or Miniconda).
If you don't know what the CLI is, you can go for the graphical interface download option here - \url{https://docs.continuum.io/anaconda/install/}
If you know what packages you are going to use, you can download Miniconda and load packages in individually. If you're not sure yet, just download the whole Anaconda collection of packages.
\section{Jupyter Notebook}
I use this to enter all the code that makes up a programme in Python. As well as the code, you can leave free text notes to remind you or others of what your code does.
The good news is if you downloaded the full set of packages with Anaconda, it has Jupyter Notebook already installed. If you use Miniconda, then instruction for downloading it using the CLI are here - \url{https://jupyter.org/install} (using the conda-forge repository of packages).
Alternatively, you can access a browser-based version of Jupyter Notebook here - \url{https://jupyter.org/try}. I don't know much about this... sorry. I also don't know what the difference is between Jupyter Notebook and JupyterLab - it may well be an improvement!
\section{Using the Command Line}
So, now is the time to get used to using this. It is an app on your computer that lets you input direct text commands to the computer. It's exceedingly powerful, but you don't need to know many commands to get going. The following works for MacOS (and Linux I would've thought) - the Windows commands will be similar, but you may need to ask someone else how to do this on a PC.
On MacOS open Terminal.
Type \code{conda list} (then press enter/return)
If you've downloaded Anaconda or Miniconda successfully then you should see a list of package names scrolling down. If it doesn't work, close Terminal, re-open, and try again.
Now try \code{conda update conda} to make sure you're up to date. You will have to enter \code{y} (for yes) when prompted.
Now type \code{jupyter notebook}
This should open up the notebook in your browser, and allow you to use the browser for further code writing.
\section{Other Packages}
If you've installed Miniconda you'll have to individually download some packages. In Terminal, paste this:
\code{conda install numpy matplotlib pandas scipy}
\section{Your First Notebook}
In your browser's Jupyter Notebook, go to File - New Notebook - Python3
Try pasting this into your newly opened Notebook:
\begin{minted}[bgcolor=light-gray]{python}
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 8*np.pi, 0.1) # start,stop,step
y = np.cos(x)
plt.plot(x,y)
plt.show()
\end{minted}
then hit the Run button.
If all has gone to plan, a cosine graph should magically appear.
\section{Astropy}
Astropy is a collection of useful things for astro. Here is how to install it if you didn't download the full Anaconda package (which has Astropy already) - \url{https://docs.astropy.org/en/stable/install.html}. Scroll down to the bit about how to install using conda.
Try pasting and running this in a Jupyter Notebook:
\begin{minted}[bgcolor=light-gray]{python}
# Kepler's 3rd law gives semi-major axis (a)
# from period (P) and stellar mass (M_star)
from astropy import units as u
from astropy import constants as const
import numpy as np
# Pd = period in days; Ps = period in seconds
# ENTER a value for planet's orbital period (in days)
Pd = 365.25 * u.d
Ps = (Pd / u.d) * (24 * 60 * 60 * u.s)
# ENTER a value for M_s (mass of star) in solar masses
M_star = 1.00 * const.M_sun
k = (4 * np.pi**2)/(const.G * M_star)
# a = the cube root of the period (in seconds) squared
a = (Ps**2/k)**(1/3)
# semi-major axis in astronomical units (AU)
a/const.au * u.au
\end{minted}
If everything is set up ok, when you run this code the output should be:
\code{0.99998741 AU}
\section{Advanced Coding}
Prof David Kipping (the 'exomoon guy' from the Cool Worlds Lab, Columbia University) tweeted a request for information on astro python courses... here's a list of the replies he got:
\url{https://github.com/howardisaacson/Intro-to-Astro-2021}
\url{https://github.com/semaphoreP/codeastro}
\url{https://www.astrosprint2021.com/}
\url{https://prappleizer.github.io/index.html}
\url{https://www.youtube.com/playlist?list=PLbJEQKXFZR1uATg2dyyKUDFnqtTzkUdZv}
\url{https://zingale.github.io/comp_astro_tutorial/intro.html}
\url{https://github.com/pmelchior/usrp-sciprog}
\url{https://www.ocf.berkeley.edu/~ipasha/python/}
\url{https://github.com/jbchampagne/pythontutorials/tree/master/workshop_2021}
\end{document}