def render_slp_component_element_values(): with open(FILE_NAME_COMPS, 'r') as f: d = cPickle.load(f) mn = d['mean'] cid = np.amax(np.abs(mn), axis=1)[:, np.newaxis] gf = GeoField() gf.load('data/pres.mon.mean.nc', 'pres') gf.slice_spatial(None, [20, 89]) mnd = gf.reshape_flat_field(cid) f = render_component_single(mnd[0, :, :], gf.lats, gf.lons, (0, 0.15), None, 'NH Extratropical Components - max values') f.savefig('figs/slp_nh_component_maxima_sameaxis.pdf') f = render_component_single(mnd[0, :, :], gf.lats, gf.lons, None, None, 'NH Extratropical Components - max values') f.savefig('figs/slp_nh_component_maxima.pdf') f = plt.figure() plt.hist(cid, bins=40) plt.title('Histogram of max values @ grid points across components') plt.xlabel('Maximum value [-]') plt.ylabel('Frequency [-]') f.savefig('figs/slp_nh_compmax_hist.pdf') return f
def render_slp_component_element_values(): with open(FILE_NAME_COMPS, 'r') as f: d = cPickle.load(f) mn = d['mean'] cid = np.amax(np.abs(mn), axis = 1)[:, np.newaxis] gf = GeoField() gf.load('data/pres.mon.mean.nc', 'pres') gf.slice_spatial(None, [20, 89]) mnd = gf.reshape_flat_field(cid) f = render_component_single(mnd[0, :, :], gf.lats, gf.lons, (0, 0.15), None, 'NH Extratropical Components - max values') f.savefig('figs/slp_nh_component_maxima_sameaxis.pdf') f = render_component_single(mnd[0, :, :], gf.lats, gf.lons, None, None, 'NH Extratropical Components - max values') f.savefig('figs/slp_nh_component_maxima.pdf') f = plt.figure() plt.hist(cid, bins = 40) plt.title('Histogram of max values @ grid points across components') plt.xlabel('Maximum value [-]') plt.ylabel('Frequency [-]') f.savefig('figs/slp_nh_compmax_hist.pdf') return f
def plot_components_slp(): gf = GeoField() gf.load('data/pres.mon.mean.nc', 'pres') gf.slice_spatial(None, [20, 89]) with open('results/slp_nh_var_bootstrap_results_b1000_cosweights.bin', 'r') as f: d = cPickle.load(f) mn = d['mean'] mn_gf = gf.reshape_flat_field(mn) t_start = datetime.now() thr = 1.0 / mn.shape[0]**0.5 pool = Pool(4) render_list_triples = [ ([mn_gf[i, ...], mn_gf[i, ...] * (np.abs(mn_gf[i,...]) > thr)], [ 'Mean', 'Mean:Thr'], gf.lats, gf.lons, False, # 'figs/nhemi_comp%02d_varimax_mn.png' % (i+1), 'figs/nhemi_comp%02d_varimax_mn.pdf' % (i+1), 'Component %d' % (i+1)) for i in range(mn.shape[1])] # use less nodes to render the maps due to memory constraints pool.map(render_set_stub, render_list_triples) pool.close() # clean up memory del render_list_triples print("DONE after [%s]." % (datetime.now() - t_start))
def render_slp_components(): with open('results/slp_nh_var_bootstrap_results_b1000.bin', 'r') as f: d = cPickle.load(f) mn = d['mean'] mn_mask = (np.abs(mn) > 1.0 / mn.shape[0]**0.5) mn_thr = mn * mn_mask print np.sum(np.sum(mn_mask, axis= 1) == 0) cid = np.argmax(np.abs(mn) * mn_mask, axis = 1)[:, np.newaxis] + 1 gf = GeoField() gf.load('data/pres.mon.mean.nc', 'pres') gf.slice_spatial(None, [20, 89]) mnd = gf.reshape_flat_field(cid) # plt.figure() # plt.hist(cid, bins = 43) # plt.show() f = render_component_single(mnd[0, :, :], gf.lats, gf.lons, False, None, 'NH Extratropical Components', cmap = plt.get_cmap('gist_ncar')) plt.show()
def render_slp_component_element_values(): with open('results/slp_nh_var_bootstrap_results_b1000_cosweights.bin', 'r') as f: d = cPickle.load(f) mn = d['mean'] cid = np.amax(np.abs(mn), axis = 1)[:, np.newaxis] gf = GeoField() gf.load('data/pres.mon.mean.nc', 'pres') gf.slice_spatial(None, [20, 89]) mnd = gf.reshape_flat_field(cid) f = render_component_single(mnd[0, :, :], gf.lats, gf.lons, False, None, 'NH Extratropical Components - max values') plt.show()
db = t2 - t1 # print progress print( "PROGRESS [%s]: %d/%d complete (sorting %g percent), predicted completion at %s." % (str(t2), bsmp_done, LNO_COUNT, int((da.seconds + da.microseconds / 10e6) / (db.seconds + db.microseconds / 10e6) * 100), str(t2 + dt))) var_comp /= (bsmp_done - 1) print("DONE at %s after %s" % (str(datetime.now()), str(datetime.now() - t_start))) # reshape all the fields back into correct spatial dimensions to match lon/lat of the original geofield max_comp = gf.reshape_flat_field(max_comp[BULK_STEP, :, :]) min_comp = gf.reshape_flat_field(min_comp[EXTREMA_MEMORY, :, :]) mean_comp = gf.reshape_flat_field(mean_comp) var_comp = gf.reshape_flat_field(var_comp) # Ud and Ur are reshaped into new variables as the old ones may be reused in a new computation Ud_gf = gf.reshape_flat_field(Ud) Ur_gf = gf.reshape_flat_field(Ur) # save the results to file with open('results/lno05_results.bin', 'w') as f: cPickle.dump( { 'Ud': Ud_gf, 'Ur': Ur_gf, 'max': max_comp,
if __name__ == '__main__': # load geo-field gf = GeoField() gf.load('data/pres.mon.mean.nc', 'pres') gf.transform_to_anomalies() gf.normalize_monthly_variance() gf.slice_spatial(None, [20, 89]) gf.slice_date_range(date(1950, 1, 1), date(2012, 3, 1)) # load the components with open('results/slp_nh_var_bootstrap_results_b1000_cosweights.bin', 'r') as f: d = cPickle.load(f) # convert to unit vectors mn = d['mean'] mn = mn / np.sum(mn**2, axis = 0) ** 0.5 # mark maxima mx_pos = np.argmax(mn**2, axis = 0) print mx_pos mx_loc = np.zeros_like(mn) mx_loc[(mx_pos, np.arange(mn.shape[1]))] = 1.0 mx_loc_gf = gf.reshape_flat_field(mx_loc) sio.savemat("results/component_maxima_locations.mat", { 'mx_loc' : mx_loc_gf })
da = t2 - t1a db = t2 - t1 # print progress print("PROGRESS [%s]: %d/%d complete (sorting %g percent), predicted completion at %s." % (str(t2), bsmp_done, LNO_COUNT, int((da.seconds + da.microseconds/10e6) / (db.seconds + db.microseconds/10e6) * 100), str(t2 + dt))) var_comp /= (bsmp_done - 1) print("DONE at %s after %s" % (str(datetime.now()), str(datetime.now() - t_start))) # reshape all the fields back into correct spatial dimensions to match lon/lat of the original geofield max_comp = gf.reshape_flat_field(max_comp[BULK_STEP, :, :]) min_comp = gf.reshape_flat_field(min_comp[EXTREMA_MEMORY, :, :]) mean_comp = gf.reshape_flat_field(mean_comp) var_comp = gf.reshape_flat_field(var_comp) # Ud and Ur are reshaped into new variables as the old ones may be reused in a new computation Ud_gf = gf.reshape_flat_field(Ud) Ur_gf = gf.reshape_flat_field(Ur) # save the results to file with open('results/lno05_results.bin', 'w') as f: cPickle.dump({ 'Ud' : Ud_gf, 'Ur' : Ur_gf, 'max' : max_comp, 'min' : min_comp, 'mean' : mean_comp, 'var' : var_comp}, f) t_start = datetime.now()