-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_mean_profiles.py
70 lines (44 loc) · 1.62 KB
/
extract_mean_profiles.py
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
"""
Script extracts hourly domain-mean profiles of
T,P,QV,QC,U,V,W
from a COSMO run.
and saves it into a new file.
"""
from netCDF4 import Dataset
import numpy as np
import os
if __name__=='__main__':
varnames = ['T','P','QV','QC','U','V']
tarvars = {varn : np.zeros(48,50) for varn in varnames}
#create target netcdf file atmos.nc
basepath = '/users/adeli/'
tarnc = Dataset(basepath+'atmos.nc','w')
#open src nc
srcpath = '/scratch/snx3000/adeli/project_B2/512x512_7Kkmnowind_1km'
srcpath +='/rawfiles/h0a0_test/60_homo/seed76996/output/'
coutputfiles = os.listdir(srcpath)
#extract hourly mean fields
# extract first day only
filter(lambda x: x.startswith('lfff01'),outputfiles)
coutputfiles.sort()
srcnc0 =Dataset(srcpath+coutputfiles[0])
#create dimensions
for dim in srcnc0.dimensions: # of type 'unicode'
sz = srcnc0.dimensions[dim].size # of type 'int'
tarnc.createDimension(dim, sz)
for var in varnames:
tarnc.createVariable(var,float,dimensions=('time','lev'))
ti = 0
for coutfl in coutputfiles:
# iterate over variables
srcnc = Dataset(srcpath+coutfl,'r')
for varn in varnames:
# extract domain mean
var = srcnc.variables[varn][:]
dommeanprof = np.mean(var,axis=(2,3))
# write to target
tarnc.variables[var][ti,:] = dommeanprof
ti+=0
srcnc.close()
# close target netcdf file
tarnc.close()