Skip to content
Snippets Groups Projects
Commit ce382ad7 authored by Peters, Wouter's avatar Peters, Wouter
Browse files

updated time_avg_fluxes to work properly during cycles

parent b32c5216
No related branches found
No related tags found
No related merge requests found
......@@ -30,13 +30,11 @@ def time_avg(dacycle,avg='transcom'):
daily_avg(dacycle,avg)
if new_month(dacycle):
monthly_avg(dacycle,avg)
monthly_avg(dacycle,avg)
if new_year(dacycle):
yearly_avg(dacycle,avg)
yearly_avg(dacycle,avg)
longterm_avg(dacycle,avg)
longterm_avg(dacycle,avg)
def new_month(dacycle):
""" check whether we just entered a new month"""
......@@ -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')
......@@ -119,12 +121,18 @@ def monthly_avg(dacycle,avg):
while sd < ed:
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,13 +157,17 @@ 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')
fileinfo[filename] = date
years = set([d.year for d in fileinfo.values()])
sd = datetime.datetime(min(years),1,1)
ed = datetime.datetime(max(years)+1,1,1)
......@@ -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')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment