Skip to content

Instantly share code, notes, and snippets.

@nuxion
Last active October 28, 2022 15:23
Show Gist options
  • Save nuxion/3fcfe03892e297b7afd07e4e67c5e4e5 to your computer and use it in GitHub Desktop.
Save nuxion/3fcfe03892e297b7afd07e4e67c5e4e5 to your computer and use it in GitHub Desktop.
OTB RAM memory limit
# This example shows how to limit the memory usage of OTB
# with the OTB_MAX_RAM_HINT variable in MB we configurate OTB to use
# that value as limit, if it needs more memory for a image then it
# will chunk the image to fit the memory limit.
#
# Also, a function which calculates the limit memory base on the system
# is provided. It requires psutil to be installed in the environment:
# pip install psutil
#
# For more information refer to OTB Docs:
# https://www.orfeo-toolbox.org/CookBook-6.6.1/AdvancedUse.html
import psutil
import subprocess
def calculate_otb_ram(rate=0.6):
"""Based on the available memory in the system,
it calculates a percentage of that memory to be used
by OTB
https://www.orfeo-toolbox.org/CookBook-6.6.1/AdvancedUse.html
"""
avail = (psutil.virtual_memory().available / (1024*1024))
ram=round(avail * rate)
return ram
NIR="NIR_utm.tif"
RGB="RGB_utm.tif"
NIR_SI="NIR_si.tif"
superimpose=f"""/bin/bash -c 'source /opt/OTB/otbenv.profile; OTB_MAX_RAM_HINT={RAM} otbcli_Superimpose -inr {RGB} -inm {NIR} -out "{NIR_SI}?&gdal:co:COMPRESS=LZW&gdal:co:TILED=YES&gdal:co:BIGTIFF=YES" uint8'"""
subprocess.run(superimpose, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
# from https://github.com/labfunctions/containers/
ARG ROOT_CONTAINER=jupyter/minimal-notebook:python-3.8.8
FROM $ROOT_CONTAINER
LABEL maintainer="Damián Silvani <[email protected]>"
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
USER root
# ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
apt-transport-https \
build-essential \
ca-certificates \
curl \
file \
gdal-bin \
gnupg \
libgdal-dev \
libgl1 \
libproj-dev \
libspatialindex-dev \
proj-bin \
python3-dev \
python3-venv \
wget \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
## Install gcloud SDK
RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] http://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list \
&& curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key --keyring /usr/share/keyrings/cloud.google.gpg add - \
&& apt-get update -y && apt-get install google-cloud-cli -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
## Install Orfeo Toolbox
ARG otb_version=8.1.0
ENV OTB_PACKAGE_NAME=OTB-${otb_version}-Linux64
# Install numpy (needed for OTB)
USER ${NB_UID}
RUN pip install --upgrade pip \
&& pip install numpy
# Download and run OTB setup
USER root
WORKDIR /opt
RUN wget -q https://www.orfeo-toolbox.org/packages/$OTB_PACKAGE_NAME.run \
&& chmod +x ./$OTB_PACKAGE_NAME.run \
&& ./$OTB_PACKAGE_NAME.run \
&& rm -f $OTB_PACKAGE_NAME.run \
&& ln -s /opt/$OTB_PACKAGE_NAME /opt/OTB
USER ${NB_UID}
## Install python requirements
ADD requirements.txt /tmp/requirements.txt
RUN pip install --upgrade pip \
&& pip install -r /tmp/requirements.txt
WORKDIR "${HOME}"
GDAL==3.0.4
markupsafe==2.0.1
pysatproc==0.1.9
unetseg==0.2.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment