def make_plot(in_dir, in_files, title, out_filename, error): x_axis = partmc.log_grid(min=1e-8, max=1e-5, n_bin=3) x_centers = x_axis.centers() counter = 0 hist_array = np.zeros([len(x_centers), config.i_loop_max]) error = np.zeros([3]) for file in in_files: ncf = scipy.io.netcdf.netcdf_file(in_dir + file, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() dry_diameters = particles.dry_diameters() hist = partmc.histogram_1d(dry_diameters, x_axis, weights=1 / particles.comp_vols) hist_array[:, counter] = hist counter = counter + 1 plt.clf() for i_loop in range(0, config.i_loop_max): plt.loglog(x_axis.centers(), hist_array[:, i_loop], 'k') plt.errorbar(x_axis.centers(), np.average(hist_array, axis=1), np.std(hist_array, axis=1)) avg = np.average(hist_array, axis=1) std = np.std(hist_array, axis=1) error = std / avg print 'avg and std ', avg, std, error plt.axis([1e-8, 1e-5, 1e4, 1e11]) plt.xlabel("dry diameter (m)") plt.ylabel("number density (m^{-3})") plt.title(title) fig = plt.gcf() fig.savefig(out_filename)
def process_data(in_filename_list, out_filename): total_value = None for in_filename in in_filename_list: ncf = scipy.io.netcdf.netcdf_file(in_filename, 'r') particles = partmc.aero_particle_array_t(ncf) env_state = partmc.env_state_t(ncf) ncf.close() dry_diameters = particles.dry_diameters() * 1e6 # m to um masses = particles.masses() * 1e9 # kg to ug value = partmc.histogram_1d(dry_diameters, x_axis, weights=masses / particles.comp_vols) value /= 1e6 # m^{-3} to cm^{-3} if total_value is None: total_value = value else: total_value += value total_value /= len(in_filename_list) np.savetxt(out_filename, total_value) mask = np.ma.make_mask(total_value <= 0.0) masked_total_value = np.ma.array(total_value, mask=mask) return (masked_total_value.min(), masked_total_value.max())
def make_plot(in_dir, in_filename1, in_filename2, in_filename3, out_filename, title, ccn_cn_i, ccn_cn_j): print in_filename1, in_filename2, in_filename3 ncf = scipy.io.netcdf.netcdf_file(in_dir + in_filename1, 'r') particles1 = partmc.aero_particle_array_t(ncf) ncf.close() ncf = scipy.io.netcdf.netcdf_file(in_dir + in_filename2, 'r') particles2 = partmc.aero_particle_array_t(ncf) ncf.close() ncf = scipy.io.netcdf.netcdf_file(in_dir + in_filename3, 'r') particles3 = partmc.aero_particle_array_t(ncf) ncf.close() x_axis = partmc.log_grid(min=1e-10, max=1e-4, n_bin=30) x_centers = x_axis.centers() wet_diameters1 = particles1.diameters() wet_diameters2 = particles2.diameters() wet_diameters3 = particles3.diameters() hist1 = partmc.histogram_1d(wet_diameters1, x_axis, weights=1 / particles1.comp_vols) hist2 = partmc.histogram_1d(wet_diameters2, x_axis, weights=1 / particles2.comp_vols) hist3 = partmc.histogram_1d(wet_diameters3, x_axis, weights=1 / particles3.comp_vols) is_activated = (wet_diameters3 > 2e-6) sum_tot = sum(1 / particles3.comp_vols) * 1e-6 num_act = sum(1 / particles3.comp_vols[is_activated]) * 1e-6 print title, num_act, sum_tot, num_act / sum_tot * 100 ccn_cn_ratio[ccn_cn_i, ccn_cn_j] = num_act / sum_tot plt.clf() plt.semilogx(x_axis.centers(), hist1, label='0 min') plt.semilogx(x_axis.centers(), hist2, label='2 mins') plt.semilogx(x_axis.centers(), hist3, label='10 mins') plt.legend(loc='upper left') plt.xlabel("wet diameter (m)") plt.ylabel("number density (m^{-3})") plt.title(title) fig = plt.gcf() fig.savefig(out_filename)
def make_plot(in_dir, in_filename1, in_filename2, in_filename3, out_filename): print in_filename1, in_filename2, in_filename3 ncf = scipy.io.netcdf.netcdf_file(in_dir + in_filename1, 'r') particles1 = partmc.aero_particle_array_t(ncf) ncf.close() ncf = scipy.io.netcdf.netcdf_file(in_dir + in_filename2, 'r') particles2 = partmc.aero_particle_array_t(ncf) ncf.close() ncf = scipy.io.netcdf.netcdf_file(in_dir + in_filename3, 'r') particles3 = partmc.aero_particle_array_t(ncf) ncf.close() x_axis = partmc.log_grid(min=1e-10, max=1e-4, n_bin=50) x_centers = x_axis.centers() dry_diameters1 = particles1.dry_diameters() dry_diameters2 = particles2.dry_diameters() dry_diameters3 = particles3.dry_diameters() hist1 = partmc.histogram_1d(dry_diameters1, x_axis, weights=particles1.masses(exclude=["H2O"]) / particles1.comp_vols) hist2 = partmc.histogram_1d(dry_diameters2, x_axis, weights=particles2.masses(exclude=["H2O"]) / particles2.comp_vols) hist3 = partmc.histogram_1d(dry_diameters3, x_axis, weights=particles3.masses(exclude=["H2O"]) / particles3.comp_vols) plt.clf() plt.loglog(x_axis.centers(), hist1, label='initial') plt.loglog(x_axis.centers(), hist2, label='6 hours') plt.loglog(x_axis.centers(), hist3, label='12 hours') plt.legend(loc='center right') plt.axis([5e-9, 1e-4, 1e-14, 1e-7]) plt.grid(True) plt.xlabel("dry diameter (m)") plt.ylabel(r"mass concentration ($\rm kg \, m^{-3}$)") fig = plt.gcf() fig.savefig(out_filename)
def make_plot(in_files, f1, f2, f3, f4, f5): x_axis = partmc.log_grid(min=1e-10, max=1e-4, n_bin=100) x_centers = x_axis.centers() counter = 0 hist_array_num = np.zeros([len(x_centers), config.i_loop_max]) hist_array_mass = np.zeros([len(x_centers), config.i_loop_max]) for file in in_files: ncf = scipy.io.netcdf.netcdf_file(config.netcdf_dir + '/' + file, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() dry_diameters = particles.dry_diameters() hist = partmc.histogram_1d(dry_diameters, x_axis, weights=1 / particles.comp_vols) hist_array_num[:, counter] = hist hist = partmc.histogram_1d(dry_diameters, x_axis, weights=particles.masses(exclude=["H2O"]) / particles.comp_vols) hist_array_mass[:, counter] = hist counter = counter + 1 hist_array_gav_num = np.exp(np.average(np.log(hist_array_num), axis=1)) hist_array_gstd_num = np.exp(np.std(np.log(hist_array_num), axis=1)) e_bar_top_num = hist_array_gav_num * hist_array_gstd_num e_bar_bottom_num = hist_array_gav_num / hist_array_gstd_num e_bars_num = np.vstack((hist_array_gav_num - e_bar_bottom_num, e_bar_top_num - hist_array_gav_num)) hist_array_gav_mass = np.exp(np.average(np.log(hist_array_mass), axis=1)) hist_array_gstd_mass = np.exp(np.std(np.log(hist_array_mass), axis=1)) e_bar_top_mass = hist_array_gav_mass * hist_array_gstd_mass e_bar_bottom_mass = hist_array_gav_mass / hist_array_gstd_mass e_bars_mass = np.vstack((hist_array_gav_mass - e_bar_bottom_mass, e_bar_top_mass - hist_array_gav_mass)) np.savetxt(f1, x_axis.centers()) np.savetxt(f2, hist_array_gav_num) np.savetxt(f3, e_bars_num) np.savetxt(f4, hist_array_gav_mass) np.savetxt(f5, e_bars_mass)
def make_plot(in_dir, in_filename, out_filename, out_data_name): print in_filename ncf = scipy.io.netcdf.netcdf_file(in_dir+in_filename, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() x_axis = partmc.log_grid(min=1e-9,max=1e-5,n_bin=100) x_centers = x_axis.centers() diameters = particles.diameters() pure_bc = ((particles.masses(include = ["BC"]) > 0) & (particles.masses(include = ["SO4"]) == 0)) pure_so4 = ((particles.masses(include = ["SO4"]) > 0) & (particles.masses(include = ["BC"]) == 0)) with_bc = (particles.masses(include = ["BC"]) > 0) with_so4 = (particles.masses(include = ["SO4"]) > 0) mixed_bc_so4 = ((particles.masses(include = ["SO4"]) > 0) & (particles.masses(include = ["BC"]) > 0)) hist = partmc.histogram_1d(diameters, x_axis, weights = 1 / particles.comp_vols) / 1e6 hist_bc = partmc.histogram_1d(diameters[pure_bc], x_axis, weights = 1 / particles.comp_vols[pure_bc]) /1e6 hist_so4 = partmc.histogram_1d(diameters[pure_so4], x_axis, weights = 1 / particles.comp_vols[pure_so4]) /1e6 hist_mixed = partmc.histogram_1d(diameters[mixed_bc_so4], x_axis, weights = 1 / particles.comp_vols[mixed_bc_so4]) / 1e6 plt.clf() plt.loglog(x_centers*1e6, hist, 'r-', label = 'total') plt.loglog(x_centers*1e6, hist_bc, 'k-', label = 'pure bc') plt.loglog(x_centers*1e6, hist_so4, 'b-', label = 'pure so4') plt.loglog(x_centers*1e6, hist_mixed, 'g-', label = 'mixed so4 and bc') plt.axis([1e-3, 2e-0, 1e-1, 1e4]) plt.xlabel("dry diameter / micrometer") plt.ylabel("number density / cm^{-3}") plt.legend(loc = "upper left") plt.grid(True) fig = plt.gcf() fig.savefig(out_filename) np.savetxt("diameter_values.txt", x_centers*1e6) np.savetxt(out_data_name+"_total_acc_bc1.txt", hist) np.savetxt(out_data_name+"_bc_acc_bc1.txt", hist_bc) np.savetxt(out_data_name+"_so4_acc_bc1.txt", hist_so4) np.savetxt(out_data_name+"_mixed_acc_bc1.txt", hist_mixed)
def get_plot_data_bc(filename, value_min=None, value_max=None): ncf = scipy.io.netcdf.netcdf_file(filename, 'r') particles = partmc.aero_particle_array_t(ncf) env_state = partmc.env_state_t(ncf) ncf.close() diameters = particles.dry_diameters() * 1e6 x_axis = partmc.log_grid(min=diameter_axis_min, max=diameter_axis_max, n_bin=num_diameter_bins) value = partmc.histogram_1d(diameters, x_axis, weights=1 / particles.comp_vols) value /= 1e6 return (value, x_axis.centers())
def make_plot(in_dir, in_files, title, out_filename): x_axis = partmc.log_grid(min=1e-10,max=1e-4,n_bin=100) x_centers = x_axis.centers() counter = 0 hist_array = np.zeros([len(x_centers),config.i_loop_max]) for file in in_files: ncf = scipy.io.netcdf.netcdf_file(in_dir+file, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() dry_diameters = particles.dry_diameters() hist = partmc.histogram_1d(dry_diameters, x_axis, weights = 1 / particles.comp_vols) hist_array[:,counter] = hist counter = counter+1 # hist_array_av = np.average(hist_array,axis = 1) # hist_array_std = np.std(hist_array, axis = 1) # hist_array_std_clipped = np.minimum(hist_array_std, hist_array_av - 1) # e_bars = np.vstack((hist_array_std_clipped, hist_array_std)) hist_array_gav = np.exp(np.average(np.log(hist_array),axis = 1)) hist_array_gstd = np.exp(np.std(np.log(hist_array), axis = 1)) e_bar_top = hist_array_gav * hist_array_gstd e_bar_bottom = hist_array_gav / hist_array_gstd e_bars = np.vstack((hist_array_gav - e_bar_bottom, e_bar_top - hist_array_gav)) plt.clf() # for i_loop in range(0,config.i_loop_max): # plt.loglog(x_axis.centers(), hist_array[:,i_loop], 'k') a = plt.gca() # gets the axis a.set_xscale("log") # x axis log a.set_yscale("log") # y axis log plt.errorbar(x_axis.centers(), hist_array_gav, e_bars) plt.axis([5e-9, 5e-6, 1e4, 1e11]) plt.xlabel("dry diameter (m)") plt.ylabel("number density (m^{-3})") # plt.title(title) plt.grid(True) fig = plt.gcf() fig.savefig(out_filename)
def make_plot(in_dir, in_filename, out_filename): print in_filename ncf = scipy.io.netcdf.netcdf_file(in_dir + in_filename, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() x_axis = partmc.log_grid(min=1e-10, max=1e-4, n_bin=100) x_centers = x_axis.centers() dry_diameters = particles.dry_diameters() hist = partmc.histogram_1d(dry_diameters, x_axis, weights=1 / particles.comp_vols) plt.clf() plt.loglog(x_axis.centers(), hist) plt.axis([1e-10, 1e-4, 1e7, 1e15]) plt.xlabel("dry diameter (m)") plt.ylabel("number density (m^{-3})") fig = plt.gcf() fig.savefig(out_filename)
def make_plot(in_filename, out_filename, title): ncf = scipy.io.netcdf.netcdf_file(in_filename, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() bc_volume = particles.volumes(include=["BC"]) bc = particles.masses(include=["BC"]) dry_mass = particles.masses(exclude=["H2O"]) bc_frac = bc / dry_mass coat_frac = 1 - bc / dry_mass is_bc = (bc_frac > 0.05) dry_diameters = particles.dry_diameters() core_diameters = (6 / math.pi * bc_volume)**(1. / 3.) coating_thickness = (dry_diameters - core_diameters) / 2. ratio = coating_thickness / dry_diameters print ratio.max() x_axis = partmc.linear_grid(min=0, max=ratio.max(), n_bin=50) hist1d = partmc.histogram_1d(coating_thickness[is_bc] / dry_diameters[is_bc], x_axis, weights=1 / particles.comp_vols[is_bc]) print hist1d plt.clf() a = plt.gca() a.set_xscale("linear") a.set_yscale("log") plt.plot(x_axis.centers(), hist1d) plt.axis([x_axis.min, x_axis.max, 1e8, 1e12]) plt.grid(True) plt.xlabel("coating thickness / dry diameter") plt.ylabel("number concentration (m^{-3})") plt.title(title) fig = plt.gcf() fig.savefig(out_filename)
netcdf_dir = "../../scenarios/4_nucleate/out/" netcdf_pattern = "urban_plume_wc_0001_(.*).nc" time_filename_list = partmc.get_time_filename_list(netcdf_dir, netcdf_pattern) dist_array = np.zeros([len(time_filename_list), 100]) times = np.zeros([len(time_filename_list)]) i_counter = 0 diam_axis = partmc.log_grid(min=1e-10, max=1e-6, n_bin=100) diam_axis_edges = diam_axis.edges() for [time, filename, key] in time_filename_list: print time, filename, key ncf = scipy.io.netcdf.netcdf_file(filename, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() dry_diameters = particles.dry_diameters() hist = partmc.histogram_1d(dry_diameters, diam_axis, weights=particles.masses(include=["SO4"]) / particles.comp_vols) dist_array[i_counter, :] = hist times[i_counter] = time i_counter += 1 np.savetxt("data/banana_so4_dist.txt", dist_array) np.savetxt("data/banana_diam.txt", diam_axis_edges) np.savetxt("data/banana_times.txt", times)
def make_plot(in_filename,out_filename,title): ncf = scipy.io.netcdf.netcdf_file(in_filename, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() so4 = particles.masses(include = ["SO4"])/particles.aero_data.molec_weights[0] nh4 = particles.masses(include = ["NH4"])/particles.aero_data.molec_weights[3] no3 = particles.masses(include = ["NO3"])/particles.aero_data.molec_weights[1] bc = particles.masses(include = ["BC"])/particles.aero_data.molec_weights[18] oc = particles.masses(include = ["OC"])/particles.aero_data.molec_weights[17] print 'min nh4 ', min(particles.masses(include = ["NH4"])), max(nh4), min(no3), max(no3) ion_ratio = (2*so4 + no3) / nh4 is_neutral = (ion_ratio < 2) dry_diameters = particles.dry_diameters() x_axis = partmc.log_grid(min=1e-8,max=1e-6,n_bin=70) y_axis = partmc.linear_grid(min=0,max=30.0,n_bin=100) x_centers = x_axis.centers() bin_so4 = partmc.histogram_1d(dry_diameters, x_axis, weights = so4) bin_nh4 = partmc.histogram_1d(dry_diameters, x_axis, weights = nh4) bin_no3 = partmc.histogram_1d(dry_diameters, x_axis, weights = no3) print 'bin_so4 ', bin_so4[40] print 'bin_nh4 ', bin_nh4[40] print 'bin_no3 ', bin_no3[40] bin_ratio = (2*bin_so4 + bin_no3)/ bin_nh4 np.isnan(bin_ratio) # checks which elements in c are NaN (produces array with True and False) bin_ratio[np.isnan(bin_ratio)] = 0 # replaces NaN with 0. useful for plotting print 'bin_ratio ', bin_ratio[40] diameter_bins = x_axis.find(dry_diameters) print 'diameter_bins ', diameter_bins is_40 = (diameter_bins == 40) # for i in range(len(dry_diameters)): # if diameter_bins[i] == 40: # print 'particle info', so4[i], nh4[i], no3[i], ion_ratio[i] so4_40 = so4[is_40] nh4_40 = nh4[is_40] no3_40 = no3[is_40] bc_40 = bc[is_40] oc_40 = oc[is_40] ion_ratio_40 = ion_ratio[is_40] # data = [(so4_40[i],nh4_40[i], no3_40[i], ion_ratio_40[i]) for i in range(len(so4_40) data = zip(so4_40, nh4_40, no3_40, bc_40, oc_40, ion_ratio_40) data.sort(key = lambda x: x[5]) for (so,nh,no,bc,oc,ir) in data: print so,nh,no,bc,oc,ir print 'sums ', sum(so4[is_40]), sum(nh4[is_40]), sum(no3[is_40]), (2*sum(so4[is_40])+ sum(no3[is_40])) / sum(nh4[is_40]) print 'sums/number ', sum(so4[is_40])/len(so4_40), sum(nh4[is_40])/len(nh4_40), sum(no3[is_40])/len(no3_40) hist2d = partmc.histogram_2d(dry_diameters, ion_ratio, x_axis, y_axis, weights = 1/particles.comp_vols) plt.clf() plt.pcolor(x_axis.edges(), y_axis.edges(), hist2d.transpose(),norm = matplotlib.colors.LogNorm(), linewidths = 0.1) a = plt.gca() plt.semilogx(x_centers, bin_ratio, 'w-', linewidth = 3) plt.semilogx(x_centers, bin_ratio, 'k-', linewidth = 1) a.set_xscale("log") a.set_yscale("linear") plt.axis([x_axis.min, x_axis.max, y_axis.min, y_axis.max]) plt.xlabel("dry diameter (m)") plt.ylabel("ion ratio") cbar = plt.colorbar() cbar.set_label("number density (m^{-3})") plt.title(title) fig = plt.gcf() fig.savefig(out_filename)
import partmc netcdf_dir = "../../scenarios/4_nucleate/out_wei-0_lowbg2/" netcdf_pattern = "nucleate_wc_0001_(.*).nc" time_filename_list = partmc.get_time_filename_list(netcdf_dir, netcdf_pattern) size_dist_array = np.zeros([len(time_filename_list), 100]) times = np.zeros([len(time_filename_list)]) i_counter = 0 diam_axis = partmc.log_grid(min=1e-4, max=1e0, n_bin=100) diam_axis_edges = diam_axis.edges() for [time, filename, key] in time_filename_list: print time, filename, key ncf = scipy.io.netcdf.netcdf_file(filename, 'r') particles = partmc.aero_particle_array_t(ncf) ncf.close() dry_diameters = particles.dry_diameters() * 1e6 # in micrometers hist = partmc.histogram_1d(dry_diameters, diam_axis, weights=1 / particles.comp_vols) size_dist_array[i_counter, :] = hist / 1e6 # in cm^{-3} times[i_counter] = time i_counter += 1 np.savetxt("data/banana_size_dist_wc_wei-0_lowbg2.txt", size_dist_array) np.savetxt("data/banana_diam_wc_wei-0_lowbg2.txt", diam_axis_edges) np.savetxt("data/banana_times_wc_wei-0_lowbg2.txt", times)
for i_loop in range (0, i_loop_max): netcdf_pattern = "brownian_part_0%03d_(.*).nc" % (i_loop+1) print netcdf_pattern time_filename_list = partmc.get_time_filename_list(config.netcdf_dir, netcdf_pattern) i_counter = 0 for [time, filename, key] in time_filename_list: print time, filename, key ncf = scipy.io.netcdf.netcdf_file(filename, 'r') particles = partmc.aero_particle_array_t(ncf) env_state = partmc.env_state_t(ncf) ncf.close() wet_diameters = particles.diameters() hist = partmc.histogram_1d(wet_diameters, x_axis, weights = 1 / particles.comp_vols) # total_number = sum(1/particles.comp_vols) # total_mass = sum(particles.masses()/particles.comp_vols) time_array[i_counter]= time / 3600. array_num[i_counter,i_loop,:]= hist hist = partmc.histogram_1d(wet_diameters, x_axis, weights = particles.masses() / particles.comp_vols) array_mass[i_counter,i_loop,:]= hist i_counter += 1 print "array_num ", array_num.shape for i_ensemble in range(0, i_loop_max): print "i_ensemble ", i_ensemble num_avg[:,i_ensemble,:] = np.average(array_num[:,:i_ensemble+1,:], axis = 1)