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.
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.
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
If you just want to put a third-party library in CASA, you can also use casa-pip casa-pip.
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