-
Notifications
You must be signed in to change notification settings - Fork 1
/
nc2png_cli.py
174 lines (159 loc) · 6.94 KB
/
nc2png_cli.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 9 19:41:58 2020
@author: smrak@bu.edu
"""
import xarray, os, glob, datetime
import numpy as np
import matplotlib.pyplot as plt
import cartomap.geogmap as gm
import subprocess
import cartopy.crs as ccrs
from scipy import ndimage
from dateutil import parser
from argparse import ArgumentParser
import platform
projection = 'ortographic'
mlat_levels = [-65, -75, 60, 75]
sep = os.sep
def main(idir=None, wl=None, alt_km=None, odir=None, tlim=None,
laplace=0,auroral_oval=0,clabel=0, srad=1.0,
eta: bool = False,
lat0 = None, lon0 = None, save=1):
cmax = 10 if clabel else 25
clw = 1 if clabel else 0.5
if auroral_oval:
apx=1
else:
apx = 0
# print (odir)
if odir is None:
if wl != 'geo':
if laplace:
odir = os.path.join(idir, "{}_{}_{}_lap{}".format(wl, alt_km, int(eta), sep))
else:
odir = os.path.join(idir, "{}_{}_{}{}".format(wl, alt_km, int(eta), sep))
else:
if laplace:
odir = os.path.join(idir, "{}_{}_{}_lap{}".format(wl, srad, alt_km, sep))
else:
odir = os.path.join(idir, "{}_{}_{}{}".format(wl, srad, alt_km, sep))
if not os.path.exists(odir):
print (odir)
if platform.system() == "Windows":
subprocess.call('mkdir "{}"'.format(odir), timeout=2, shell=True)
else:
subprocess.call('mkdir -p "{}"'.format(odir), timeout=2, shell=True)
assert os.path.exists(odir)
if wl != 'geo':
EOFF = np.array(sorted(glob.glob(idir + "*_{}km_{}_{}*.nc".format(alt_km, wl, int(eta)))))
else:
EOFF = np.array(sorted(glob.glob(idir + "*_{}km_{}_{}.nc".format(alt_km, wl, srad))))
f_times = []
for f in EOFF:
try:
f_times.append(parser.parse(os.path.split(f)[1][:14]))
except:
pass
f_times = np.array(f_times)
assert (f_times.size > 0)
if tlim is None:
idt = np.ones(f_times.size,dtype=bool)
else:
tlim = [parser.parse(tlim[0]), parser.parse(tlim[1])]
assert len(tlim) == 2
idt = (f_times >= tlim[0]) & (f_times <= tlim[1])
#
F = EOFF[idt]
#
for i,f in enumerate(F):
t = f_times[idt][i]
save_fn = odir + "{}_{}_{}.png".format(t.strftime("%Y%m%d_%H%M%S"), alt_km, wl)
EOF = xarray.open_dataset(f)
if wl != 'geo':
try:
t_sdo = EOF.time_sdo.values.astype('datetime64[s]').astype(datetime.datetime).strftime("%Y-%m-%d %H:%M")
except:
t_sdo = EOF.time_image.values.astype('datetime64[s]').astype(datetime.datetime).strftime("%Y-%m-%d %H:%M")
title = '{}, Alt = {} km\nT-Image = {}'.format(t, EOF.alt_km.values, t_sdo)
else:
title = '{}, Alt = {} km'.format(t, EOF.alt_km.values)
fig, ax = gm.plotCartoMap(projection=projection, lon0=lon0, lat0=lat0,
figsize=[8,5],
lonlim=[-180,180], latlim=[-90,90],
title = title,
meridians=np.arange(-180,180.1,40), parallels=np.arange(-80,81,20),
background_color='grey',
# apex=apx, mlat_levels=mlat_levels, mlat_colors='b',
mlat_labels=0)
sza = EOF.sza.values
night = np.copy(sza)
inight = (sza > 90)
if laplace:
lap = abs(ndimage.laplace(EOF.of.values))
lap[lap<0.001] = np.nan
lapm = 0.1
lap[lap>lapm] = lapm
night[inight] = 0
night[~inight] = 1
OF = ax.pcolormesh(EOF.glon.values, EOF.glat.values, EOF.of.values, cmap='gray',
vmin=0, vmax=1,
transform=ccrs.PlateCarree())
try:
ax.contour(EOF.glon.values, EOF.glat.values, lap, cmap='nipy_spectral',
levels=np.linspace(0.005, lapm, 20),
transform=ccrs.PlateCarree())
except:
pass
if clabel:
try:
OFC = ax.contour(EOF.glon.values, EOF.glat.values, EOF.of.values,
cmap='jet', linewidths=clw,
levels=np.linspace(0.0, 1.0, cmax),
transform=ccrs.PlateCarree())
if clabel:
ax.clabel(OFC, OFC.levels, inline=True)
except:
pass
else:
OF = ax.pcolormesh(EOF.glon.values, EOF.glat.values, EOF.of.values, cmap='gray',
vmin=0, vmax=1,
transform=ccrs.PlateCarree())
try:
Zcont = EOF.of.values
Zcont[inight] = np.nan
OFC = ax.contour(EOF.glon.values, EOF.glat.values, Zcont,
colors='r', linewidths=clw,
levels=np.linspace(np.nanmin(Zcont), 1.0, cmax),
transform=ccrs.PlateCarree())
if clabel:
ax.clabel(OFC, OFC.levels, inline=True, fmt='%.2f')
except:
pass
posn0 = ax.get_position()
cax = fig.add_axes([posn0.x0+posn0.width+0.01, posn0.y0, 0.02, posn0.height])
fig.colorbar(OF, cax=cax, label='EOF',format='%.2f')
EOF.close()
if save:
fig.savefig(save_fn)
plt.close(fig)
else:
break
if __name__ == '__main__':
p = ArgumentParser()
p.add_argument('folder', help='input folder', type=str)
p.add_argument('--wl', help='eclipse wavelength. If uniform type geo', default='geo')
p.add_argument('--odir', help='Save images to:', default=None)
p.add_argument('--altkm', help='altitude in km', default = 150, type=int)
p.add_argument('--tlim', help='time limit, 2 args "start" "stop"', nargs=2, default=None, type=str)
p.add_argument('--srad', help='altitude in km', default = 1.0, type=float)
p.add_argument('--eta', help='Parallactic angle = ?', action='store_true')
p.add_argument('--lon0', help='Map center: longitude default = 0', type=int, default=0)
p.add_argument('--lat0', help='Map center: Latitude default = 0', type=int, default=0)
p.add_argument('--laplace', help='Plot Laplace?', action='store_true')
p.add_argument('--oval', help='Plot auroral oval?', action='store_true')
p.add_argument('--clabel', help='Render countor labels?', action='store_true')
P = p.parse_args()
main(idir=P.folder, wl=P.wl, alt_km=P.altkm, odir=P.odir, tlim=P.tlim,
laplace=P.laplace, auroral_oval=P.oval, clabel=P.clabel, srad=P.srad,
lat0=P.lat0, lon0=P.lon0, eta=P.eta, save=1)