我有一个与另一个程序接口的程序来处理数据。我编写的程序有三个主要部分(由下面的###行分隔)。当我运行整个程序时,它不起作用(特别是第二个子进程调用不执行并生成一个摘要文件,程序的第三部分负责处理)。但是,每当我将它们分成三个独立的程序并在终端中运行它们时,一切都很好。有关如何将其作为一个程序运行的任何提示或建议吗?
#### part 1:
import glob
import subprocess
import os
import datetime
import matplotlib.pyplot as plt
import csv
import re
import ntpath
x = open('data.txt', 'w')
m = open('graphing_data.txt', 'w')
ckopuspath= '/Volumes/DAVIS/sfit-ckopus/ckopus'
command_init = 'sfit4Layer0.py -bv5 -fh'
subprocess.call(command_init.split(), shell=False)
for line in open('sfit4.ctl', 'r'): # write in later specific directory line
p = line.strip()
if p.startswith('band.1.nu_start'):
a,b = p.split('=')
b = float(b)
b = "{0:.3f}".format(b)
lowwav = b
lowwav = float(lowwav)
if p.startswith('band.1.nu_stop'):
a,b = p.split('=')
b = float(b)
b = "{0:.3f}".format(b)
highwav = b
highwav = float(highwav)
with open('/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/info.txt', 'rt') as infofile: # the info.txt file created by CALPY
for count, line in enumerate(infofile):
with open('t15asc.4', 'w') as t:
lat = re.search('Latitude of location:\s*([^;]+)', line, re.IGNORECASE).group(0)
lat = lat.split()
lat = lat[3]
lat = float(lat)
lon = re.search('Longitude of location:\s*([^;]+)', line, re.IGNORECASE).group(0)
lon = lon.split()
lon = lon[3]
lon = float(lon)
date = re.search('Time of measurement \(UTC\): ([^;]+)', line).group(0)
date = date.split()
yeardate = date[4]
yeardate = yeardate.split('-')
year = int(yeardate[0])
month = int(yeardate[1])
day = int(yeardate[2])
time = date[5]
time = time.split(':')
hour = int(time[0])
minute = int(time[1])
second = time[2]
second = second.split('.')
second = int(second[0])
dur = re.search('Duration of measurement \[s\]: ([^;]+)', line).group(0)
dur = dur.split()
dur = float(dur[4])
numpoints = re.search('Number of values of one scan:\s*([^;]+)', line, re.IGNORECASE).group(0)
numpoints = numpoints.split()
numpoints = float(numpoints[6])
fov = re.search('semi FOV \[rad\] :\s*([^;]+)', line, re.IGNORECASE).group(0)
fov = fov.split()
fov = fov[3]
fov = float(fov[1:])
sza = re.search('sun Azimuth \[deg\]:\s*([^;]+)', line, re.IGNORECASE).group(0)
sza = sza.split()
sza = float(sza[3])
snr = 0.0000
roe = 6396.2
res = 0.5000
calpy_path = '/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/140803/*' # the CALPY output files!
files1 = glob.glob(calpy_path)
with open(files1[count], 'r') as g:
ints = []
ints_count = 0
for line in g:
wave_no, intensity = [float(item) for item in line.split()]
if lowwav <= wave_no <= highwav:
ints.append(str(intensity))
ints_count = ints_count + 1
spacebw = (highwav - lowwav)/ ints_count
d = datetime.datetime(year, month, day, hour, minute, second)
t.write('{:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}{:>12.5f}'.format(sza,roe,lat,lon,snr)) # line 1
t.write("\n")
t.write('{:>10d}{:>5d}{:>5d}{:>5d}{:>5d}{:>5d}'.format(year,month,day,hour,minute,second)) # line 2
t.write("\n")
t.write( ('{:%Y/%m/%d %H:%M:%S}'.format(d)) + "UT Solar Azimuth:" + ('{:>6.3f}'.format(sza)) + " Resolution:" + ('{:>6.4f}'.format(res)) + " Duration:" + ('{:>6.2f}'.format(dur))) # line 3
t.write("\n")
t.write('{:>22.12f}{:>22.12f}{:>22.12e}{:>15d}'.format(lowwav,highwav,spacebw,ints_count)) # line 4
t.write("\n")
t.write('\n'.join(map(str, ints)) + '\n')
######################################## part 2:
command_two = 'sfit4Layer0.py -bv5 -fs'
subprocess.call(command_two.split(), shell=False) #writes the summary file
######################################## part 3
infile = '/Volumes/DAVIS/calpy_em27_neu/spectra_out_demo/summary'
lines = open(infile, 'r').readlines()
line_number = 0
line4= lines[line_number + 3]
variables = line4.split(" ")
sample = variables[1]
time = variables[2]
Z = variables[3]
A = variables[4]
D = variables[5]
R = variables[6]
P = variables[7]
V = variables[8]
E = variables[9]
x.write('{0} {1} {2} {3} '.format(sample, time, Z, A))
time = time[:-2]
numofgas = lines[line_number + 5]
numofgas = int(numofgas.strip())
x.write(str(numofgas))
count2 = 0
while count2 < numofgas:
gasinfo = lines[line_number + 7 + count2]
data = gasinfo.split()
gasnum = data[0]
gasname = data[1]
IFPRF = data[2]
apprcol = data[3]
retcol = data[4]
x.write(' {0} {1}'.format(gasname, retcol))
m.write('{0},{1}\n'.format(time, retcol))
count2 = count2 + 1
line17 = lines[line_number + 10 + count2]
print(numofgas)
params = line17.split()
bandstart = params[1]
bandstop = params[2]
space = params[3]
nptsb = params[4]
pmax = params[5]
fovdia = params[6]
meansnr = params[7]
x.write(' {0} {1} {2} {3} '.format(bandstart, bandstop, nptsb, meansnr))
line21 = lines[line_number + 14 + count2]
info = line21.split()
fitrms = info[0]
chi2 = info[1]
dofsall = info[2]
dofstrg = info[3]
iter = info[4]
maxxiter = info[5]
x.write('{0} {1} {2} {3} {4}'.format(fitrms, chi2, dofsall, dofstrg, iter))
x.write('\n')
x.close()
m.close()