Python for Astronomy and Astrophysics
With an intrinsically visual study like astronomy, it is no wonder astronomers have such a close relationship with data. In the olden days, hours would be poured over acquiring standardised visual plates and comparing them with observed data films to find supernovae, or manually going through reels of printed radio energy outputs which would detect the first pulsars.
Modern astronomy relies on data more than ever before. Individualistic observations are replaced by large scale data surveys conducted by highly specialised instruments producing terabytes of data, available for astronomers to conduct research on a wide range of areas. Data acquisition, filtration, analysis, and visualisation shall be your bread and butter. To prepare ourselves for modern astronomy we will familiarise ourselves with one of the most widely-used programming languages in the field today.
Python is a general-purpose programming language used for everything from web-development to simulating complex physical systems. Python is what’s known as a ‘high-level’ programming language. It’s got strong abstraction, which means it can run without worrying too much about system details like memory. Python strongly prefers readability, with its syntax being closer to natural and mathematical languages. Where other languages use semicolons to signify commands, Pythonic commands are denoted by new lines. While scopes for loops, conditions, and functions might be specified within braces, Python uses indents.
Python is extremely ubiquitous, and there’s a lot of ways you might be able to use it. Many Linux distributions and MacOS already ship with Python on the system. Windows does not ship with Python built in, but can be installed easily using the Python installer for Windows, or by using an environment manager like Conda (which is the preferable way to install it).
That’s not all though. You can run Python code without installing it on your computer. More on that later.
For this lab? Pretty much. While some AstroLABs do not involve a data analysis or visualisation component, it is virtually impossible to go through AstroLAB without using the language. Don’t worry though! Python is a super important skill to have no matter where life takes you. It is also, you’ll find, very approachable and easy to use, despite the seemingly complex aura programming carries.
Nope! While I’ll encourage installing Python and messing around with it (you’ll easily get a hang of it and develop crucial problem-solving skills), you don’t have to go through any of it. Several online websites let you use their resources for free and run Python on it, with many popular data analysis already available. Google Colab is a big one, and lab notebooks available on AstroLAB are made on Colab.
Google Research’s Colaboratory or Colab connects you to a ‘notebook’ on your browser, where you can write code and it executes under the hood on Google’s resources. There is a time limit on low long a runtime can last (about 12 hours depending on resource availability), but it is more than enough for most purposes. At any rate, you get to save your notebooks on your Google Drive, where you can share and run them whenever you like. If you want to use specific datasets or files, Colab can connect to your Google Drive by ‘mounting’ it like a Physical drive, where you can access pretty much all files. Learn more about Colab on the Colab website or this GeekFlare page.
Good question! Every AstroLAB surrounds a particular experiment and has its own webpage (click the AstroLABs tab on the main menu to check them out!). Each webpage contains a manual, which is your primary experimental resource. The manual outlines theory behind the experiment, experimental procedures, and how to use any other resources provided on the webpage (like Python notebooks and data files). The manual will prompt you on where and when to use the notebooks, but in general it is a good idea to read through the manual and then check out the notebook.
You can, but we recommend that you click the File option on the main notebook menu and select the Save a copy in Drive option. This makes a copy of the notebook in your Google Drive and the you can save changes you make on the notebook before you submit it.
Your instructor will let you know how you can best submit your work, but in general you can click on the File main menu option and then select Download -> Downlaod .ipynb to download your notebook as an IPYNB file, which you can then upload to LMS or email to your instructor.
We have four separate Google Colab notebooks, each focusing on different aspects of the Python programming language. It’s designed to be completed in sequence, and will touch on basic syntax and elementary data visualisation.
Operators and Datatypes
Iterables and Looping
Functions and Libraries
- Amos et. al. Python Basics: A Practical Introduction to Python 3. Pages 48-64, 186-209.
- Schmidt and Völschow Numerical Python in Astronomy and Astrophysics: A Practical Guide to Astrophysical Problem Solving. Pages 1-8.
- Khan Academy unit on circular motion and gravitation.
- Ryden and Peterson Foundations of Astrophysics. Chapter 3.
- Amos et. al. Python Basics: A Practical Introduction to Python 3. Pages 135-167.
Making introductory Python exercises is often highly iterative. To comprehensively identify the concepts needed to build a firm base for any programming language, the voices of those on that learning journey are most important.
For this reason, if you had trouble going through the different parts of these notebooks, or have a Python related question, or just have a suggestion you’d like to share, you can email me at shoaib.shamsi[at]lums[dot]edu[dot]pk. Your feedback helps make these exercises better!