Skip to content

Commit e860134

Browse files
author
adelepoulle
committed
Refactoring py eddy tracker cls
1 parent 6f94dd0 commit e860134

15 files changed

+3092
-2337
lines changed

setup.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# -*- coding: utf-8 -*-
22
from setuptools import setup, find_packages, Extension
33
from Cython.Distutils import build_ext as cython_build_ext
4+
import numpy
45

56
setup(
6-
name="pyeddytracker",
7+
name="pyEddyTracker",
78
version='2.0.3',
89
description="Py-Eddy-Tracker libraries",
910
classifiers=['Development Status :: Alpha',
@@ -15,6 +16,9 @@
1516
packages=find_packages('src'),
1617
package_dir={'': 'src'},
1718
scripts=[
19+
'src/scripts/EddyIdentification',
20+
'src/scripts/EddyTracking',
21+
'src/scripts/EddyTrackingFull',
1822
'src/scripts/make_eddy_track_aviso.py',
1923
'src/scripts/make_eddy_track_CLS.py',
2024
'src/scripts/make_eddy_track_ROMS.py'],
@@ -23,7 +27,8 @@
2327
'build_ext': cython_build_ext,
2428
},
2529
ext_modules=[Extension("py_eddy_tracker.tools",
26-
["src/py_eddy_tracker/tools.pyx"])],
30+
["src/py_eddy_tracker/tools.pyx"],
31+
include_dirs=[numpy.get_include()])],
2732
setup_requires=[
2833
'numpy>=1.9'],
2934
install_requires=[
@@ -34,5 +39,4 @@
3439
'pyyaml',
3540
'pyproj',
3641
],
37-
)
38-
42+
)

src/py_eddy_tracker/__init__.py

