Commit bb9a3efc authored by Peters, Wouter's avatar Peters, Wouter
Browse files

new functionality added

parent 0f291269
#!/usr/bin/env python
# longterm_fluxes.py
"""
Author : peters
Revision History:
File created on 20 Dec 2012.
"""
import os
import sys
import datetime
from dateutil.relativedelta import relativedelta
import subprocess
def longterm_avg(rundir,avg):
""" Function to average a set of files in a folder from monthly to yearly means """
if avg not in ['transcom','olson','country','flux1x1']:
raise IOError,'Choice of averaging invalid'
if not os.path.exists(rundir):
raise IOError,'rundir requested (%s) does not exist, exiting...'%rundir
yeardir = os.path.join(rundir , 'data_%s_yearly'%avg )
files = os.listdir(yeardir)
longtermdir = os.path.join(rundir,'data_%s_longterm'%avg)
if not os.path.exists(longtermdir):
print "Creating new output directory " + longtermdir
os.makedirs(longtermdir)
sd = datetime.datetime(1999,1,1)
files = [f for f in files if avg in f]
dates = []
for filename in files:
date=datetime.datetime.strptime(filename.split('.')[-2],'%Y')
dates.append( date )
avg_files = [os.path.join(yeardir,k) for k in files]
if len(avg_files) > 0 :
command = ['ncra','-O']+ avg_files + [os.path.join(longtermdir,'%s_fluxes.%04d-%04d.nc'%(avg,dates[0].year, dates[-1].year))]
status = subprocess.check_call(command)
if __name__ == "__main__":
rundir = "/Storage/CO2/peters/ctdas-ei-gfed2-glb6x4-griddedNH/analysis"
try:
avg=sys.argv[1]
except:
avg='transcom'
longterm_avg(rundir,avg)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment