diff --git a/python/Makefile b/python/Makefile new file mode 100755 index 0000000..4e53d14 --- /dev/null +++ b/python/Makefile @@ -0,0 +1,40 @@ +SHELL = /bin/sh + +.SUFFIXES: +.SUFFIXES: .cpp .o + +cxxflags += -fPIC -O3 +ldflags += -shared +bindir = gatdaem1d + +srcdir = ../src +objdir = ./obj +includes = -I$(srcdir) -I$(FFTW_ROOT)/include -I../submodules/cpp-utils/include +libs = -L$(FFTW_ROOT)/lib -lfftw3 +library = $(bindir)/gatdaem1d.so + +all: compile link +allclean: clean compile link + +objects = \ + $(objdir)/gatdaem1d.o \ + +$(objects): $(objdir)/%.o: $(srcdir)/%.cpp + mkdir -p $(objdir) + @echo ' ' + @echo Compiling $< + $(cxx) -c $(includes) $(cxxflags) $< -o $@ + +compile: $(objects) + +link: $(objects) + mkdir -p $(bindir) + @echo ' ' + @echo Creating library + $(cxx) $(ldflags) $(objects) $(libs) -o $(library) + +clean: + @echo ' ' + @echo Cleaning + rm -f $(objects) + rm -f $(library) \ No newline at end of file diff --git a/python/README.md b/python/README.md index b424f50..b710b2d 100644 --- a/python/README.md +++ b/python/README.md @@ -4,23 +4,20 @@ The Python (>=v3.5) interface consists of a C/C++ shared library (.so on Linux or .dll on Windows) called gatdaem1d which contains time-domain forward modelling and derivative functions which are called by the Python interpreter. ## Compiling and installing the C/C++ shared libraries -First the shared library needs to be built with CMake. See one of the CMake build scripts in the root directory of the ga-aem source code repository. If you are only interested in the Python interface, you need only build the ***`python_bindings`*** target. +Ensure that the GNU C++ compiler is available, and FFTW has been installed. -## Install directory contents -After being built successfully the install directory should contain, -- [ga-aem-install-dir]/python contains the package set up or installation function `setup.py`. -- [ga-aem-install-dir]/python/gatdaem1d contains the file `__init__.py` which is the package's Python classes and function code. It is also where the compiled shared library will reside after compilation. - - On Linux the shared library is [ga-aem-install-dir]/python/gatdaem1d/gatdaem1d.so. - - On Windows the shared library is [ga-aem-install-dir]/python/gatdaem1d/gatdaem1d.dll. -- [ga-aem-install-dir]/python/examples contains example Python usage code. +Mac OSX currently has issues using cmake and brew installed gcc compilers. So we are stuck using a Makefile. Simply type "make" in the python folder. On Linux, the easiest option is to use "make" also. + +Two environment variables need to be set before compilation using "export cxx=g++" and "export FFTW_ROOT=". + +On Windows, follow the documentation in the root folder. Once the library is compiled, go ahead and pip install. ## PIP install of the Python package - To install as a python package you can then, ```bash - cd [ga-aem-install-dir]/python - python -m pip install . + pip install . ``` -- Note that **`python`** may need to be **`python3`** on your system. +- Note that **`python`** may need to be **`python3`** on your system, and make sure the correct environment is activated/sourced. ## Examples - The directory [ga-aem-install-dir]/python/examples contains an example of how to use the gatdaem1d package. diff --git a/python/gatdaem1d/__init__.py b/python/gatdaem1d/__init__.py index 551e6ae..15e548e 100644 --- a/python/gatdaem1d/__init__.py +++ b/python/gatdaem1d/__init__.py @@ -11,12 +11,9 @@ #Function to load the shared library def load_library(): - import platform; - if(platform.system() == "Windows"): - ext = '.dll' - else: - ext = '.so' - libname = os.path.join(os.path.dirname(os.path.realpath(__file__)),"gatdaem1d"+ext) + files = os.listdir(os.path.dirname(os.path.realpath(__file__))) + libname = [file for file in files if 'gatdaem1d' in file][0] + libname = os.path.join(os.path.dirname(os.path.realpath(__file__)),libname) lib = ctypes.CDLL(libname) return lib; diff --git a/python/pyproject.toml b/python/pyproject.toml new file mode 100644 index 0000000..c8a692f --- /dev/null +++ b/python/pyproject.toml @@ -0,0 +1,29 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "gatdaem1d" +version = "2.0.1" +description = "Time-domain airborne electromagnetic forward modelling" +readme = { file = "README.md", content-type = "text/markdown" } +requires-python = ">=3.10" +authors = [{ name = "Ross C Brodie", email = "ross.c.brodie@ga.gov.au" }] +keywords = ["electromagnetic", "geophysics"] +dependencies = [ + "numpy", + "matplotlib" +] +classifiers = [ + 'License :: OSI Approved', + 'License :: OSI Approved :: GNU General Public License v2 (GPLv2)', + 'Programming Language :: Python :: 3', + 'Topic :: Scientific/Engineering :: Physics', + 'Topic :: Electromagnetic :: Airborne :: Forward modelling :: Geophysics' +] + +[tool.setuptools.packages.find] +where = ["."] + +[tool.setuptools.package-data] +gatdaem1d = ['gatdaem1d.*'] \ No newline at end of file diff --git a/python/setup.py b/python/setup.py deleted file mode 100644 index 93d2208..0000000 --- a/python/setup.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python -# Setup Script -# Author: Leon Foks -# March 10 2017 -# Updated: Ross C Brodie, March 20 2017 -# Updated: Ross C Brodie, March 21 2024 to Version 2.0 -# Updated: Leon Foks, June 5 2024. Added .dylib -import sys -import os -from os.path import join - -# Test Python's version -major, minor = sys.version_info[0:2] -if (major, minor) < (3, 5): - sys.stderr.write('\nPython 3.5 or later is needed to use this package\n') - sys.exit(1) - -try: - from setuptools import setup -except ImportError: - pass - -setup(name='gatdaem1d', - packages=['gatdaem1d'], - package_dir={'gatdaem1d':'gatdaem1d'}, - package_data={'gatdaem1d':['gatdaem1d.so', 'gatdaem1d.dll', 'gatdaem1d.dylib']}, - scripts=[], - version='2.0.1', - description='Time-domain airborne electromagnetic forward modelling.', - long_description='Time-domain airborne electromagnetic forward modelling. Python interface to C++ library for forward model and derivative calculations for airborne electromagnetic (AEM) systems used in geophysics.', - classifiers=[ - 'Development Status :: 4 - Beta', - 'License :: OSI Approved :: GNU General Public License v2 (GPLv2)', - 'Programming Language :: Python :: 3.5', - 'Topic :: Scientific/Engineering :: Physics', - 'Topic :: Electromagnetic :: Airborne :: Forward modelling :: Geophysics', - ], - author='Ross C Brodie, Geoscience Australia and Leon Foks', - author_email='ross.c.brodie at ga.gov.au', - install_requires=[ - 'numpy>=1.11', - ], - url='https://github.com/GeoscienceAustralia/ga-aem', - license='GNU General Public License v2 (GPLv2)')