Lines changed: 127 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,67 @@
11
# -*- coding: utf-8 -*-
2+
from argparse import ArgumentParser
3+
import logging
4+
5+
6+
class ColoredFormatter(logging.Formatter):
7+
COLOR_LEVEL = dict(
8+
CRITICAL="\037[37;41m",
9+
ERROR="\033[31;47m",
10+
WARNING="\033[30;47m",
11+
INFO="\033[36m",
12+
DEBUG="\033[34m",
13+
)
14+
15+
def __init__(self, message):
16+
super(ColoredFormatter, self).__init__(message)
17+
18+
def format(self, record):
19+
color = self.COLOR_LEVEL.get(record.levelname, '')
20+
color_reset = '\033[0m'
21+
model = color + '%s' + color_reset
22+
record.msg = model % record.msg
23+
record.funcName = model % record.funcName
24+
record.module = model % record.module
25+
record.levelname = model % record.levelname
26+
return super(ColoredFormatter, self).format(record)
27+
28+
29+
class EddyParser(ArgumentParser):
30+
"""General parser for applications
31+
"""
32+
33+
FORMAT_LOG = "%(levelname)-8s %(asctime)s %(module)s." \
34+
"%(funcName)s :\n\t\t\t\t\t%(message)s"
35+
36+
def __init__(self, *args, **kwargs):
37+
super(EddyParser, self).__init__(*args, **kwargs)
38+
self.add_base_argument()
39+
40+
def add_base_argument(self):
41+
"""Base arguments
42+
"""
43+
self.add_argument('-v', '--verbose',
44+
dest='logging_level',
45+
default='ERROR',
46+
help='Levels : DEBUG, INFO, WARNING,'
47+
' ERROR, CRITICAL')
48+
49+
def parse_args(self, *args, **kwargs):
50+
# set up logging to CONSOLE
51+
console = logging.StreamHandler()
52+
console.setFormatter(ColoredFormatter(self.FORMAT_LOG))
53+
# add the handler to the root logger
54+
logging.getLogger('').addHandler(console)
55+
# Parsing
56+
opts = super(EddyParser, self).parse_args(*args, **kwargs)
57+
# set current level
58+
logging.getLogger().setLevel(getattr(logging, opts.logging_level.upper()))
59+
return opts
60+
61+
262
VAR_DESCR = dict(
363
time=dict(
4-
attr_name='new_time_tmp',
64+
attr_name='time',
565
nc_name='j1',
666
nc_type='int32',
767
nc_dims=('Nobs',),
@@ -34,7 +94,8 @@
3494
)
3595
),
3696
lon=dict(
37-
attr_name='new_lon_tmp',
97+
attr_name='lon',
98+
compute_type='float64',
3899
nc_name='lon',
39100
nc_type='float32',
40101
nc_dims=('Nobs',),
@@ -43,7 +104,8 @@
43104
)
44105
),
45106
lat=dict(
46-
attr_name='new_lat_tmp',
107+
attr_name='lat',
108+
compute_type='float64',
47109
nc_name='lat',
48110
nc_type='float32',
49111
nc_dims=('Nobs',),
@@ -52,7 +114,7 @@
52114
)
53115
),
54116
amplitude=dict(
55-
attr_name='new_amp_tmp',
117+
attr_name='amplitude',
56118
nc_name='A',
57119
nc_type='float32',
58120
nc_dims=('Nobs',),
@@ -69,6 +131,7 @@
69131
nc_name='L',
70132
nc_type='float32',
71133
nc_dims=('Nobs',),
134+
scale_factor=1e-3,
72135
nc_attr=dict(
73136
long_name='speed radius scale',
74137
units='km',
@@ -78,7 +141,8 @@
78141
)
79142
),
80143
speed_radius=dict(
81-
attr_name='new_uavg_tmp',
144+
attr_name='speed_radius',
145+
scale_factor=100,
82146
nc_name='U',
83147
nc_type='float32',
84148
nc_dims=('Nobs',),
@@ -90,7 +154,7 @@
90154
)
91155
),
92156
eke=dict(
93-
attr_name='new_teke_tmp',
157+
attr_name='eke',
94158
nc_name='Teke',
95159
nc_type='float32',
96160
nc_dims=('Nobs',),
@@ -102,7 +166,8 @@
102166
)
103167
),
104168
radius_e=dict(
105-
attr_name='new_radii_e_tmp',
169+
attr_name='radius_e',
170+
scale_factor=1e-3,
106171
nc_name='radius_e',
107172
nc_type='float32',
108173
nc_dims=('Nobs',),
@@ -112,6 +177,18 @@
112177
description='effective eddy radius',
113178
)
114179
),
180+
radius_s=dict(
181+
attr_name='radius_s',
182+
scale_factor=1e-3,
183+
nc_name='L',
184+
nc_type='float32',
185+
nc_dims=('Nobs',),
186+
nc_attr=dict(
187+
long_name='speed radius scale',
188+
units='km',
189+
description='speed eddy radius',
190+
)
191+
),
115192
track=dict(
116193
attr_name=None,
117194
nc_name='track',
@@ -134,4 +211,46 @@
134211
description='observation sequence number (XX day intervals)',
135212
)
136213
),
137-
)
214+
contour_e=dict(
215+
attr_name=None,
216+
nc_name='contour_e',
217+
nc_type='f4',
218+
nc_dims=('contour_points', 'Nobs',),
219+
nc_attr=dict(
220+
long_name='positions of effective contour points',
221+
description='lons/lats of effective contour points; lons (lats) '
222+
'in first (last) half of vector',
223+
)
224+
),
225+
contour_s=dict(
226+
attr_name=None,
227+
nc_name='contour_s',
228+
nc_type='f4',
229+
nc_dims=('contour_points', 'Nobs',),
230+
nc_attr=dict(
231+
long_name='positions of speed-based contour points',
232+
description='lons/lats of speed-based contour points; lons (lats) '
233+
'in first (last) half of vector',
234+
)
235+
),
236+
uavg_profile=dict(
237+
attr_name=None,
238+
nc_name='uavg_profile',
239+
nc_type='f4',
240+
nc_dims=('uavg_contour_count', 'Nobs',),
241+
nc_attr=dict(
242+
long_name='radial profile of uavg',
243+
description='all uavg values from effective contour inwards to '
244+
'smallest inner contour (pixel == 1)',
245+
)
246+
),
247+
shape_error=dict(
248+
attr_name=None,
249+
nc_name='shape_error',
250+
nc_type='f2',
251+
nc_dims=('Nobs',),
252+
nc_attr=dict(
253+
units='%',
254+
)
255+
),
256+
)

