# 5. Expressions (10 min)¶

In the previous sub-module, Cell statistics and basic band math (10 min), we executed some simple mathmatical operations on Images; however, sometimes we have multi-step operations or more complicated expressions we wish to apply.

For that, we can use the `.expression()` method built-in to the Image class.

In this tutorial we’ll shift back to DMSP-OLS data and take a sneak peak at a process for intercalibrating DMSP data. As noted in Introduction to nighttime light data (20 min), this is necessary when analyzing a DMSP-OLS time series. We get into this in much more detail in a later tutorial on DMSP-OLS intercalibration, but for now, we’ll introduce the use of expressions in Google Earth Engine.

In order to manage memory for our interactive maps when publishing, we’ll split this into two notebooks, A and B – but consider them a single exercise.

Part A:

1. Invert a 1996 DMSP-OLS composite using built-in functions.

Part B:

1. Conduct an equivalent operation using `.expression()`

2. Apply a simple polynomial formula for DMSP-OLS intercalibration co-efficients to a 1996 composite.

## 5.1. Invert an image with Image functions¶

### 5.1.1. Initialize map with DMSP-OLS layer¶

First, as we’ve often done, let’s initialize a map object.

Here we’ll center our scene on Mexico City.

And we’ll pull the annual DMSP-OLS nightime lights composite for 1996, using the “stable lights” band.

```# reminder that if you are installing libraries in a Google Colab instance you will be prompted to restart your kernal

try:
import geemap, ee
except ModuleNotFoundError:
!pip install geemap
else:
!conda install mamba -c conda-forge -y
!mamba install geemap -c conda-forge -y
import geemap, ee
```
```try:
ee.Initialize()
except Exception as e:
ee.Authenticate()
ee.Initialize()

# # initialize our map and center on Mexico City, Mexico
lat = 19.43
lon = -99.13
map1 = geemap.Map(center=[lat,lon],zoom=6)

dmsp1996 = ee.Image("NOAA/DMSP-OLS/NIGHTTIME_LIGHTS/F121996").select('stable_lights')
map1
```

### 5.1.2. Invert an image¶

Let’s say we want to invert an image. We know our max value (our Digital Number, if you recall) for DMSP-OLS images is 63. So if we multiply everything by -1 and then add 63. So, for example what was a DN of 63 before (the max) is now a 0 and vice versa.

We can do this using the built-in functions `.multiply` and `.add()`

Note: we selected our band, `stable_lights` above and saved to our variable `dmsp1996`, but if we didnt before we would have to do that here to explicitly apply our operation, including expression, to a specific band.

```dmsp1996_inv = dmsp1996.multiply(-1).add(63)
```

### 5.1.3. Visualize the inverse image¶

We’ll view the transformed (inverted) image we just created and compare it to the original image with a split map.

Note: to ensure we’re comparing them on the same scale, we’re using the visual parameters in the `.addLayer()` function, which allows us to do a few things to our image, such as clip the image to “min” and “max” values.

Warning, this is based on `ipyleaflet` a Python library that does not play well with Google Colab, so the split panel display will not work in the Google Colab environment but should on your local machine.
```map2 = geemap.Map(center=[lat,lon],zoom=6)
Next we’ll do this with the `expression()` method.