This guide helps users migrate from the original Fortran/C++ 3DEX to py3dex.
export CFITSIO=/path/to/cfitsio
export HEALPIX=/path/to/healpix
export LAPACK=/path/to/lapack
export BLAS=/path/to/blas
./configure
makepip install py3dex| Fortran Module | Python Module |
|---|---|
| f3dex_utils | py3dex.utils |
| f3dex_transforms | py3dex.transforms |
| f3dex_stats | py3dex.stats |
| f3dex_cosmotools | py3dex.cosmotools |
| f3dex_fitstools | py3dex.fitstools |
| survey.cpp | py3dex.survey |
Most function names are preserved, with these conventions:
- Fortran UPPERCASE → Python lowercase
- Underscores preserved
- Array indexing: Fortran 1-based → Python 0-based
Examples:
! Fortran
CALL gen_kln(kln, nnmax, nlmax, rmax)
CALL gen_cln(cln, kln, nnmax, nlmax, rmax)
CALL BJL(l, x, jl)# Python
kln = transforms.gen_kln(nnmax, nlmax, rmax)
cln = transforms.gen_cln(kln, nnmax, nlmax, rmax)
jl = transforms.bjl(l, x)! Fortran: 1-based indexing
REAL(DP), DIMENSION(1:nnmax, 0:nlmax, 0:nmmax) :: almn
almn(1, 0, 0) = value# Python: 0-based indexing
almn = np.zeros((nnmax, nlmax+1, nmmax+1), dtype=complex)
almn[0, 0, 0] = value! Fortran
CALL write_almn(filename, almn, header, nnmax, nlmax, nmmax)# Python
fitstools.write_almn(filename, almn, header)| Fortran/C++ | Python |
|---|---|
bin/survey2almn |
py3dex-survey2almn |
bin/survey2almn_lm |
py3dex-survey2almn-lm |
bin/survey2almn_interactive |
py3dex-survey2almn-interactive |
bin/almnfile2rmap |
py3dex-almnfile2rmap |
bin/compute_qln |
py3dex-compute-qln |
Arguments remain the same:
# Fortran
bin/survey2almn survey.txt almn.fits cln.fits 20 20 256 2000.0
# Python
py3dex-survey2almn survey.txt almn.fits cln.fits 20 20 256 2000.0Python version produces compatible FITS files:
- almn files: Binary tables with real/imaginary extensions
- cln files: Binary tables with kln optional extension
- Maps: HEALPix format via healpy
Files can be read by both versions.
Format unchanged:
# phi theta r (mass)
1.234 0.567 456.78
2.345 1.678 567.89
...
| Fortran/C++ | Python |
|---|---|
OpenMP (!$OMP) |
multiprocessing / Numba parallel |
MPI (mpirun) |
mpi4py |
- Python version may use more memory for large datasets
- Use
survey2almn_lmfor nside > 1024
- First run slower due to Numba JIT compilation
- Subsequent runs comparable to Fortran
- Use
JAXbackend for GPU acceleration (experimental)
Fortran:
PROGRAM survey2almn
USE f3dex_transforms
USE f3dex_fitstools
INTEGER :: nnmax=20, nlmax=20, nside=256
REAL(DP) :: rmax=2000.0
REAL(DP), ALLOCATABLE :: survey(:,:), kln(:,:), cln(:,:)
COMPLEX(DP), ALLOCATABLE :: almn(:,:,:)
CALL read_survey('survey.txt', survey)
CALL gen_kln(kln, nnmax, nlmax, rmax)
CALL gen_cln(cln, kln, nnmax, nlmax, rmax)
CALL survey2almn_opt(nside, nnmax, nlmax, nlmax, survey, nbpts, almn, kln)
CALL write_almn('almn.fits', almn, nnmax, nlmax, nlmax)
END PROGRAMPython:
import numpy as np
from py3dex import transforms, fitstools
nnmax, nlmax, nside, rmax = 20, 20, 256, 2000.0
survey = np.loadtxt('survey.txt')
kln = transforms.gen_kln(nnmax, nlmax, rmax)
cln = transforms.gen_cln(kln, nnmax, nlmax, rmax)
almn = transforms.survey2almn_simple(
survey, len(survey), nside, nnmax, nlmax, nlmax, kln
)
fitstools.write_almn('almn.fits', almn)C++:
survey surv;
surv.fromSurveyFile("params.txt", true);
surv.applySelectionFunction(selFunc);Python:
from py3dex.survey import Survey
surv = Survey()
surv.from_parameter_file("params.txt", load_data=True)
surv.apply_selection_function(sel_func)- Fortran: Uses
REAL(DP)(typically float64) - Python: Uses NumPy float64 by default
- Results should match to machine precision
- Fortran: Direct HEALPix library calls
- Python: Uses healpy wrapper
- Some advanced HEALPix features may differ
- Fortran: Uses STOP and error codes
- Python: Uses exceptions (try/except)
Currently not implemented in Python:
- Some optimized assembly routines
- Direct MPI decomposition (use mpi4py wrapper)
- Specific compiler optimizations
# If you get import errors
import sys
sys.path.insert(0, '/path/to/3DEX/src')
import py3dex# Ensure Numba is installed
pip install numba
# For first-run compilation
import py3dex.transforms
# Warm up JIT
_ = py3dex.transforms.bjl(0, 1.0)# Use low-memory version
py3dex-survey2almn-lm survey.txt almn.fits cln.fits 20 20 1024 2000.0For migration issues, please:
- Check this guide
- See README_PYTHON.md
- Open an issue on GitHub
Improvements to this migration guide are welcome!