|
| 1 | +from datetime import datetime, timedelta |
| 2 | +from py_eddy_tracker import start_logger |
| 3 | +from py_eddy_tracker.dataset.grid import UnRegularGridDataset |
| 4 | +from numpy import zeros, arange, float |
| 5 | +from netCDF4 import Dataset |
| 6 | + |
| 7 | +# for plotting |
| 8 | +from matplotlib import pyplot as plt |
| 9 | + |
| 10 | +class RomsDataset(UnRegularGridDataset): |
| 11 | + |
| 12 | + __slots__ = list() |
| 13 | + |
| 14 | + def init_speed_coef(self, uname, vname): |
| 15 | + # xi_u and eta_v must be specified because this dimension are not use in lon/lat |
| 16 | + u = self.grid(uname, indexs=self.indexs) |
| 17 | + v = self.grid(vname, indexs=self.indexs) |
| 18 | + u = self.rho_2d(u.T).T |
| 19 | + v = self.rho_2d(v) |
| 20 | + self._speed_norm = (v ** 2 + u ** 2) ** .5 |
| 21 | + |
| 22 | + |
| 23 | + @staticmethod |
| 24 | + def rho_2d(x): |
| 25 | + """Transformation to have u or v on same grid than h |
| 26 | + """ |
| 27 | + M, Lp = x.shape |
| 28 | + new_x = zeros((M + 1, Lp)) |
| 29 | + new_x[1:-1] = .5 * (x[:-1] + x[1:]) |
| 30 | + new_x[0] = new_x[1] |
| 31 | + new_x[-1] = new_x[-2] |
| 32 | + return new_x |
| 33 | + |
| 34 | + @staticmethod |
| 35 | + def psi2rho(var_psi): |
| 36 | + """Transformation to have vrt on same grid than h |
| 37 | + """ |
| 38 | + M, L = var_psi.shape |
| 39 | + Mp=M+1; Lp=L+1 |
| 40 | + Mm=M-1; Lm=L-1 |
| 41 | + var_rho = zeros((Mp, Lp)) |
| 42 | + var_rho[1:M,1:L]=0.25*(var_psi[0:Mm,0:Lm]+var_psi[0:Mm,1:L]+var_psi[1:M,0:Lm]+var_psi[1:M,1:L]) |
| 43 | + var_rho[0,:]=var_rho[1,:] |
| 44 | + var_rho[Mp-1,:]=var_rho[M-1,:] |
| 45 | + var_rho[:,0]=var_rho[:,1] |
| 46 | + var_rho[:,Lp-1]=var_rho[:,L-1] |
| 47 | + return var_rho |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + start_logger().setLevel('DEBUG') |
| 54 | + |
| 55 | + # Pick a depth |
| 56 | + s_rho = 5 # 1000 m |
| 57 | + depths = arange(-3500, 0, 500) # Make sure it is the same than used to generate the 'horizontal_section' files. |
| 58 | + |
| 59 | + depth = depths[s_rho] |
| 60 | + |
| 61 | + # Time loop |
| 62 | + for time in range(7000, 7020): |
| 63 | + |
| 64 | + |
| 65 | + infiletime = time%10 |
| 66 | + filetime = time - time%10 |
| 67 | + |
| 68 | + # Using times: |
| 69 | + grid_name = './GIGATL6/gigatl6_1h_horizontal_section.' + '{0:05}'.format(filetime) + '.nc' |
| 70 | + |
| 71 | + # or using dates |
| 72 | + realyear_origin = datetime(2004,1,15) |
| 73 | + date1 = realyear_origin + timedelta(days=float(time)/2.) |
| 74 | + date2 = date1 + timedelta(days=float(4.5)) |
| 75 | + |
| 76 | + filedate = '{0:04}'.format(date1.year)+'-'+\ |
| 77 | + '{0:02}'.format(date1.month) + '-'+\ |
| 78 | + '{0:02}'.format(date1.day)+ '-'+\ |
| 79 | + '{0:04}'.format(date2.year)+'-'+\ |
| 80 | + '{0:02}'.format(date2.month) + '-' +\ |
| 81 | + '{0:02}'.format(date2.day) |
| 82 | + |
| 83 | + print(filedate) |
| 84 | + |
| 85 | + lon_name, lat_name = 'lon', 'lat' |
| 86 | + |
| 87 | + |
| 88 | + h = RomsDataset(grid_name, lon_name, lat_name, |
| 89 | + indexs=dict(time=infiletime, |
| 90 | + eta_rho=slice(500, 800), |
| 91 | + xi_rho=slice(500, 800), |
| 92 | + eta_v=slice(500, 800-1), |
| 93 | + xi_u=slice(500, 800-1), |
| 94 | + s_rho=s_rho) |
| 95 | + ) |
| 96 | + |
| 97 | + # Must be set with time of grid |
| 98 | + date = date1 |
| 99 | + |
| 100 | + # Identification every 2 mm |
| 101 | + a, c = h.eddy_identification('ow', 'u', 'v', date, z_min = -10, z_max = -0.1, step = 0.05, pixel_limit=(10, 2000), shape_error=40, force_height_unit='m',force_speed_unit='m/s',vorticity_name='vrt') |
| 102 | + |
| 103 | + filename = 'gigatl6_1h_horizontal_section_' + '{0:04}'.format(-depth) + '_' |
| 104 | + |
| 105 | + with Dataset(date.strftime('Anticyclonic_' + filename + '%Y%m%d%H.nc'), 'w') as h: |
| 106 | + a.to_netcdf(h) |
| 107 | + with Dataset(date.strftime('Cyclonic_' + filename + '%Y%m%d%H.nc'), 'w') as h: |
| 108 | + c.to_netcdf(h) |
| 109 | + |
| 110 | + # PLOT |
| 111 | + |
| 112 | + |
| 113 | + fig = plt.figure(figsize=(15,7)) |
| 114 | + ax = fig.add_axes([.03,.03,.94,.94]) |
| 115 | + ax.set_title('Eddies detected -- Cyclonic(red) and Anticyclonic(blue)') |
| 116 | + #ax.set_ylim(-75,75) |
| 117 | + ax.set_xlim(250,360) |
| 118 | + ax.set_aspect('equal') |
| 119 | + a.display(ax, color='b', linewidth=.5) |
| 120 | + c.display(ax, color='r', linewidth=.5) |
| 121 | + ax.grid() |
| 122 | + fig.savefig('eddies_' + date.strftime( filename + '%Y%m%d%H') +'.png') |
| 123 | + |
| 124 | + |
0 commit comments