8 from PIL
import Image
as im
9 from PIL
import ImageDraw
10 from PIL
import ImageFilter
11 from PIL
import ImageMath
12 from PIL
import ImageEnhance
13 from PIL
import ImageOps
as imo
14 from PIL
import ImageColor
as imc
16 import viirs_files
as vf
19 import matplotlib
as mpl
20 import matplotlib.pyplot
as plt
21 from matplotlib
import cm
34 wlstr = [
"410",
"445",
"490",
"550",
"670",
"865",
"1240",
"1610",
"2250"]
39 RGB2YCbCr = np.array([[0.257, 0.504, 0.098],[-0.148, -0.291, 0.439],[0.439, -0.368, -0.071]])
40 YCbCr2RGB = np.array([[1.0, 0.0, 1.596],[1.0, -0.392, -0.813],[1.0, 2.017, 0.0]])
43 y = RGB2YCbCr[0,0]*r + RGB2YCbCr[0,1]*g + RGB2YCbCr[0,2]*b
44 u = RGB2YCbCr[1,0]*r + RGB2YCbCr[1,1]*g + RGB2YCbCr[1,2]*b
45 v = RGB2YCbCr[2,0]*r + RGB2YCbCr[2,1]*g + RGB2YCbCr[2,2]*b
49 r = YCbCr2RGB[0,0]*y + YCbCr2RGB[0,1]*cb + YCbCr2RGB[0,2]*cr
50 g = YCbCr2RGB[1,0]*y + YCbCr2RGB[1,1]*cb + YCbCr2RGB[1,2]*cr
51 b = YCbCr2RGB[2,0]*y + YCbCr2RGB[2,1]*cb + YCbCr2RGB[2,2]*cr
55 RGB2YUV = np.array([[0.299, 0.587, 0.114],[-0.14713, -0.28886, 0.436],[0.615, -0.51499, -0.10001]])
56 YUV2RGB = np.array([[1.0, 0.0, 1.13983],[1.0, -0.39465, -0.58060],[1.0, 2.03211, 0.0]])
59 y = (RGB2YUV[0,0]*r + RGB2YUV[0,1]*g + RGB2YUV[0,2]*b)
60 u = (RGB2YUV[1,0]*r + RGB2YUV[1,1]*g + RGB2YUV[1,2]*b)
61 v = (RGB2YUV[2,0]*r + RGB2YUV[2,1]*g + RGB2YUV[2,2]*b)
65 r = (YUV2RGB[0,0]*y + YUV2RGB[0,1]*u + YUV2RGB[0,2]*v)
66 g = (YUV2RGB[1,0]*y + YUV2RGB[1,1]*u + YUV2RGB[1,2]*v)
67 b = (YUV2RGB[2,0]*y + YUV2RGB[2,1]*u + YUV2RGB[2,2]*v)
72 y = Kr*r + (1-Kr-Kb)*g + Kb*b
80 g = (y - Kr*r - Kb*b)/(1-Kr-Kb)
87 imhist,bins = np.histogram(im.flatten(),nbr_bins)
93 im2 = np.interp(im.flatten(),bins[:-1],cdf)
95 plt.plot(bins[:-1],imhist)
96 plt.plot(bins[:-1],cdf)
100 plt.plot(np.divide(im2[0:10000],im.flatten()[0:10000]))
104 return im2.reshape(im.shape), cdf
109 val = (val - minval) / (maxval - minval)
111 return cm.nipy_spectral(val)
114 fig = plt.figure(facecolor=
'k')
115 ds = xr.load_dataset(filepath,group=
'/geophysical_data',mask_and_scale=
True)[ds_str].values
117 ml = mpl.cm.ScalarMappable(norm=
None, cmap=
'turbo')
118 ml.set_clim(mina, maxa)
120 aec = ml.to_rgba(ds, alpha=
None)
121 fig.figimage(aec,resize=
True)
122 loc = plt.LinearLocator(11)
123 fig.set_figwidth(fig.get_figwidth()*1.1)
124 fig.set_figheight(fig.get_figheight()*1.04)
125 axa = fig.add_axes([0.94, 0.1, 0.012, 0.8], frameon=
True)
126 axa.tick_params(colors=
'c', labelsize=10, length=20, width=2, pad=5, right=
True)
127 axa.yaxis.set_major_locator(loc)
128 tvals = loc.tick_values(mina, maxa)
129 fig.colorbar(ml, cax=axa, ticks=tvals )
130 fig.suptitle(x + title_str, color=
'c', size=10, y=0.99)
132 plt.savefig(outpath, facecolor=fig.get_facecolor())
137 blue = xr.load_dataset(filepath,group=
'/observations',mask_and_scale=
True)[
'rhot_490'].values
138 green = xr.load_dataset(filepath,group=
'/observations',mask_and_scale=
True)[
'rhot_550'].values
139 red = xr.load_dataset(filepath,group=
'/observations',mask_and_scale=
True)[
'rhot_670'].values
140 ro = np.clip(np.nan_to_num(red),0,1.0)
141 go = np.clip(np.nan_to_num(green),0,1.0)
142 bo = np.clip(np.nan_to_num(blue),0,1.0)
143 ao = np.ones_like(ro)
149 maxo_all = max(np.max(ro), np.max(go), np.max(bo)) + 0.001
155 ga = np.ones_like(ro)*gamma
156 rg = np.power(ro, ga)
157 gg = np.power(go, ga)
158 bg = np.power(bo, ga)
159 yg = np.power(yh, ga)
162 rg = np.multiply(ys,ro)
163 gg = np.multiply(ys,go)
164 bg = np.multiply(ys,bo)
167 im_red = im.fromarray(np.uint8(np.clip(rg,0,1.0)*scale))
168 im_green = im.fromarray(np.uint8(np.clip(gg,0,1.0)*scale))
169 im_blue = im.fromarray(np.uint8(np.clip(bg,0,1.0)*scale))
170 im_alpha = im.fromarray(np.uint8(np.clip(ao,0,1.0)*scale))
172 im_rgb = im.merge(
"RGBA", (im_red, im_green, im_blue, im_alpha))
173 return np.asarray(im_rgb)/255.0
179 dtdb_dirpath = args[1]
185 bc = np.int(args[3][0])-1
186 gc = np.int(args[3][1])-1
187 rc = np.int(args[3][2])-1
196 command =
"date > " + logfile
197 result = os.system(command)
199 dtdb_dircontents = os.listdir(dtdb_dirpath)
200 dtdb_dircontents.sort()
202 for x
in dtdb_dircontents:
207 filepath = dtdb_dirpath +
"/" + x
209 if not os.path.isfile(filepath):
212 if (dataset ==
"retrievals"):
213 image_path = dtdb_dirpath +
"/IMAGES"
214 if not os.path.isdir(image_path):
216 image_path = image_path +
"/geophysical_data"
218 for dname
in [
"ae",
"aot_550",
"fmf_550",
"sse"]:
220 outfilename = x +
"_" + dname +
".png"
221 path = image_path +
"_" + dname
222 if not os.path.isdir(path):
224 outpath = path +
"/" + outfilename
227 title_str =
' Angstrom Exponent'
231 elif dname ==
"aot_550":
232 title_str =
' Log base 10 of Aerosol Optical Depth at 550 nm'
241 elif dname ==
"fmf_550":
242 title_str =
' Fine Mode Fraction'
247 title_str =
' Log base 10 of Normalized Sum of Squares Error'
248 ds_str =
'residual_error'
252 print (
"invalid dataset name: ", dname)
258 elif (dataset ==
"histogram"):
259 image_path = dtdb_dirpath +
"/IMAGES"
260 if not os.path.isdir(image_path):
262 image_path1 = image_path +
"/histograms"
263 outfilename1 = x +
"_hist_410.png"
264 if not os.path.isdir(image_path1):
265 os.mkdir(image_path1)
266 outpath1 = image_path1 +
"/" + outfilename1
267 image_path2 = image_path +
"/maps"
268 outfilename2 = x +
"_map.png"
269 if not os.path.isdir(image_path2):
270 os.mkdir(image_path2)
271 outpath2 = image_path2 +
"/" + outfilename2
284 hist = np.zeros((NWL, nbins, nbins))
285 cmap = cm.get_cmap(
'turbo')
286 fig1 = plt.figure(figsize=(16,10))
287 fig2 = plt.figure(figsize=(16,8))
290 ax2 = fig2.add_subplot(2,4,1)
296 rfl0 = xr.load_dataset(filepath,group=
'/observations')[
'rhot_'+wlstr[refwl]].values[st[0]:st[0]+ct[0],st[1]:st[1]+ct[1]]
297 lrfl0 = np.log10(np.clip(rfl0, a_min=0.0001, a_max=1.0))
298 rflbs = xr.load_dataset(filepath,group=
'/observations')[
'rhot_'+wlstr[refbs]].values[st[0]:st[0]+ct[0],st[1]:st[1]+ct[1]]
299 lrflbs = np.log10(np.clip(rflbs, a_min=0.0001, a_max=1.0))
300 ax2 = fig2.add_subplot(2,4,5)
301 ax2.set_title(
"log10(RHOT_"+wlstr[refwl]+
")")
302 ml = mpl.cm.ScalarMappable(norm=
None, cmap=cmap)
303 ml.set_clim(min0, max0)
305 aec = ml.to_rgba(lrfl0, alpha=
None)
310 for wl
in range(NWL):
327 rfl1 = xr.load_dataset(filepath,group=
'/observations')[
'rhot_'+wlstr[wl]].values[st[0]:st[0]+ct[0],st[1]:st[1]+ct[1]]
328 lrfl1 = lrflbs - np.log10(np.clip(rfl1, a_min=0.0001, a_max=1.0))
331 hist[wl], edges = np.histogramdd((lrfl0.flatten(),lrfl1.flatten()), range=[[min0,max0],[min1,max1]],bins=nbins, density=
True)
332 except Exception
as inst:
335 print (
"Numpy.histogramdd failure ... exiting")
338 ax1 = fig1.add_subplot(2,4,wl1)
339 xpos0, ypos0 = np.meshgrid(edges[0][:-1]+edges[0][1:], edges[1][:-1]+edges[1][1:])
340 cp1 = ax1.contourf(xpos0/2, ypos0/2, np.clip(hist[wl].T, a_min=hmin, a_max=hmax), levels=100, cmap=cmap)
341 ax1.set_title(
"RHOT_"+wlstr[wl])
342 ax1.set_xlabel(
"log10 (RHOT_"+wlstr[refwl]+
")")
344 ax1.set_ylabel(
"log10(RHOT_"+wlstr[wl]+
" / RHOT_"+wlstr[refwl]+
")")
349 ax1.tick_params(labelleft=bticks, left=bticks)
350 ax1.grid(b=
True, which=
'major', color=
'#666666', linestyle=
'-')
352 ax1.grid(b=
True, which=
'minor', color=
'#999999', linestyle=
'-', alpha=0.2)
355 ax2 = fig2.add_subplot(2,4,wl2)
356 ax2.set_title(
"log10(RHOT_"+wlstr[wl]+
" / RHOT_"+wlstr[refwl]+
")")
358 ml.set_clim(min1, max1)
360 aec = ml.to_rgba(lrfl1, alpha=
None)
367 plt.savefig(outpath2, facecolor=fig2.get_facecolor())
372 loc = plt.LinearLocator(11)
373 tvals = loc.tick_values(hmin, hmax)
374 ml = mpl.cm.ScalarMappable(norm=
None, cmap=
'turbo')
375 ml.set_clim(hmin, hmax)
376 fig1.subplots_adjust(bottom=0.1, top=0.9, left=0.1, right=0.9, wspace=0.02, hspace=0.02)
377 cb_ax = fig1.add_axes([0.93, 0.1, 0.02, 0.8])
378 fig1.colorbar(ml, cax=cb_ax, ticks=tvals)
381 plt.savefig(outpath1, facecolor=fig1.get_facecolor())
389 if (dataset ==
"observations"):
391 bluec = xr.load_dataset(filepath,group=
'/observations',mask_and_scale=
True)[
'rhot_490'].values
392 greenc = xr.load_dataset(filepath,group=
'/observations',mask_and_scale=
True)[
'rhot_550'].values
393 redc = xr.load_dataset(filepath,group=
'/observations',mask_and_scale=
True)[
'rhot_670'].values
394 elif (dataset ==
"aot"):
395 ocean = xr.load_dataset(filepath,group=
'/geophysical_data')[
'AOT_ocean']
396 land = xr.load_dataset(filepath,group=
'/geophysical_data')[
'AOT_land']
397 land = xr.load_dataset(filepath,group=
'/geolocation')[
'land_water']
398 bluec = ocean[bc,:,:]
399 greenc = ocean[gc,:,:]
401 np.putmask(bluec, lw>0, land[0,:,:])
402 np.putmask(greenc, lw>0, land[1,:,:])
403 np.putmask(redc, lw>0, land[2,:,:])
404 elif (dataset ==
"cloudmask"):
405 cldmsk = xr.load_dataset(filepath,group=
'/meteorology')[
'cloud_mask']
406 cldtst = xr.load_dataset(filepath,group=
'/quality')[
'cloud_test']
407 bluec = np.zeros_like(cldmsk, dtype=np.int16)
408 greenc = np.zeros_like(cldmsk, dtype=np.int16)
409 redc = np.zeros_like(cldmsk, dtype=np.int16)
410 nomask = np.zeros_like(cldmsk, dtype=np.int16)
412 np.putmask(greenc[:,:], cldtst[:,:]<0, -cldtst[:,:])
413 np.putmask(bluec[:,:], cldtst[:,:]<0, nomask[:,:])
414 np.putmask(redc[:,:], cldtst[:,:]>0, cldtst[:,:])
415 np.putmask(bluec[:,:], cldtst[:,:]>0, nomask[:,:])
417 except Exception
as e:
418 print (
" Error loading dataset ... exiting")
421 if (dataset ==
"observations" or dataset ==
"aot" or dataset ==
"cloudmask"):
422 ro = np.clip(np.nan_to_num(redc),0,10.0)
423 go = np.clip(np.nan_to_num(greenc),0,10.0)
424 bo = np.clip(np.nan_to_num(bluec),0,10.0)
426 if np.isnan(np.sum(ro)):
427 print (np.max(ro), np.max(go), np.max(bo))
438 mino_all = min(np.min(ro), np.min(go), np.min(bo))
442 maxo_all = max(np.max(ro), np.max(go), np.max(bo)) + 0.001
448 ga = np.ones_like(ro)*gamma
449 rg = np.power(ro, ga)
450 gg = np.power(go, ga)
451 bg = np.power(bo, ga)
452 yg = np.power(yh, ga)
455 rg = np.multiply(ys,ro)
456 gg = np.multiply(ys,go)
457 bg = np.multiply(ys,bo)
460 im_red = im.fromarray(np.uint8(np.clip(rg,0,1.0)*scale))
461 im_green = im.fromarray(np.uint8(np.clip(gg,0,1.0)*scale))
462 im_blue = im.fromarray(np.uint8(np.clip(bg,0,1.0)*scale))
464 im_rgb = im.merge(
"RGB", (im_red, im_green, im_blue))
467 set = dataset.partition(
"/")
468 outfilename = x +
"_" + set[2] +
"_" + colors +
".png"
470 image_path = dtdb_dirpath +
"/IMAGES"
471 if not os.path.isdir(image_path):
473 image_path = image_path +
"/" + set[0]
474 if not os.path.isdir(image_path):
476 image_path = image_path + set[1] + set[2] +
"_" + colors
477 if not os.path.isdir(image_path):
480 outpath = image_path +
"/" + outfilename
481 im_rgb.save( outpath )