In order to generate the earth model les IASP91.HED and IASP91.TBL, first run program REMODL,then program SETBRN. The program REMODL has the earth model hardwired. Note: These binary lesCANNOT be moved between platforms. They are included with SEISAN for each respective distribution.If lost, they must be regenerated on the same platform
分类: 学习
python与shell交互
之前用到了subprocess模块,但有人提出可能会导致阻塞,最近在网上看到了pexpect模块,操作起来就已经跟perl很像了。
import pexpect
child=pexpect.spawn("sac")
child.sendline("r test.sac")
child.sendline("div 100")
child.sendline("w over")
child.sendline("q")
python多任务,线程池
25个线程要做完1000个任务
import threading,time,random,os,threadpool
from shutil import copyfile
def dealing(cata):
print threading.current_thread().name
print 'entering ',cata
os.popen('sh DORFTN '+cata).read()
# os.chdir('..')
if __name__ == "__main__":
cata=[c for c in os.listdir('.') if os.path.isdir(c)]
pool=threadpool.ThreadPool(25)
requests=threadpool.makeRequests(dealing,cata)
[pool.putRequest(req) for req in requests]
pool.wait()
如果函数内有多个参数,可如下处理:
import threadpool, os, glob
out=[]
def processing(sample,trace):
tmp=sample.split('/')
tracefile=trace+'/'+tmp[-1]
ret=os.popen('./doublet_search %s %s' % (sample,tracefile)).read()
print(ret)
if __name__=='__main__':
traces=glob.glob('Trace/2*')
samples=glob.glob('Template/2*')
arglist=[]
for sample in samples:
Zs=glob.glob(sample+'/*.?HZ')
for Z in Zs:
sam=Z[0:-1]
for trace in traces:
arglist.append(([sam,trace],None))
# print(arglist)
pool_t=threadpool.ThreadPool(8)
requests=threadpool.makeRequests(processing,arglist)
[pool_t.putRequest(req) for req in requests]
pool_t.wait()
****注:一般在函数中不要用到os.chdir,因为chdir是全局的,如果要用,则要用绝对路径。
numpy数组数据更改
a[:,2:4]=10
a[a>5]=10
np.where(a>5,a,10) # if a>5, values equal to a themselves, else equal to 10
a.clip(3,10) # if a<3, =3, if a<10, =10
保持ssh链接
sudo vim /etc/ssh/ssh_config
ServiceAliveInterval 20
ServiceAliveCountMax 3
如何把包含中文字体的matplotlib图件保存为pdf文件
import matplotlib as mpl
from matplotlib.backends.backend_pdf import PdfPages
mpl.rcParams['pdf.fonttype']=42
mpl.rcParams['font.family']='WenQuanYi Zen Hei'
with PdfPages('test.pdf') as pdf:
xxxx # plot some figure
pdf.savefig()
#questions/45870858/runtimeerror-failed-to-open-truetype-font-in-matplotlib-backends-pdf-in-python stackoverflow
生成每周三的日期
import pandas as pd
import numpy as np
dates=pd.date_range('20180101','20181231',freq='W-WED')
# W-MON:从指定星期几开始算起,每周# 星期几缩写:MON/TUE/WED/THU/FRI/SAT/SUN
#转换日期
dates_array=dates.to_pydatetime()
dates_list=np.vectorize(lambda x: x.strftime('%Y-%m-%d'))(dates_array).
matplotlib限定colorbar的range
fig,ax=plt.subplots(figsize=(8,8))
cmap=plt.get_cmap('seismic')
cmap.set_bad(color='gray')
cb=ax.matshow(matdiff,cmap=cmap)
fig.colorbar(cb,ax=ax)
cb.set_clim(vmin=-1.0,vmax=1.0)
plt.show()
如何获取ndarray中索引以外的其它所有值
方法一:
import numpy as np
a = np.array([0,1,2,3,4,5,5,6,7,8,9])
print(a)
print(a.sum())
# [0 1 2 3 4 5 5 6 7 8 9]
# 50
a = np.ma.array(a, mask=False)
a.mask[3] = True
print(a)
print(a.sum())
# [0 1 2 -- 4 5 5 6 7 8 9]
# 47
方法二
a_new = np.delete(a, 3, 0)
这个3可以是个索引列表
C字符串
想把一列台站名输出成一行,用空格隔开,写了个C。
fgets(buff,1024,fp);
strncpy(temp,buff,strlen(buff)-1);
printf('%s ',temp);
发现当台站名由长变短时,会“继承”上一个台站的后面几个字符,比如前一个台站名为L5317,而后一个为Y02时,前一个正确,而后一个会变成Y0217.怀疑是temp保存了上一次的’\0’的位置,所以在每次printf后强行temp[0]=’\0’,发现没用,考虑是上次的字符串终止符还在。
我试了以下几种方法,都行:
1 每次printf后,temp[strlen(a)-1]=’0’;
2 用malloc给temp分配内存,读到了新的,就realloc一次,记得最后free掉; temp=(char *)realloc(temp,sizeof(char)*(strlen(buff)-1))
3 直接把buff数组的最后一个值换成’\0’,然后输出buff。 buff[strlen(buff)-1]=’\0′