Call l2bin with Python

Use this Forum to find information on, or ask a question about, NASA Earth Science data.
Post Reply
Posts: 12
Joined: Thu Nov 04, 2021 6:34 pm America/New_York

Call l2bin with Python

by m_brown » Thu Nov 11, 2021 2:23 pm America/New_York


Apologies if this question appears twice - it's not clear if the initial submission went through.

I have some questions re. the mechanics of calling SeaDAS functions from Python code. I am currently working with l2bin, so I'll tailor my questions to that:

Q1: I have written a Python wrapper function for l2bin that I call from other Python code which allows me to call l2bin and pass it arguments. What is the proper way to call SeaDAS functions from Python code? I currently do the following:

cmdInL2 = 'ifile='+inL2File
cmdOutL3 = 'ofile='+outL3File
cmdRes = 'resolution=Q'
cmdProd = 'l3bprod=chlor_a'

cmd = ['l2bin', cmdInL2, cmdOutL3, cmdRes, cmdProd]

Is there a more straightforward way than using the subprocess module?

Q2: Can I specify all of the l2bin arguments (e.g. input file(s), resolution, products) in a single parameter file (e.g. par.txt) that I pass to l2bin in this call: "l2bin par=par.txt"? Can the parameter file even contain the output file? How are l2bin arguments defined that aren't specified by the user through the command line or a user parameter file (e.g. are there default values or a sensor-specific parameter file that also gets called to cover any arguments not defined by the user)?



OB SeaDAS - xuanyang02
Subject Matter Expert
Subject Matter Expert
Posts: 362
Joined: Tue Feb 09, 2021 5:42 pm America/New_York
Answers: 1

Re: Call l2bin with Python

by OB SeaDAS - xuanyang02 » Fri Nov 12, 2021 4:54 pm America/New_York

I think you are doing the right way to call 'l2bin' from python code. There is an example of calling 'l2bin' in $OCSSWROOT/bin/

def run_l2bin(proc):
Set up for and perform L2 binning.
prog = os.path.join(proc.ocssw_bin, 'l2bin')
if not os.path.exists(prog):
print ("Error! Cannot find executable needed for {0}".\
args = 'infile=' + proc.input_file
args += ' ofile=' + proc.output_file
args += ' ' + get_options(proc.par_data)
cmd = ' '.join([prog, args])

logging.debug('Running l2bin cmd: ' + cmd)
if cfg_data.verbose:
print ('l2bin cmd: ' + cmd)
ret_val = execute_command(cmd)
if ret_val != 0:
if os.path.exists(proc.output_file):
msg = '-I- The l2bin program returned a status value of {0}. Proceeding with processing, using the output l2 bin file {1}'.format(ret_val, proc.output_file)
ret_val = 0
msg = '-I- The l2bin program produced a bin file with no data. No further processing will be done.'
return ret_val

You can do

Code: Select all

l2bin par=l2bin*.par
If you do l2bin in GUI, you can click "Save parameters" to save your argument to a parfile. An example par file could be


There is a $OCSSWROOT/share/common/l2binmatch_defaults.par to cover any arguments not defined by the user.

Posts: 669
Joined: Fri Jan 29, 2021 5:51 pm America/New_York
Answers: 2

Re: Call l2bin with Python

by gnwiii » Sat Nov 13, 2021 9:06 pm America/New_York

m_brown wrote: Thu Nov 11, 2021 2:23 pm America/New_York Apologies if this question appears twice - it's not clear if the initial submission went through.
I have also made posts that seem to have vanished.

I think your basic question has been answered, but I assume you are using Python to script batch processing, so plan to run l2gen many times. One thing I have learned doing batch processing with OCSSW tools is that there will be glitches. After a few years, more code was devoted to error checks than the actual processing, but the software was catching issues quickly and usually there was enough detail to identify the underlying issue. Keeping detail processing logs (e.g., l2gen output) is helpful when questions are raised about a particular image. The multilevel_processor has basic checks that you would do well to copy. It is also helpful to collect statistics. Timing details for each processing step can be helpful for planning. Flag percentages (from l2gen output) are also useful for future reference, and outliers in these values can help identify problems.

Post Reply