def num2str(n, tz=False): """ converts from matplotlib date format to a string With tz=True also shows the timezone """ # could also use .isoformat if tz: return pylab.num2date(n).strftime('%Y-%m-%d %H:%M:%S.%f %Z') else: return pylab.num2date(n).strftime('%Y-%m-%d %H:%M:%S.%f')
def on_key(event): self.controller.send_message('key: {}'.format(event.key)) self.controller.event = event if event.key == 'z': dt = pd.to_datetime( plt.num2date(event.xdata).strftime('%Y-%m-%d %H:%M:%S')) self.controller.view.controlls.accordeon_start.value = dt.__str__( ) elif event.key == 'x': dt = pd.to_datetime( plt.num2date(event.xdata).strftime('%Y-%m-%d %H:%M:%S')) self.controller.view.controlls.accordeon_end.value = dt.__str__( ) elif event.key == 'a': self.controller.view.controlls.accordeon_alt.value = str( event.ydata) self._tmp_alt_x = event.xdata
def generate_xticks(self, all_days, monthsamp=1): """ generate xticks all_days: array of times (days since 01-0-0) """ dd = pl.num2date(all_days) xticks = [] xticklabels = [] last_month = dd[0].month last_year = dd[0].year xticks.append(all_days[0]) strmon = get_month_string(dd[0].month) if self.yearonly: xticklabels.append(str(dd[0].year)) else: xticklabels.append(strmon + '\n' + str(dd[0].year)) for d in dd: if (d.month != last_month) | (d.year != last_year): xticks.append(pl.date2num(d)) # save tick location mstr = get_month_string(d.month) if self.yearonly: xticklabels.append(str(d.year)) else: if (d.year != last_year): xticklabels.append(mstr + '\n' + str(d.year)) else: xticklabels.append(mstr + '\n' + '') last_month = d.month last_year = d.year if self.transpose: scal = self.rescaley else: scal = self.rescalex xticks = np.asarray(xticks) # /// remap to image coordinates ny, nx = np.shape(self.hov) w = (xticks - pl.date2num(self.t_min)) / (pl.date2num(self.t_max) - pl.date2num(self.t_min)) # weights for positions on x-axis xticks = w * nx * scal xticks = list(xticks) # here we have monthly ticks which we can now subsample xticks = xticks[::monthsamp] xticklabels = xticklabels[::monthsamp] self.x_major_locator = pl.FixedLocator(xticks) self.x_major_formatter = pl.FixedFormatter(xticklabels)
def generate_monthly_timeseries(t, sday='01'): """ generate a vector monthly timeseries t: time (numeric) """ tn = [] d = pl.num2date(t) for i in range(len(t)): y = str(d[i].year).zfill(4) m = str(d[i].month).zfill(2) s = y + '-' + m + '-' + sday tn.append(pl.datestr2num(s)) return tn
def __init__(self, time, value, var_unc=None, rescalex=1, rescaley=1, lat=None, lon=None, transpose=False, use_cdo=True): """ Hovmoeller class This class implements the functionality to generate hovmoeller plots. It allows to calculate all relevant data by itself or using the CDO's for calculation of e.g. zonal means time : datetime vector with time information value : ndarray data to be analyzed. if the argument lat is provided it is assumed that lat/lon are 2D matrices In this case the value is expected to be a 3D variables as value(time,ny,nx) var_unc : ndarray additional array with variance information (can be used to show uncertainties) - not really validated so far rescalex : int rescaling factor for x-variable (used to blow up the plot) rescaley : int rescaling factor for y-variable (used to blow up the plot) lat : ndarray latitude coordinates lon : ndarray longitude coordinates transpose : bool transpose the data matrix EXAMPLES ======== #a minimum example for a time-latitude plot #get data from e.g. file; here random data t1=datestr2num('2011-05-01'); t2=datestr2num('2011-08-31') d = num2date(linspace(t1,t2,1000.)) lats=linspace(-90.,90.,181) + 0.5; lats = lats[:-1] lons = linspace(-180.,180.,361) + 0.5; lons = lons[:-1] LONS,LATS = meshgrid(lons,lats) ny,nx = LATS.shape; nt = len(d) dat = rand(nt,ny,nx) #create a hovmoeller object myhov = hovmoeller(d,dat,lat=LATS,rescaley=20,rescalex=10) myhov.time_to_lat(dlat=1.,yticksampling=1) myhov.plot(title='Test Hovmoeller plot',ylabel='lat',xlabel='days',origin='lower',xtickrotation=30,climits=[-1.,1.]) show() Another example, that does NOT calculate the hovmoeller plot by itself, but uses functionality from pyCMBS Data object file='wachmos_sm_1978-2010_int_monthly_t63.nc' D = Data(file,'var100',read=True) myhov = hovmoeller(num2date(D.time),None,rescaley=20,rescalex=20) myhov.plot(climits=[0.,0.5],input=D,xtickrotation=90,cmap='jet_r') show() """ #/// check consistency /// if value is not None: if len(time) == len(value): pass else: sys.exit('Inconsistent sizes of time and value (hovmoeller)') if lat is not None and shape(lat) != shape(value[0, :, :]): print shape(lat), shape(value[0, :, :]) sys.exit('Inconsistent latitudes and data (hovmoeller)') #/// set values of class /// self.time = time self.transpose = transpose ntim = len(self.time) t = pl.date2num(time) self.t_min = pl.num2date(np.floor(t.min())) self.t_max = pl.num2date(np.ceil(t.max())) if value is not None: self.value = value.copy() self.value = ma.array(self.value, mask=isnan(self.value)) # now reshape everything to [time,ngridcells] self.value.shape = (ntim, -1) if var_unc is None: self.var_unc = None else: self.var_unc = var_unc.copy() self.var_unc.shape = (ntim, -1) self.hov_var = None self.rescalex = rescalex self.rescaley = rescaley if lat is not None: self.lat = lat.copy() self.lat.shape = (-1) else: self.lat = None if lon is not None: self.lon = lon.copy() self.lon.shape = (-1) else: self.lon = None self.hov = None
def dateplt1(t,y,add=None,yl=5,fxlim=None,fylim=None,fighold=False,nomth=False,**kwargs): from matplotlib.dates import YearLocator,DateFormatter,MonthLocator if add is None: ymin = np.nanmin(y) - 0.02 * (abs(np.nanmax(y) - np.nanmin(y))) ymax = np.nanmax(y) + 0.02 * (abs(np.nanmax(y) - np.nanmin(y))) if isinstance(y,np.ma.MaskedArray): data_extent = np.where(y.mask == False) if len(data_extent) == 0: data_start = 0 data_end = y.size - 1 else: data_start = data_extent[0][0] data_end = data_extent[0][-1] else: data_start = 0 data_end = y.size - 1 if (isinstance(t[0],np.float64) is True) or (isinstance(t[0],np.float32) is True): datemin = num2date(t[data_start]) datemax = num2date(t[data_end]) elif isinstance(t[0],dt): datemin = t[data_start] datemax = t[data_end] else: return None datediff = datemax - datemin datediff = datediff / 50 datemin = datemin - datediff datemax = datemax + datediff if fighold is True: plt.hold(False) fig = plt.gcf() plt.clf() else: fig = plt.figure() if (fylim is not None): if type(fylim) is not tuple: print "option <fylim> must be a tuple" return None (ymin,ymax) = fylim ax = fig.add_subplot(111,autoscale_on=False,ylim=(ymin,ymax)) if (np.sign(ymax) != np.sign(ymin)): ax.axhline(0.0,c='black',lw=1.2,alpha=0.7) line1 = ax.plot(t,y,'+-',**kwargs) years = YearLocator(yl) yearsFmt = DateFormatter('%Y') if nomth == False: months = MonthLocator(1) ax.set_xlim(datemin,datemax) #fig.autofmt_xdate() ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) if nomth == False: ax.xaxis.set_minor_locator(months) ax.grid(True) my_fmt_xdate(ax) ##plt.show() ##ylabel('temperature anomaly') plt.figure(fig.number) return line1 else: xchange = 0 ychange = 0 fig = plt.gcf() ax = plt.gca() years = YearLocator(yl) yearsFmt = DateFormatter('%Y') months = MonthLocator(1) line1 = ax.plot(t,y,'+-',**kwargs) if isinstance(y,np.ma.MaskedArray): data_extent = np.where(y.mask == False) if len(data_extent) == 0: data_start = 0 data_end = y.size - 1 else: data_start = data_extent[0][0] data_end = data_extent[0][-1] else: data_start = 0 data_end = y.size - 1 if (isinstance(t[0],np.float64) is True) or (isinstance(t[0],np.float32) is True): datemin = num2date(t[data_start]) datemax = num2date(t[data_end]) elif isinstance(t[0],dt): datemin = t[data_start] datemax = t[data_end] else: return None datediff = datemax - datemin datediff = datediff / 50 datemin = datemin - datediff datemax = datemax + datediff ymin = np.nanmin(y) - 0.02 * (abs(np.nanmax(y) - np.nanmin(y))) ymax = np.nanmax(y) + 0.02 * (abs(np.nanmax(y) - np.nanmin(y))) (oldymin,oldymax) = ax.get_ylim() (oldxmin,oldxmax) = ax.get_xlim() if (fylim is not None): if type(fylim) is not tuple: print "option <fylim> must be a tuple" return None (ymin,ymax) = fylim if datemin.toordinal() < oldxmin: xchange = 1 oldxmin = datemin.toordinal() if datemax.toordinal() > oldxmax: xchange = 1 oldxmax = datemax.toordinal() if ymin < oldymin: ychange = 1 oldymin = ymin if ymax > oldymax: ychange = 1 oldymax = ymax if xchange: plt.setp(ax,xlim=ax.set_xlim(oldxmin,oldxmax)) if ychange: plt.setp(ax,ylim=ax.set_ylim(oldymin,oldymax)) ax.xaxis.set_major_locator(years) ax.grid(True) my_fmt_xdate(ax) plt.figure(fig.number) return line1
def dateplt_m2(t,y,yl=5,fxlim=None,fylim=None,ticksize=10,nomth=False,fmt='+-',**kwargs): """Different from dateplt_m in that it's just a 2-digit year for x tick marks""" from matplotlib.dates import YearLocator,DateFormatter,MonthLocator xchange = 0 ychange = 0 fig = plt.gcf() ax = plt.gca() years = YearLocator(yl) yearsFmt = DateFormatter("'%y") if nomth == False: months = MonthLocator(1) if isinstance(y,np.ma.MaskedArray): data_extent = np.where(y.mask == False) if len(data_extent) == 0: data_start = 0 data_end = y.size - 1 else: data_start = data_extent[0][0] data_end = data_extent[0][-1] else: data_start = 0 data_end = y.size - 1 if (isinstance(t[0],np.float64) is True) or (isinstance(t[0],np.float32) is True): datemin = num2date(t[data_start]) datemax = num2date(t[data_end]) elif isinstance(t[0],dt): datemin = t[data_start] datemax = t[data_end] else: return None datediff = datemax - datemin datediff = datediff / 50 datemin = datemin - datediff datemax = datemax + datediff ymin = np.nanmin(y) - 0.02 * (abs(np.nanmax(y) - np.nanmin(y))) ymax = np.nanmax(y) + 0.02 * (abs(np.nanmax(y) - np.nanmin(y))) (oldymin,oldymax) = ax.get_ylim() (oldxmin,oldxmax) = ax.get_xlim() if (np.sign(ymax) != np.sign(ymin)): ax.axhline(0.0,c='black',lw=1.2,alpha=0.7) line1 = ax.plot(t,y,fmt,**kwargs) # ax.set_xlim(datemin,datemax) ax.grid(True) if (datemin.toordinal() < oldxmin) | (oldxmin == 0.): xchange = 1 oldxmin = datemin.toordinal() if datemax.toordinal() > oldxmax: xchange = 1 oldxmax = datemax.toordinal() if ymin < oldymin: ychange = 1 oldymin = ymin if ymax > oldymax: ychange = 1 oldymax = ymax if (fylim is not None): if type(fylim) is not tuple: print "option <fylim> must be a tuple" return None (ymin,ymax) = fylim plt.setp(ax,ylim=ax.set_ylim(ymin,ymax)) else: if xchange: plt.setp(ax,xlim=ax.set_xlim(oldxmin,oldxmax)) if ychange: plt.setp(ax,ylim=ax.set_ylim(oldymin,oldymax)) ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) if nomth == False: ax.xaxis.set_minor_locator(months) xtl = ax.get_xticklabels() ytl = ax.get_yticklabels() plt.setp(xtl,'size',ticksize) plt.setp(ytl,'size',ticksize) ##plt.show() ##my_fmt_xdate(ax,rot=45,hal='right') plt.figure(fig.number) return line1
wind_gust1_f2 = buoy.variables['wind_gust1_f2'][:] pressure = buoy.variables['pressure'][:] wave_h_max = buoy.variables['wave_h_max'][:] wind_dir1_f2 = buoy.variables['wind_dir1_f2'][:] dew_point = buoy.variables['dew_point'][:] avg_wind_int1_f2 = buoy.variables['avg_wind_int1_f2'][:] cm_dir3 = buoy.variables['cm_dir3'][:] cm_dir2 = buoy.variables['cm_dir2'][:] longitude = buoy.variables['longitude'][:] avg_wind_int1 = buoy.variables['avg_wind_int1'][:] avg_wind_int2 = buoy.variables['avg_wind_int2'][:] wind_gust2 = buoy.variables['wind_gust2'][:] avg_dir2 = buoy.variables['avg_dir2'][:] wind_gust1 = buoy.variables['wind_gust1'][:] #converte data em juliano (time) para datetime datat = pl.num2date(time) #figuras pl.figure() pl.subplot(311) pl.plot(datat,wave_hs,'.') pl.ylim(0,7) pl.subplot(312) pl.plot(datat,wave_period,'.') pl.ylim(0,25) pl.subplot(313) pl.plot(datat,wave_dir,'.') pl.ylim(0,360) pl.show()
def get_timed_data_sheet(wb, fkeys, pkeys, date_nums, ras, decs, azs, elevs): """ See if the timed data sheet exist and create it if not A timed data sheet is basically the power VtoF data as a function of time (rows) and frequency/polarization (columns). This initiates the sheet but does not fill it. fill_map_sheet() does that. @param wb : the spreadsheet @type wb : openpyxl.Wotkbook() instance @param fkeys : frequency-based keys into the data @type fkeys : list of float @param pkeys : polarization-based keys @type pkeys : list of str @param date_nums : datetime for each sample @type date_nums : matplotlib date numbers @param ras : right ascensions @type ras : list of floats (hr) @param decs : declinations @type decs : list of floats (deg) @param azs : azimuths @type azs : list of floats @param elevs : elevations @type elevs : list of float @return: datasheet """ timedata_sheet = wb.get_sheet_by_name("TimeData") if timedata_sheet == None: # Doesn't exist. Make it. if diag: print("Trying to create a new data sheet") try: timedata_sheet = wb.create_sheet(1) except Exception as details: print("Could not create sheet") print(details) return None timedata_sheet.title = "TimeData" if diag: print(timedata_sheet,"has been given the name",\ timedata_sheet.title) # make column titles column_names = ["Date/Time", "R.A.", "Decl.", "Az", "Elev"] for freq in fkeys: for pol in pkeys: column_names.append(("%5.2f-%3s" % (freq, pol)).strip()) for index in range(len(column_names)): timedata_sheet.cell(row=0, column=index).value = column_names[index] # Fill the columns for count in range(len(date_nums)): timedata_sheet.cell(row = count+1, column = get_column_id(timedata_sheet,"Date/Time")).value = \ num2date(date_nums[count]) timedata_sheet.cell(row = count+1, column = get_column_id(timedata_sheet,"R.A.")).value = \ ras[count] timedata_sheet.cell(row = count+1, column = get_column_id(timedata_sheet,"Decl.")).value = \ decs[count] timedata_sheet.cell(row = count+1, column = get_column_id(timedata_sheet,"Elev")).value = \ elevs[count] timedata_sheet.cell(row = count+1, column = get_column_id(timedata_sheet,"Az")).value = \ azs[count] if diag: print(timedata_sheet.title, "sheet exists") return timedata_sheet