Skip to content

Instantly share code, notes, and snippets.

@Eastonium
Last active August 29, 2015 14:08
Show Gist options
  • Save Eastonium/f6f4bcc4e335e1b07c8a to your computer and use it in GitHub Desktop.
Save Eastonium/f6f4bcc4e335e1b07c8a to your computer and use it in GitHub Desktop.
import glob
import os
#This is for editing ECOR RAW files
path = '/data/home/gervais/prod/jobs/1121/collection/twp/twpecorE31.00'
os.chdir(path)
for filename in glob.glob(os.path.join(path, '*.flx')):
print filename;
with open(filename,'r') as file:
data = file.readlines();
length = int(len(data));
for i in range(length):
if data[i].startswith("Flux"):#This is used to find the header of a certain line of data, then modify the line after it.
explode = data[i+1].split();
data[i+1] = data[i+1].replace(explode[1], str(-float(explode[1])));
data[i+1] = data[i+1].replace(explode[2], str('%.3e'%float(-9999)));#This line replaces a value with the fill value "-9999" in the proper form
#print data[i] + data[i+1];
elif data[i].startswith("Average"):
explode = data[i].split();
if explode[6].startswith("-"):
data[i] = data[i].replace(explode[6], " "+explode[6][1:]);
else:
data[i] = data[i].replace(" "+explode[6], "-"+explode[6]);
if explode[7].startswith("-"):
data[i] = data[i].replace(explode[7], str('%.3e'%float(-9999)));
else:
data[i] = data[i].replace(" "+explode[7], str('%.3e'%float(-9999)));
#print data[i];
elif data[i].startswith("Dump2: "):#These if statements find "Dump" lines and accomidate if the first value on the line starts with a "-"
explode = data[i].split();
if explode[5].startswith("-"):
data[i] = data[i].replace(explode[5], " "+explode[5][1:]);
else:
data[i] = data[i].replace(" "+explode[5], "-"+explode[5]);
if explode[6].startswith("-"):
data[i] = data[i].replace(explode[6], str('%.3e'%float(-9999)));
else:
data[i] = data[i].replace(" "+explode[6], str('%.3e'%float(-9999)));
#print data[i];
elif data[i].startswith("Dump2:"):
explode = data[i].split();
if explode[4].startswith("-"):
data[i] = data[i].replace(explode[4], " "+explode[4][1:]);
else:
data[i] = data[i].replace(" "+explode[4], "-"+explode[4]);
if explode[5].startswith("-"):
data[i] = data[i].replace(explode[5], str('%.3e'%float(-9999)));
else:
data[i] = data[i].replace(" "+explode[5], str('%.3e'%float(-9999)));
#print data[i];
elif data[i].startswith("Dump8: "):
explode = data[i].split();
if explode[2].startswith("-"):
data[i] = data[i].replace(explode[2], " "+explode[2][1:]);
else:
data[i] = data[i].replace(" "+explode[2], "-"+explode[2]);
if explode[3].startswith("-"):
data[i] = data[i].replace(explode[3], str('%.3e'%float(-9999)));
else:
data[i] = data[i].replace(" "+explode[3], str('%.3e'%float(-9999)));
#print data[i];
elif data[i].startswith("Dump8:"):
explode = data[i].split();
if explode[1].startswith("-"):
data[i] = data[i].replace(explode[1], " "+explode[1][1:]);
else:
data[i] = data[i].replace(" "+explode[1], "-"+explode[1]);
if explode[2].startswith("-"):
data[i] = data[i].replace(explode[2], str('%.3e'%float(-9999)));
else:
data[i] = data[i].replace(" "+explode[2], str('%.3e'%float(-9999)));
#print data[i];
with open(filename,'w') as file:
file.writelines(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment