time_avg_fluxes.py 1.76 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
#!/usr/bin/env python
# time_avg_fluxes.py

"""
Author : peters 

Revision History:
File created on 20 Dec 2012.

"""
import sys
sys.path.append('../../')
import os
import sys
import shutil
import datetime
from da.analysis.daily_fluxes import daily_avg
from da.analysis.monthly_fluxes import monthly_avg
from da.analysis.yearly_fluxes import yearly_avg

def time_avg(dacycle,avg='transcom'):
    """ Function to create a set of averaged files in a folder, needed to make longer term means """
    
    if avg not in ['transcom','olson','country']:
        raise IOError,'Choice of averaging invalid'

    analysisdir = dacycle['dir.analysis']

    if not os.path.exists(analysisdir):
        raise IOError,'analysis dir requested (%s) does not exist, exiting...'%analysisdir

    daily_avg(analysisdir,avg)

    if new_month(dacycle):
        monthly_avg(analysisdir,avg)

    if new_year(dacycle):
        yearly_avg(analysisdir,avg)

def new_month(dacycle):
    """ check whether we just entered a new month"""

    this_month = dacycle['time.start'].month
    prev_month = (dacycle['time.start']-dacycle['cyclelength']).month

    return (this_month != prev_month)

def new_year(dacycle):
    """ check whether we just entered a new year"""

    this_year = dacycle['time.start'].year
    prev_year = (dacycle['time.start']-dacycle['cyclelength']).year

    return (this_year != prev_year)

if __name__ == "__main__":

    from da.tools.initexit import CycleControl

    sys.path.append('../../')

    dacycle = CycleControl(args={'rc':'../../ctdas-ei-gfed2-glb6x4-griddedNH.rc'})
    dacycle.setup()
    dacycle.parse_times()

    while dacycle['time.end'] < dacycle['time.finish']:

        time_avg(dacycle,avg='transcom')
69
        time_avg(dacycle,avg='olson')
70
71
        dacycle.advance_cycle_times()