yearly_fluxes.py 1.92 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
23
24
25
26
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
73
74
75
76
#!/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 yearly_avg(rundir,avg):
    """ Function to average a set of files in a folder from monthly to yearly means """

    if avg not in ['transcom','olson']:
        raise IOError,'Choice of averaging invalid'
    if no os.path.exists(rundir):
        raise IOError,'rundir requested (%s) does not exist, exiting...'%rundir

    monthdir = rundir + 'data_%s_monthly'%avg
    files  = os.listdir(monthdir)
    yeardir = os.path.join(rundir,'data_%s_yearly'%avg)
    if not os.path.exists(yeardir):
        print "Creating new output directory " + yeardir
        os.makedirs(yeardir)
        sd = datetime.datetime(1999,1,1)
    else: 
        files_yearly = os.listdir(monthdir)
        files_yearly.sort()
            
    files = [f for f in files if '-' in f]

    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)

    while sd < ed: 

        nd = sd + relativedelta(years=+1)
        
        avg_files = [os.path.join(monthdir,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(yeardir,'%s_fluxes.%s.nc'%(avg,sd.strftime('%Y')))]
        
            status = subprocess.check_call(command)

        sd = nd



if __name__ == "__main__":

    
    rundir = "/Storage/CO2/ivar/zeus_output/ICDC9/ctdas_co2c13_terdisbcbmanscale_neweps449_test/analysis/"

    try:
        avg=sys.argv[1]
    except:
        avg='transcom'

    yearly_avg(rundir,avg)