海洋研究のための Python
  31 Mar. 2021
環境設定
Python3 のインストール
[Mac, Win]
以下のウェブサイトを開いて、 "Release version list" をクリックしてください。
https://www.python.org/downloads/
スクロールダウンして、ご自身の PC にあったインストーラーを選んでください。

[Mac]
ダウンロードして得られた python-3.7.4-macosx10.9.pkg ファイルをダブルクリックし、Python3 をインストールします。

[Win]
32 bit バージョンの Windows は、以下を選んでください。

Windows x86 web-based installer.

64 bit バージョンはこちらです。

Windows x86-64 web-based installer.

どちらのバージョンかわからない場合は、以下を参照してください: https://support.microsoft.com/ja-jp/help/958406)

その後、以下の様にパッケージをインストールしてください。


[Mac]
ターミナルは、以下にあります。

Applications > Utilities> Terminal.app

[Win]
Windows では、Cygwinを使います。Cygwin は以下を参照してインストールしてください。

Command prompt を利用しても良いです:

Program > Accessory > Command prompt

[Mac, Win]
Python3 がインストールできたか、確認しましょう。

[inouejun:examples]$ which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
[inouejun:examples]$ which python
/usr/bin/python
[inouejun:examples]$ python -V
Python 3.7.4


テキストエディタ

プログラミングでは、テキストエディタは非常に重要です。"find and replace"を使うことで、無用なプログラミングを避けることができます。

[Mac, Win]
BBEdit (https://www.barebones.com/products/bbedit/). Free Download をクリックしてください。BBEdit の使い方は、こちら をご覧ください。

[Win]
Notepad++ をご利用ください。

[Mac, Win]
テキストファイルを保存するときは、改行コードを Unix にしてください。

BBEdit

Notepad++

Edit > EOL Conversion


hello_world.py

exercise files をダウンロードしてください。以下のプログラムを使います。

exercises and examples/printing_text/examples/hello_world.py

以下の手順にしたがって、hello_world.py が入っているディレクトリに terminal を移動します。

[Mac, Cygwin]
examples ディレクトリをドラッグ&ドロップします。
[Command prompt]
アドレスをコピー&ペーストします。

[Mac, Win]
ターミナルで printing_text/examples ディレクトリに入れたか確認します。 pwd タイプします。

[inouejunmp:examples]$ pwd
/Users/inouejunmp/Downloads/exercises and examples/printing_text/examples

lsコマンドで、ディレクトリに入っているファイルを表示できます。

[inouejun:examples]$ ls
comment.py print_length.py
....

[Mac, Win]
エディタで hello_world.py を開きます。

以下のコマンドを入力します。

$ python3 hello_world.py

すると、以下の文章が返ってきます。

Hello world

基本的な準備と操作は、これで完了です。あとは Google 検索などを多用すれば、おおよそのことができるはずです。以下からは、ちょっと難しくなります。

折れ線グラフ
plot_python.tar.gz

以下のコマンドで pdf ファイルが作成されるはずです。

python3 plot_python.py

このスクリプトは、T. Sato さんから教えていただきました。ありがとうございます。(2020 年 10 月)

Cartopy を使った地図の作成

matplotlibCartopy をインストールする必要があります。 Cartopy を動かすのに必要なパッケージをいくつかインストールする必要があります。ここでは、plot_latlon.tar.gz を動かすために必要なパッケージをインストールします。

Mac へのインストール

Mac には割とスムーズにインストールできました。日々適当を参考にしました。

git clone https://github.com/OSGeo/proj.4.git
cd proj.4/
git checkout -b 4.9.0 tags/4.9.0
./configure
make
make install

brew install geos
pip3 install shapely cartopy --no-binary shapely --no-binary cartopy
pip3 install matplotlib
pip3 install scipy

pip3 install pandas
すると、
ImportError: NumPy 1.10+ is required to install cartopy. 
というメッセージが長文の間に出ましたが、numpy がなくても plot_latlon.py は動きました。

python3 plot_latlon.py
pip3 install xlrd==1.2.0
pip3 install openpyxl
python3 plot_latlon.py

以上で、plot_latlon.py が動きました。


Linux へのインストール

Linux (CentOS 7) へのインストールは、色々試してやっとできました (2021/3/1)。日々適当JURI*GISjupyter-notebook で〜によると、Cartpy をインストールするには、geos と proj.4 と言うパッケージが必要です。さらに、Cartopy を pip を使ってインストールする場合には、-no-binary と言うフラッグが必要です。

実際に、以下の例題にある plot_latlon.py を走らせると、エラーが出ました。

[pre jinoue:plot_latlon]$ python3 plot_latlon.py
python3: geos_ts_c.cpp:3612: int GEOSCoordSeq_getSize_r(GEOSContextHandle_t, const geos::geom::CoordinateSequence*, unsigned int*): Assertion `0 != cs' failed.
中止 (コアダンプ)

こちらの記事が参考になると思います。 以下のような操作を行うと良いそうです。

pip uninstall cartopy
pip uninstall shapely
pip install shapely cartopy --no-binary shapely --no-binary cartop

しかし、解決は一筋縄では行かないかもしれません。私の場合は geos のバージョンに問題があったようで、単に pip uninstall するだけでは解決しませんでした。

そこで、geos のバージョンを変化させることにしました。http://download.osgeo.org/geos/ をブラウザで開くと様々なバージョンがあるので、いくつか wget でダウンロードして試していくうちに、3.8.1 で上記のエラーが出ずに動くようになりました。Arial がない、と言うエラーが新たに出ましたが、plot_latlon.py が動いてアウトプット (pdf ファイル) は無事に得られました)。

wget wget http://download.osgeo.org/geos/geos-3.8.1.tar.bz2
tar -jxvf geos-3.8.1.tar.bz2
cd geos-3.8.1/
./configure
make
sudo make install

pip3 install shapely cartopy --no-binary shapely --no-binary cartopy


[pre jinoue:plot_latlon]$ python3 plot_latlon.py
findfont: Font family ['Arial'] not found. Falling back to DejaVu Sans.

以上です。

こちらも参考になると思います (2021 年 3 月)。

 

エラー対策

以下のようなエラーが出ました。

/usr/local/lib/python3.7/site-packages/cartopy/mpl/feature_artist.py in draw(self=<cartopy.mpl.feature_artist.FeatureArtist object>, renderer=<matplotlib.backends.backend_agg.RendererAgg object>, *args=(), **kwargs={})
123 return
124
=> 125 ax = self.get_axes()
126 feature_crs = self._feature.crs
127
ax undefined, self = <cartopy.mpl.feature_artist.FeatureArtist object>, self.get_axes undefined
AttributeError: 'FeatureArtist' object has no attribute 'get_axes'
args = ("'FeatureArtist' object has no attribute 'get_axes'",)
with_traceback = <built-in method with_traceback of AttributeError object>

太文字の部分をそのまま用いて Google 検索したところ、こちらを発見しました、

change 'ax = pc.get_axes()' to 'ax = pc.axes' will work !

と言う投稿があったので、これを真似て、
/usr/local/lib/python3.7/site-packages/cartopy/mpl/feature_artist.py の 125 行目を以下のようにコメントアウト & 変更したら動きました。

#ax = self.get_axes()
ax = self.axes

リンク

JURI*GIS

 PROJ などをソースコードからインストールする方法。

 




Orthographic
orthographic.tar.gz

以下のコマンドで pdf ファイルが作成されるはずです。

python3 orthographic.py



緯度経度を用いたプロット

python3 plot_latlon.py

plot_latlon.tar.gz
このスクリプトは、T. Sato さんから教えていただきました。(2020 年 10 月)


CTD data から緯度経度、温度、塩分データを抽出

ret_LatLon_Temp_Sal_CTD.tar.gz

(2020 年 11 月)


断面図
cross-section_python.tar.gz
PDF ファイルが作成されるまで、1 分弱かかります。
このスクリプトは、T. Sato さんから教えていただきました。(2020 年 10 月)
海面構造


map上に sea surface height と velosity (速度) を描画するためのスクリプト

SSH_python.tar.gz

dataset-duacs-nrt-global-merged-allsat-phy-1oct.nc は copernicus から入手できるそうです。このスクリプトは、T. Sato さんの作品です。(2020 年 10 月)
Small tips

List filesファイルをリストする

[Mac]
ls コマンド。

$ ls
coding_dna.txt sequences.fasta
genomic_dna.py writing_a_fasta_file.py

[Win]

dir コマンド.。

$ dir

上の階層に移動

$ cd ..

カレントディレクトリのパスを表示

$ pwd
xxxx/exercises and examples/reading_files/exercises

[Mac]
エディターでファイルを開く

$ open genomic_dna.py


リンク

matplotlib

CCHDO: CLIVAR and Carbon Hydrographic Data Office

スクリプス海洋研究所の機関。1996 年に設立。Diggs (2016) に概要がある。

CTD データフォーマット