複数設定の計算 multiple settings calc
bdir=os.getcwd()
for index,dirname in enumerate(dirnames):
os.chdir(bdir+'/'+dirname)
os.system('ipython ../dir1_emeof_jraeof.py')
# --- coding: utf-8 --
import os.path, os
dirnames=['global', 'tropics']
eof_area=[int(item) for item in str_eof_area[1:-1].split(',')]
(3)の例(全体) ------------------------------
(2)の例(部分) ------------------------------
in_file = open('./eof_area.txt', 'r')
str_eof_area=in_file.read()
in_file.close()
print dirname
print eof_area
print eof_area2
eof_area2=[int(item) for item in str_eof_area[1:-1].split(',')]
# 計算領域設定が読めることのテスト
in_file = open(dirname+'/eof_area.txt', 'r')
str_eof_area=in_file.read()
in_file.close()
# EOF計算領域設定とテキストファイルへの書き出し
eof_area=[latss[index],latns[index]]
str_eof_area=str(eof_area)
out_file = open(dirname+'/eof_area.txt', 'w')
out_file.write(str_eof_area)
out_file.close()
for index,dirname in enumerate(dirnames):
if not os.path.exists(dirname): # なかったら作る
os.mkdir(dirname)
dirnames=['global', 'tropics']
latss= [ -90, -30]
latns= [ 90, 30]
設定を変えた複数の計算を行うことは多い.たとえば,EOF解析で解析領域を変える計算を10通り行うといった場合だ.この場合に以下のようにすると,プログラムの効率が高く,またミスも起きづらいと思う.
1)複数ディレクトリを作り,それぞれのディレクトリに計算設定を,テキストファイルで書き込み,そのプログラムでテストの読み込みも行う.
2)解析スクリプトを,計算設定をcurrentディレクトリから読み込むように作る.
3)(2)の解析スクリプトを,動かす親スクリプトを作り,その親スクリプトはos.chdirでディレクトリを移動して,それぞれのディレクトリで解析スクリプトを子プロセスとして動かす.
1)の例(全部) ------------------------------
# --- coding: utf-8 --
import os.path, os
Often we need to multiple calculation using different settings. For example, one may conduct EOF analysis for 10 different regions. In such a case, the following approach is efficient for program development, and relatively safe for mistakes.
1)We write a script that produces multiple directories and write setting in text file.
2)We prepare a analysis script that reads setting from the current directory.This script can be run by itself or can be called as a child script from a parent script.
3)We write a parent script that changes current directory and run the child script.
### Example of (1) (whole script)
import os.path, os
dirnames=['global', 'tropics']
latss= [ -90, -30]
latns= [ 90, 30]
for index,dirname in enumerate(dirnames):
if not os.path.exists(dirname):
os.mkdir(dirname)
# set EOF analysis region and write it out to a text file.
eof_area=[latss[index],latns[index]]
str_eof_area=str(eof_area)
out_file = open(dirname+'/eof_area.txt', 'w')
out_file.write(str_eof_area)
out_file.close()
# read-test of the setting from a text file.
in_file = open(dirname+'/eof_area.txt', 'r')
str_eof_area=in_file.read()
in_file.close()
eof_area2=[int(item) for item in str_eof_area[1:-1].split(',')]
print dirname
print eof_area
print eof_area2
### Example of (2) (only corresponding part)
in_file = open('./eof_area.txt', 'r')
str_eof_area=in_file.read()
in_file.close()
eof_area=[int(item) for item in str_eof_area[1:-1].split(',')]
### Example of (3) (whole script)
import os.path, os
dirnames=['global', 'tropics']
bdir=os.getcwd()
for index,dirname in enumerate(dirnames):
os.chdir(bdir+'/'+dirname)
os.system('ipython ../dir1_emeof_jraeof.py')