src/py_eddy_tracker/global_tracking.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,57 +8,56 @@ class GlobalTracking(object):
88
"""
99
"""
1010

11-
def __init__(self, eddy, ymd_str):
12-
self.ymd_str = ymd_str
11+
def __init__(self, eddy, date):
12+
self.date = date
1313
self.eddy = eddy
1414

1515
@property
1616
def sign_type(self):
17-
return self.eddy.SIGN_TYPE
17+
return self.eddy.sign_type
1818

1919
def create_variable(self, handler_nc, kwargs_variable,
20-
attr_variable, data):
20+
attr_variable, data, scale_factor=None):
2121
var = handler_nc.createVariable(
2222
zlib=True,
2323
complevel=1,
2424
**kwargs_variable)
2525
for attr, attr_value in attr_variable.iteritems():
2626
var.setncattr(attr, attr_value)
27-
28-
var[:] = data
29-
30-
var.setncattr('min', var[:].min())
31-
var.setncattr('max', var[:].max())
27+
var[:] = data
28+
var.set_auto_maskandscale(False)
29+
if scale_factor is not None:
30+
var.scale_factor = scale_factor
31+
32+
try:
33+
var.setncattr('min', var[:].min())
34+
var.setncattr('max', var[:].max())
35+
except ValueError:
36+
logging.warn('Data is empty')
3237

3338
def write_netcdf(self):
3439
"""Write a netcdf with eddy
3540
"""
36-
eddy_size = None
37-
for key in VAR_DESCR:
38-
attr_name = VAR_DESCR[key]['attr_name']
39-
if attr_name is not None and hasattr(self.eddy, attr_name):
40-
eddy_size = len(getattr(self.eddy, attr_name))
41-
break
42-
43-
filename = '%s_%s.nc' % (self.sign_type, self.ymd_str)
41+
eddy_size = len(self.eddy.tmp_observations)
42+
print dir(self)
43+
exit()
44+
filename = '%s_%s.nc' % (self.sign_type, self.date.strftime('%Y%m%d'))
4445
with Dataset(filename, 'w', format='NETCDF4') as h_nc:
4546
logging.info('Create intermediary file %s', filename)
4647
# Create dimensions
47-
logging.debug('Create Dimensions "Nobs"')
48+
logging.debug('Create Dimensions "Nobs" : %d', eddy_size)
4849
h_nc.createDimension('Nobs', eddy_size)
4950
# Iter on variables to create:
50-
for key, value in VAR_DESCR.iteritems():
51-
attr_name = value['attr_name']
52-
if attr_name is None or not hasattr(self.eddy, attr_name):
53-
continue
54-
logging.debug('Create Variable %s', value['nc_name'])
51+
for name, _ in self.eddy.tmp_observations.dtype:
52+
logging.debug('Create Variable %s', VAR_DESCR[name]['nc_name'])
5553
self.create_variable(
5654
h_nc,
57-
dict(varname=value['nc_name'],
58-
datatype=value['nc_type'],
59-
dimensions=value['nc_dims']),
60-
value['nc_attr'],
61-
getattr(self.eddy, attr_name))
55+
dict(varname=VAR_DESCR[name]['nc_name'],
56+
datatype=VAR_DESCR[name]['nc_type'],
57+
dimensions=VAR_DESCR[name]['nc_dims']),
58+
VAR_DESCR[name]['nc_attr'],
59+
self.eddy.tmp_observations.obs[name],
60+
scale_factor=None if 'scale_factor' not in VAR_DESCR[name] else VAR_DESCR[name]['scale_factor'])
6261

6362
# Add cyclonic information
6463
self.create_variable(

0 commit comments

Comments
 (0)