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

updated time_avg_fluxes to work properly during cycles

parent b32c5216
......@@ -30,10 +30,8 @@ def time_avg(dacycle,avg='transcom'):
daily_avg(dacycle,avg)
if new_month(dacycle):
monthly_avg(dacycle,avg)
if new_year(dacycle):
yearly_avg(dacycle,avg)
longterm_avg(dacycle,avg)
......@@ -105,6 +103,10 @@ def monthly_avg(dacycle,avg):
files = os.listdir(daydir) # get daily files
files = [f for f in files if '-' in f]
if len(files) < 28:
print 'No month is yet complete, skipping monthly average'
return
fileinfo = {}
for filename in files: # parse date from each of them
date=datetime.datetime.strptime(filename.split('.')[-2],'%Y-%m-%d')
......@@ -120,11 +122,17 @@ def monthly_avg(dacycle,avg):
nd = sd + relativedelta(months=+1)
ndays_in_month = (nd-sd).days
avg_files = [os.path.join(daydir,k) for k,v in fileinfo.iteritems() if v < nd and v >= sd]
if len(avg_files) > 0:
if len(avg_files) != ndays_in_month: # only once month complete
#print 'New month (%02d) is not yet complete, skipping monthly average'%(sd.month)
pass
else:
targetfile = os.path.join(monthdir,'%s_fluxes.%s.nc'%(avg,sd.strftime('%Y-%m')))
if not os.path.exists(targetfile):
print "New month (%02d) is complete, I have %d days for the next file"%(sd.month,ndays_in_month)
command = ['ncra','-O']+ avg_files + [targetfile]
status = subprocess.check_call(command)
else:
......@@ -149,6 +157,10 @@ def yearly_avg(dacycle,avg):
files = os.listdir(monthdir) # get monthly files
files = [f for f in files if '-' in f]
if not files:
print "No full year finished yet, skipping yearly average..."
return
fileinfo = {}
for filename in files:
date=datetime.datetime.strptime(filename.split('.')[-2],'%Y-%m')
......@@ -165,12 +177,14 @@ def yearly_avg(dacycle,avg):
avg_files = [os.path.join(monthdir,k) for k,v in fileinfo.iteritems() if v < nd and v >= sd]
if len(avg_files) > 0 :
if not len(avg_files) == 12 :
print "Year %04d not finished yet, skipping yearly average..."%sd.year
else:
targetfile = os.path.join(yeardir,'%s_fluxes.%s.nc'%(avg,sd.strftime('%Y')))
if not os.path.exists(targetfile):
print "Year %04d is complete, I have 12 months for the next file"%sd.year
command = ['ncra','-O']+ avg_files + [targetfile]
print command
status = subprocess.check_call(command)
sd = nd
......@@ -193,6 +207,10 @@ def longterm_avg(dacycle,avg):
files = os.listdir(yeardir)
files = [f for f in files if avg in f]
if not files:
print "No full year finished yet, skipping longterm average..."
return
dates = []
for filename in files:
date=datetime.datetime.strptime(filename.split('.')[-2],'%Y')
......
Supports Markdown
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