def save(self,outpath=None,outdir=None,fname=None,tight=True): """ Args: outpath: absolute path to output .png (required, or next two are) outdir: absolute path to directory only fname: filename only """ # if (outpath is None) and (fname is None) and (outdir is None): if hasattr(self,'fpath'): fpath = getattr(self,'fpath',None) # os.path.join(self.outdir,self.fname)) elif isinstance(fname,str): fpath = os.path.join(outdir,fname) elif hasattr(self,'fname') and hasattr(self,'outdir'): fpath = os.path.join(self.outdir,self.fname) else: assert outpath is not None fpath = outpath utils.trycreate(fpath) if tight: self.fig.tight_layout() self.fig.savefig(fpath,bbox_inches='tight') else: self.fig.savefig(fpath)#,bbox_inches='tight') print(("Saving figure {0}".format(fpath))) plt.close(self.fig)
def get_tempdata_fpath(caseutc, fmt, vrbl, validutc, initutc, mem): if caseutc is None: casestr = "cNone" else: casestr = utils.string_from_time('dir', caseutc, strlen='day') if fmt is None: fmt = "fNone" if vrbl is None: vrbl = "vNone" if validutc is None: validstr = "uNone" else: validstr = utils.string_from_time('dir', validutc, strlen='minute') if initutc is None: initstr = "iNone" else: initstr = utils.string_from_time('dir', initutc, strlen='hour') if mem is None: mem = "mNone" fname = "temp_{}_{}_{}_{}_{}_{}.npy".format(casestr, initstr, validstr, vrbl, fmt, mem) fpath = os.path.join(tempdir_root, fname) utils.trycreate(fpath, debug=False) return fpath
def save(self, tight=True, close=True): utils.trycreate(self.fpath) if tight: self.fig.tight_layout() self.fig.savefig(self.fpath) print("Saving figure to {}".format(self.fpath)) if close: plt.close(self.fig) return
def save(self, tight=True, close=True): """ Overriden for optimal settings. """ import evac.utils as utils utils.trycreate(self.fpath) self.fig.savefig(self.fpath) print("Saving figure to {}".format(self.fpath)) plt.close(self.fig) return
def _load_data(nc_fpath, vrbl, npyfpaths): W = WRFOut(nc_fpath) data = W.get(vrbl=vrbl)[:, 0, :, :] assert data.shape[0] == len(npyfpaths) for tidx, npyfpath in enumerate(npyfpaths): utils.trycreate(npyfpath, debug=False) _save(arr=data[tidx, :, :], file=npyfpath) return
def compute_all(self, save_output=False): """ If datadir/fname are not None, computed stats are saved. Else, they are just returned in the dictionary. """ SCORES = dict(a=self.a, b=self.b, c=self.c, d=self.d) for score in self.scores.keys(): SCORES[score] = self.get(score) if save_output: datapath = save_output utils.trycreate(datapath) print("Saving all data arrays to {}".format(datapath)) N.savez(file=datapath, **SCORES) print("Saved.") return SCORES
def catalog_members(self,): """ Create the members dictionary. """ members = {} for mem in self.membernames: mem_datadir = self.datadir / mem utils.trycreate(mem_datadir,isdir=True) mem_rundir = self.wrfrundir / mem utils.trycreate(mem_rundir,isdir=True) members[mem] = {'datadir':mem_datadir, 'rundir':mem_rundir,} time.sleep(1) return members
def _get_plot_options1(self,vrbl=None,*args,**kwargs): """ Filter arguments and key-word arguments for plotting methods. Whatever is in dictionary will overwrite defaults in the plotting method. These may be * fhrs (forecast hour plotting times - or all) * ensmems (ensemble members, or all) """ # Get plotting levels if not already given # TODO: rewrite this using hasattr() or something. if vrbl: S = Scales(vrbl) if not 'levels' in kwargs: kwargs['levels'] = S.clvs if not 'cmap' in kwargs: kwargs['cmap'] = S.cm # Specific things for certain variables if vrbl in ('REFL_10CM',"REFL_comp"): pass # Save all figures to a subdirectory if 'subdir' in kwargs: utils.trycreate(subdir,is_folder=True) # What times are being plotted? # If all, return list of all times if 'utc' in kwargs: pass elif ('fchr' not in kwargs) or (kwargs['fchr'] == 'all'): kwargs['utc'] = E.list_of_times # Does this pick up on numpy arange? elif isinstance(kwargs['fchr'], (list,tuple)): kwargs['utc'] = [] for f in kwargs['fchr']: utc = self.inittime + datetime.timedelta(seconds=3600*f) kwargs['fchr'].append(utc) # Make domain smaller if requested # Save data before plotting clskwargs['save_data'] = kwargs.get('save_data',False) return clskwargs,plotkwargs,mplkwargs
def plot(self,va,lv,times,outdir=False,**kwargs): for t in times: fig = plt.figure() data = self.get(va,lv,t) m, x, y = self.basemap_setup() if 'scale' in kwargs: S = kwargs['scale'] f1 = m.contour(x,y,data,S,colors='k') else: f1 = m.contour(x,y,data,colors='k') if kwargs['title']: title = utils.string_from_time('title',t) plt.title(title) if 'wind_overlay' in kwargs: jet = kwargs['wind_overlay'] wind = self.get('wind',lv,t) windplot = m.contourf(x,y,wind,jet,alpha=0.6) plt.colorbar(windplot) elif 'W_overlay' in kwargs: Wscale = kwargs['W_overlay'] W = self.get('W',lv,t) windplot = m.contourf(x,y,W,alpha=0.6) plt.colorbar(windplot) # if self.C.colorbar: # plt.colorbar(orientation='horizontal') datestr = utils.string_from_time('output',t) fname = '_'.join(('ECMWF',va,str(lv),datestr)) + '.png' print(("Plotting {0} at {1} for {2}".format( va,lv,datestr))) plt.clabel(f1, inline=1, fmt='%4u', fontsize=12, colors='k') utils.trycreate(outdir) plt.savefig(os.path.join(outdir,fname)) plt.clf() plt.close()
def plot_thumbnails(plotutc,vrbl): """ Plot data, including verification if it exists. There are three plotting options. The d01 and d02 domains, as is, and a third interpolation to common (inner) domain. """ if vrbl == 'REFL_comp': R_large = Radar(plotutc,radardir) R_small = copy.copy(R_large) R_large.get_subdomain(**d01_limdict,overwrite=True) R_small.get_subdomain(**d02_limdict,overwrite=True) for nloop,dom in enumerate((1,1,2)): if nloop == 0: # This go round, do not zoom in on outer domain R = R_large ld = {} opt = "large" else: R = R_small ld = d02_limdict opt = "small" fhstr = utils.pretty_fhr(fhr) W = E.arbitrary_pick(dom=dom,dataobj=True) fig,axes = plt.subplots(nrows=3,ncols=4,figsize=(10,8)) # fname = "test_verif_d{:02d}_{}_{}".format(dom,opt) fname = "test_verif_d{:02d}_{}_{}".format(dom,opt,fhstr) lats,lons = E.get_latlons(dom=dom) nx,ny = E.get_nx_ny(dom=dom) cen_lat,cen_lon = E.get_cenlatlons(dom=dom) # xx,yy = E.get_xx_yy(dom=dom) for plot,ax in zip(plotlist,axes.flat): print("Plotting {}".format(plot)) if plot == 'Verif': # first plot - observed radar R.plot_radar(fig=fig,ax=ax,drawcounties=True,cb=False) ax.set_title(plot) elif isinstance(plot,int): memname = 'm{:02d}'.format(plot) # members (plots 3 to 12) # if dom == 1: # Plot only overlapping domain # data = E.get( print("Getting data.") data = E.get('REFL_comp',utc=plotutc,dom=dom,members=memname)[0,0,0,:,:] print("Setting up axis.") # BE = BirdsEye(ax=ax,fig=fig,proj='lcc') BE = BirdsEye(ax=ax,fig=fig,proj='merc') print("Plotting on axis.") BE.plot2D(data,save=False,drawcounties=True, # lats=lats,lons=lons, clvs=S.clvs,cmap=S.cm, # nx=nx,ny=ny, W=W,cb=False, # x=xx,y=yy, cen_lat=cen_lat,cen_lon=cen_lon,**ld) print("Setting axis title.") title = 'Member {:02d}'.format(plot) ax.set_title(title) else: ax.grid('off') ax.axis('off') fig.tight_layout() outfpath = os.path.join(outdir,fname) utils.trycreate(outfpath) fig.savefig(outfpath) print("Saving figure to",outfpath)
def plot_quicklook(self, outdir, what='all', fname=None, ecc=0.2): """ Plot quick images of objects identified. Args: what (str): the type of quickplot outdir (str): where to put the quickplot fname (str,optional): if None, automatically name. ecc (float): eccentricity of object, for discriminating (later) """ assert what in ("all", "qlcs", "ecc", "shapeindex", "ratio", "extent", "4-panel", "pca") def label_objs(bmap, ax, locs): for k, v in locs.items(): xpt, ypt = bmap(v[1], v[0]) # bbox_style = {'boxstyle':'square','fc':'white','alpha':0.5} # bmap.plot(xpt,ypt,'ko',)#markersize=3,zorder=100) # ax.text(xpt,ypt,k,ha='left',fontsize=15) ax.annotate(k, xy=(xpt, ypt), xycoords="data", zorder=1000, fontsize=7, fontweight='bold') # pdb.set_trace() return def do_label_array(bmap, ax): locs = dict() for o in self.objects.itertuples(): locs[str(int(o.label))] = (o.centroid_lat, o.centroid_lon) idxarray = N.ma.masked_where(self.idxarray < 1, self.idxarray) bmap.pcolormesh( data=idxarray, x=x, y=y, ) #vmin=1,vmax=self.idxarray.max()+1) # levels=N.arange(1,self.objects.shape[0])) label_objs(bmap, ax, locs) ax.set_title("Object labels") return def __do_label(bmap, ax): locs = dict() for o, okidx in zip(self.objects.itertuples(), self.OKidxs): #data = skimage.preprocessing.normalize( raw_data = self.object_props[okidx].intensity_image pdb.set_trace() data = N.linalg.norm(axis=(0, 1), x=raw_data) shidx = skimage.feature.shape_index(data) sistr = "{:0.3f}".format(shidx) locs[sistr] = (o.centroid_lat, o.centroid_lon) idxarray = N.ma.masked_where(self.idxarray < 1, self.idxarray) bmap.pcolormesh( data=idxarray, x=x, y=y, ) #vmin=1,vmax=self.idxarray.max()+1) # levels=N.arange(1,self.objects.shape[0])) label_objs(bmap, ax, locs) ax.set_title("Object shape index") return def do_label_ecc(bmap, ax): locs = dict() for o in self.objects.itertuples(): eccstr = "{:0.2f}".format(o.eccentricity) locs[eccstr] = (o.centroid_lat, o.centroid_lon) idxarray = N.ma.masked_where(self.OKidxarray < 1, self.OKidxarray) bmap.pcolormesh(data=idxarray, x=x, y=y, alpha=0.5) #vmin=1,vmax=self.idxarray.max()+1) # levels=N.arange(1,self.objects.shape[0])) label_objs(bmap, ax, locs) ax.set_title("Object eccentricity") return def do_label_extent(bmap, ax): locs = dict() for o in self.objects.itertuples(): lab = "{:1.2f}".format(o.extent) locs[lab] = (o.centroid_lat, o.centroid_lon) idxarray = N.ma.masked_where(self.OKidxarray < 1, self.OKidxarray) bmap.pcolormesh(data=idxarray, x=x, y=y, alpha=0.5) #vmin=1,vmax=self.idxarray.max()+1) # levels=N.arange(1,self.objects.shape[0])) label_objs(bmap, ax, locs) ax.set_title("Object extent (fill pc. of bbox)") return def do_label_longest(bmap, ax): locs = dict() for o in self.objects.itertuples(): lab = "{}".format(int(o.longaxis_km)) locs[lab] = (o.centroid_lat, o.centroid_lon) idxarray = N.ma.masked_where(self.OKidxarray < 1, self.OKidxarray) bmap.pcolormesh(data=idxarray, x=x, y=y, alpha=0.5) #vmin=1,vmax=self.idxarray.max()+1) # levels=N.arange(1,self.objects.shape[0])) label_objs(bmap, ax, locs) ax.set_title("Object longest-side length (km)") return def do_label_pca(bmap, ax, discrim_vals=(-0.2, 0.5)): locs = dict() obj_discrim = N.zeros_like(self.OKidxarray) for o in self.objects.itertuples(): lab = "{:0.2f}".format(o.qlcsness) locs[lab] = (o.centroid_lat, o.centroid_lon) id = int(o.label) if o.qlcsness < discrim_vals[0]: discrim = 1 elif o.qlcsness > discrim_vals[1]: discrim = 3 else: discrim = 2 obj_discrim = N.where(self.idxarray == id, discrim, obj_discrim) marr = N.ma.masked_where(obj_discrim < 1, obj_discrim) pcm = bmap.pcolormesh( data=marr, x=x, y=y, alpha=0.5, vmin=1, vmax=3, cmap=M.cm.get_cmap("magma", 3), ) #vmin=1,vmax=self.idxarray.max()+1) # levels=N.arange(1,self.objects.shape[0])) mode_names = ["", "Cellular", "Ambiguous", "Linear/Complex"] def format_func(x, y): return mode_names[x] # This function formatter will replace integers with target names formatter = plt.FuncFormatter(lambda val, loc: mode_names[val]) # We must be sure to specify the ticks matching our target names cax = fig.add_axes([0.75, 0.1, 0.2, 0.04]) #cax.set_xlim(0.5, 3.5) fig.colorbar(pcm, cax=cax, ticks=(1, 2, 3), format=formatter, orientation='horizontal') # Set the clim so that labels are centered on each block label_objs(bmap, ax, locs) #plt.colorbar(pcm,cax=cax) ax.set_title("Object PC1 (QLCS-ness)") return def do_label_ratio(bmap, ax): locs = dict() for o in self.objects.itertuples(): lab = "{:1.2f}".format(o.ratio) locs[lab] = (o.centroid_lat, o.centroid_lon) idxarray = N.ma.masked_where(self.OKidxarray < 1, self.OKidxarray) bmap.pcolormesh(data=idxarray, x=x, y=y, alpha=0.5) #vmin=1,vmax=self.idxarray.max()+1) # levels=N.arange(1,self.objects.shape[0])) label_objs(bmap, ax, locs) ax.set_title("Object side-ratio (short/long)") return def do_raw_array(bmap, ax): bmap.contourf(data=self.raw_data, x=x, y=y, levels=S.clvs, cmap=S.cm) ax.set_title("Raw data") return def do_intensity_array(bmap, ax): locs = dict() for o in self.objects.itertuples(): locs[str(int(o.label))] = (o.weighted_centroid_lat, o.weighted_centroid_lon) bmap.contourf(data=self.object_field, x=x, y=y, levels=S.clvs, cmap=S.cm) label_objs(bmap, ax, locs) ax.set_title("Intensity array") return def do_table(bmap, ax): cell_text = [] table = self.objects for row in range(len(table)): cell_text.append(table.iloc[row]) tab = plt.table(cellText=cell_text, colLabels=table.columns, loc='center') ax.add_table(tab) plt.axis('off') return def do_highlow_ecc(bmap, ax, ecc, overunder): assert overunder in ("over", "under") func = N.greater_equal if overunder == "over" else N.less locs = dict() qlcs_obj = [] obj_field = N.zeros_like(self.object_field) for o in self.objects.itertuples(): if func(o.eccentricity, ecc): locs[str(int(o.label))] = (o.centroid_lat, o.centroid_lon) qlcs_obj.append(o.label) for olab in qlcs_obj: obj_field = N.where(self.idxarray == olab, self.object_field, obj_field) bmap.contourf(data=obj_field, x=x, y=y, levels=S.clvs, cmap=S.cm) label_objs(bmap, ax, locs) if overunder == "over": ax.set_title("QLCS objects (ecc > {:0.2f})".format(ecc)) else: ax.set_title("Cellular objects (ecc < {:0.2f})".format(ecc)) return # The above needs saving to pickle and loading/copying each time # a plot is made, if optimising. # 2x2: # ax1 is the raw field # ax2 is the labelled objects (ID) # ax3 is the object field, annotate row/col and lat/lon centroids # ax4 is the DataFrame? if what == "4-panel": fig, axes = plt.subplots(2, 2, figsize=(9, 7)) else: fig, axes = plt.subplots(1, 3, figsize=(9, 4)) if fname is None: fname = "quicklook.png" for n, ax in enumerate(axes.flat): # if n!=0: # continue print("Plotting subplot #", n + 1) bmap = self.create_bmap(ax=ax) x, y = bmap(self.lons, self.lats) S = Scales(vrbl='REFL_comp') if n == 0: do_raw_array(bmap, ax) elif n == 1: if what == "qlcs": do_highlow_ecc(bmap, ax, ecc, "over") elif what in ("ecc", "extent", "4-panel"): do_label_ecc(bmap, ax) else: do_intensity_array(bmap, ax) #elif what == "ecc": # do_intensity_array(bmap,ax) elif n == 2: if what == "qlcs": do_highlow_ecc(bmap, ax, ecc, "under") elif what == "all": do_label_array(bmap, ax) elif what == "ecc": #do_label_ecc(bmap,ax) do_label_ratio(bmap, ax) elif what == "shapeindex": do_label_shapeindex(bmap, ax) elif what == "ratio": do_label_ratio(bmap, ax) elif what in ("4-panel", "extent"): do_label_extent(bmap, ax) elif what == "pca": do_label_pca(bmap, ax) elif n == 3: if what == "4-panel": do_label_longest(bmap, ax) else: do_table(bmap, ax) fpath = os.path.join(outdir, fname) utils.trycreate(fpath) fig.tight_layout() fig.savefig(fpath) print("Saved figure to", fpath) plt.close(fig) # pdb.set_trace() return
def interpolate( dataA, latsA, lonsA, latsB, lonsB, cut_only=False, # cut_first=True, # remove_negative=False, save_to_fpath=None): """ Interpolate data on gridA to gridB. If cut_first, if gridA is a lot bigger (e.g., StageIV data), cut it to a smaller size to improve interpolation efficiency. """ cut_first = True if isinstance(dataA, str): dataA = N.load(dataA) if isinstance(latsA, str): latsA = N.load(latsA) lonsA = N.load(lonsA) if isinstance(latsB, str): latsB = N.load(latsB) lonsB = N.load(lonsB) assert latsA.ndim == 2 assert latsB.ndim == 2 # pdb.set_trace() if cut_only: assert dataA is not None dataB, _latsB, _lonsB = utils.return_subdomain(dataA, latsA, lonsA, cut_to_lats=latsB, cut_to_lons=lonsB) else: if cut_first: _dataA = N.copy(dataA) _latsA = N.copy(latsA) _lonsA = N.copy(lonsA) Nlim = N.max(latsB) + 0.5 Elim = N.max(lonsB) + 0.5 Slim = N.min(latsB) - 0.5 Wlim = N.min(lonsB) - 0.5 Nidx = closest(latsA[:, 0], Nlim) Eidx = closest(lonsA[0, :], Elim) Sidx = closest(latsA[:, 0], Slim) Widx = closest(lonsA[0, :], Wlim) old_method = False if old_method: s1 = slice(Sidx, Nidx + 1) s2 = slice(Widx, Eidx + 1) dataA = dataA[s1, s2] latsA = latsA[s1, s2] lonsA = lonsA[s1, s2] else: dataA = dataA[Sidx:Nidx + 1, Widx:Eidx + 1] latsA = latsA[Sidx:Nidx + 1, Widx:Eidx + 1] lonsA = lonsA[Sidx:Nidx + 1, Widx:Eidx + 1] # if "mrms" in save_to_fpath: # pdb.set_trace() bmap = create_bmap() xxA, yyA = bmap(lonsA, latsA) xxB, yyB = bmap(lonsB, latsB) xdB = len(xxB[:, 0]) ydB = len(yyB[0, :]) # if "nexrad" in save_to_fpath: # if "mrms" in save_to_fpath: dataA = N.copy(dataA) xxA = N.copy(xxA) yyA = N.copy(yyA) xxB = N.copy(xxB) yyB = N.copy(yyB) dataB = scipy.interpolate.griddata(points=(xxA.flat, yyA.flat), values=dataA.flat, xi=(xxB.flat, yyB.flat), method='linear').reshape(xdB, ydB) # if "mrms" in save_to_fpath: # pdb.set_trace() if save_to_fpath is not None: utils.trycreate(save_to_fpath, debug=False) _save(arr=dataB, file=save_to_fpath) print("Saved to", save_to_fpath) return return dataB
# TODO: we just need to loop to check the lat/lon # files have been created, or create them. We can # delete the lat/lon array files, and not load them. if os.path.exists(d02_latf): # d02_lats = N.load(d02_latf) # d02_lons = N.load(d02_lonf) pass else: d02_fname = get_wrfout_fname(initutc, 2) d02_fpath = os.path.join(m01dir, d02_fname) d02_nc = Dataset(d02_fpath) d02_lats = d02_nc.variables['XLAT'][0, :, :] d02_lons = d02_nc.variables['XLONG'][0, :, :] utils.trycreate(d02_latf, debug=False) _save(arr=d02_lats, file=d02_latf) _save(arr=d02_lons, file=d02_lonf) # d01 d01_latf, d01_lonf = get_llf_fpath(casestr, "d01_raw") if os.path.exists(d01_latf): # d01_lats = N.load(d01_latf) # d01_lons = N.load(d01_lonf) pass else: d01_fname = get_wrfout_fname(initutc, 1) d01_fpath = os.path.join(m01dir, d01_fname) d01_nc = Dataset(d01_fpath) d01_lats = d01_nc.variables['XLAT'][0, :, :]