def prova(cco2, alt1, alt2, use_model=1): diffnlte = [] hras = [] hrbs = [] temps = [] tempsgrad = [] for atm, col in zip(allatms, npl.color_set(6)): diffnlte.append(all_coeffs_nlte[(atm, cco2, 'hr_ref')][alt1:alt2] - all_coeffs_nlte[(atm, cco2, 'hr_lte')][alt1:alt2]) hra, hrb = npl.hr_from_ab_diagnondiag(all_coeffs[(atm, cco2, 'acoeff')], all_coeffs[(atm, cco2, 'bcoeff')], all_coeffs[(atm, cco2, 'asurf')], all_coeffs[(atm, cco2, 'bsurf')], atm_pt[(atm, 'temp')], atm_pt[(atm, 'surf_temp')], max_alts=66) hras.append(hra[alt1:alt2]) hrbs.append(hrb[alt1:alt2]) temps.append(atm_pt[(atm, 'temp')][alt1:alt2]) tempsgrad.append(np.gradient(atm_pt[(atm, 'temp')])[alt1:alt2]) hras = np.concatenate(hras, axis=1) hrbs = np.concatenate(hrbs, axis=1) temps = np.concatenate(temps, axis=1) tempsgrad = np.concatenate(tempsgrad, axis=1) diffnlte = np.concatenate(diffnlte, axis=1) # Mod 1: uso solo hra e hrb X = np.stack([hras, hrbs]).T Y = np.stack(diffnlte) # STANDARDIZZO LE FEATURES #scaler = StandardScaler().fit(X) #X = scaler.transform(X) scores = [] model1 = LinearRegression().fit(X, Y) print(model1.score(X, Y)) scores.append(model1.score(X, Y)) # # Mod 2: butto dentro anche temperature # X = np.stack([hras, hrbs, temps]).T # model2 = LinearRegression().fit(X, Y) # print(model2.score(X, Y)) # scores.append(model2.score(X, Y)) ################################################################## return model1.intercept_, model1.coef_
#################################################################################### ialt_strano = 41 atm = 'mle' interp_coeffs = dict() for nam in ['acoeff', 'bcoeff', 'asurf', 'bsurf', 'alpha', 'Lesc']: int_fun, signc = npl.interp_coeff_logco2(coeffs[nam], coeffs['co2profs']) interp_coeffs[(nam, 'int_fun')] = int_fun interp_coeffs[(nam, 'signc')] = signc print('Coeffs from interpolation!') mults = np.arange(0.25, 8.1, 0.25) #for nam in ['acoeff', 'bcoeff', 'asurf', 'bsurf', 'alpha', 'Lesc']: colors = npl.color_set(n_alts_trlo) for nam in ['acoeff', 'bcoeff']: figs = [] axes = [] namsurf = nam[0] + 'surf' coefff = [] for co2mult in mults: co2vmr = co2mult * atm_pt[(atm, 2, 'co2')] int_fun = interp_coeffs[(namsurf, 'int_fun')] sc = interp_coeffs[(namsurf, 'signc')] interplog = [ int_fun[ialt](co2vmr[ialt]) for ialt in range(n_alts_trlo) ] #print(co2mult, interplog)
alt_fomi, hr_fomi = npl.old_param(alts, temp, pres, co2vmr) oldco = spline(alt_fomi, hr_fomi) hr_fomi = oldco(alts) tit = 'co2: {} - atm: {}'.format(cco2, atm) xlab = 'CR (K/day)' ylab = 'Alt (km)' # labels = ['nlte_ref', 'new_param', 'np_wutop', 'np_all_wutop', 'np_aw_extended', 'np_noalpha', 'old param'] # hrs = [hr_ref, hr_calc, hr_calc_wutop, hr_calc_all, hr_calc_extended, hr_calc_alpha1, hr_fomi] labels = [ 'nlte_ref', 'np_all_wutop', 'np_aw_extended', 'np_w_lfomi', 'old param' ] hrs = [hr_ref, hr_calc_all, hr_calc_extended, hr_calc_fom, hr_fomi] colors = np.array(npl.color_set(6))[np.arange(6) != 4] fig, a0, a1 = npl.manuel_plot( alts, hrs, labels, xlabel=xlab, ylabel=ylab, title=tit, xlimdiff=(-15, 15), xlim=(-70, 10), linestyles=['-', '--', '-', '--', '-', ':', ':'], colors=colors, orizlines=[70., alts[n_alts_trlo], alts[n_alts_trhi]]) figs.append(fig) a0s.append(a0)
sys.exit() cco2 = 4 fig = plt.figure() fig, axes = plt.subplots(nrows=2, ncols=3, sharex=True, sharey=True) axes = np.squeeze(np.reshape(axes, (1, 6))) for ii, atm in enumerate(allatms): ax = axes[ii] hr_nlte_fun = all_coeffs_nlte[(atm, cco2, 'hr_nlte_fb')] + all_coeffs_nlte[ (atm, cco2, 'hr_nlte_iso')] hr_nlte_hot = all_coeffs_nlte[(atm, cco2, 'hr_nlte_hot')] hr_nlte = all_coeffs_nlte[(atm, cco2, 'hr_nlte')] hr_lte_fun, hr_lte_hot = npl.hr_LTE_FB_vs_ob(atm, cco2) hr_lte = all_coeffs_nlte[(atm, cco2, 'hr_lte')] hr_ref = all_coeffs[(atm, cco2, 'hr_ref')] cols = npl.color_set(5) if ii == 0: ax.plot(hr_lte_fun, all_alts, label='LTE fun', color=cols[0]) ax.plot(hr_lte_hot, all_alts, label='LTE hot', color=cols[2]) ax.plot(hr_lte, all_alts, label='LTE', color=cols[4]) ax.plot(hr_nlte_fun, all_alts, label='NLTE fun', color=cols[0], linestyle='--') ax.plot(hr_nlte_hot, all_alts, label='NLTE hot', color=cols[2], linestyle='--') ax.plot(hr_nlte, all_alts, label='NLTE', color=cols[4], linestyle='--')
Oprof=ovmr, O2prof=o2vmr, N2prof=n2vmr) oldco = spline(alt_fomi, hr_fomi) hr_fomi = oldco(alts) ### FIGURE tit = 'co2: {} - atm: {}'.format(cco2, atm) xlab = 'CR (K/day)' ylab = 'Alt (km)' # labels = ['nlte_ref', 'new_param', 'np_wutop', 'np_all_wutop', 'np_aw_extended', 'np_noalpha', 'old param'] # hrs = [hr_ref, hr_calc, hr_calc_wutop, hr_calc_all, hr_calc_extended, hr_calc_alpha1, hr_fomi] labels = ['nlte_ref', 'new_param', 'old_param'] hrs = [hr_ref, hr_calc, hr_fomi] colors = np.array(npl.color_set(3)) fig, a0, a1 = npl.manuel_plot(alts, hrs, labels, xlabel=xlab, ylabel=ylab, title=tit, xlimdiff=(-15, 15), xlim=(-70, 10), linestyles=['-', '-', '--'], colors=colors, orizlines=[70., alts[n_alts_trlo], alts[n_top]]) fig.savefig(cart_out_4 + 'test_calchr.pdf') #ok.
x0 = surfanom # per i cosi surface uso la anomaly di surface temperature acos = acos[:, :n_alts] corrco = np.empty_like(acos[0]) for i in range(acos[0].shape[0]): corrco[i] = np.corrcoef(x0, acos[:, i])[1, 0] cico, regrco, _, _ = npl.linearregre_coeff(x0, acos) regrcoef[(conam, 'R')] = corrco regrcoef[(conam, 'c')] = cico regrcoef[(conam, 'm')] = regrco for conam in ['acoeff', 'bcoeff']: fig = plt.figure() for ialt, col in zip(range(n_alts), npl.color_set(n_alts)): plt.plot(coefsolv[conam].eofs()[0][:, ialt], alts, color=col) plt.xlim(-0.02, 0.02) plt.title(conam + ' - eof 0') fig.savefig(cartou + '{}_eof0.pdf'.format(conam)) for conam in ['acoeff', 'bcoeff']: fig = plt.figure() for ialt, col in zip(range(n_alts), npl.color_set(n_alts)): plt.plot(np.abs(regrcoef[(conam, 'R')][:, ialt]), alts, color=col) #plt.xlim(-0.02, 0.02) plt.title(conam + ' - rcorr') fig.savefig(cartou + '{}_rcorr.pdf'.format(conam)) fig = plt.figure() plt.plot(coefsolv['asurf'].eofs()[0], alts, label='asurf')
# # Mod 2: butto dentro anche temperature # X = np.stack([hras, hrbs, temps]).T # model2 = LinearRegression().fit(X, Y) # print(model2.score(X, Y)) # scores.append(model2.score(X, Y)) ################################################################## return model1.intercept_, model1.coef_ alt1 = 40 alt2 = 53 cco2 = 7 fig2, ax2 = plt.subplots() for atm, col in zip(allatms, npl.color_set(6)): diff = all_coeffs_nlte[(atm, cco2, 'hr_ref')][alt1:alt2] - all_coeffs_nlte[ (atm, cco2, 'hr_lte')][alt1:alt2] hra, hrb = npl.hr_from_ab_diagnondiag(all_coeffs[(atm, cco2, 'acoeff')], all_coeffs[(atm, cco2, 'bcoeff')], all_coeffs[(atm, cco2, 'asurf')], all_coeffs[(atm, cco2, 'bsurf')], atm_pt[(atm, 'temp')], atm_pt[(atm, 'surf_temp')], max_alts=66) ax2.plot(diff, np.arange(alt1, alt2), color=col, label=atm) ax2.plot(hra[alt1:alt2], np.arange(alt1, alt2), color=col, linestyle='--') ax2.plot(hrb[alt1:alt2], np.arange(alt1, alt2), color=col, linestyle=':') ax2.grid() ax2.legend()
npl.adjust_ax_scale(a0s) npl.adjust_ax_scale(a1s) npl.plot_pdfpages( cart_out + 'check_newparam_LTE_final_{}_correcto.pdf'.format(tip), figs) #pickle.dump(interp_coeffs, open(cart_out + 'interp_coeffs_v1_LTE.p', 'w')) for tip in ['unifit', 'varfit']: var_cosi = np.linspace(0., 1., 20) co2profs = [ atm_pt[('mle', 1, 'co2')] * (1 - cos) + atm_pt[('mle', 6, 'co2')] * cos for cos in var_cosi ] colors = npl.color_set(20) ########################################################################### figs = [] axes = [] for atm in allatms: temp = atm_pt[(atm, 'temp')] surf_temp = atm_pt[(atm, 'surf_temp')] hrs_interp = [] for co2pr in co2profs: # i coeffs universali: coeffs = [] for nam in ['acoeff', 'bcoeff', 'asurf', 'bsurf']: int_fun = interp_coeffs[(tip, nam, 'int_fun')]
# print('Atm {}. Unstable ialt {}'.format(atm, ialt)) # axes[0].plot(np.mean([coeff[:n_alts, ialt-1][:-2], coeff[:n_alts, ialt+1][2:]], axis = 0), alts[1:-1], color = col, linestyle = '--') # if np.abs(mco[ialt, ialt])/np.abs(np.mean([mco[ialt-1, ialt-1], mco[ialt+1, ialt+1]])) > 1.5: # print('Atm {}. Unstable ialt {}'.format(atm, ialt)) # axes[1].plot(np.mean([mco[:n_alts, ialt-1][:-2], mco[:n_alts, ialt+1][2:]], axis = 0), alts[1:-1], color = col, linestyle = '--') # axes[0].set_title('c') # axes[1].set_title('m') # plt.suptitle(conam) # figs.append(fig) # npl.plot_pdfpages(cartou + 'regrcoeff.pdf', figs) # ### OK! now I use the dotprods and the regrcoef to reconstruct the a and b coeffs, and compute the hr and check wrt the reference and the varfit5. cart_out = '/home/fabiano/Research/lavori/CO2_cooling/new_param/LTE/' tot_coeff_co2 = pickle.load(open(cart_out + 'tot_coeffs_co2_v2_LTE.p', 'rb')) colors = npl.color_set(5) colors = colors[:3] + [colors[4]] figs = [] a0s = [] a1s = [] for cco2 in range(1, 8): for atm, dp, dp1, sa in zip(allatms, dotprods, dotprods1, surfanom): temp = atm_pt[(atm, 'temp')] surf_temp = atm_pt[(atm, 'surf_temp')] coeffs = dict() for conam in ['acoeff', 'bcoeff', 'asurf', 'bsurf']: if 'surf' in conam: coeffs[conam] = regrcoef[(cco2, conam, 'c')] + regrcoef[ (cco2, conam, 'm')] * sa
def prova(cco2, alt1, alt2, use_model=1): diffnlte = [] hras = [] hrbs = [] temps = [] tempsgrad = [] for atm, col in zip(allatms, npl.color_set(6)): diffnlte.append(all_coeffs_nlte[(atm, cco2, 'hr_ref')][alt1:alt2] - all_coeffs_nlte[(atm, cco2, 'hr_lte')][alt1:alt2]) hra, hrb = npl.hr_from_ab_decomposed(all_coeffs[(atm, cco2, 'acoeff')], all_coeffs[(atm, cco2, 'bcoeff')], all_coeffs[(atm, cco2, 'asurf')], all_coeffs[(atm, cco2, 'bsurf')], atm_pt[(atm, 'temp')], atm_pt[(atm, 'surf_temp')], max_alts=66) hras.append(hra[alt1:alt2]) hrbs.append(hrb[alt1:alt2]) temps.append(atm_pt[(atm, 'temp')][alt1:alt2]) tempsgrad.append(np.gradient(atm_pt[(atm, 'temp')])[alt1:alt2]) hras = np.concatenate(hras) hrbs = np.concatenate(hrbs) temps = np.concatenate(temps) tempsgrad = np.concatenate(tempsgrad) diffnlte = np.concatenate(diffnlte) # Mod 1: uso solo hra e hrb X = np.stack([hras, hrbs]).T Y = np.stack(diffnlte) # STANDARDIZZO LE FEATURES #scaler = StandardScaler().fit(X) #X = scaler.transform(X) scores = [] model1 = LinearRegression().fit(X, Y) print(model1.score(X, Y)) scores.append(model1.score(X, Y)) # Mod 2: butto dentro anche temperature X = np.stack([hras, hrbs, temps]).T model2 = LinearRegression().fit(X, Y) print(model2.score(X, Y)) scores.append(model2.score(X, Y)) # Mod 3: solo temp e tempgrad X = np.stack([hras, hrbs, temps, tempsgrad]).T model3 = LinearRegression().fit(X, Y) print(model3.score(X, Y)) scores.append(model3.score(X, Y)) # Mod 4: butto dentro temps X = np.stack([hras, temps]).T model4 = LinearRegression().fit(X, Y) print(model4.score(X, Y)) scores.append(model4.score(X, Y)) # Mod 5: solo temp e tempgrad X = np.stack([temps, tempsgrad]).T model5 = LinearRegression().fit(X, Y) print(model5.score(X, Y)) scores.append(model5.score(X, Y)) models = [model1, model2, model3, model4, model5] ################################################################## return scores