File: nc2asc.py

package info (click to toggle)
cmor 3.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 16,960 kB
  • sloc: ansic: 28,094; f90: 13,872; python: 12,423; sh: 3,738; makefile: 111
file content (90 lines) | stat: -rwxr-xr-x 1,893 bytes parent folder | download | duplicates (5)
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
87
88
89
90
#!/usr/bin/env python

from __future__ import print_function
import cdms2
import sys
import genutil
cdms2.setAutoBounds('on')

type = 'd'

order = 'zxty'
# order='xty'
#order = None
#order= 'txy'
var = 'ta'
if len(sys.argv) > 2:
    fnm = sys.argv[1]
    fout = sys.argv[2]
else:
    fnm = '/ipcc/20c3m/atm/mo/%s/ncar_ccsm3_0/run1/%s_A1.20C3M_1.CCSM.atmm.1870-01_cat_1879-12.nc' % (
        var, var)
    fout = 'Test/%s.asc' % (var)

f = cdms2.open(fnm)

ntimes = 3
print('var:', var)
# s=f(var,time=slice(0,3),latitude=(-20,20),order=order,squeeze=1)
if order is not None:
    if order.find('z') > -1:
        s = f(var, time=slice(0, ntimes), order=order, squeeze=1,
              longitude=(-180, 180, 'con'), level=slice(5, 12))
        print(s.getLevel()[:])
    else:
        s = f(var, time=slice(0, ntimes), order=order,
              squeeze=1, longitude=(-180, 180, 'con'))
else:
    s = f(var, time=slice(0, ntimes), squeeze=1)
# s=s[:,::4,::4]
print('Read in', s.shape)
try:
    p = s.getLevel()
    p.id = 'pressure'
    p.units = 'Pa'
except BaseException:
    pass
# print genutil.minmax(s)
f.close()

f = open(fout, 'w')

ndim = s.rank()
print('Dumping')
print(s.id, file=f)
print(s.units, file=f)
print(ndim, file=f)

for i in range(ndim):
    ax = s.getAxis(i)
    print(len(ax), file=f)

for i in range(ndim):
    ax = s.getAxis(i)
    if ax.isLatitude():
        print('latitude', file=f)
    elif ax.isLongitude():
        print('longitude', file=f)
    else:
        print(ax.id, file=f)
    print(ax.units, file=f)
    print(ax.id)
    print(ax.units)
    for j in ax[:]:
        print(j, end=' ', file=f)
    print(file=f)
    for j in ax.getBounds().flat[:]:
        print(j, end=' ', file=f)
    print(file=f)
f.flush()

s = s.filled(120).astype(type)
s = s.flat
j = 0
for i in s[:]:
    print(i, end=' ', file=f)
    j += 1
print(file=f)
print(j, s[-1])

f.close()