我正在运行一个Python命令+ API,以访问ECMWF(欧洲中距离天气预报中心)数据服务器(称为MARS)并下载一些文件(天气数据)。我启动与外壳的Python代码(我使用csh)做./python_script.py
的壳
如果我下载一个文件(range(1999, 2000)
脚本中带有1999年,而不是range(1998, 2000)
下面的Python脚本失败示例),则运行良好。现在,我想下载其中的许多文件,从而使它们循环播放数年。
我的问题是,似乎Python脚本不会等待shell命令/ API完成并在下一年继续使用。这会导致错误。生成文件,但大小为零。
我想知道是否可以指定Python脚本,以便在继续进行下一个for / loop步骤之前等待在shell窗口中找到一些关键字。
我知道在那种情况下我正在使用某些特定的API,并且可能会找到另一种特定于API的解决方案来执行此操作,但是对我来说,在外壳中标识一些打印输出似乎更容易。
例如,“传输速率”似乎仅在作业完成后才出现在shell窗口中,请参阅我从shell保存的日志(最后成功的行)(带有)./python_script.py >& log_file.log
。
我的PYTHON代码是:
#!/usr/bin/env python
for year in range(1998, 2000):
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
server.retrieve({
"class": "e2",
"dataset": "era20c",
"date": '%d-07-01/%d-07-02' % (year,year),
"domain": "g",
"area" : "12/-72/-67/22",
"grid" : "1.0/1.0",
"expver": "1",
"param": "214.140/233.140",
"step": "3/9/15/21",
"format" : "netcdf",
"stream": "wave",
"target": '/home/nicolas/hycom/hycom_data/ECMWF/ERA20C/forecast/%d/test_era20c_wave_set1.nc' % (year),
"time": "06",
"type": "fc",
})
我仅记录一个文件的日志最后几行(成功):
2016-02-13 16:00:21 Request is complete
2016-02-13 16:00:21 Transfering 239.441 Kbytes into /home/nicolas/hycom/hycom_data/ECMWF/ERA20C/forecast/1999/test_era20c_wave_set1.nc
2016-02-13 16:00:21 From http://stream.ecmwf.int/data/atls04/data/data01/scratch/_grib2netcdf-atls04-95e2cf679cd58ee9b4db4dd119a05a8d-JLUk0w.nc
2016-02-13 16:00:28 Transfer rate 32.6278 Kbytes/s
不需要在循环中导入,也可能是indentaiton问题。如果您的脚本如您所提供的那样,retrieve
则不在循环中。python中的缩进非常重要。
尝试通过以下方式重写脚本:
#!/usr/bin/env python
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
for year in range(1998, 2000):
server.retrieve({
"class": "e2",
"dataset": "era20c",
"date": '%d-07-01/%d-07-02' % (year,year),
"domain": "g",
"area" : "12/-72/-67/22",
"grid" : "1.0/1.0",
"expver": "1",
"param": "214.140/233.140",
"step": "3/9/15/21",
"format" : "netcdf",
"stream": "wave",
"target": '/home/nicolas/hycom/hycom_data/ECMWF/ERA20C/forecast/%d/test_era20c_wave_set1.nc' % (year),
"time": "06",
"type": "fc",
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句