Bootstrap: docker
From: ubuntu:22.04

%help
    This is a container for Single Pulsar Noise Analysis (SPNA) for IPTA DR3. 

%files
    #dr3_noise /opt/dr3_noise
    clockfiles /opt/clockfiles
    #solar_wind_linear.py /opt/scripts/solar_wind_linear.py 

    
%labels
    DESCRIPTION Singularity image for Single Pulsar Noise Analysis (SPNA).
    Package_list Jupyter + Conda + Tempo2 + PSRCHIVE + PulsePortraiture + PINT + Libstempo + enterprise + mpi4py + tensorflow-probability + bilby + pandas + fastshermanmorrison + hasasia + dynesty + PTMCMC + la_forge + enterprise_warp + Tensiometer.    
    Author IPTA
    Version v3
    Date April 4, 2026    

%environment
    # Set PATH to include the micromamba environment and binaries
    export PATH=/opt/micromamba:/opt/micromamba/envs/PTA_env/bin:$PATH

    # Base environment paths
    export TEMPO2=/opt/micromamba/envs/PTA_env/share/tempo2
    export PSRCHIVE=/opt/micromamba/envs/PTA_env/share/

    # Set library paths and prefixes
    export TEMPO2_PREFIX=$TEMPO2
    export LD_LIBRARY_PATH=$TEMPO2/lib:$LD_LIBRARY_PATH

    # Set MAMBA_ROOT_PREFIX to the directory where micromamba is installed
    export MAMBA_ROOT_PREFIX=/opt/micromamba
    
    # Set CONDA_PREFIX for compatibility with older tools expecting conda paths
    export CONDA_PREFIX=/opt/micromamba/envs/PTA_env
    
    # Set DEBIAN_FRONTEND to prevent interactive prompts during package installations
    export DEBIAN_FRONTEND=noninteractive
    #export PATH=/usr/local/bin:$PATH
    #export PYTHONPATH=$PYTHONPATH:/opt/scripts/


%post
    # --- System packages ---
    apt-get update && apt-get install -y --no-install-recommends \
        libx11-6 libx11-dev libxext6 libxext-dev libxft2 libxft-dev libxt6 libxt-dev libpng-dev x11-apps \
        build-essential git wget curl cmake autoconf automake libtool python-pkg-resources\
        libcfitsio-dev libfftw3-dev libgsl-dev libhdf5-dev libboost-all-dev \
        libreadline-dev libncurses5-dev python3-dev python3-pip \
        gfortran ca-certificates bash f2c libf2c2-dev pandoc && \
        rm -rf /var/lib/apt/lists/*

    # --- Install micromamba ---
    mkdir -p /opt/micromamba
    curl -L https://micro.mamba.pm/api/micromamba/linux-64/latest | tar -xvj -C /opt/micromamba --strip-components=1 bin/micromamba
    export MAMBA_ROOT_PREFIX=/opt/micromamba
    export PATH=/opt/micromamba:$PATH

    # --- Create necessary directories for micromamba ---
    mkdir -p /opt/micromamba/envs
    mkdir -p /opt/scripts/
    
    # --- Configure channels manually ---
    cat > /opt/micromamba/.condarc << 'EOF'
channels:
    - conda-forge
    - bioconda
    - vhaasteren
channel_priority: strict
EOF

    # --- Create Python environment ---
    micromamba create -y -p /opt/micromamba/envs/PTA_env python=3.11

    # --- Install packages in chunks ---
    micromamba install -y -p /opt/micromamba/envs/PTA_env "pint-pulsar>=1.1" black emcee nestle
    micromamba install -y -p /opt/micromamba/envs/PTA_env corner tqdm pytest pytest-xdist
    micromamba install -y -p /opt/micromamba/envs/PTA_env jupyterlab notebook ipykernel
    micromamba install -y -p /opt/micromamba/envs/PTA_env psrchive libstempo tempo2 seaborn ptmcmcsampler
    micromamba install -y -p /opt/micromamba/envs/PTA_env dynesty bilby hypothesis astropy cfitsio cryptography
    micromamba install -y -p /opt/micromamba/envs/PTA_env encor ephem fftw h5py hasasia jplephem la_forge matplotlib
    micromamba install -y -p /opt/micromamba/envs/PTA_env mpich numpy pandas pyerfa pgplot
    micromamba install -y -p /opt/micromamba/envs/PTA_env schwimmbad scikit-learn scikit-sparse scipy statsmodels
    micromamba install -y -p /opt/micromamba/envs/PTA_env suitesparse yaml acor astroml ceffyl cython
    micromamba install -y -p /opt/micromamba/envs/PTA_env bioconda::defiant dm-tree enterprise-pulsar
    micromamba install -y -p /opt/micromamba/envs/PTA_env enterprise_extensions execnet
    micromamba install -y -p /opt/micromamba/envs/PTA_env flake8 getdist 
    micromamba install -y -p /opt/micromamba/envs/PTA_env gitdb gitpython h5pulsar healpy html5lib rich mccabe
    micromamba install -y -p /opt/micromamba/envs/PTA_env mpi4py numba pyarrow pycodestyle pydyf pyflakes pyphen
    micromamba install -y -p /opt/micromamba/envs/PTA_env pypulse pytest-xdist python-graphviz smmap
    micromamba install -y -p /opt/micromamba/envs/PTA_env tensorflow-probability termcolor tinycss2 upsetplot
    micromamba install -y -p /opt/micromamba/envs/PTA_env weasyprint wrapt zopfli

    # --- Install Python packages via pip ---
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps --force-reinstall --no-cache-dir setuptools==69.0.0
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps git+https://github.com/pennucci/PulsePortraiture.git@py3
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps git+https://github.com/bvgoncharov/enterprise_warp.git
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps pastamarkers ptarcade venny4py ruamel.yaml
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps git+https://github.com/nanograv/pint_pal.git
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps git+https://github.com/bencebecsy/pta_replicator.git
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps git+https://github.com/mraveri/tensiometer.git
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps git+https://github.com/vhaasteren/fastshermanmorrison.git
    micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps git+https://github.com/GersbachKa/defiant.git

    cp -r /opt/clockfiles/* /opt/micromamba/envs/PTA_env/share/tempo2/clock/


    # Install the package inside the micromamba environment
    #micromamba run -p /opt/micromamba/envs/PTA_env pip install --no-deps /opt/dr3_noise
    micromamba install -y -p /opt/micromamba/envs/PTA_env -c conda-forge --override-channels pywavelets lmfit

    # Clean up
    #micromamba clean -p /opt/micromamba/envs/PTA_env -y
    micromamba clean -a -y
    find /opt -name ".git" -type d -exec rm -rf {} +
    #rm -rf /opt/micromamba/pkgs/*


%runscript
    # Initialize micromamba environment
    # eval "$(micromamba shell hook -s bash)"
    source /opt/micromamba/etc/profile.d/micromamba.sh
    micromamba activate /opt/micromamba/envs/PTA_env
    #export PYTHONPATH=$PYTHONPATH:/opt/scripts/

    # Run the command passed to the container
    exec /opt/micromamba/envs/PTA_env/bin/python "$@"
