Skip to content Skip to sidebar Skip to footer

Using All Elements Of A List As Argument To A System Command (netCDF Operator) In A Python Code

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 oper

Solution 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.

Solution 2:

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)

Post a Comment for "Using All Elements Of A List As Argument To A System Command (netCDF Operator) In A Python Code"