Lab2a#

Atmospheric Correction with Acolite#

Acolite can perform atmospheric correction on a variety of satellite sensors, including Landsat, Sentinel-2, PACE, EMIT, AVIRIS, among others. For more information on how to use Acolite, please refer to the [Acolite manual]:https://www.scribd.com/document/650829066/acolite-manual-20221114-0

In this example, we will use Acolite to perform atmospheric correction on a Sentinel-2 image.

In previous class, we created a Environemnt called ENVS473.

Now we need to create a new enviroment called “geo”

Launch your Anaconda prompt (miniconda):

conda env list
conda create -n geo 
conda activate geo
conda install mamba -c conda-forge
mamba install -c conda-forge python=3.10 gdal=3.7.2 hvplot geoviews rioxarray rasterio geopandas fiona=1.9.4 jupyter earthaccess jupyter_bokeh h5py h5netcdf spectral scikit-image seaborn jupyterlab dask ray-default leafmap hypercoast
# %pip install "hypercoast[extra]"

Import libraries#

import os
import hypercoast

Download sample data#

Please go to Copernicus Browser, and creat an account, so that you can download Sentinel 2 data.

Or you can download data from this URL: opengeos/datasets

# Assuming you've already downloaded the file to your local folder
work_dir = r"C:\Users\C00553090\Downloads\Sediment_data\Sediment_data"
filepath = os.path.join(work_dir, "S2A_MSIL1C_20160920T164452_N0204_R083_T15RYN_20160920T164450.SAFE.zip")

# Now you have the file path for the locally stored file
print(f"Using local file: {filepath}")
# This line takes the filepath string (which represents the path to the ZIP file) and removes the .zip extension from it.
input_dir = filepath.replace(".zip", "")
# This line checks whether the directory represented by input_dir exists. The os.path.exists() function returns True if the path exists and False if it doesn't.
if not os.path.exists(input_dir):
    #If the directory does not exist, this line raises a FileNotFoundError with a custom error message. This is done using Python's raise statement, which generates an exception.
    raise FileNotFoundError(f"Directory {input_dir} not found")

Download Acolite software#

# acolite_dir: This variable will store the path to the directory where the ACOLITE software is downloaded and saved.
acolite_dir = hypercoast.download_acolite(work_dir)
# hypercoast includes functions to download and configure various datasets and software, such as ACOLITE.
# This function is designed to automatically download the ACOLITE and install it in the specified directory: work_dir.
hypercoast.download_acolite(work_dir)
print(f"Acolite directory: {acolite_dir}")

Run Acolite#

out_dir = os.path.join(work_dir, "output")
out_dir
hypercoast.run_acolite(
    acolite_dir=acolite_dir,
    input_file=input_dir,
    out_dir=out_dir,
    l2w_parameters="Rrs_*,chl_oc3,chl_re_mishra,spm_nechad2016",
    rgb_rhot=True,
    rgb_rhos=False,
    map_l2w=True,
)

Batch processing#

To process multiple images, put all the images in a folder. For example, unzip all the images in the data folder. Then, run the following code to make sure that all image folders are listed.

input_dir = os.path.join(work_dir, "data")
input_files = [os.path.join(input_dir, f) for f in os.listdir(input_dir)]
input_files

Run the following code to process all images in the data folder.

hypercoast.run_acolite(
    acolite_dir=acolite_dir,
    input_file=input_files,
    out_dir=out_dir,
    l2w_parameters="Rrs_*,chl_oc3,chl_re_mishra,spm_nechad2016",
    rgb_rhot=True,
    rgb_rhos=False,
    map_l2w=True,
)