def extrapolate_pha(lon, lat, lonr, latr, maskr, arr): cx, cy = np.cos(arr), np.sin(arr) cx = mask_interp(lon, lat, cx) cy = mask_interp(lon, lat, cy) cx = cx(np.vstack((lonr, latr)).T, nnear=6, p=2) cy = cy(np.vstack((lonr, latr)).T, nnear=6, p=2) bad = np.logical_or(np.isnan(cy), np.isnan(cx)) if np.any(bad): import pdb pdb.set_trace() arr = np.arctan2(cy, cx) return arr
def _create_nc_gr3(self, ncfile, var): data = netCDF4.Dataset(ncfile) # Read the grid file X = data.variables['longitude'][:] Y = data.variables['latitude'][:] if len(X.shape) == 1: xx, yy = np.meshgrid(X, Y) lon = self.hgrid.longitude lat = self.hgrid.latitude res = xr.open_dataset(ncfile) _, index = np.unique(res['time'], return_index=True) res = res.isel(time=index) #time0=[np.datetime64(x) for x in time0] arri = res[var][:] arri_time = arri.interp(time=self.t0.strftime('%Y-%m-%d %H:%M:%S')) #geo_idx = (np.abs(date2num(time0)-date2num(self.t0))).argmin() # closest timestep if len(arri_time.shape) > 2: arri_time = arri_time[0] # get surface level src = mask_interp(xx, yy, arri_time.to_masked_array()) tb = src(np.vstack((lon, lat)).T, nnear=1, p=2) if np.any(np.isnan(tb)): import pdb pdb.set_trace() self._create_constante_gr3(tb)
def extrapolate_amp(lon, lat, lonr, latr, maskr, arr): arr = mask_interp(lon, lat, arr) arri = arr(np.vstack((lonr, latr)).T, nnear=6, p=2) bad = np.isnan(arri) if np.any(bad): import pdb pdb.set_trace() return arri
def get_elev(self, config): Time = date2num(self.t0) if 'tidal' in config: self.cons = config['tidal'].get('cons', None) OpenBoundaries.add_tide(self, config['tidal']) if 'residual' in config: OpenBoundaries.add_res(self, config['residual']) total = np.zeros(shape=(len(self.llon), 1)) # get tide if self.tidal: var = list(self.HC.keys())[0] # horizontal interpolation tmp = get_tide(self.constidx, self.tfreq, self.HC[var], np.array(Time), np.mean(self.llat)) total += tmp if self.residual: if 'longitude' in self.res_file.dims: lon_name = 'longitude' lat_name = 'latitude' else: lon_name = 'lon' lat_name = 'lat' xx, yy = np.meshgrid(self.res_file[lon_name][:], self.res_file[lat_name][:]) var = self.res_vars[0] arri = self.res_file[var][:] arri_time = arri.interp( time=num2date(date2num(np.datetime64(num2date( Time)))).strftime('%Y-%m-%d %H:%M:%S')) arr = mask_interp(xx, yy, arri_time.to_masked_array()) tb = arr(np.vstack((self.llon, self.llat)).T, nnear=6, p=2) total[:, 0] = total[:, 0] + tb return total
def create_Dthnc(self, fileout, TimeSeries): if '2D' in fileout: self.i23d = 2 else: self.i23d = 3 tin = [np.datetime64(num2date(x)) for x in TimeSeries] if self.residual: if 'longitude' in self.res_file.dims: lon_name = 'longitude' lat_name = 'latitude' else: lon_name = 'lon' lat_name = 'lat' xx, yy = np.meshgrid(self.res_file[lon_name][:], self.res_file[lat_name][:]) # create file if self.i23d == 3: Nlev = self.zz.shape[1] else: Nlev = 1 time_Series, nc = create_ncTH( fileout, len(self.llon), Nlev, self.ivs, np.round((TimeSeries - TimeSeries[0]) * 24 * 3600)) for n in range(0, len(TimeSeries)): total = np.zeros(shape=(self.ivs, len(self.llon), Nlev)) # get tide if self.tidal: var = self.HC.keys() for i, v in enumerate(sorted(var)): # horizontal interpolation tmp = get_tide(self.constidx, self.tfreq, self.HC[v], np.array(TimeSeries[n]), self.lat0) if self.i23d > 2: # vertical interpolation tmp = vertical_extrapolation(tmp, self.zz, z0=self.z0) total[i, :, :] = total[i, :, :] + tmp if self.residual: var = self.res_vars for i, v in enumerate(sorted(var)): arri = self.res_file[v][:] arri_time = arri.interp(time=num2date(date2num( tin[n])).strftime('%Y-%m-%d %H:%M:%S')) if self.i23d > 2: tb = np.ndarray((len(self.llon), Nlev)) tmp = np.ndarray( (len(self.llon), arri_time.shape[0])) * np.nan for nlev in range(0, arri_time.shape[0]): if np.any(arri_time[nlev].to_masked_array()): arr = mask_interp( xx, yy, arri_time[nlev].to_masked_array()) if len(arr.z) > 1: tmp[:, nlev] = arr(np.vstack( (self.llon, self.llat)).T, nnear=1, p=2) zi = self.res_file['lev'][:].values if np.mean(zi) > 0: zi = zi * -1 for p in range(0, tmp.shape[0]): if self.zz.shape[1] == 2: # 2D total_depth = self.zz[p, 0] bad = np.isnan(tmp[p, :]) depth = zi[~bad] vel = tmp[p, ~bad] depth = np.insert(depth, 0, 0, axis=0) ve = 0 tot = 0 for dep in range(0, len(depth) - 1): dz = depth[dep] - depth[dep + 1] dx = vel[dep] ve += dx * dz tot += dz tb[p, :] = ve / np.abs(tot) else: # 3D bad = np.isnan(tmp[p, :]) caca = interp1d(zi[~bad], tmp[p, ~bad], fill_value="extrapolate") tb[p, :] = caca(self.zz[p, :]) else: arr = mask_interp(xx, yy, arri_time.to_masked_array()) tb = arr(np.vstack((self.llon, self.llat)).T, nnear=6, p=2) if np.any(np.isnan(tb)): print('probleme') total[i, :, :] = total[i, :, :] + np.reshape( tb, (len(self.llon), Nlev)) total = np.transpose(total, (1, 2, 0)) if np.isnan(total).any(): import pdb pdb.set_trace() if n % 100 == 0: self.logger.info( 'For timestep=%.f, max=%.4f, min=%.4f , max abs diff=%.4f' % (TimeSeries[n], total.max(), total.min(), abs(np.diff(total, n=1, axis=0)).max())) time_Series[n, :, :, :] = total nc.close()
def get_tracet_at_node(self, config): Time = date2num(self.t0) self.llat, self.llon, self.zz = self.get_all_nodes() self.zz = np.array(self.zz) Nlev = self.zz.shape[1] total = np.zeros(shape=(len(self.llon), Nlev)) Time = date2num(self.t0) OpenBoundaries.add_res(self, config) if 'longitude' in self.res_file.dims: lon_name = 'longitude' lat_name = 'latitude' else: lon_name = 'lon' lat_name = 'lat' xx, yy = np.meshgrid(self.res_file[lon_name][:], self.res_file[lat_name][:]) zi = self.res_file['lev'][:].values if np.mean(zi) > 0: zi = zi * -1 var = self.res_vars[0] arri = self.res_file[var][:] arri_time = arri.interp( time=num2date(date2num(np.datetime64(num2date(Time)))).strftime( '%Y-%m-%d %H:%M:%S')) tb = np.ndarray((len(self.llon), Nlev)) tmp = np.ndarray((len(self.llon), arri_time.shape[0])) * np.nan for nlev in range(0, arri_time.shape[0]): if np.any(arri_time[nlev].to_masked_array()): arr = mask_interp(xx, yy, arri_time[nlev].to_masked_array()) if len(arr.z) > 1: tmp[:, nlev] = arr(np.vstack((self.llon, self.llat)).T, nnear=1, p=2) tpm = fill_in_gap(tmp) if self.zs.shape[1] == 2: # 2D for p in range(0, tmp.shape[0]): total_depth = self.zz[p, 0] bad = np.isnan(tmp[p, :]) depth = zi[~bad] vel = tmp[p, ~bad] depth = np.insert(depth, 0, 0, axis=0) ve = 0 tot = 0 for dep in range(0, len(depth) - 1): dz = depth[dep] - depth[dep + 1] dx = vel[dep] ve += dx * dz tot += dz tb[p, :] = ve / np.abs(tot) else: # 3D varin = np.fliplr(tmp) #[:,~bad]) zin = np.fliplr(np.matlib.repmat(zi, varin.shape[0], 1)) for nl in range(0, self.zz.shape[1]): zout = self.zz[:, nl] tb[:, nl] = np.squeeze( interpvert.interpz1d(varin, zin, zout, np=varin.shape[0], nzin=varin.shape[1], nzout=1, kz=1, null_value=-9.9e15)) return tb
def get_uv(self, config): Time = date2num(self.t0) self.llon = copy.deepcopy(self.xs) self.llat = copy.deepcopy(self.ys) Nlev = self.zz.shape[1] total = np.zeros(shape=(2, len(self.llon), Nlev)) Time = date2num(self.t0) if 'tidal' in config: self.cons = config['tidal'].get('cons', None) OpenBoundaries.add_tide(self, config['tidal']) if 'residual' in config: OpenBoundaries.add_res(self, config['residual']) # get tide if self.tidal: var = self.HC.keys() for i, v in enumerate(sorted(var)): # horizontal interpolation tmp = get_tide(self.constidx, self.tfreq, self.HC[v], np.array(Time), np.mean(self.llat)) tmp = vertical_extrapolation(tmp, self.zs, z0=self.z0) tpm = fill_in_gap(tmp) total[i, :, :] = total[i, :, :] + tmp if self.residual: if 'longitude' in self.res_file.dims: lon_name = 'longitude' lat_name = 'latitude' else: lon_name = 'lon' lat_name = 'lat' xx, yy = np.meshgrid(self.res_file[lon_name][:], self.res_file[lat_name][:]) var = self.res_vars zi = self.res_file['lev'][:].values if np.mean(zi) > 0: zi = zi * -1 for i, v in enumerate(sorted(var)): arri = self.res_file[v][:] arri_time = arri.interp( time=num2date(date2num(np.datetime64(num2date( Time)))).strftime('%Y-%m-%d %H:%M:%S')) tb = np.ndarray((len(self.llon), Nlev)) tmp = np.ndarray((len(self.llon), arri_time.shape[0])) * np.nan for nlev in range(0, arri_time.shape[0]): if np.any(arri_time[nlev].to_masked_array()): arr = mask_interp(xx, yy, arri_time[nlev].to_masked_array()) if len(arr.z) > 1: tmp[:, nlev] = arr(np.vstack( (self.llon, self.llat)).T, nnear=1, p=2) tpm = fill_in_gap(tmp) if self.zs.shape[1] == 2: # 2D for p in range(0, tmp.shape[0]): total_depth = self.zs[p, 0] bad = np.isnan(tmp[p, :]) depth = zi[~bad] vel = tmp[p, ~bad] depth = np.insert(depth, 0, 0, axis=0) ve = 0 tot = 0 for dep in range(0, len(depth) - 1): dz = depth[dep] - depth[dep + 1] dx = vel[dep] ve += dx * dz tot += dz tb[p, :] = ve / np.abs(tot) else: # 3D varin = np.fliplr(tmp) #[:,~bad]) zin = np.fliplr(np.matlib.repmat(zi, varin.shape[0], 1)) for nl in range(0, self.zs.shape[1]): zout = self.zs[:, nl] tb[:, nl] = np.squeeze( interpvert.interpz1d(varin, zin, zout, np=varin.shape[0], nzin=varin.shape[1], nzout=1, kz=1, null_value=-9.9e15)) total[i, :, :] = total[i, :, :] + tb return total