def setUp(self): self.d = Domain(4326, "-te 25 70 35 72 -ts 100 100") # define a test Nansat object test_domain = Domain(4326, "-lle -180 -90 180 90 -ts 500 500") self.n = NANSAT.from_domain(test_domain, array=np.ones([500, 500]))
def create_merged_swaths(self, ds, EPSG=4326, **kwargs): """Merge swaths, add dataseturi, and return Nansat object. EPSG options: - 4326: WGS 84 / longlat - 3995: WGS 84 / Arctic Polar Stereographic """ nn = {} nn[0] = Doppler( nansat_filename( ds.dataseturi_set.get(uri__endswith='%d.nc' % 0).uri)) lon0, lat0 = nn[0].get_geolocation_grids() nn[1] = Doppler( nansat_filename( ds.dataseturi_set.get(uri__endswith='%d.nc' % 1).uri)) lon1, lat1 = nn[1].get_geolocation_grids() nn[2] = Doppler( nansat_filename( ds.dataseturi_set.get(uri__endswith='%d.nc' % 2).uri)) lon2, lat2 = nn[2].get_geolocation_grids() nn[3] = Doppler( nansat_filename( ds.dataseturi_set.get(uri__endswith='%d.nc' % 3).uri)) lon3, lat3 = nn[3].get_geolocation_grids() nn[4] = Doppler( nansat_filename( ds.dataseturi_set.get(uri__endswith='%d.nc' % 4).uri)) lon4, lat4 = nn[4].get_geolocation_grids() connection.close() dlon = np.mean([ np.abs(np.mean(np.gradient(lon0, axis=1))), np.abs(np.mean(np.gradient(lon1, axis=1))), np.abs(np.mean(np.gradient(lon2, axis=1))), np.abs(np.mean(np.gradient(lon3, axis=1))), np.abs(np.mean(np.gradient(lon4, axis=1))) ]) nx = len( np.arange( np.array([ lon0.min(), lon1.min(), lon2.min(), lon3.min(), lon4.min() ]).min(), np.array([ lon0.max(), lon1.max(), lon2.max(), lon3.max(), lon4.max() ]).max(), dlon)) dlat = np.mean([ np.abs(np.mean(np.gradient(lat0, axis=0))), np.abs(np.mean(np.gradient(lat1, axis=0))), np.abs(np.mean(np.gradient(lat2, axis=0))), np.abs(np.mean(np.gradient(lat3, axis=0))), np.abs(np.mean(np.gradient(lat4, axis=0))) ]) ny = len( np.arange( np.array([ lat0.min(), lat1.min(), lat2.min(), lat3.min(), lat4.min() ]).min(), np.array([ lat0.max(), lat1.max(), lat2.max(), lat3.max(), lat4.max() ]).max(), dlat)) if ny is None: ny = np.array([ nn[0].shape()[0], nn[1].shape()[0], nn[2].shape()[0], nn[3].shape()[0], nn[4].shape()[0] ]).max() ## DETTE VIRKER IKKE.. #sensor_view = np.sort( # np.append(np.append(np.append(np.append( # nn[0]['sensor_view'][0,:], # nn[1]['sensor_view'][0,:]), # nn[2]['sensor_view'][0,:]), # nn[3]['sensor_view'][0,:]), # nn[4]['sensor_view'][0,:])) #nx = sensor_view.size #x = np.arange(nx) #def func(x, a, b, c, d): # return a*x**3+b*x**2+c*x+d #def linear_func(x, a, b): # return a*x + b #azimuth_time = np.sort( # np.append(np.append(np.append(np.append( # nn[0].get_azimuth_time(), # nn[1].get_azimuth_time()), # nn[2].get_azimuth_time()), # nn[3].get_azimuth_time()), # nn[4].get_azimuth_time())) #dt = azimuth_time.max() - azimuth_time[0] #tt = np.arange(0, dt, dt/ny) #tt = np.append(np.array([-dt/ny], dtype='<m8[us]'), tt) #tt = np.append(tt, tt[-1]+np.array([dt/ny, 2*dt/ny], dtype='<m8[us]')) #ny = len(tt) ## AZIMUTH_TIME #azimuth_time = (np.datetime64(azimuth_time[0])+tt).astype(datetime) #popt, pcov = curve_fit(func, x, sensor_view) ## SENSOR VIEW ANGLE #alpha = np.ones((ny, sensor_view.size))*np.deg2rad(func(x, *popt)) #range_time = np.sort( # np.append(np.append(np.append(np.append( # nn[0].get_range_time(), # nn[1].get_range_time()), # nn[2].get_range_time()), # nn[3].get_range_time()), # nn[4].get_range_time())) #popt, pcov = curve_fit(linear_func, x, range_time) ## RANGE_TIME #range_time = linear_func(x, *popt) #ecefPos, ecefVel = Doppler.orbital_state_vectors(azimuth_time) #eciPos, eciVel = ecef2eci(ecefPos, ecefVel, azimuth_time) ## Get satellite hour angle #satHourAng = np.deg2rad(Doppler.satellite_hour_angle(azimuth_time, ecefPos, ecefVel)) ## Get attitude from the Envisat yaw steering law #psi, gamma, phi = np.deg2rad(Doppler.orbital_attitude_vectors(azimuth_time, satHourAng)) #U1, AX1, S1 = Doppler.step_one_calculations(alpha, psi, gamma, phi, eciPos) #S2, U2, AX2 = Doppler.step_two_calculations(satHourAng, S1, U1, AX1) #S3, U3, AX3 = Doppler.step_three_a_calculations(eciPos, eciVel, S2, U2, AX2) #U3g = Doppler.step_three_b_calculations(S3, U3, AX3) #P3, U3g, lookAng = Doppler.step_four_calculations(S3, U3g, AX3, range_time) #dcm = dcmeci2ecef(azimuth_time, 'IAU-2000/2006') #lat = np.zeros((ny, nx)) #lon = np.zeros((ny, nx)) #alt = np.zeros((ny, nx)) #for i in range(P3.shape[1]): # ecefPos = np.matmul(dcm[0], P3[:,i,:,0, np.newaxis]) # lla = ecef2lla(ecefPos) # lat[:,i] = lla[:,0] # lon[:,i] = lla[:,1] # alt[:,i] = lla[:,2] #lon = lon.round(decimals=5) #lat = lat.round(decimals=5) # DETTE VIRKER: lonmin = np.array( [lon0.min(), lon1.min(), lon2.min(), lon3.min(), lon4.min()]).min() lonmax = np.array( [lon0.max(), lon1.max(), lon2.max(), lon3.max(), lon4.max()]).max() latmin = np.array( [lat0.min(), lat1.min(), lat2.min(), lat3.min(), lat4.min()]).min() latmax = np.array( [lat0.max(), lat1.max(), lat2.max(), lat3.max(), lat4.max()]).max() if nx is None: nx = nn[0].shape()[1] + nn[1].shape()[1] + nn[2].shape()[1] + nn[3].shape()[1] + \ nn[4].shape()[1] # prepare geospatial grid merged = Nansat.from_domain( Domain( NSR(EPSG), '-lle %f %f %f %f -ts %d %d' % (lonmin, latmin, lonmax, latmax, nx, ny))) ## DETTE VIRKER IKKE.. #merged = Nansat.from_domain(Domain.from_lonlat(lon, lat, add_gcps=False)) #merged.add_band(array = np.rad2deg(alpha), parameters={'wkv': 'sensor_view'}) dfdg = np.ones((self.N_SUBSWATHS)) * 5 # Hz (5 Hz a priori) for i in range(self.N_SUBSWATHS): dfdg[i] = nn[i].get_uncertainty_of_fdg() nn[i].reproject(merged, tps=True, resample_alg=1, block_size=2) # Initialize band arrays inc = np.ones( (self.N_SUBSWATHS, merged.shape()[0], merged.shape()[1])) * np.nan fdg = np.ones( (self.N_SUBSWATHS, merged.shape()[0], merged.shape()[1])) * np.nan ur = np.ones( (self.N_SUBSWATHS, merged.shape()[0], merged.shape()[1])) * np.nan valid_sea_dop = np.ones( (self.N_SUBSWATHS, merged.shape()[0], merged.shape()[1])) * np.nan std_fdg = np.ones( (self.N_SUBSWATHS, merged.shape()[0], merged.shape()[1])) * np.nan std_ur = np.ones( (self.N_SUBSWATHS, merged.shape()[0], merged.shape()[1])) * np.nan for ii in range(self.N_SUBSWATHS): inc[ii] = nn[ii]['incidence_angle'] fdg[ii] = nn[ii]['fdg'] ur[ii] = nn[ii]['Ur'] valid_sea_dop[ii] = nn[ii]['valid_sea_doppler'] # uncertainty of fdg is a scalar std_fdg[ii][valid_sea_dop[ii] == 1] = dfdg[ii] # uncertainty of ur std_ur[ii] = nn[ii].get_uncertainty_of_radial_current(dfdg[ii]) # Calculate incidence angle as a simple average mean_inc = np.nanmean(inc, axis=0) merged.add_band(array=mean_inc, parameters={ 'name': 'incidence_angle', 'wkv': 'angle_of_incidence' }) # Calculate fdg as weighted average mean_fdg = nansumwrapper((fdg/np.square(std_fdg)).data, axis=0) / \ nansumwrapper((1./np.square(std_fdg)).data, axis=0) merged.add_band( array=mean_fdg, parameters={ 'name': 'fdg', 'wkv': 'surface_backwards_doppler_frequency_shift_of_radar_wave_due_to_surface_velocity' }) # Standard deviation of fdg std_mean_fdg = np.sqrt(1. / nansumwrapper( (1. / np.square(std_fdg)).data, axis=0)) merged.add_band(array=std_mean_fdg, parameters={'name': 'std_fdg'}) # Calculate ur as weighted average mean_ur = nansumwrapper((ur/np.square(std_ur)).data, axis=0) / \ nansumwrapper((1./np.square(std_ur)).data, axis=0) merged.add_band(array=mean_ur, parameters={ 'name': 'Ur', }) # Standard deviation of Ur std_mean_ur = np.sqrt(1. / nansumwrapper( (1. / np.square(std_ur)).data, axis=0)) merged.add_band(array=std_mean_ur, parameters={'name': 'std_ur'}) # Band of valid pixels vsd = np.nanmin(valid_sea_dop, axis=0) merged.add_band(array=vsd, parameters={ 'name': 'valid_sea_doppler', }) # Add file to db fn = os.path.join( product_path( self.module_name(), nansat_filename( ds.dataseturi_set.get(uri__endswith='.gsar').uri)), os.path.basename( nansat_filename( ds.dataseturi_set.get( uri__endswith='.gsar').uri)).split('.')[0] + '_merged.nc') merged.export(filename=fn) ncuri = 'file://localhost' + fn new_uri, created = DatasetURI.objects.get_or_create(uri=ncuri, dataset=ds) connection.close() return merged