|
6 | 6 | import numpy as np
|
7 | 7 | import xarray as xr
|
8 | 8 |
|
9 |
| -from slcl1butils.legacy_ocean.ocean.xPolarSpectrum import find_closest_ww3 |
| 9 | +from slcl1butils.legacy_ocean.ocean.xPolarSpectrum import haversine |
10 | 10 |
|
11 | 11 | # from ocean.xspectrum import from_ww3
|
12 | 12 | # from ocean.xPolarSpectrum import find_closest_ww3
|
13 | 13 | from slcl1butils.legacy_ocean.ocean.xspectrum import from_ww3
|
14 | 14 | from slcl1butils.raster_readers import resource_strftime
|
15 | 15 | from slcl1butils.symmetrize_l1b_spectra import symmetrize_xspectrum
|
16 | 16 | 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 |
17 | 67 |
|
18 | 68 |
|
19 | 69 | def resampleWW3spectra_on_TOPS_SAR_cartesian_grid(dsar, xspeckind):
|
|
0 commit comments