FROM r-base:4.1.1

RUN apt update \
 && apt remove -y binutils \
 && apt install -y \
 # Common dependencies
 curl \
 build-essential \ 
 git \ 
 gfortran \ 
 cmake \
 # R-WRFHYDRO dependencies
 libcurl4 \
 libssl-dev \
 libxml2-dev \
 libgdal-dev \
 proj-bin \
 netcdf-bin \
 nco \
 libharfbuzz-dev \
 libfribidi-dev \
 # PARFLOW dependencies 
 libopenblas-dev \ 
 liblapack-dev \ 
 openssh-client \ 
 openssh-server \ 
 openmpi-bin \ 
 libopenmpi-dev \ 
 tcl-dev \ 
 tk-dev \
 gdal-bin \ 
 && rm -rf /var/lib/apt/lists/*
 

# libcurl4-openssl-dev
 # might not need ^. try below

#apt install -y libharfbuzz-dev  libfribidi-dev
#install.r textshaping

##############
# R-WRFHYDRO #
##############

# install devtools
RUN Rscript -e "install.packages('devtools', dependencies=TRUE)"

# install rwrfhydro
RUN Rscript -e "devtools::install_github('NCAR/rwrfhydro')"

# missing dependencies - rgdal
RUN apt update && apt install -y \
    libudunits2-dev \
 && rm -rf /var/lib/apt/lists/*
RUN Rscript -e "install.packages('rgdal', dependencies=TRUE)"


###########
# PARFLOW #
###########



RUN mkdir -p /home/parflow 
WORKDIR /home/parflow

ENV PARFLOW_DIR=/usr/local \
    PATH=$PATH:$PARFLOW_DIR/bin \
    PARFLOW_MPIEXEC_EXTRA_FLAGS="--mca mpi_yield_when_idle 1 --oversubscribe --allow-run-as-root"

# Hypre
RUN wget -q https://github.com/hypre-space/hypre/archive/v2.17.0.tar.gz \
 && tar xf v2.17.0.tar.gz \
 && cd hypre-2.17.0/src \
 && ./configure --prefix=$PARFLOW_DIR \
 && make install \
 && cd ../.. && rm -fr hypre-2.17.0 v2.17.0.tar.gz

# Silo
RUN curl "https://wci.llnl.gov/sites/wci/files/2021-09/silo-4.11.tgz" -o "silo-4.11.tgz" \
 && tar -xf silo-4.11.tgz \
 && cd silo-4.11 \
 && ./configure  \
    --prefix=$PARFLOW_DIR \
    --disable-silex \
    --disable-hzip \
    --disable-fpzip \
 && make install \
 && cd .. && rm -rf silo-4.11 silo-4.11.tgz

# ParFlow
RUN git clone -b master --single-branch https://github.com/parflow/parflow.git parflow \
 && mkdir -p build \
 && cd build \
 && CC=mpicc CXX=mpic++ LDFLAGS="-lcurl" cmake ../parflow \
    -DPARFLOW_AMPS_LAYER=mpi1 \
    -DPARFLOW_AMPS_SEQUENTIAL_IO=TRUE \
    -DHYPRE_ROOT=$PARFLOW_DIR \
    -DSILO_ROOT=$PARFLOW_DIR \
    -DPARFLOW_ENABLE_TIMING=TRUE \
    -DPARFLOW_HAVE_CLM=TRUE \
    -DCMAKE_INSTALL_PREFIX=$PARFLOW_DIR \
 && make install \
 && cd .. && rm -fr parflow build


#############
# MINICONDA #
#############

WORKDIR /tmp

# install and configure miniconda in /opt/miniconda so that it's accessible
# to all system users. 
ENV CONDA_PATH=/opt/miniconda

# needs to be declared separately from CONDA_PATH since it uses that
# environment variable
ENV PATH=$CONDA_PATH/bin:$CONDA_PATH/condabin:$PATH

ENV CONDA_VERSION=Miniconda3-py37_22.11.1-1-Linux-x86_64.sh
RUN wget https://repo.anaconda.com/miniconda/${CONDA_VERSION} \
 && bash ${CONDA_VERSION} -b -p $CONDA_PATH \
 && rm -f ${CONDA_VERSION} 

###############
# PYTHON LIBS #
###############

RUN conda install -c conda-forge mamba -y


# install gdal <3.2 from conda-forge. This version allows you to
# 'import gdal' instead of 'from osgeo import gdal' which is required by pftools
RUN mamba install -c conda-forge -y \
    "gdal<3.2.0" \
    "aioredis<2.0" \
    matplotlib \
    sqlite \
    tornado \
    geopandas \
    xarray 

RUN pip install \
    hs_restclient==1.3.6 \
    shapely \
    multiprocessing-logging==0.3.0 \
    python-cas==1.5.0 \
    pyshp \
    numpy \
    redis \
    pftools==0.0.6 \
    parflowio \
    parflow_subsetter
###    #    geopandas \
###    #    pyproj

EXPOSE 80
WORKDIR /srv
ENTRYPOINT ["python", "app.py"]

