def main_data(start, end, **kwargs): span = kwargs["span"] region = kwargs["region"] get_columns = kwargs["get_columns"] accumulate = kwargs["accumulate"] date_ax, date_ax_str = get_date_ax(start, end) N = len(date_ax_str) skipping_date_str = [] accumulate_data = [] data = [] for i, day in enumerate(date_ax_str): print("{}/{}: {}".format(i + 1, N, day)) print("start: {}, end: {}".format(start, end)) year = day[2:4] month = day[4:6] #ファイル名の生成 wind_file_name = "../data/csv_w/ecm" + day[2:] + ".csv" ice_file_name = "../data/csv_iw/" + day[2:] + ".csv" ic0_145_file_name = "../data/csv_ic0/IC0_" + day + ".csv" sit_145_file_name = "../data/csv_sit/SIT_" + day + ".csv" coeff_file_name = "../data/csv_A_30/ssc_amsr_ads" + str(year) + str( month) + "_" + str(span) + "_fin.csv" hermert_file_name = "../data/csv_Helmert_30/Helmert_30_" + str( day)[:6] + ".csv" # wind10m_file_name = "../data/netcdf4/" + day[2:] + ".csv" # t2m_file_name = "../data/netcdf4/" + day[2:] + ".csv" skipping_boolean = ("coeff" not in get_columns) and (not all([ os.path.isfile(wind_file_name), os.path.isfile(ice_file_name), os.path.isfile(coeff_file_name) ])) if ("ic0_145" in get_columns): skipping_boolean = ("coeff" not in get_columns) and (not all([ os.path.isfile(wind_file_name), os.path.isfile(ice_file_name), os.path.isfile(coeff_file_name), os.path.isfile(ic0_145_file_name) ])) if ("sit_145" in get_columns): skipping_boolean = ("coeff" not in get_columns) and (not all([ os.path.isfile(wind_file_name), os.path.isfile(ice_file_name), os.path.isfile(coeff_file_name), os.path.isfile(sit_145_file_name) ])) if skipping_boolean == True: print("\tSkipping " + day + " file...") date_ax_str.remove(day) bb = date(int(day[:4]), int(day[4:6]), int(day[6:])) date_ax.remove(bb) skipping_date_str.append(day) continue data = pd.DataFrame({"data_idx": np.array(ocean_grid_145).ravel()}) if "ex_1" in get_columns: print("\t{}\n\t{}\n\t{}\n\t{}".format(wind_file_name, ice_file_name, coeff_file_name)) tmp = calc_data.get_w_regression_data(wind_file_name, ice_file_name, coeff_file_name) data = pd.concat([data, tmp], axis=1) if "ex_2" in get_columns: print("\t{}\n\t{}\n\t{}\n\t{}".format(wind_file_name, ice_file_name, hermert_file_name)) tmp = calc_data.get_w_hermert_data(wind_file_name, ice_file_name, hermert_file_name) data = pd.concat([data, tmp], axis=1) if "w" in get_columns: print("\t{}".format(wind_file_name)) tmp = calc_data.get_1day_w_data(wind_file_name) data = pd.concat([data, tmp], axis=1) if "iw" in get_columns: print("\t{}".format(ice_file_name)) tmp = calc_data.get_1day_iw_data(ice_file_name) data = pd.concat([data, tmp], axis=1) if "ic0_145" in get_columns: print("\t{}".format(ic0_145_file_name)) tmp = calc_data.get_1day_ic0_data(ic0_145_file_name) data = pd.concat([data, tmp], axis=1) if "sit_145" in get_columns: print("\t{}".format(sit_145_file_name)) tmp = calc_data.get_1day_sit_data(sit_145_file_name) data = pd.concat([data, tmp], axis=1) if "coeff" in get_columns: print("\t{}".format(coeff_file_name)) tmp = calc_data.get_1month_coeff_data(coeff_file_name) data = pd.concat([data, tmp], axis=1) if "hermert" in get_columns: print("\t{}".format(hermert_file_name)) tmp = calc_data.get_1month_hermert_data(hermert_file_name) data = pd.concat([data, tmp], axis=1) """ if "w10m" in get_columns: tmp = calc_data.get_1day_w10m_data(wind10m_file_name) data = pd.concat([data, tmp], axis=1) if "t2m" in get_columns: tmp = calc_data.get_1day_t2m_data(t2m_file_name) data = pd.concat([data, tmp], axis=1) """ data = calc_data.get_masked_region_data(data, region) if ("coeff" in get_columns): print("\tSelected only coeff data. Getting out of the loop...") continue if accumulate == True: data_1 = data.drop("data_idx", axis=1) print("\t{}".format(data_1.columns)) accumulate_data.append(np.array(data_1)) if accumulate == True: print("accumulate: True\tdata type: array") return date_ax, date_ax_str, skipping_date_str, accumulate_data else: print("accumulate: False\tdata type: DataFrame") return date_ax, date_ax_str, skipping_date_str, data
def H_ts_A_month(): def plot_param_1g(save_name): plot_param_list = ["A", "theta", "epsilon2"] for i, item in enumerate(plot_param_list): plot_data_1g = data_m_1g[item].loc[:, ["mean", "std", "50%"]] plot_data_1g["2sigma_pos"] = plot_data_1g['mean']+2*np.sqrt(plot_data_1g['std']) plot_data_1g["2sigma_neg"] = plot_data_1g['mean']-2*np.sqrt(plot_data_1g['std']) dates = pd.date_range("2013", periods=12, freq='MS') plot_data_1g["Month"] = dates fig, ax = plt.subplots() plt.subplot(311+i) ax.plot(dates, plot_data_1g['mean'], '-') ax.plot(dates, plot_data_1g["2sigma_pos"], '-') ax.plot(dates, plot_data_1g["2sigma_neg"], '-') ax.xaxis.set_major_formatter(mdates.DateFormatter('%m')) d = plot_data_1g['Month'].values plt.fill_between(d, plot_data_1g['2sigma_pos'], plot_data_1g['2sigma_neg'], facecolor='green', alpha=0.2, interpolate=True) plt.savefig(save_name) plt.close() def plot_param_2g(save_name): plot_param_list = ["A", "theta", "epsilon2"] for i, item in enumerate(plot_param_list): for j, is_np in enumerate([1,0]): plot_data_2g_np_pos = data_m_2g.loc[(is_np), (item, ["mean", "std", "50%"])] plot_data_2g_np_pos["2sigma_pos"] = plot_data_2g_np_pos['mean']+2*np.sqrt(plot_data_2g_np_pos['std']) plot_data_2g_np_pos["2sigma_neg"] = plot_data_2g_np_pos['mean']-2*np.sqrt(plot_data_2g_np_pos['std']) plot_data_2g_np_pos["Month"] = np.arange(1, 13, 1) plt.subplot(321+j+i*2) plt.plot_date(plot_data_2g_np_pos['Month'], plot_data_2g_np_pos['mean'], '-') plt.plot_date(plot_data_2g_np_pos['Month'], plot_data_2g_np_pos["2sigma_pos"], '-') plt.plot_date(plot_data_2g_np_pos['Month'], plot_data_2g_np_pos["2sigma_neg"], '-') d = plot_data_2g_np_pos['Month'].values plt.fill_between(d, plot_data_2g_np_pos['2sigma_pos'], plot_data_2g_np_pos['2sigma_neg'], facecolor='green', alpha=0.2, interpolate=True) plt.savefig(save_name) plt.close() def plot_param_3g(plot_param_item, save_name): for i, is_np in enumerate([1, 0]): plot_data_3g_np_pos = data_m_2g.loc[(is_np), (plot_param_item, ["mean", "std", "50%"])] plot_data_3g_np_pos["2sigma_pos"] = plot_data_3g_np_pos[(plot_param_item, "mean")] + 2*np.sqrt(plot_data_3g_np_pos[(plot_param_item, "std")]) plot_data_3g_np_neg["2sigma_neg"] = plot_data_3g_np_neg[(plot_param_item, "mean")] - 2*np.sqrt(plot_data_3g_np_neg[(plot_param_item, "std")]) #plot_data_3g_np_neg["Month"] = np.arange(1, 13, 1) for j, subplot_idx in enumerate([0,1,2]): plt.subplot(321+i+subplot_idx*2) plt.plot_date(np.arange(1, 13, 1), plot_data_3g_np_pos.loc[(j), (plot_param_item, "mean")], '-') plt.plot_date(np.arange(1, 13, 1), plot_data_3g_np_pos.loc[(j), ("2sigma_pos")], '-') plt.plot_date(np.arange(1, 13, 1), plot_data_3g_np_pos.loc[(j), ("2sigma_neg")], '-') plt.fill_between(np.arange(1, 13, 1), plot_data_3g_np_pos.loc[(j), ("2sigma_pos")], plot_data_3g_np_pos.loc[(j), ("2sigma_neg")], facecolor='green', alpha=0.2, interpolate=True) plt.savefig(save_name) plt.close() y_list = ["03", "04", "05", "06", "07", "08", "09", "10", "13", "14", "15", "16"] month_list = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"] for y in y_list: data_m = pd.DataFrame([]) for m in month_list: yymm = "20" + y + m hermert_file_name = "../data/csv_Helmert_30/Helmert_30_" + yymm + ".csv" data = calc_data.get_1month_hermert_data(hermert_file_name) data = pd.concat([latlon_ex["Name"], data], axis=1) #data = data.drop(["Lat", "Lon", "Label", "idx1", "idx2"], axis=1) rank_np = np.zeros(145**2) rank_np[data[data.Name=="north_polar"].index] = 1 data["rank_np"] = rank_np rank_R2 = np.ones(145**2) rank_R2[data[data.R2<=(1/3)**2].index] = 0 rank_R2[data[data.R2>(2/3)**2].index] = 2 data["rank_R2"] = rank_R2 data = data.dropna() #https://code.i-harness.com/ja/q/1c29878 print(data.isnull().sum().sum()) data["yymm"] = [pd.to_datetime(yymm, format="%Y%m")] * len(data) data_m = pd.concat([data_m, data]) """ describe_data = data.groupby("rank_np")[["A", "theta", "epsilon2"]].describe() describe_data = data.groupby("rank_np").agg({ 'A': [np.nanmean, np.nanstd, np.nanmedian], 'theta': [np.nanmean, np.nanstd, np.nanmedian], 'epsilon2': [np.nanmean, np.nanstd, np.nanmedian] }) pd.concat([data_all, data.loc[:, ["A", "theta", "epsilon2"]]]) data_m.append(np.array(data.loc[:, ["A", "theta", "epsilon2"]])) """ #data_m = np.array(data_m) #月ごとに全てのエリアの平均などを取得 data_m_1g = data_m.groupby("yymm")[["A", "theta", "epsilon2"]].describe().sort_index(level='yymm') #月ごとにrank_npで分類したものを取得 data_m_2g = data_m.groupby(["rank_np", "yymm"])[["A", "theta", "epsilon2"]].describe().sort_index(level='yymm') #月ごとにrank_npとrank_R2で分類したものを取得 data_m_3g = data_m.groupby(["rank_np", "rank_R2", "yymm"])[["A", "theta", "epsilon2"]].describe().sort_index(level='yymm') #1gのプロット plot_param_1g(save_name) #2gのプロット plot_param_2g(save_name) #3gのプロット plot_param_list = ["A", "theta", "epsilon2"] for item in plot_param_list: plot_param_3g(item, save_name) """ #data_m_A = data_m[:,:,0].T #A_ave = np.nanmean(data_m_A, axis=0) #A_std = np.nanstd(data_m_A, axis=0) #https://stackoverflow.com/questions/29329725/pandas-and-matplotlib-fill-between-vs-datetime64 yydd_str = "20" + y tmp = pd.to_datetime(["20" + y + m for m in month_list]) dates = pd.date_range(yydd_str, periods=12, freq='M') #dates = pd.to_datetime(["20" + y + m for m in month_list], format="%Y%m") data = pd.DataFrame({"A_ave": A_ave, "A_std": A_std, "Date": dates}) data["A_2sigma_pos"] = data['A_ave']+2*data['A_std'] data["A_2sigma_neg"] = data['A_ave']-2*data['A_std'] plt.figure() plt.plot_date(data['Date'], data['A_ave'], '-') plt.plot_date(data['Date'], data["A_2sigma_pos"], '-') plt.plot_date(data['Date'], data["A_2sigma_neg"], '-') d = data['Date'].values plt.fill_between(d, data['A_2sigma_pos'], data['A_2sigma_neg'], facecolor='green', alpha=0.2, interpolate=True) plt.xticks(rotation=25) """ #http://pandas.pydata.org/pandas-docs/version/0.15.0/visualization.html """ plt.plot(price.index, price, 'k') plt.plot(ma.index, ma, 'b') plt.fill_between(mstd.index, ma-2*mstd, ma+2*mstd, color='b', alpha=0.2) """ """
def H_ts_A_year(): def y_plot_param_1g(save_name): plot_param_list = ["A", "theta", "epsilon2"] for i, item in enumerate(plot_param_list): plot_data_1g = data_m_1g[item].loc[:, ["mean", "std", "50%"]] plot_data_1g["2sigma_pos"] = plot_data_1g['mean']+2*np.sqrt(plot_data_1g['std']) plot_data_1g["2sigma_neg"] = plot_data_1g['mean']-2*np.sqrt(plot_data_1g['std']) plot_data_1g["Year"] = np.array([2003,2004,2005,2006,2007,2008,2009,2010,2013,2014,2015,2016]) plt.subplot(311+i) plt.plot_date(plot_data_1g['Year'], plot_data_1g['mean'], '-') plt.plot_date(plot_data_1g['Year'], plot_data_1g["2sigma_pos"], '-') plt.plot_date(plot_data_1g['Year'], plot_data_1g["2sigma_neg"], '-') d = plot_data_1g['Year'].values plt.fill_between(d, plot_data_1g['2sigma_pos'], plot_data_1g['2sigma_neg'], facecolor='green', alpha=0.2, interpolate=True) plt.savefig(save_name) plt.close() def y_plot_param_2g(save_name): plot_param_list = ["A", "theta", "epsilon2"] for i, item in enumerate(plot_param_list): for j, is_np in enumerate([1,0]): plot_data_2g_np_pos = data_m_2g.loc[(is_np), (item, ["mean", "std", "50%"])] plot_data_2g_np_pos["2sigma_pos"] = plot_data_2g_np_pos['mean']+2*np.sqrt(plot_data_2g_np_pos['std']) plot_data_2g_np_pos["2sigma_neg"] = plot_data_2g_np_pos['mean']-2*np.sqrt(plot_data_2g_np_pos['std']) plot_data_2g_np_pos["Year"] = np.array([2003,2004,2005,2006,2007,2008,2009,2010,2013,2014,2015,2016]) plt.subplot(321+j+i*2) plt.plot_date(plot_data_2g_np_pos['Year'], plot_data_2g_np_pos['mean'], '-') plt.plot_date(plot_data_2g_np_pos['Year'], plot_data_2g_np_pos["2sigma_pos"], '-') plt.plot_date(plot_data_2g_np_pos['Year'], plot_data_2g_np_pos["2sigma_neg"], '-') d = plot_data_2g_np_pos['Year'].values plt.fill_between(d, plot_data_2g_np_pos['2sigma_pos'], plot_data_2g_np_pos['2sigma_neg'], facecolor='green', alpha=0.2, interpolate=True) plt.savefig(save_name) plt.close() def y_plot_param_3g(plot_param_item, save_name): for i, is_np in enumerate([1, 0]): plot_data_3g_np_pos = data_m_2g.loc[(is_np), (plot_param_item, ["mean", "std", "50%"])] plot_data_3g_np_pos["2sigma_pos"] = plot_data_3g_np_pos[(plot_param_item, "mean")] + 2*np.sqrt(plot_data_3g_np_pos[(plot_param_item, "std")]) plot_data_3g_np_neg["2sigma_neg"] = plot_data_3g_np_neg[(plot_param_item, "mean")] - 2*np.sqrt(plot_data_3g_np_neg[(plot_param_item, "std")]) #plot_data_3g_np_neg["Month"] = np.arange(1, 13, 1) year_array = np.array([2003,2004,2005,2006,2007,2008,2009,2010,2013,2014,2015,2016]) for j, subplot_idx in enumerate([0,1,2]): plt.subplot(321+i+subplot_idx*2) plt.plot_date(year_array, plot_data_3g_np_pos.loc[(j), (plot_param_item, "mean")], '-') plt.plot_date(year_array, plot_data_3g_np_pos.loc[(j), ("2sigma_pos")], '-') plt.plot_date(year_array, plot_data_3g_np_pos.loc[(j), ("2sigma_neg")], '-') plt.fill_between(year_array, plot_data_3g_np_pos.loc[(j), ("2sigma_pos")], plot_data_3g_np_pos.loc[(j), ("2sigma_neg")], facecolor='green', alpha=0.2, interpolate=True) plt.savefig(save_name) plt.close() y_list = ["03", "04", "05", "06", "07", "08", "09", "10", "13", "14", "15", "16"] month_list = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"] for m in month_list: data_m = pd.DataFrame([]) for y in y_list: yymm = "20" + y + m hermert_file_name = "../data/csv_Helmert_30/Helmert_30_" + yymm + ".csv" data = calc_data.get_1month_hermert_data(hermert_file_name) data = pd.concat([latlon_ex["Name"], data], axis=1) #data = data.drop(["Lat", "Lon", "Label", "idx1", "idx2"], axis=1) rank_np = np.zeros(145**2) rank_np[data[data.Name=="north_polar"].index] = 1 data["rank_np"] = rank_np rank_R2 = np.ones(145**2) rank_R2[data[data.R2<=(1/3)**2].index] = 0 rank_R2[data[data.R2>(2/3)**2].index] = 2 data["rank_R2"] = rank_R2 data = data.dropna() #https://code.i-harness.com/ja/q/1c29878 print(data.isnull().sum().sum()) data["Year"] = [int("20"+y)] * len(data) data_m = pd.concat([data_m, data]) #年ごとに全てのエリアの平均などを取得 data_m_1g = data_m.groupby("Year")[["A", "theta", "epsilon2"]].describe().sort_index(level='Year') #年ごとにrank_npで分類したものを取得 data_m_2g = data_m.groupby(["rank_np", "Year"])[["A", "theta", "epsilon2"]].describe().sort_index(level='Year') #年ごとにrank_npとrank_R2で分類したものを取得 data_m_3g = data_m.groupby(["rank_np", "rank_R2", "Year"])[["A", "theta", "epsilon2"]].describe().sort_index(level='Year') #1gのプロット y_plot_param_1g(save_name) #2gのプロット y_plot_param_2g(save_name) #3gのプロット plot_param_list = ["A", "theta", "epsilon2"] for item in plot_param_list: y_plot_param_3g(item, save_name)
def H_ts_A_month(m_plot=True, y_plot=True): dirs_1g = "../result_h/H_ts_month_1g/" mkdir(dirs_1g) dirs_2g = "../result_h/H_ts_month_2g/" mkdir(dirs_2g) dirs_3g = "../result_h/H_ts_month_3g/" mkdir(dirs_3g) dirs_year = "../result_h/H_ts_year/" mkdir(dirs_year) def plot_param_1g(data_m_1g, save_name): fig, axes = plt.subplots(3, 1) dates = pd.date_range("2001", periods=12, freq='MS') plot_data_1g = data_m_1g["A"].loc[:, ["mean", "std", "50%"]] plot_data_1g["2sigma_pos"] = plot_data_1g['mean'] + 1 * np.sqrt( plot_data_1g['std']) plot_data_1g["2sigma_neg"] = plot_data_1g['mean'] - 1 * np.sqrt( plot_data_1g['std']) #plot_data_1g["Month"] = dates axes[0].plot(dates, plot_data_1g['mean'], '-', color="k") #d = plot_data_1g['Month'].values axes[0].fill_between(dates, plot_data_1g['2sigma_pos'], plot_data_1g['2sigma_neg'], facecolor='green', alpha=0.3, interpolate=True) #axes[0].set_ylim([0, 0.025]) axes[0].set_ylabel('A') #axes[0].set_title('A, ' + str_year) axes[0].xaxis.set_major_formatter(mdates.DateFormatter('%m')) plot_data_1g_theta = data_m_1g["theta"].loc[:, ["mean", "std", "50%"]] plot_data_1g_theta["2sigma_pos"] = plot_data_1g_theta[ 'mean'] + 2 * np.sqrt(plot_data_1g_theta['std']) plot_data_1g_theta["2sigma_neg"] = plot_data_1g_theta[ 'mean'] - 2 * np.sqrt(plot_data_1g_theta['std']) axes[1].plot(dates, plot_data_1g_theta['mean'], '-', color="k") axes[1].fill_between(dates, plot_data_1g_theta['2sigma_pos'], plot_data_1g_theta['2sigma_neg'], facecolor='lightskyblue', alpha=0.3, interpolate=True) #axes[1].set_ylim([-180, 180]) axes[1].set_ylim([-60, 60]) #axes[1].set_yticks([-180, -120, -60, 0, 60, 120, 180]) axes[1].set_ylabel('theta') #axes[1].set_title('theta, ' + str_year) axes[1].xaxis.set_major_formatter(mdates.DateFormatter('%m')) plot_data_1g_e2 = data_m_1g["epsilon2"].loc[:, ["mean", "std", "50%"]] plot_data_1g_e2["2sigma_pos"] = plot_data_1g_e2['mean'] + 2 * np.sqrt( plot_data_1g_e2['std']) plot_data_1g_e2["2sigma_neg"] = plot_data_1g_e2['mean'] - 2 * np.sqrt( plot_data_1g_e2['std']) axes[2].plot(dates, plot_data_1g_e2['mean'], '-', color="k") axes[2].fill_between(dates, plot_data_1g_e2['2sigma_pos'], plot_data_1g_e2['2sigma_neg'], facecolor='silver', alpha=0.3, interpolate=True) #axes[2].set_ylim([-180, 180]) #axes[2].set_yticks([-180, -120, -60, 0, 60, 120, 180]) axes[2].set_ylabel('e2') #axes[2].set_title('e2, ' + str_year) axes[2].xaxis.set_major_formatter(mdates.DateFormatter('%m')) #plt.setp(axes[0].get_xticklabels(), visible=False) #plt.setp(axes[1].get_xticklabels(), visible=False) plt.tight_layout() plt.savefig(save_name, dpi=900) plt.close() def plot_param_2g(data_m_2g, save_name): plot_param_list = ["A", "theta", "epsilon2"] fig, axes = plt.subplots(3, 2) dates = pd.date_range("2001", periods=12, freq='MS') axes[0, 0].set_title("Polar Region") axes[0, 1].set_title("Coastal Region") for i, item in enumerate(plot_param_list): for j, is_np in enumerate([1, 0]): plot_data_2g_np_pos = data_m_2g.loc[(is_np), (item, ["mean", "std", "50%"])] plot_data_2g_np_pos["2sigma_pos"] = plot_data_2g_np_pos[ (item, 'mean')] + 2 * np.sqrt(plot_data_2g_np_pos[(item, 'std')]) plot_data_2g_np_pos["2sigma_neg"] = plot_data_2g_np_pos[ (item, 'mean')] - 2 * np.sqrt(plot_data_2g_np_pos[(item, 'std')]) #plot_data_2g_np_pos["Month"] = np.arange(1, 13, 1) ax = axes[i, j] #plt.subplot(321+j+i*2) ax.plot_date(dates, plot_data_2g_np_pos[(item, 'mean')], '-', color="k") #d = plot_data_2g_np_pos['Month'].values ax.fill_between(dates, plot_data_2g_np_pos['2sigma_pos'], plot_data_2g_np_pos['2sigma_neg'], facecolor='lightskyblue', alpha=0.3, interpolate=True) if i == 0: ax.set_ylim([0, 0.025]) #ax.set_yticks([0, 0.025]) elif i == 1: ax.set_ylim([-180, 180]) ax.set_yticks([-180, -120, -60, 0, 60, 120, 180]) elif i == 2: ax.set_ylim([0, 1.5]) #ax.set_yticks([0, 0.025]) ax.xaxis.set_major_formatter(mdates.DateFormatter('%m')) axes[0, 0].set_ylabel('A') axes[1, 0].set_ylabel('theta') axes[2, 0].set_ylabel('e2') axes[2, 0].set_xlabel("Month") axes[2, 1].set_xlabel("Month") """ plt.setp(axes[0, 0].get_xticklabels(), visible=False) plt.setp(axes[1, 0].get_xticklabels(), visible=False) plt.setp(axes[0, 1].get_xticklabels(), visible=False) plt.setp(axes[1, 1].get_xticklabels(), visible=False) plt.setp(axes[0, 1].get_yticklabels(), visible=False) plt.setp(axes[1, 1].get_yticklabels(), visible=False) plt.setp(axes[2, 1].get_yticklabels(), visible=False) """ plt.tight_layout() plt.savefig(save_name, dpi=900) plt.close() def plot_param_3g(data_m_3g, plot_param_item, save_name): fig, axes = plt.subplots(3, 2) dates = pd.date_range("2001", periods=12, freq='MS') axes[0, 0].set_title("Polar Region") axes[0, 1].set_title("Coastal Region") for i, is_np in enumerate([1, 0]): plot_data_3g_np_pos = data_m_3g.loc[(is_np), (plot_param_item, ["mean", "std", "50%"])] plot_data_3g_np_pos["2sigma_pos"] = plot_data_3g_np_pos[ (plot_param_item, "mean")] + 2 * np.sqrt(plot_data_3g_np_pos[ (plot_param_item, "std")]) plot_data_3g_np_pos["2sigma_neg"] = plot_data_3g_np_pos[ (plot_param_item, "mean")] - 2 * np.sqrt(plot_data_3g_np_pos[ (plot_param_item, "std")]) for j, subplot_idx in enumerate([2, 1, 0]): ax = axes[j, i] ax.plot(dates, plot_data_3g_np_pos.loc[(subplot_idx), (plot_param_item, "mean")], '-', color="k") ax.fill_between(dates, plot_data_3g_np_pos.loc[(subplot_idx), ("2sigma_pos")], plot_data_3g_np_pos.loc[(subplot_idx), ("2sigma_neg")], facecolor='lightskyblue', alpha=0.3, interpolate=True) if plot_param_item == "A": for ax in axes: ax.set_ylim([0, 0.025]) elif plot_param_item == "theta": for ax in axes: ax.set_ylim([-180, 180]) ax.set_yticks([-180, -120, -60, 0, 60, 120, 180]) elif plot_param_item == "e2": for ax in axes: ax.set_ylim([0, 1.5]) axes[0, 0].set_ylabel('R2 High') axes[1, 0].set_ylabel('R2 Middle') axes[2, 0].set_ylabel('R2 Low') axes[2, 0].set_xlabel("Month") axes[2, 1].set_xlabel("Month") """ plt.setp(axes[0, 0].get_xticklabels(), visible=False) plt.setp(axes[1, 0].get_xticklabels(), visible=False) plt.setp(axes[0, 1].get_xticklabels(), visible=False) plt.setp(axes[1, 1].get_xticklabels(), visible=False) plt.setp(axes[0, 1].get_yticklabels(), visible=False) plt.setp(axes[1, 1].get_yticklabels(), visible=False) plt.setp(axes[2, 1].get_yticklabels(), visible=False) """ plt.tight_layout() plt.savefig(save_name, dpi=900) plt.close() def plot_param_1g_through_years(data_1g_dic, save_name): fig, axes = plt.subplots(3, 1) dates1 = pd.date_range("2003", "2010", freq='MS').append( pd.date_range("2013", "2017", freq='MS')) dates = dates1[:-1] data_A = pd.DataFrame([]) data_theta = pd.DataFrame([]) data_e2 = pd.DataFrame([]) for y in y_list: data_A = pd.concat([data_A, data_1g_dic[y]["1g_A"]]) data_theta = pd.concat([data_theta, data_1g_dic[y]["1g_theta"]]) data_e2 = pd.concat([data_e2, data_1g_dic[y]["1g_e2"]]) axes[0].plot(dates, data_A['mean'], '-', color="k") axes[0].fill_between(dates, data_A['mean'] + 2 * np.sqrt(data_A['std']), data_A['mean'] - 2 * np.sqrt(data_A['std']), facecolor='green', alpha=0.3, interpolate=True) axes[0].set_ylim([0, 0.025]) axes[0].set_ylabel('A') axes[0].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) axes[1].plot(dates, data_theta['mean'], '-', color="k") axes[1].fill_between( dates, data_theta['mean'] + 2 * np.sqrt(data_theta['std']), data_theta['mean'] - 2 * np.sqrt(data_theta['std']), facecolor='green', alpha=0.3, interpolate=True) axes[1].set_ylim([-180, 180]) axes[1].set_ylabel('theta') axes[1].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) axes[2].plot(dates, data_e2['mean'], '-', color="k") axes[2].fill_between(dates, data_e2['mean'] + 2 * np.sqrt(data_e2['std']), data_e2['mean'] - 2 * np.sqrt(data_e2['std']), facecolor='green', alpha=0.3, interpolate=True) #axes[2].set_ylim([0, 0.025]) axes[2].set_ylabel('e2') axes[2].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) plt.tight_layout() plt.savefig(save_name, dpi=900) plt.close() def plot_param_2g_through_years(data_2g_dic, save_name): fig, axes = plt.subplots(3, 2) dates1 = pd.date_range("2003", "2010", freq='MS').append( pd.date_range("2013", "2017", freq='MS')) dates = dates1[:-1] data_A_np = pd.DataFrame([]) data_A_coastal = pd.DataFrame([]) data_theta_np = pd.DataFrame([]) data_theta_coastal = pd.DataFrame([]) data_e2_np = pd.DataFrame([]) data_e2_coastal = pd.DataFrame([]) for y in y_list: data_A_np = pd.concat([data_A_np, data_2g_dic[y]["2g_A_polar"]]) data_A_coastal = pd.concat( [data_A_coastal, data_2g_dic[y]["2g_A_coastal"]]) data_theta_np = pd.concat( [data_theta_np, data_2g_dic[y]["2g_theta_polar"]]) data_theta_coastal = pd.concat( [data_theta_coastal, data_2g_dic[y]["2g_theta_coastal"]]) data_e2_np = pd.concat([data_e2_np, data_2g_dic[y]["2g_e2_polar"]]) data_e2_coastal = pd.concat( [data_e2_coastal, data_2g_dic[y]["2g_e2_coastal"]]) axes[0, 0].plot(dates, data_A_np['mean'], '-', color="k") axes[0, 0].fill_between(dates, data_A_np['mean'] + 2 * np.sqrt(data_A_np['std']), data_A_np['mean'] - 2 * np.sqrt(data_A_np['std']), facecolor='lightskyblue', alpha=0.3, interpolate=True) axes[0, 0].set_ylim([0, 0.025]) axes[0, 0].set_ylabel('A') axes[0, 0].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) axes[0, 1].plot(dates, data_A_coastal['mean'], '-', color="k") axes[0, 1].fill_between( dates, data_A_coastal['mean'] + 2 * np.sqrt(data_A_coastal['std']), data_A_coastal['mean'] - 2 * np.sqrt(data_A_coastal['std']), facecolor='lightskyblue', alpha=0.3, interpolate=True) axes[0, 1].set_ylim([0, 0.025]) axes[0, 1].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) axes[1, 0].plot(dates, data_theta_np['mean'], '-', color="k") axes[1, 0].fill_between( dates, data_theta_np['mean'] + 2 * np.sqrt(data_theta_np['std']), data_theta_np['mean'] - 2 * np.sqrt(data_theta_np['std']), facecolor='lightskyblue', alpha=0.3, interpolate=True) axes[1, 0].set_ylim([-180, 180]) axes[1, 0].set_ylabel('theta') axes[1, 0].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) axes[1, 1].plot(dates, data_theta_coastal['mean'], '-', color="k") axes[1, 1].fill_between(dates, data_theta_coastal['mean'] + 2 * np.sqrt(data_theta_coastal['std']), data_theta_coastal['mean'] - 2 * np.sqrt(data_theta_coastal['std']), facecolor='lightskyblue', alpha=0.3, interpolate=True) axes[1, 1].set_ylim([-180, 180]) axes[1, 1].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) axes[2, 0].plot(dates, data_e2_np['mean'], '-', color="k") axes[2, 0].fill_between( dates, data_e2_np['mean'] + 2 * np.sqrt(data_e2_np['std']), data_e2_np['mean'] - 2 * np.sqrt(data_e2_np['std']), facecolor='lightskyblue', alpha=0.3, interpolate=True) #axes[2, 0].set_ylim([0, 0.025]) axes[2, 0].set_ylabel('e2') axes[2, 0].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) axes[2, 1].plot(dates, data_e2_coastal['mean'], '-', color="k") axes[2, 1].fill_between( dates, data_e2_coastal['mean'] + 2 * np.sqrt(data_e2_coastal['std']), data_e2_coastal['mean'] - 2 * np.sqrt(data_e2_coastal['std']), facecolor='lightskyblue', alpha=0.3, interpolate=True) #axes[2, 1].set_ylim([0, 0.025]) axes[2, 1].xaxis.set_major_formatter(mdates.DateFormatter('%y%m')) #axes[2, 0].set_xlabel("Year") #axes[2, 1].set_xlabel("Year") plt.tight_layout() plt.savefig(save_name, dpi=900) plt.close() y_list = [ "03", "04", "05", "06", "07", "08", "09", "10", "13", "14", "15", "16" ] month_list = [ "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" ] data_1g_dic = {} data_2g_dic = {} data_3g_dic = {} for y in y_list: data_m = pd.DataFrame([]) for m in month_list: yymm = "20" + y + m hermert_file_name = "../data/csv_Helmert_30/Helmert_30_" + yymm + ".csv" data = calc_data.get_1month_hermert_data(hermert_file_name) data = pd.concat([latlon_ex["Name"], data], axis=1) #data = data.drop(["Lat", "Lon", "Label", "idx1", "idx2"], axis=1) rank_np = np.zeros(145**2) rank_np[data[data.Name == "north_polar"].index] = 1 data["rank_np"] = rank_np rank_R2 = np.ones(145**2) rank_R2[data[data.R2 <= (1 / 3)**2].index] = 0 rank_R2[data[data.R2 > (2 / 3)**2].index] = 2 data["rank_R2"] = rank_R2 data = data.dropna() #https://code.i-harness.com/ja/q/1c29878 print(data.isnull().sum().sum()) data["yymm"] = [pd.to_datetime(yymm, format="%Y%m")] * len(data) data_m = pd.concat([data_m, data]) #月ごとに全てのエリアの平均などを取得 data_m_1g = data_m.groupby("yymm")[[ "A", "theta", "epsilon2" ]].describe().sort_index(level='yymm') #月ごとにrank_npで分類したものを取得 data_m_2g = data_m.groupby(["rank_np", "yymm" ])[["A", "theta", "epsilon2" ]].describe().sort_index(level='yymm') #月ごとにrank_npとrank_R2で分類したものを取得 data_m_3g = data_m.groupby(["rank_np", "rank_R2", "yymm" ])[["A", "theta", "epsilon2" ]].describe().sort_index(level='yymm') if m_plot == True: #1gのプロット save_name_1g = dirs_1g + "1g_" + y + ".png" plot_param_1g(data_m_1g, save_name_1g) #2gのプロット save_name_2g = dirs_2g + "2g_" + y + ".png" plot_param_2g(data_m_2g, save_name_2g) #3gのプロット plot_param_list = ["A", "theta", "epsilon2"] for item in plot_param_list: save_name_3g = dirs_3g + item + "/" + "3g_" + y + ".png" plot_param_3g(data_m_3g, item, save_name_3g) """ sns.tsplot(data=data_m_A, ci="sd") plt.plot(np.nanmean(data_m_A, axis=0)) data_m_theta = data_m[:,:,1].T sns.tsplot(data=data_m_theta, ci="sd") data_m_e2 = data_m[:,:,2].T sns.tsplot(data=data_m_e2, ci="sd") """ tmp_1g = { "1g_A": data_m_1g["A"].loc[:, ["mean", "std", "50%"]], "1g_theta": data_m_1g["theta"].loc[:, ["mean", "std", "50%"]], "1g_e2": data_m_1g["epsilon2"].loc[:, ["mean", "std", "50%"]] } data_1g_dic[y] = tmp_1g tmp_2g = { "2g_A_polar": data_m_2g.loc[(1), ("A", ["mean", "std", "50%"])], "2g_A_coastal": data_m_2g.loc[(0), ("A", ["mean", "std", "50%"])], "2g_theta_polar": data_m_2g.loc[(1), ("theta", ["mean", "std", "50%"])], "2g_theta_coastal": data_m_2g.loc[(0), ("theta", ["mean", "std", "50%"])], "2g_e2_polar": data_m_2g.loc[(1), ("epsilon2", ["mean", "std", "50%"])], "2g_e2_coastal": data_m_2g.loc[(0), ("epsilon2", ["mean", "std", "50%"])] } data_2g_dic[y] = tmp_2g tmp_3g = { "3g_A_polar": data_m_3g.loc[(1), ("A", ["mean", "std", "50%"])], "3g_A_coastal": data_m_3g.loc[(0), ("A", ["mean", "std", "50%"])], "3g_theta_polar": data_m_3g.loc[(1), ("theta", ["mean", "std", "50%"])], "3g_theta_coastal": data_m_3g.loc[(0), ("theta", ["mean", "std", "50%"])], "3g_e2_polar": data_m_3g.loc[(1), ("epsilon2", ["mean", "std", "50%"])], "3g_e2_coastal": data_m_3g.loc[(0), ("epsilon2", ["mean", "std", "50%"])] } data_3g_dic[y] = tmp_3g if y_plot == True: #1g save_name_1g_year = dirs_year + "1g.png" plot_param_1g_through_years(data_1g_dic, save_name_1g_year) #2g save_name_2g_year = dirs_year + "2g.png" plot_param_2g_through_years(data_2g_dic, save_name_2g_year) return data_1g_dic, data_2g_dic, data_3g_dic