02_sam.py 2.87 KB
Newer Older
v99 Workshop's avatar
v99 Workshop committed
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
77
78
79
80
81
82
83
84
85
86
"""
author: Christoph Heim
date:	20.06.2019
"""
import os, glob, subprocess
import numpy as np
from datetime import datetime, timedelta
from multiprocessing import Pool
from cdo import Cdo

def run_cdo(inp_file, out_file, dt, var_name):
    #print('-sellevidx,1/28 -setdate,'+'{:%Y-%m-%d}'.format(dt)+
    #                    ' -settime,'+'{:%H:%M:%S}'.format(dt)+' '+inp_file)
    #print('-sellevidx,1/28 -settaxis,'+'{:%Y-%m-%d,%H:%M:%S,3hour}'.format(dt)+
    #                    ' '+inp_file)

    tmp_file = '../SCu/data/SAM_RAW/'+var_name+'_tmp_{:%d%H}'.format(dt)+'.nc'

    bash_command = ('ncatted -O -a units,lon,o,c,degrees_east ' + 
                    '-a units,lat,o,c,degrees_north '+inp_file+
                    ' '+tmp_file)

    process = subprocess.Popen(bash_command.split(), stdout=subprocess.PIPE)
    output, error = process.communicate()

    ofile = cdo.sellonlatbox(270,280,-24,-14,
                 #input=('-sellevidx,1/28 '+inp_file),
                 #input=('-sellevidx,1/28 -setdate,'+'{:%Y-%m-%d}'.format(dt)+
                 #       ' -settime,'+'{:%H:%M:%S}'.format(dt)+' '+inp_file),
                 input=('-sellevidx,1/28 -settaxis,'+
                        '{:%Y-%m-%d,%H:%M:%S,3hour}'.format(dt)+
                       ' '+tmp_file),
                 output=out_file)

if __name__ == '__main__':
    #################
    var_names = ['QC', 'QV', 'TABS']

    spl_inds = {
        'QC':{'start':-16,'end':-6}, 
        'TABS':{'start':-18,'end':-8}, 
    }

    model = 'SAM'
    res 	= 4

    out_base_dir = os.path.join('/work','ka1081','2019_06_Hackathon_Mainz',
                                'christoph_heim','SCu','data')
    data_dir = os.path.join('/work','ka1081','DYAMOND')
    dt = 7.5
    base_time = datetime(2016,8,1)
    #################

    cdo = Cdo()

    var_name = 'TABS'
    #var_name = 'QC'

    model_name = model + '-' + str(res) + 'km'
    inp_dir	= os.path.join(data_dir, model_name, 'OUT_3D')
    model_path = os.path.join(out_base_dir, model + '-' + str(res)+'km')

    print(os.path.join(inp_dir, '*_'+var_name))
    inp_files_glob = glob.glob(os.path.join(inp_dir, '*_'+var_name+'.nc'))
    times = [base_time + timedelta(seconds=dt*int(
                f[spl_inds[var_name]['start']:spl_inds[var_name]['end']])) \
                             for f in inp_files_glob]
    #times = np.sort(np.asarray(times))

    for ind,inp_file in enumerate(inp_files_glob):
        dt = times[ind]
        #print('{:%Y%m%d}'.format(dt))
        if dt >= datetime(2016,8,11,0) and dt < datetime(2016,8,15,0):
            print(inp_file)
            print(dt)
            print()

            out_file = os.path.join(model_path,'{:%Y%m%d}'.format(dt),
                                    var_name+'_{:%H}'.format(dt)+'.nc')

            print(out_file)
            if not os.path.exists(out_file):
                run_cdo(inp_file, out_file, dt, var_name)
            #quit()