1.2 Environmental Setup
Learning Objectives
After completing this lecture, students should be able to:
- understand the purpose of virtual environments and the command-line interface (CLI)
- activate and manage a Python virtual environment
- use basic Unix shell commands
- run Python scripts from the command line
Concept
Virtual Environments
Modern scientific software is continuously updated to fix bugs, improve usability, and introduce new features. However, software updates can sometimes create compatibility issues with other packages or with the operating system. As a result, code that previously worked may suddenly fail after a system update.
Virtual environments provide a solution to this problem. A virtual environment allows us to install specific versions of Python packages that are isolated from the system-wide installation. Once a working environment is created, the same environment can be activated at any time, ensuring that the code runs consistently.
This approach is important for reproducibility, which is a fundamental principle in scientific research.
Command Line Interface (CLI) vs Graphical User Interface (GUI)
Most personal computers use a Graphical User Interface (GUI), such as Windows or macOS. In a GUI environment, users interact with software through visual elements such as windows, icons, and buttons.
Although GUI systems are convenient and user-friendly, they require additional computational steps to display graphical elements and manage interactions. Many of these processes occur behind the scenes.
In contrast, the Command Line Interface (CLI) provides a more direct way to communicate with the computer. In CLI environments, users interact with the system by typing commands in a terminal.
Examples of CLI environments include the Unix shell.
Advantages of CLI include:
- faster execution of tasks
- easier automation using scripts
- efficient management of large datasets
- direct interaction with computational systems
Because climate model simulations often require large computational resources, researchers commonly use CLI-based workflows.
A quick reference for commonly used Unix shell commands is available here:
https://climate.usu.edu/people/yoshi/unix101/index.html
High-Performance Computing (HPC)
Many climate model simulations are performed on high-performance computing systems (HPC) or supercomputers.
These systems are designed to maximize computational performance and are typically based on Unix or Linux operating systems.
Users usually cannot access these systems directly in person. Instead, they connect remotely through a network using secure shell connections (SSH). While graphical interfaces can sometimes be used remotely, they are often slow and inefficient over network connections.
For this reason, most HPC systems rely heavily on command-line interfaces for efficient operation.
Learning how to work with the Unix shell is therefore an essential skill for climate modeling.
In this class, students can create an account on our local server called Zion. Zion is a Linux-based computing system that stores large and diverse climate datasets used for research and teaching.
Although it is possible to run the exercises on your personal laptop, it is strongly recommended that you log in to Zion to perform the course tasks. Using the server allows you to access the datasets directly and practice the workflow commonly used in climate research environments.
Programming Languages in Climate Modeling
Many state-of-the-art climate models are written primarily in Fortran, a programming language designed for high-performance scientific computing.
A typical workflow in Fortran-based modeling involves three steps:
- Write the source code
- Compile the code
- Run the executable program
Compiled languages such as Fortran can execute numerical calculations very efficiently. In many cases, Fortran programs may run significantly faster than interpreted languages such as Python.
However, Python has become widely used in scientific research because it provides:
- flexible scripting capabilities
- powerful scientific libraries
- efficient data analysis and visualization tools
In this course we will use Python to implement simplified climate models. Although Python is slower than compiled languages, the coding structure in this course is designed to resemble the workflow used in large climate modeling systems:
- prepare the model code
- run numerical simulations
- analyze and visualize model output
This structure helps students understand how real climate modeling systems operate.
Model / Equation
Before implementing climate models, it is useful to review basic numerical operations that will be used in Python.
Suppose we define two numbers:
- \(A\)
- \(B\)
We can perform several basic mathematical operations:
- Addition: \(A + B\)
- Subtraction: \(A - B\)
- Multiplication: \(A \times B\)
- Division: \(A / B\)
- Exponentiation: \(A^B\)
These operations form the foundation of numerical calculations used in scientific modeling.
In computer programming, the result of some operations may depend on the type of number used. For example, numbers can be stored as:
- integers (whole numbers)
- floating-point numbers (decimal numbers)
Because of this difference, the result of operations such as division may vary depending on the data type. Understanding how numbers are represented in Python is important for numerical modeling.
Sample Code
Sample Script
Example Code
import numpy as np
# Change values here to test different cases
A = 3
B = 2
print("A =", A, "type:", type(A))
print("B =", B, "type:", type(B))
print("\nBasic operations")
print("A + B =", A + B)
print("A - B =", A - B)
print("A * B =", A * B)
print("A ** B =", A ** B)
print("\nDivision behavior")
print("Default: A / B =", A / B)
print("Integer: A / B =", int(A / B))
print("Round: A / B =", round(A / B))
print("\nFloating-point precision test")
print("A - B =", A - B)
You can download the script here:
To run the script from the command line:
$ conda activate env_psc6125
$ python 1_1-sample_script.py
Tip
Commands beginning with$indicate commands that should be typed in the terminal.
Do not type the$symbol itself.
Exercise
1. Practice Basic Unix Shell Commands
Run the following commands in your terminal:
$ ls
$ mkdir test
$ cd test
$ vi test_file.txt
$ ls
$ cat test_file.txt
These commands allow you to create directories, edit files, and display file contents.
2. Manage the Python Environment
Practice activating and deactivating your conda environment.
$ conda deactivate
$ conda env list
$ conda activate env_psc6125
3. Edit and Run a Python Script
Create a copy of the script and modify it.
$ cp 1_1-sample_script.py 1_1-sample_script-v2.py
$ vi 1_1-sample_script-v2.py
$ python 1_1-sample_script-v2.py
Change the values to:
A = 3.0
B = 2.0
Then try:
A = 0.3
B = 0.2
Observe how the numerical output changes depending on the data type and values.
Key Points
- Virtual environments isolate software installations and ensure reproducible computing environments.
- The course Python environment is named
env_psc6125. - Climate modeling workflows frequently rely on command-line tools rather than graphical interfaces.
- The Unix shell is widely used to manage files, run scripts, and execute simulations.
- Python scripts can be executed directly from the command line to perform numerical calculations.
Homework
- (Optional) Log in to the Zion server using SSH.
$ ssh -Y username@zion.serv.usu.edu
Replace username with your Zion account name.
- Activate the Python environment:
$ conda activate env_psc6125
- Practice basic Unix shell commands:
$ ls
$ cd
$ mkdir
A quick reference for Unix commands is available here:
https://climate.usu.edu/people/yoshi/unix101/index.html
- Run the sample Python script:
$ python 1_1-sample_script.py
- Modify the script and test different values for
AandB, such as:
A = 3.0
B = 2.0
and
A = 0.3
B = 0.2
Observe how the numerical output changes depending on the values and data types.