Use the CASA Toolkit in your own Python environment

Overview

CASA (Common Astronomy Software Application) and its core CASA Toolkit are very useful for data analysis such as ALMA, but to use casapy (actually, IPython shell generated by casapy.py) ) Must be executed. For this reason, it cannot be executed in its own Python environment, and it is difficult to perform interactive and highly reproducible analysis with, for example, Jupyter notebook (IPython notebook). So, let's copy the CASA Toolkit on Mac OS X and make it available as a module in your own Python environment [^ linux].

[^ linux]: This method was created by the author as a Mac OS X version with reference to here. Thing. It is not an officially supported method.

How to copy

The CASA Toolkit has been imported into casapy as a module called casac, so copy the relevant scripts (* .py) and shared libraries (* .so, * .dylib). However, this is not enough, and you need to change the path of the Python shared library used by each shared library to the path of your own Python environment. Add PYTHONPATH and CASAPATH so that Python can read the last copied casac.py.

The series of work ... should be completed by executing the following shell script [^ cltools] [^ pyver] [^ casaver] [^ dependencies]. Before execution, customize the CASA path (casaapp), copy destination path (ʻoutput), and * PATH export destination (profile`) to suit your environment. Of course, be sure to make a backup before executing!

$ ./extract_casatk_osx.sh

[^ cltools]: Xcode Command Line Tools are required to run the script. [^ pyver]: If you have multiple Python environments such as pyenv, select Python 2.7.x before running. This is to match the Python version inside CASA. [^ casaver]: The script is written according to the directory structure of CASA on Mac OS X, so it will not work properly on Linux. In addition, we have confirmed the operation only with CASA 4.5.0. [^ dependencies]: Some tools depend on the data inside CASA (casa-data) and will not work if you uninstall or move the source CASA (such as ʻia). CASAPATH` is set for these.

Module import

All you have to do now is run Python and import the casac. For example, the CASA image related tool (ʻia`) can be used by specifying as follows!

$ python # or ipython, jupyter notebook
>>> import casac
>>> ia = casac.casac.image()
>>> ia.open('casaimage.image')
True

Finally

If you just want to put a third-party library in CASA, you can also use casa-pip casa-pip.

Shell script

extract_casatk_osx.sh


casaapp=/Applications/CASA.app # customize this!
outputdir=${HOME}/Documents    # customize this!
profile=~/.bash_profile        # customize this!

echo "CASA directory:  " ${casaapp}
echo "Output directory:" ${outputdir}
echo -n "Press any key to continue >"
read INPUT

echo "Step 1: creating casatk directory"
casart=${casaapp}/Contents
casatk=${outputdir}/casatk
mkdir -p ${casatk}/Frameworks
mkdir -p ${casatk}/Resources/python/__casac__

echo "Step 2: copying libraries and Python scripts from CASA app"
echo "        this may take a while ..."
cp -a ${casart}/Frameworks/lib ${casatk}/Frameworks
cp -a ${casart}/Frameworks/*.jar ${casatk}/Frameworks
cp -a ${casart}/Frameworks/*.dylib ${casatk}/Frameworks
cp -a ${casart}/Frameworks/Qt*.framework ${casatk}/Frameworks
cp -a ${casart}/Resources/python/casac.py ${casatk}/Resources/python
cp -a ${casart}/Resources/python/__casac__/*.so ${casatk}/Resources/python/__casac__
cp -a ${casart}/Resources/python/__casac__/*.py ${casatk}/Resources/python/__casac__

echo "Step 3: changing Python-framework's path of libraries"
libpy_so=@loader_path/../../../Frameworks/Python.framework/Versions/2.7/Python
libpy_dylib=@loader_path/lib/Python.framework/Versions/2.7/Python
libpy_user=`python -c "import glob; from distutils import sysconfig;
print(glob.glob(sysconfig.get_config_var('LIBDIR')+'/libpython*')[0])"`

for f in ${casatk}/Resources/python/__casac__/*.so ; do
    install_name_tool -change ${libpy_so} ${libpy_user} ${f}
done

for f in ${casatk}/Frameworks/*.dylib ; do
    install_name_tool -change ${libpy_dylib} ${libpy_user} ${f}
done

echo "Step 4: exporting PYTHONPATH and CASAPATH to profile"
casapath="${casart} darwin socorro-\$(uname -p) \$(hostname -s)"
pythonpath="${casatk}/Resources/python"
echo "export CASAPATH=\"${casapath}\"" >> ${profile}
echo "export PYTHONPATH=\$PYTHONPATH:${pythonpath}" >> ${profile}

echo "Done!"

Recommended Posts

Use the CASA Toolkit in your own Python environment
Easily use your own functions in Python
Use Python in your environment from Win Automation
[Python] logging in your own module
Create your own Linux commands in Python
[Python] When you want to import and use your own package in the upper directory
Get your own IP address in Python
How to use the C library in Python
Use the latest pip in a virtualenv environment
Don't use readlines () in your Python for statement!
Import your own modules in Grasshopper's Python development
Use jupyter-lab installed in python virtual environment (venv)
Use Python in Anaconda environment with VS Code
Use the LibreOffice app in Python (3) Add library
python: Use your own class for numpy ndarray
Use pydantic when reading environment variables in Python
Use os.getenv to get environment variables in Python
Use a free GPU in your favorite environment
Use dates in Python
Use Valgrind in Python
virtual environment in python
Use profiler in Python
Development environment in Python
I want to use Python in the environment of pyenv + pipenv on Windows 10
Create your own Big Data in Python for validation
Create your own Random Dot Stereogram (RDS) in Python.
Try to improve your own intro quiz in Python
Let's use the open data of "Mamebus" in Python
[Road to intermediate Python] Define in in your own class
Call your own python module from the ROS package
How to use the model learned in Lobe in Python
I want to use the R dataset in python
Let's use def in python
Use let expression in Python
Use Anaconda in pyenv environment
Use Measurement Protocol in Python
Download the file in Python
Use callback function in Python
Find the difference in Python
Use parameter store in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Handle environment variables in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Organize your Python development environment
Use Spyder in Python IDE
Get your heart rate from the fitbit API in Python!
Check the operation of Python for .NET in each environment
How to use the __call__ method in a Python class
Commands often used in the development environment during Python implementation
Run the intellisense of your own python library with VScode.
How to use pyenv and pyenv-virtualenv in your own way
After enabling the python virtual environment in the batch file, run the python file
[Python] Make your own LINE bot
Getting the arXiv API in Python
Wrap (part of) the AtCoder Library in Cython for use in Python
Use fabric as is in python (fabric3)
Python in the browser: Brython's recommendation
Save the binary file in Python