From ce382ad73fea38a1ba962e48c77d8dcf98f672c6 Mon Sep 17 00:00:00 2001
From: Wouter Peters <wouter.peters@wur.nl>
Date: Mon, 14 Jul 2014 14:13:43 +0000
Subject: [PATCH] updated time_avg_fluxes to work properly during cycles

---
 da/analysis/time_avg_fluxes.py | 36 +++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/da/analysis/time_avg_fluxes.py b/da/analysis/time_avg_fluxes.py
index 1748b0e..d0a17a0 100755
--- a/da/analysis/time_avg_fluxes.py
+++ b/da/analysis/time_avg_fluxes.py
@@ -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')
-- 
GitLab