Skip to content

Commit 788180d

Browse files
committed
store coloc information
1 parent 56708ea commit 788180d

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

slcl1butils/coloc/coloc_IW_WW3spectra.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,64 @@
66
import numpy as np
77
import xarray as xr
88

9-
from slcl1butils.legacy_ocean.ocean.xPolarSpectrum import find_closest_ww3
9+
from slcl1butils.legacy_ocean.ocean.xPolarSpectrum import haversine
1010

1111
# from ocean.xspectrum import from_ww3
1212
# from ocean.xPolarSpectrum import find_closest_ww3
1313
from slcl1butils.legacy_ocean.ocean.xspectrum import from_ww3
1414
from slcl1butils.raster_readers import resource_strftime
1515
from slcl1butils.symmetrize_l1b_spectra import symmetrize_xspectrum
1616
from slcl1butils.utils import xndindex
17+
COLOC_LIMIT_SPACE = 100 # km
18+
COLOC_LIMIT_TIME = 3 # hours
19+
20+
def find_closest_ww3(ww3_path, lon, lat, time):
21+
"""
22+
Find spatio-temporal closest point in ww3 file
23+
24+
Args:
25+
ww3_path (str): path to the WW3 file
26+
lon (float): longitude of interest
27+
lat (float): latitude of interest
28+
time (datetime or tuple of int): Tuple of the form (year, month, day, hour, minute, second)
29+
30+
31+
Returns:
32+
(int): time indice of spatio-temporal closest point
33+
"""
34+
from datetime import datetime
35+
import numpy as np
36+
import xarray as xr
37+
ww3spec = xr.open_dataset(ww3_path)
38+
mytime = np.datetime64(time) if type(time)==datetime else np.datetime64(datetime(*time))
39+
time_dist = np.abs(ww3spec.time-mytime)
40+
41+
isel = np.where(time_dist==time_dist.min())
42+
spatial_dist = haversine(lon, lat, ww3spec[{'time':isel[0]}].longitude, ww3spec[{'time':isel[0]}].latitude)
43+
# logging.debug(spatial_dist)
44+
myind = isel[0][np.argmin(spatial_dist.data)]
45+
time_dist_minutes = (ww3spec[{'time': myind}].time - mytime).data / (1e9 * 60)
46+
logging.debug('Wave Watch III closest point @ {} km and {} minutes'.format(np.round(spatial_dist,2),
47+
time_dist_minutes))
48+
if abs(time_dist_minutes)>COLOC_LIMIT_TIME*60 or spatial_dist>COLOC_LIMIT_SPACE:
49+
logging.debug('closest in time then in space is beyond the limits -> No ww3 spectra will be associated ')
50+
myind = None
51+
52+
selection = xr.DataArray(myind,attributes={'method':'closest in time then in space',
53+
'limits of selection in space (km)':COLOC_LIMIT_SPACE,
54+
'limit of selection in time (hours)':COLOC_LIMIT_TIME,
55+
'delta time (minutes)':time_dist_minutes,
56+
'delta space (m)':spatial_dist,
57+
'longitude WW3 spectra':ww3spec[{'time':isel[0]}].longitude.data,
58+
'latitude WW3 spectra':ww3spec[{'time':isel[0]}].latitude.data,
59+
'date WW3 spectra':ww3spec[{'time': myind}].time.data,
60+
'file WW3 spectra':ww3_path
61+
},
62+
name='index of the WW3 spectra selected'
63+
)
64+
65+
66+
return selection
1767

1868

1969
def resampleWW3spectra_on_TOPS_SAR_cartesian_grid(dsar, xspeckind):

0 commit comments

Comments
 (0)