monthly_fluxes.py 2.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
# daily_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 monthly_avg(rundir,avg):
    """ Function to average a set of files in a folder from daily to monthly means """
    
    if avg not in ['transcom','olson']:
        raise IOError,'Choice of averaging invalid'
23
    if not os.path.exists(rundir):
24
25
        raise IOError,'rundir requested (%s) does not exist, exiting...'%rundir

26
    daydir = os.path.join(rundir , 'data_%s_daily'%avg)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    files  = os.listdir(daydir)
    monthdir = os.path.join(rundir,'data_%s_monthly'%avg)
    if not os.path.exists(monthdir):
        print "Creating new output directory " + monthdir
        os.makedirs(monthdir)
        sd = datetime.datetime(1999,1,1)
    else: 
        files_monthly = os.listdir(monthdir)
        files_monthly.sort()
        fm = files_monthly[-1]
        #sd = datetime.datetime(int(fm[9:13]),int(fm[14:16]),1)
        sd = datetime.datetime.strptime(fm.split('.')[-2],'%Y-%m')
    print sd
            
    files = [f for f in files if '-' in f]

    fileinfo = {}
    for filename in files:
        date=datetime.datetime.strptime(filename.split('.')[-2],'%Y-%m-%d')
        fileinfo[filename] = date

    years = [d.year for d in fileinfo.values()]
    months = set([d.month for d in fileinfo.values()])
    
    if sd > datetime.datetime(1999,2,1):
        sd = sd + relativedelta(months=+1)
    else: sd = datetime.datetime(min(years),1,1)
    ed = datetime.datetime(max(years)+1,1,1)

    while sd < ed: 

        nd = sd + relativedelta(months=+1)
        
        avg_files = [os.path.join(daydir,k) for k,v in fileinfo.iteritems() if v < nd and v >= sd]
        print sd, nd , len(avg_files)
       
        if len(avg_files) > 0: 
            command = ['ncra']+ avg_files + [os.path.join(monthdir,'%s_fluxes.%s.nc'%(avg,sd.strftime('%Y-%m')))]
        
            status = subprocess.check_call(command)

        sd = nd

if __name__ == "__main__":

    try:
73
74
75
76
77
78
        rundir = sys.argv[1]
    except:
        rundir  = "/Storage/CO2/ivar/zeus_output/ICDC9/ctdas_co2c13_terdisbcb_manscale/analysis/"

    try:
        avg=sys.argv[2]
79
80
    except:
        avg='transcom'
81
82

    monthly_avg(rundir,avg)