1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| import numpy as np import netCDF4 as nc import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D import os, math import show_ssha_map as ssm from mpl_toolkits.basemap import Basemap
def find_max(data_matrix): new_data = [] for i in range(len(data_matrix)): new_data.append(max(data_matrix[i])) print("data_matrix最大值为", max(new_data))
def find_min(data_matrix): new_data = [] for i in range(len(data_matrix)): new_data.append(min(data_matrix[i])) print(min(new_data))
def variables_extact(file): lon_list = [] lat_list = [] ssha_list = [] ssha_w_list = [] for f in file: '''第一重数据集--*.nc Dataset''' dataset_nc = nc.Dataset(f) '''第二重数据集--groups Dataset''' data_01 = dataset_nc.groups['data_01'] lon = (data_01.variables['longitude'][:]) lat = (data_01.variables['latitude'][:]) '''第三重数据集--ku Dataset''' ku = data_01.groups['ku'] ssha = (ku.variables['ssha'][:]) data_lon = np.array(lon) data_lat = np.array(lat) data_ssha_all = np.array(ssha) data_ssha = np.where(data_ssha_all != 32767, data_ssha_all * 100, 0) data_ssha_w = data_ssha * 100 lon_list.extend(data_lon) lat_list.extend(data_lat) ssha_list.extend(data_ssha) ssha_w_list.extend(data_ssha_w) lon_all = np.array(lon_list) lat_all = np.array(lat_list) ssha_all = np.array(ssha_list) ssha_w_all = np.array(ssha_w_list) print(lon_all, lon_all.shape) print(lat_all, lat_all.shape) print(ssha_all, ssha_all.shape) return lon_all, lat_all, ssha_all, ssha_w_all
def show_ssha_3d(lon, lat, ssha, ssha_w): lon_lat_ssha = np.vstack((lon, lat, ssha_w)).T print(lon.shape, lat.shape, ssha_w.shape) print(lon_lat_ssha[:, 2], lon_lat_ssha.shape) ssha_mean_list = [] for a in range(360): for b in range(180): sum = 0 k_list = [] for i, j, k in lon_lat_ssha: if all((abs(i) > a, abs(i) <= a + 1, abs(j) > b, abs(j) <= b + 1)): sum += 1 k_list.extend([k]) mean_ssha = math.fsum(k_list) / sum print(sum, k_list, mean_ssha) ssha_mean_list.extend([mean_ssha]) print(ssha_mean_list)
map = Basemap(projection='cyl', llcrnrlat=-90., urcrnrlat=90., llcrnrlon=0., urcrnrlon=361., resolution='l', lat_0=0, lon_0=180) map.drawmapboundary() map.fillcontinents(color='gray', lake_color='aqua') map.drawstates() map.drawcoastlines() lons, lats = map.makegrid(1, 6598) lats = lats[::-1] x, y = map(lon, lat) map.drawparallels(np.arange(-90., 91., 30.), labels=[1, 0, 0, 0], fontsize=12) map.drawmeridians(np.arange(-180., 181., 60.), labels=[0, 0, 0, 1], fontsize=12) contour_map = map.contour(x, y, ssha_mean_list, 15, linewidths=1.5) plt.savefig(output_path + 'interpolote.png') plt.show(contour_map)
''' fig = plt.figure() # ax = Axes3D(fig) ax2 = plt.axes(projection='3d')
X = lon Y = lat X, Y = np.meshgrid(X, Y) print(len(X), len(Y), len(ssha)) Z = np.expand_dims(ssha, axis=1) ax2.plot_surface(X, Y, Z, alpha=0.3, cmap='rainbow') # ax2.contour(X, Y, Z, zdir='z', offset=-3, cmap="rainbow") # ax2.contourf(X, Y, Z, zdir='z', offset=-3, cmap="rainbow") plt.savefig(output_path + '_3d.png') plt.show() '''
if __name__ == '__main__': postfix = '.nc' input_path = '/Users/leo/Desktop/MarineTechTest5/Data_Jason3/' output_path = '/Users/leo/Desktop/MarineTechTest5/Results/'
if not os.path.exists(output_path): os.mkdir(output_path) file_list = os.listdir(input_path) day_list = [] for i in file_list: if i.endswith(postfix): file_nc = input_path + i day_list.append(file_nc) print(day_list) lon, lat, ssha, ssha_w = variables_extact(day_list) show_ssha_3d(lon, lat, ssha, ssha_w)
|