def scale_data(data, syear): """Scales the input array by month lenghs starting from january of start year""" scale = np.zeros(data.shape[0]) for m in range(len(scale)): mo = np.int(m % 12 + 1) scale[m] = 1E-3 / (mr(np.int(syear + np.floor(m / 12)), mo)[1] * 24 * 60 * 60) # input is g(CH4) m-2 month-1 # output is kg(CH4) m-2 s-1 for m in range(len(scale)): data[m, :, :] = data[m, :, :] * scale[m] return data
def total_stat(total_dict, new_procate_dict): total_file.writerow( ['\ufeffmonth', '#total_sku', 'total_sales', 'total_gmv', 'avg_price']) for month in total_dict: sku = len(total_dict[month]['sku']) month_days = mr(int(month[:4]), int(month[-2:]))[1] sales, gmv = month_sales('all_stat', total_dict[month]['vol'], total_dict[month]['price'], month_days, new_procate_dict) if sales == 0: print('sales 0') print([month, sku, sales, gmv, avg_price]) avg_price = 0 else: avg_price = gmv / sales total_file.writerow([month, sku, sales, gmv, avg_price])
def sku_stat(stat_dict, new_procate_dict): final_file.writerow( ['\ufeffcategory', 'month', '#sku', 'month_sales', 'gmv', 'avg_price']) for category in stat_dict: for month in stat_dict[category]: sku = len(stat_dict[category][month]['sku']) month_days = mr(int(month[:4]), int(month[-2:]))[1] sales, gmv = month_sales(category, stat_dict[category][month]['vol'], stat_dict[category][month]['price'], month_days, new_procate_dict) if sales == 0: print('sales 0') print([category, month, sku, sales, gmv, avg_price]) avg_price = 0 else: avg_price = gmv / sales final_file.writerow([category, month, sku, sales, gmv, avg_price])
def last_months_last_day(): '''Return datetime object for last day of previous month Ex: today = 2019-06-15 last_months_last_day() >>> Timestamp (2019, 5, 31) ''' t = dt.datetime.today() m = t.month y = t.year if m == 1: y -= 1 m = 12 else: m -= 1 d = mr(y, m)[1] return dt.datetime(y, m, d)
def get(self, *args, **kwargs): t = date.today() y, m = t.year, t.month self.date_from_str = self.request.GET.get("date_from") self.date_to_str = self.request.GET.get("date_to") try: self.date_from = dateutil.parser.parse(self.date_from_str) self.date_to = dateutil.parser.parse(self.date_to_str) except: self.date_from_str = date(y, m, 1).isoformat() self.date_to_str = date(y, m, mr(y, m)[1]).isoformat() self.date_from = dateutil.parser.parse(self.date_from_str) self.date_to = dateutil.parser.parse(self.date_to_str) self.budget_pk = self.request.GET.get("budget") self.budget = Budget.objects.filter(pk=self.budget_pk).first() if self.budget is None: self.budget = Budget.objects.last() self.budget_pk = self.budget.pk self.comparisons = [Comparison(self.date_from, self.date_to, en) for en in self.budget.entry_set.all()] self.comparisons.append(UnbudgetedComparison(self.date_from, self.date_to, self.budget)) return super(Compare, self).get(*args, **kwargs)
def endofmonth(dates): dts = np.array([ datetime(dt.year, dt.month, mr(dt.year, dt.month)[1]) for dt in dates ]) return dts
plt.title('Annual CH4 fluxes from [' + str(lat_min) + 'N:90N]') plt.ylabel('Tg', rotation=0) ax = plt.gca() ax.yaxis.set_label_coords(-0.02, 1) ax.set_xlim([2000, 2015]) plt.xlabel('year') plt.grid(linestyle='dotted') outf = output_dir + 'CH4_timeseries_2000-' + str(ye) + ifmt plt.savefig(outf) print("Created image:", outf) if plot_annual == True: # Monthly averages (calculate month length) mscale = np.zeros(12) for m in range(12): mscale[m] = mr(2001, m + 1)[1] x = np.linspace(1, 12, 12) obsmodels = map_obs fig, axs = plt.subplots(1, 2, figsize=(15, 8)) for omodel in obsmodels: col = obsmodels.index(omodel) ofile = glob(base_dir + 'wetlandCH4*' + omodel + '*.nc') odata = Dataset(ofile[0], 'r') try: olats = odata.variables['latitude'][:] except: olats = odata.variables['lat'][:] try: olons = odata.variables['longitude'][:] except: olons = odata.variables['lon'][:]