|
33 | 33 | from scipy.ndimage import binary_erosion |
34 | 34 | from scipy.ndimage import minimum_filter |
35 | 35 | import numpy as np |
36 | | -from netCDF4 import Dataset |
37 | 36 | from .tools import index_from_nearest_path, distance_matrix |
38 | | -from . import VAR_DESCR, VAR_DESCR_inv |
39 | | -from .observations import EddiesObservations |
40 | 37 | import logging |
41 | 38 |
|
42 | 39 |
|
43 | | -class VirtualEddiesObservations(EddiesObservations): |
44 | | - """Class to work with virtual obs |
45 | | - """ |
46 | | - |
47 | | - @property |
48 | | - def elements(self): |
49 | | - elements = super(VirtualEddiesObservations, self).elements |
50 | | - elements.extend(['track', 'segment_size', 'dlon', 'dlat']) |
51 | | - return elements |
52 | | - |
53 | | - |
54 | | -class TrackEddiesObservations(EddiesObservations): |
55 | | - |
56 | | - def extract_longer_eddies(self, nb_min, nb_obs): |
57 | | - m = nb_obs >= nb_min |
58 | | - nb_obs_select = m.sum() |
59 | | - logging.info('Selection of %d observations', nb_obs_select) |
60 | | - eddies = TrackEddiesObservations(size=nb_obs_select) |
61 | | - eddies.sign_type = self.sign_type |
62 | | - for var, _ in eddies.obs.dtype.descr: |
63 | | - eddies.obs[var] = self.obs[var][m] |
64 | | - return eddies |
65 | | - |
66 | | - @property |
67 | | - def elements(self): |
68 | | - elements = super(TrackEddiesObservations, self).elements |
69 | | - elements.extend(['track', 'n', 'virtual']) |
70 | | - return elements |
71 | | - |
72 | | - def create_variable(self, handler_nc, kwargs_variable, |
73 | | - attr_variable, data, scale_factor=None): |
74 | | - var = handler_nc.createVariable( |
75 | | - zlib=True, |
76 | | - complevel=1, |
77 | | - **kwargs_variable) |
78 | | - for attr, attr_value in attr_variable.iteritems(): |
79 | | - var.setncattr(attr, attr_value) |
80 | | - |
81 | | - var[:] = data |
82 | | - |
83 | | - #~ var.set_auto_maskandscale(False) |
84 | | - if scale_factor is not None: |
85 | | - var.scale_factor = scale_factor |
86 | | - |
87 | | - try: |
88 | | - var.setncattr('min', var[:].min()) |
89 | | - var.setncattr('max', var[:].max()) |
90 | | - except ValueError: |
91 | | - logging.warn('Data is empty') |
92 | | - |
93 | | - def write_netcdf(self): |
94 | | - """Write a netcdf with eddy obs |
95 | | - """ |
96 | | - eddy_size = len(self.observations) |
97 | | - sign_type = 'Cyclonic' if self.sign_type == -1 else 'Anticyclonic' |
98 | | - filename = '%s.nc' % sign_type |
99 | | - with Dataset(filename, 'w', format='NETCDF4') as h_nc: |
100 | | - logging.info('Create file %s', filename) |
101 | | - # Create dimensions |
102 | | - logging.debug('Create Dimensions "Nobs" : %d', eddy_size) |
103 | | - h_nc.createDimension('Nobs', eddy_size) |
104 | | - # Iter on variables to create: |
105 | | - for name, _ in self.observations.dtype.descr: |
106 | | - logging.debug('Create Variable %s', VAR_DESCR[name]['nc_name']) |
107 | | - self.create_variable( |
108 | | - h_nc, |
109 | | - dict(varname=VAR_DESCR[name]['nc_name'], |
110 | | - datatype=VAR_DESCR[name]['nc_type'], |
111 | | - dimensions=VAR_DESCR[name]['nc_dims']), |
112 | | - VAR_DESCR[name]['nc_attr'], |
113 | | - self.observations[name], |
114 | | - scale_factor=None if 'scale_factor' not in VAR_DESCR[name] else VAR_DESCR[name]['scale_factor']) |
115 | | - |
116 | | - # Add cyclonic information |
117 | | - self.create_variable( |
118 | | - h_nc, |
119 | | - dict(varname=VAR_DESCR['type_cyc']['nc_name'], |
120 | | - datatype=VAR_DESCR['type_cyc']['nc_type'], |
121 | | - dimensions=VAR_DESCR['type_cyc']['nc_dims']), |
122 | | - VAR_DESCR['type_cyc']['nc_attr'], |
123 | | - self.sign_type) |
124 | | - # Global attr |
125 | | - self.set_global_attr_netcdf(h_nc) |
126 | | - |
127 | | - def set_global_attr_netcdf(self, h_nc): |
128 | | - h_nc.title = 'Cyclonic' if self.sign_type == -1 else 'Anticyclonic' + ' eddy tracks' |
129 | | - #~ h_nc.grid_filename = self.grd.grid_filename |
130 | | - #~ h_nc.grid_date = str(self.grd.grid_date) |
131 | | - #~ h_nc.product = self.product |
132 | | - |
133 | | - #~ h_nc.contour_parameter = self.contour_parameter |
134 | | - #~ h_nc.shape_error = self.shape_error |
135 | | - #~ h_nc.pixel_threshold = self.pixel_threshold |
136 | | - |
137 | | - #~ if self.smoothing in locals(): |
138 | | - #~ h_nc.smoothing = self.smoothing |
139 | | - #~ h_nc.SMOOTH_FAC = self.SMOOTH_FAC |
140 | | - #~ else: |
141 | | - #~ h_nc.smoothing = 'None' |
142 | | - |
143 | | - #~ h_nc.evolve_amp_min = self.evolve_amp_min |
144 | | - #~ h_nc.evolve_amp_max = self.evolve_amp_max |
145 | | - #~ h_nc.evolve_area_min = self.evolve_area_min |
146 | | - #~ h_nc.evolve_area_max = self.evolve_area_max |
147 | | -#~ |
148 | | - #~ h_nc.llcrnrlon = self.grd.lonmin |
149 | | - #~ h_nc.urcrnrlon = self.grd.lonmax |
150 | | - #~ h_nc.llcrnrlat = self.grd.latmin |
151 | | - #~ h_nc.urcrnrlat = self.grd.latmax |
152 | | - |
153 | | - |
154 | 40 | class Amplitude (object): |
155 | 41 | """ |
156 | 42 | Class to calculate *amplitude* and counts of *local maxima/minima* |
|
0 commit comments