【发布时间】:2016-07-12 05:06:49
【问题描述】:
1。简介
我有一堆 netcdf 格式的文件。
每个文件都包含不同时期某地的气象状况(每小时数据)。
我需要为每个文件提取前 12 小时的数据。所以我选择使用NCO(netcdf operator)来处理。
NCO 适用于终端环境。使用>ncks -d Time 0,11 input.nc output.nc,我可以获得一个名为out.nc的数据文件,其中包含in.nc的前12小时数据。
2。我的尝试
我想将所有进程保存在我的 ipython 笔记本中。但我坚持两个方面。
如何在python循环中执行终端代码
如何将python中的字符串转入终端代码。
例如,这是我的假代码。
files = os.listdir('.')
for file in files:
filename,extname = os.path.splitext(file)
if extname == '.nc':
output = filename + "_0-12_" + extname
## The code below was my attempt
!ncks -d Time 0,11 file output`
3。结论
基本上,我的目标是让假代码!ncks -d Time 0,11 file output 成真。这意味着:
- 直接在python循环中执行netcdf运算符...
- ...使用
filename,这是python环境中的一个字符串。
抱歉我的问题不清楚。任何建议将不胜感激!
【问题讨论】:
-
"
...input.nc output.nc,我可以得到一个名为 out.nc 的数据文件,其中包含 in.nc 的前 12 小时数据" 我可以假设这种文件不一致是错字吗? -
我试过
os.system("my command here")和subprocess.call([])。他们都未能实现。 -
我的意思是你看过文档吗,
subprocess.call不会让你只使用ncks而不指定完整路径或shell=True -
谢谢你们!我在 ipython notebook 环境中尝试过
subprocess.call和subprocess.check_output。第二个有效。但它们都在 python 控制台中运行良好。
标签: python linux loops terminal netcdf