def get_swath_coord(iC): swath_x, swath_y = get_cs_coords(x_cs[iC], y_cs[iC], orientation_in + 90., X, Y, h = res, isPeriodic = True, max_r = swath_width/2.) return swath_x, swath_y
y_c = 4*R_i # y-coordinate for the island centre # Create the coordinate system for this data <<<<<<< HEAD dx = 1600.0 X, Y = np.meshgrid(np.arange(74)*dx, np.arange(20)*dx) # Get coordinates of the cross section along the swath h = dx*1. # Resolution in the along-swath direction ======= X, Y = np.meshgrid(np.arange(0., 116000., 100.), np.arange(0., 31900., 100.)) # Get coordinates of the cross section along the swath h = 100. # Resolution in the along-swath direction >>>>>>> 62279a4ff074ba2a906de6d583e07e7c7ce0c696 x_cs, y_cs = get_cs_coords(x_c, y_c, orientation_in, X, Y, h = h) # Truncate to one domain - i.e. remove points that exit a boundary removal = [] for i in xrange(len(x_cs)): if (np.min(X) > x_cs[i]) or (np.max(X) < x_cs[i]) or (np.min(Y) > y_cs[i]) or (np.max(Y) < y_cs[i]): removal.append(i) x_cs = np.delete(x_cs, removal) y_cs = np.delete(y_cs, removal) R_i /= 1000. # Convert the island radius to km # Calculate the distance from the island centre in the along swath direction R = -np.array([np.round(x, 0) for x in np.sign(x_cs - x_c)*np.sqrt((x_cs - x_c)**2 + (y_cs - y_c)**2)]) - R_i*1000.
def get_chunk_coord(iC): print 'get_chunk_coord(' + str(iC) + ')' iR = np.where(np.min(np.abs(R - chunk_Rs[chunk_keys[-1]][iC])) == np.abs(R - chunk_Rs[chunk_keys[-1]][iC]))[0][0] chunks_x, chunks_y = get_cs_coords(x_cs[iR], y_cs[iR], wind_dir_0 + 90., X, Y, h = res, isPeriodic = True, max_r = chunk_width/2.) return chunks_x, chunks_y
lsm = landseamask.variables['lsm'][0, 0, :, :] * 1. y = landseamask.variables['latitude'][:] * 1. x = landseamask.variables['longitude'][:] * 1. # create 2D coordinate mesh X, Y = np.meshgrid(x, y) landseamask.close() # We know from our domain definition where the centre of the island should be R_i = 1000.0 * (50.0 / np.pi)**0.5 # island radius x_c = 100000.0 + R_i y_c = 4 * R_i x_cs, y_cs = get_cs_coords(x_c, y_c, wind_dir_0, X, Y, h=100.) hours = ["{0:02d}".format(h) for h in xrange(0, 24, 3)] # use dictionaries to hold the stash codes for the variables var_keys = { 'theta': u'STASH_m01s00i004', 'qv': u'STASH_m01s00i010', 'w': u'STASH_m01s00i150' } var_cmaps = {'theta': 'hot', 'qv': 'BrBG', 'w': 'bwr'} var_factor = {'theta': 1., 'qv': 1000., 'w': 1.} var_units = {'theta': '(K)', 'qv': r'(g kg$^{-1}$)', 'w': r'(m s^{-1}$)'} # use dictionary to hold the hovmollers hovmollers = {} for key in var_keys.keys(): hovmollers[key] = np.zeros((18 * len(hours), len(x_cs)))
def get_cs(it): # only need data to the height just above 3000 m iz3000 = np.where(np.abs(z - 5000) == np.min(np.abs(z - 5000)))[0][0] + 1 # leave the rest of the levels = 0 my_kind = 2 d = 2000. print('[' + dt.now().strftime("%H:%M:%S") + '] Interpolating to the cross section coordinates...') START = dt.now() print('[' + dt.now().strftime("%H:%M:%S") + '] Starting interpolation...') start = dt.now() theta_cs = bilinear_interpolation( X, Y, bouy_nc.variables[theta_key][it, :iz3000, :, :] * 1., x_cs, y_cs, kind=my_kind, d=d) end = dt.now() print('Finished interpolating Theta, elapsed time: ' + str(end - start)) start = dt.now() q_cs = bilinear_interpolation(X, Y, bouy_nc.variables[q_key][it, :iz3000, :, :] * 1., x_cs, y_cs, kind=my_kind, d=d) end = dt.now() print('Finished interpolating q, elapsed time: ' + str(end - start)) start = dt.now() w_cs = bilinear_interpolation(X, Y, bouy_nc.variables[w_key][it, :iz3000, :, :] * 1., x_cs, y_cs, kind=my_kind, d=d) end = dt.now() print('Finished interpolating w, elapsed time: ' + str(end - start)) start = dt.now() zi_cs = bilinear_interpolation(X, Y, zi_nc.variables[zi_key][:], x_cs, y_cs, kind=my_kind, d=d) end = dt.now() print('Finished interpolating zi, elapsed time: ' + str(end - start)) start = dt.now() lcl_cs = bilinear_interpolation(X, Y, zi_nc.variables[lcl_key][:], x_cs, y_cs, kind=my_kind, d=d) end = dt.now() print('Finished interpolating lcl, elapsed time: ' + str(end - start)) END = dt.now() print('[' + dt.now().strftime("%H:%M:%S") + '] Completed interpolation in ' + str(END - START) + "...") #================================= Section 4 ==================================# # # # Plot the cross section and anomalies # # # #==============================================================================# #print('Starting section 4') # Translate the cross section coordinates into the distance downwind of the island R = np.sign(x_c - x_cs) * np.sqrt((x_cs - x_c)**2 + (y_cs - y_c)**2) # Calculate the anomaly from the horizontal mean theta_cs_anom = np.zeros_like(theta_cs) q_cs_anom = np.zeros_like(q_cs) theta_hm = np.nanmean(bouy_nc.variables[theta_key][it, :iz3000, :, :], axis=(1, 2)) q_hm = np.nanmean(bouy_nc.variables[q_key][it, :iz3000, :, :], axis=(1, 2)) for i in xrange(theta_cs.shape[1]): theta_cs_anom[:, i] = theta_cs[:, i] - theta_hm q_cs_anom[:, i] = q_cs[:, i] - q_hm # Plotting params text_pos_x = -R_i / 1000. + R_i * 0.1 / 1000. text_pos_y = 100. fig_width = 10 # inches fig = plt.figure() fig.set_size_inches(fig_width, fig_width / 1.5) ax = plt.subplot(311) plt.contourf(R / 1000., z[:iz3000] / 1000., theta_cs_anom, cmap='bwr', levels=[l for l in np.linspace(-1., 1., 11) if l != 0.], extend='both') plt.colorbar(label=r'$\theta$ Anomaly (K)') plt.title('Potential Temperature Anomaly (K) Cross Section') plt.ylim([0, 5]) r_i = np.array([-R_i, R_i]) / 1000. plt.plot(r_i, [0, 0], 'k', lw=5) plt.text(text_pos_x, text_pos_y, 'Island') plt.plot(R / 1000., zi_cs[it, :] / 1000., 'k', lw=2) plt.plot(R / 1000., lcl_cs[it, :] / 1000., 'grey', lw=2) plt.ylabel('Height (km)') plt.setp(ax.get_xticklabels(), visible=False) ax1 = plt.subplot(312, sharex=ax) plt.contourf(R / 1000., z[:iz3000] / 1000., q_cs_anom * 1000., cmap='BrBG', levels=[l for l in np.linspace(-2., 2., 11) if l != 0.], extend='both') plt.colorbar(label=r'q Anomaly (g kg$^{-1}$)') plt.title('Specific Humidity Anomaly (g kg$^{-1}$) Cross Section') plt.ylim([0, 5]) plt.plot(r_i, [0, 0], 'k', lw=5) plt.text(text_pos_x, text_pos_y, 'Island') plt.plot(R / 1000., zi_cs[it, :] / 1000., 'k', lw=2) plt.plot(R / 1000., lcl_cs[it, :] / 1000., 'grey', lw=2) plt.ylabel('Height (km)') plt.setp(ax1.get_xticklabels(), visible=False) plt.subplot(313, sharex=ax) plt.contourf(R / 1000., z[:iz3000] / 1000., w_cs, cmap='bwr', levels=[l for l in np.linspace(-2.5, 2.5, 11) if l != 0.], extend='both') plt.colorbar(label=r'w (m s$^{-1}$)') plt.title('Vertical Velocity (m s$^{-1}$) Cross Section') plt.ylim([0, 5]) plt.plot(r_i, [0, 0], 'k', lw=5) plt.text(text_pos_x, text_pos_y, 'Island') plt.plot(R / 1000., zi_cs[it, :] / 1000., 'k', lw=2) plt.plot(R / 1000., lcl_cs[it, :] / 1000., 'grey', lw=2) plt.ylabel('Height (km)') plt.xlabel('Distance from Island mid-point (km)') plt.suptitle('Along-wind Cross Section, at T+' + str(int(times[it])) + 'mins', fontsize=15) plt.tight_layout() fig.subplots_adjust(top=0.88) plt.savefig('../AlongWind_interpolated_T' + "{0:04d}".format(int(times[it])) + '.png', dpi=100) plt.close('all') #plt.show() #================================= Section 5 ==================================# # # # Calculate cross sections perpendicular to the flow and plot them # # # #==============================================================================# # Choose distance from island mid-point to do the cross section # e.g. 20 km r_targets = [10000.0, 20000.0, 40000.0, 60000.0] for r_target in r_targets: # Find where the distance to the island is nearest to the target distance R_pos = np.where((R > 0), R, np.nan) iR = np.where( np.abs(R_pos - r_target) == np.nanmin(np.abs(R_pos - r_target)))[0][0] x_cs2, y_cs2 = get_cs_coords(x_cs[iR], y_cs[iR], wind_dir_0 + 90., X, Y, h=100.0) ### Testing ### if testing: fig = plt.figure() my_x = 12. fig.set_size_inches(my_x, my_x * 32. / 116.) plt.contourf(X / 1000., Y / 1000., lwp_data, levels=[0, 1e-8, 1], colors=['k', 'w']) plt.contourf(X / 1000., Y / 1000., lsm, levels=[0, 1e-16, 1], colors=['w', 'b'], alpha=0.5) plt.plot(x_cs / 1000., y_cs / 1000., 'r', lw=3) plt.plot(x_cs2 / 1000., y_cs2 / 1000., 'b', lw=3) plt.ylabel('y (km)') plt.xlabel('x (km)') plt.text(x_cs[0], y_cs[0], 'A', fontsize=20) plt.text(x_cs[-1], y_cs[-1], 'B', fontsize=20) plt.text(x_cs2[0], y_cs2[0], 'C', fontsize=20) plt.text(x_cs2[-1], y_cs2[-1], 'D', fontsize=20) plt.title('Cross Section Path') plt.tight_layout() plt.savefig('../Cross_Section_location2.png', dpi=100) plt.show() f = open("output.txt", "a") f.write('[' + dt.now().strftime("%H:%M:%S") + '] Starting interpolation...\n') f.close() START = dt.now() # Interpolate to cs2 start = dt.now() theta_cs2 = bilinear_interpolation( X, Y, bouy_nc.variables[theta_key][it, :iz3000, :, :], x_cs2, y_cs2, kind=my_kind, d=d) end = dt.now() print('Finished interpolating Theta, elapsed time: ' + str(end - start)) start = dt.now() q_cs2 = bilinear_interpolation( X, Y, bouy_nc.variables[q_key][it, :iz3000, :, :], x_cs2, y_cs2, kind=my_kind, d=d) end = dt.now() print('Finished interpolating q, elapsed time: ' + str(end - start)) start = dt.now() w_cs2 = bilinear_interpolation( X, Y, bouy_nc.variables[w_key][it, :iz3000, :, :], x_cs2, y_cs2, kind=my_kind, d=d) end = dt.now() print('Finished interpolating w, elapsed time: ' + str(end - start)) END = dt.now() start = dt.now() zi_cs2 = bilinear_interpolation(X, Y, zi_nc.variables[zi_key][:], x_cs2, y_cs2, kind=my_kind, d=d) end = dt.now() print('Finished interpolating zi, elapsed time: ' + str(end - start)) END = dt.now() start = dt.now() lcl_cs2 = bilinear_interpolation(X, Y, zi_nc.variables[lcl_key][:], x_cs2, y_cs2, kind=my_kind, d=d) end = dt.now() print('Finished interpolating lcl, elapsed time: ' + str(end - start)) END = dt.now() f = open('output.txt', 'a') f.write('[' + dt.now().strftime("%H:%M:%S") + '] Completed interpolation in ' + str(END - START) + "...\n") f.close() # Translate the cross section coordinates into the distance away from the island downwind mid-point R2 = -np.sign(x_cs[iR] - x_cs2) * np.sqrt((x_cs2 - x_cs[iR])**2 + (y_cs2 - y_cs[iR])**2) # Calculate the anomaly from the horizontal mean theta_cs2_anom = np.zeros_like(theta_cs2) q_cs2_anom = np.zeros_like(q_cs2) for i in xrange(theta_cs2.shape[1]): theta_cs2_anom[:, i] = theta_cs2[:, i] - theta_hm q_cs2_anom[:, i] = q_cs2[:, i] - q_hm # Plotting params text_pos_x = -R_i / 1000. + R_i * 0.1 / 1000. text_pos_y = 100. fig = plt.figure() ax = plt.subplot(311) plt.contourf(R2 / 1000., z[:iz3000] / 1000., theta_cs2_anom, cmap='bwr', levels=[l for l in np.linspace(-1., 1., 11) if l != 0.], extend='both') plt.colorbar() plt.title('Potential Temperature Anomaly (K) Cross Section') plt.ylim([0, 5]) r_i = np.array([-R_i, R_i]) / 1000. plt.plot(r_i, [0, 0], 'k', lw=10) plt.text(text_pos_x, text_pos_y, 'Island') plt.plot(R2 / 1000., zi_cs2[it, :] / 1000., 'k', lw=2) plt.plot(R2 / 1000., lcl_cs2[it, :] / 1000., 'grey', lw=2) plt.ylabel('Height (m)') plt.setp(ax.get_xticklabels(), visible=False) ax1 = plt.subplot(312, sharex=ax) plt.contourf(R2 / 1000., z[:iz3000] / 1000., q_cs2_anom * 1000., cmap='BrBG', levels=[l for l in np.linspace(-2., 2., 11) if l != 0.], extend='both') plt.colorbar() plt.title('Specific Humidity Anomaly (g kg$^{-1}$) Cross Section') plt.ylim([0, 5]) plt.plot(r_i, [0, 0], 'k', lw=10) plt.text(text_pos_x, text_pos_y, 'Island') plt.plot(R2 / 1000., zi_cs2[it, :] / 1000., 'k', lw=2) plt.plot(R2 / 1000., lcl_cs2[it, :] / 1000., 'grey', lw=2) plt.ylabel('Height (m)') plt.setp(ax1.get_xticklabels(), visible=False) plt.subplot(313, sharex=ax) plt.contourf(R2 / 1000., z[:iz3000] / 1000., w_cs2, cmap='bwr', levels=[l for l in np.linspace(-2.5, 2.5, 11) if l != 0.], extend='both') plt.colorbar() plt.title('Vertical Velocity (m s$^{-1}$) Cross Section') plt.ylim([0, 5]) plt.plot(r_i, [0, 0], 'k', lw=10) plt.text(text_pos_x, text_pos_y, 'Island') plt.plot(R2 / 1000., zi_cs2[it, :] / 1000., 'k', lw=2) plt.plot(R2 / 1000., lcl_cs2[it, :] / 1000., 'grey', lw=2) plt.ylabel('Height (m)') plt.xlabel('Distance from Island mid-point (km)') plt.suptitle('Cross-wind Cross Section (' + str(int(r_target / 1000.)) + 'km downwind), at T+' + str(int(times[it])) + 'mins', fontsize=15) plt.tight_layout() fig.subplots_adjust(top=0.88) plt.savefig('../CrossWind_smoothed_' + str(int(r_target / 1000.)) + 'km_T' + "{0:04d}".format(int(times[it])) + '.png', dpi=100) plt.close('all')