0

I've a python code performs some operator on some netCDF files. It has names of netCDF files as a list. I want to calculate ensemble average of these netCDF files using netCDF operator ncea (the netCDF ensemble average). However to call NCO, I need to pass all list elements as arguments as follows:

 filelist = [file1.ncf file2.ncf file3.ncf ........ file50.ncf]

ncea file1.ncf file2.ncf ......file49.ncf file50.ncf output.cdf

Any idea how this can be achieved.

ANy help is greatly appreciated.

Vakratund
  • 169
  • 1
  • 2
  • 10

2 Answers2

1
import subprocess
import shlex
args = 'ncea file1.ncf file2.ncf ......file49.ncf file50.ncf output.cdf'
args = shlex.split(args)
p = subprocess.Popen(args,stdout=subprocess.PIPE)
print p.stdout # Print stdout if you need.
Rikka
  • 999
  • 8
  • 19
-1

I usually do the following:

Build a string containing the ncea command, then use the os module to execute the command inside a python script

import os

out_file = './output.nc'

ncea_str = 'ncea '
for file in filelist:
    ncea_str += file+' '

 os.system(ncea_str+'-O '+out_file)

EDIT:

import subprocess

outfile = './output.nc'
ncea_str = '{0} {1} -O {2}'.format('ncea', ' '.join(filelist), out_file)
subprocess.call(ncea_str, shell=True)
N1B4
  • 3,377
  • 1
  • 21
  • 24
  • The documentation of `os.system()` suggests using the `subprocess` module instead. Also repeatedly concatenating strings this way may be inefficient as strings are immutable and the intermediate results start getting longer and each iteration more bytes are copied around. More idiomatic would be `' '.join(filenames)`. – BlackJack Aug 13 '14 at 16:10
  • `subprocess` will spawn a new process, we don't want that in this case. We just want to execute a command line operation. Good point about using `join`. – N1B4 Aug 13 '14 at 18:04
  • `os.system()` also starts at least one new process: a shell. And when the argument starts programs, in this case `ncea`, another process is started from that shell. So directly executing `ncea` with `subprocess` without an intermediate shell process is one less process. – BlackJack Aug 14 '14 at 12:25
  • 1
    Why the `shell=True`? It starts an additional shell process and you have to be sure `ncea_str` never contains anything that any system shell thinks is something special. Even spaces in file names are ”special” and would break this call. `subprocess.call(['ncea'] + file_names + ['-O', out_filename])` is one less process and more robust. – BlackJack Aug 19 '14 at 06:35
  • New to `subprocess`, didn't realize you could omit the `shell=True` (saw an example using it). Appreciate the feedback. – N1B4 Aug 25 '14 at 20:16