FROM ubuntu:24.04

WORKDIR /src

RUN mkdir -p /src
RUN apt-get -y clean && apt -y update && apt install -y apt-utils && apt -y upgrade

RUN DEBIAN_FRONTEND=noninteractive \
    apt install -y --no-install-recommends \
    build-essential \
    ca-certificates \
    gfortran \
    wget \
    git \
    libbz2-dev \
    libgsl-dev \
    libssl-dev \
    libcfitsio-dev \
    libcfitsio-bin \
    # libhdf5 needed by h5py
    libhdf5-dev \
    && apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

ARG mpich=3.4.3
ARG mpich_prefix=mpich-$mpich

ENV FFLAGS="-fallow-argument-mismatch" \
    FCFLAGS="-fallow-argument-mismatch"

RUN wget --no-check-certificate -nv https://www.mpich.org/static/downloads/$mpich/$mpich_prefix.tar.gz \
    && tar xvzf $mpich_prefix.tar.gz \
    && cd $mpich_prefix \
    && ./configure --with-device=ch4:ofi \
    && make -j 16 \
    && make install \
    && make clean \
    && cd .. \
    && rm -rf $mpich_prefix \
    && rm -f $mpich_prefix.tar.gz

RUN /sbin/ldconfig

# Try to prevent MKL from throttling AMD
# https://gitlab.com/NERSC/python-benchmark/-/tree/main/amd
COPY fakeintel.c /src/fakeintel.c
RUN gcc -shared -fPIC -o /usr/local/lib/libfakeintel.so /src/fakeintel.c
ENV LD_PRELOAD=/usr/local/lib/libfakeintel.so

# Install Miniconda
ENV CONDA_DIR=/opt/miniconda

ENV MINICONDA_VERSION=Miniforge3-Linux-x86_64.sh
ENV MINICONDA=https://github.com/conda-forge/miniforge/releases/latest/download/$MINICONDA_VERSION

RUN wget --no-check-certificate $MINICONDA \
    && bash $MINICONDA_VERSION -b -p $CONDA_DIR \
    && rm -f $MINICONDA_VERSION

# Update PATH environment variable
ENV PATH="$CONDA_DIR/bin:$PATH"

# Verify Miniconda installation and initialize environment
RUN conda init bash && conda update -n base -c defaults conda -y

# Set default shell to bash
SHELL ["/bin/bash", "-c"]

# Install all our dependencies
RUN conda install -y -c conda-forge \
    "python<3.13" \
    wheel \
    setuptools \
    pytest \
    "numpy<2.0" \
    scipy \
    astropy \
    healpy \
    fitsio \
    numba \
    seaborn \
    matplotlib \
    ipython \
    ipykernel \
    h5py

RUN conda install -y -c conda-forge \
    libblas=*=*mkl \
    # also tried mkl_fft<1.3.9
    mkl_fft \
    intel-cmplr-lib-rt \
    && conda clean --all -y

# Need to install mpi4py from source to link it properly to MPICH.
ENV PIP_ROOT_USER_ACTION=ignore
RUN pip install --no-cache-dir --no-binary=mpi4py mpi4py

ENV DESIUTIL_VERSION=3.4.3
ENV DESIMODEL_VERSION=0.19.2
ENV DESITARGET_VERSION=2.8.0
ENV DESISPEC_VERSION=0.68.1
ENV SPECLITE_VERSION=v0.20
ENV FASTSPECFIT_VERSION=main
#ENV FASTSPECFIT_VERSION=3.1.1

RUN pip install git+https://github.com/desihub/desiutil.git@${DESIUTIL_VERSION}#egg=desiutil
RUN pip install git+https://github.com/desihub/desimodel.git@${DESIMODEL_VERSION}#egg=desimodel
RUN pip install git+https://github.com/desihub/desitarget.git@${DESITARGET_VERSION}#egg=desitarget
RUN pip install git+https://github.com/desihub/desispec.git@${DESISPEC_VERSION}#egg=desispec
RUN pip install git+https://github.com/desihub/speclite.git@${SPECLITE_VERSION}#egg=speclite
RUN pip install   git+https://github.com/desihub/fastspecfit.git@${FASTSPECFIT_VERSION}#egg=fastspecfit

ENV DESI_SPECTRO_REDUX=/dvs_ro/cfs/cdirs/desi/spectro/redux
ENV DUST_DIR=/dvs_ro/cfs/cdirs/cosmo/data/dust/v0_1
ENV FPHOTO_DIR=/dvs_ro/cfs/cdirs/desi/external/legacysurvey/dr9
ENV FTEMPLATES_DIR=/dvs_ro/cfs/cdirs/desi/public/external/templates/fastspecfit

# Some environment variables to ensure good performance with MKL.
# https://www.diracprogram.org/doc/master/installation/mkl.html
ENV MKL_NUM_THREADS=1
ENV MKL_DYNAMIC="FALSE"
ENV OMP_NUM_THREADS=1

# Create the numba cache.
ENV HOME=/homedir
ENV NUMBA_CACHE_DIR=$HOME/numba_cache

RUN mkdir -p /homedir/numba_cache \
    && chmod -R 777 /homedir*
RUN pytest /opt/miniconda/lib/python3.12/site-packages/fastspecfit/test/test_fastspecfit.py

LABEL Maintainer="John Moustakas jmoustakas@siena.edu"
