Summary and Setup

The best way to learn how to program is to do something useful, so this introduction to Python is built around a common scientific task: data analysis.

Argo floats


This lesson is intended for people with an interest in oceanography and uses some example data from an Argo float. Argo floats are small drifting floats that spend most of their time moving up and down between the surface and mid level water (usually one or two thousand metres). They control their buoyancy by inflating and deflating a small bladder which causes them to float or sink. They usually measure the water temperature, conductivity (which tells us how salty it is) and pressure (which tells us how deep they are). Some of them also measure biochemical information such as acidity, the amount of chlorophyll or dissolved oxygen. An Argo float usually spends 10 days under the water gathering data, it then surfaces and transmits back the information it has gathered to a satellite. Once it has transmitted its data it will dive again and repeat the process. Each dive generates one “profile” of data showing the temperature and salinity of the ocean at each depth level.

Thousands of Argo floats have been deployed since 1999 and they are spread all over the oceans. Many countries operate Argo floats but they all report their data in a commonly agreed upon format and the data is archived at a series of regional Data Assembly Centres or DACS. These DACS then pass on the data to two Global Data Assembly Centres (GDACs), one in the USA and one in France. Anybody can access data from the GDACs and use it in their work. It is quite common for oceanographers to use the data from many Argo floats to look at changes in the ocean temperature and salinity.

A photo of an Argo float. From https://commons.wikimedia.org/wiki/File:Argo_float_Aranda.jpg

Setup


We expect most learners to use a pre-configured environment for this and not to install it themselves. If you are taking this course with the National Oceanography Centre please follow the instructions you have been given on how to connect to a system with everything you need.

As an emergency fallback a pre-configured MyBinder image is available which will provide all the required software. If you have not been provided with any other system then use this. Click on the “launch binder” icon below. Please note that this system does not have many computational resources and will stop your notebook if you leave it idle for a few minutes.

Run this lesson on MyBinder

If you really want/need to run this on your own computer


Anybody taking this course with the National Oceanography Centre should not have to do this!

This lesson is designed to be run on a computer with Python and the Matplotlib, Numpy, Argopy and Cartopy libraries installed.

Install Python

Users of the NOC Data Science Platform or Binder Hub can skip this section and move on to the “Obtain lesson materials” section below.

Install Miniforge

If Conda has not been installed on your machine, then install Miniforge for your OS. As the name suggests, Miniforge is a “mini” version of the Anaconda Python distribution that includes only Conda, a Python 3 distribution, and any necessary OS-specific dependencies.

For convenience here are links to the 64-bit Miniconda installers.

Windows installation

After you downloaded the Windows installer, double click on it and follow the instructions (accept license, etc.). Make sure you tick on “Add Miniforge3 to my PATH environment variable” option.

Mac OSX or Linux installation

First, download the 64-bit Python 3 install script for Miniforge either by clicking the link above or using this command in your terminal:

BASH

wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"

Run the Miniforge install script from your terminal. Follow the prompts on the installer screens. If you are unsure about any setting, accept the defaults (you can change them later if necessary).

BASH

bash Miniforge3-$(uname)-$(uname -m).sh

Once the install script completes, you can remove it.

BASH

rm Miniforge3-$(uname)-$(uname -m).sh
Initalizing Conda (Mac and Linux Only)

Near the end of the installation you will be asked if you wish to initialize conda. This will place the conda executables in your path, meaning your system can easily find them. You want to do this.

When asked the following question:

OUTPUT

Do you wish to update your shell profile to automatically initialize conda?
This will activate conda on startup and change the command prompt when activated.

Answer with “yes” (the default is no). If you pressed enter during the install and followed the default no then you can fix this typing conda init immediately after running the installer.

In order for these settings to be found you might need to close and re-open your terminal to apply the new path settings.

Verifying your Conda installation

In order to verify that you have installed Conda correctly run the conda help command. Output of the command should look similar to the following.

BASH

$ conda help
usage: conda [-h] [-V] command ...

conda is a tool for managing and deploying applications, environments and packages.

Options:

positional arguments:
  command
    clean        Remove unused packages and caches.
    config       Modify configuration values in .condarc. This is modeled
                 after the git config command. Writes to the user .condarc
                 file (/Users/drpugh/.condarc) by default.
    create       Create a new conda environment from a list of specified
                 packages.
    help         Displays a list of available conda commands and their help
                 strings.
    info         Display information about current conda install.
    init         Initialize conda for shell interaction. [Experimental]
    install      Installs a list of packages into a specified conda
                 environment.
    list         List linked packages in a conda environment.
    package      Low-level conda package utility. (EXPERIMENTAL)
    remove       Remove a list of packages from a specified conda environment.
    uninstall    Alias for conda remove.
    run          Run an executable in a conda environment. [Experimental]
    search       Search for packages and display associated information. The
                 input is a MatchSpec, a query language for conda packages.
                 See examples below.
    update       Updates conda packages to the latest compatible version.
    upgrade      Alias for conda update.

optional arguments:
  -h, --help     Show this help message and exit.
  -V, --version  Show the conda version number and exit.

conda commands available from other packages:
  env

At the bottom of the help menu you will see a section with some optional arguments for the conda command. In particular you can pass the --version flag which will return the version number. Again output should look similar to the following.

BASH

$ conda --version
conda 4.8.2

Install the Required Python Libraries

Run the following command to install the required dependencies.

BASH

$ conda install jupyterlab pip python=3.11 "numpy>2.0" matplotlib-base matplotlib-inline argopy cartopy

Launch Python interface

To start working with Python, we need to launch a program that will interpret and execute our Python commands. Below we list several options. If you don’t have a preference, proceed with the top option in the list that is available on your machine. Otherwise, you may use any interface you like.

A Jupyter Notebook provides a browser-based interface for working with Python. If you installed Miniforge, you can launch a notebook from the terminal:

If you’re using a Unix shell application, such as Terminal app in macOS, Console or Terminal in Linux, or Git Bash on Windows, execute the following command:

BASH

jupyter lab

On Windows, you can use its native Command Prompt program. The easiest way to start it up is pressing Windows Logo Key+R, entering cmd, and hitting Return. In the Command Prompt, use the following command to start Jupyter:

BASH

jupyter lab