according to google searching result, the environment variable need to be set again in python script. the method is:
new_env['OCSSWROOT'] = '/usr/local/seadas-7.5.3/ocssw'
returnCode = subprocess.call(l2genshell , env=new_env,shell=True)
there is a problem that environment variables in OCSSW_bash.env can not be add to new_env by using SOURCE command line.
But there is few question/answer to demonstrate how to parse the xxx.env file (OCSSW_bash.env) in python.
How should i add this environment variable in python script? THANKS
If you can't implement your processing tasks using bash scripts, the
$OCSSWROOT/scripts/ocssw_runnershell script addresses your problems setting the environment for OCSSW programs from python. This script sets
$OCSSWROOT/OCSSW_bash.env, and then runs the supplied OCSSW program and arguments. You might find plumbum helpful.
the detail is:
my os and software environment: VMware workstation Pro 14, ubuntu16.04 X64, seadas7.5.3, anaconda/python3.7.3, pycharm education 2019
my scheme is that using “l2gen” to process MODIS L1B data (MOD021KM.**hdf) by python script.
(1) The following coding can work in bash shell terminal:
==========================the command lines=========================================
l2gen ifile=/mnt/hgfs/share/test/MOD021KM.A2009033.0810.061.2017295060325.hdf geofile=/mnt/hgfs/share/test/MOD03.A2009033.0810.061.2017294135803.hdf ofile=/mnt/hgfs/share/test/MOD021KM.A2009033.0810.061._seadas.hdf
the perfect result file "ofile" is available.
(2) But similar coding cannot work in python script?
==========================the python script: MODIS-l2gen====================================
with open('/mnt/hgfs/share/test/processing_log.txt','w') as f:
for i,ifile in enumerate(ifiles):
maskland = 1
cloudland = 1
l2prod = 'La_748 La_859 La_869 La_vvv Rrs_vvv angstrom aot_nnn brdf brdf_748 brdf_859 brdf_869\
l2gencmd1='l2gen'+' ifile='+ ifile +' geofile='+ geofile +' ofile='+ ofile +' slat='+ \
str(slat)+' slon='+str(slon)+' elat='+str(elat)+' elon='+str(elon)+\
l2gencmd2 = 'l2gen' + ' ifile=' + ifile + ' geofile=' + geofile + ' ofile=' + ofile
l2cmdtest = 'l2gen -h'
#new_env = os.environ.copy()
#new_env['OCSSWROOT'] = '/usr/local/seadas-7.5.3/ocssw'
returnCodetest = subprocess.call(l2cmdtest, shell=True)
returnCode2 = subprocess.call(l2gencmd2, shell=True)
returnCode1 = subprocess.call(l2gencmd1 ,shell=True)
/bin/sh: 1: l2gen: not found
(3) the question:
I don't know why the python script did not can work. I have set environment according https://seadas.gsfc.nasa.gov/downloads/
source ~/.bashrcand test the
l2genin terminal, if it is okay, python should work as well.
Did you put the OCSSW environment variable settings in your
~/.bashrc? After making changes to
~/.bashrcit is best to log out and back to ensure all the running programs have consistent environment variable settings. Even with the settings in your
~/.bashrcthey may not be available when Python is started outside a terminal (e.g., using an IDE like pyCharm). In your python IDE try:
If these aren't correct then you could try the
ocssw_runnerscript to run your processing commands.
I have solved this problem. It is an environment variable setting problem in python script. Because the "source $OCSSWROOT/OCSSW_bash.env" is a temporary setting in my Linux OS (i do not know the reason). Therefore, I have to set this environment variable group in the python script. the following code is to solve this problem.
commands = 'source $OCSSWROOT/OCSSW_bash.env;l2gen -h'
process = subprocess.Popen(commands,executable='/bin/bash', stdin=subprocess.PIPE, stdout=subprocess.PIPE,shell=True)
out, err = process.communicate()
where: commands includes two command lines: 'source $OCSSWROOT/OCSSW_bash.env' and 'l2gen -h'. Note that executable is '/bin/bash'.
Thank you very much!
Thank Seadas developing group and ocean color community!