openearth / oceanwaves-python

Toolbox to handle ocean waves datasets
http://oceanwaves.readthedocs.io
MIT License
64 stars 39 forks source link

Problems writing nontationary SWAN 2D spec files #20

Open mwandres opened 6 years ago

mwandres commented 6 years ago

Hi,

First of all thanks for this great Toolbox and sorry about the rather long message. I'm having issues trying to write nonstationary 2D spectra files for SWAN and I'm not sure if I'm doing something wrong or if there's a bug but also haven't dug into the code too much.

The first issues I'm having is to include nonstat directions into my spectra:

frequency = np.arange(0.03,1.,.01) direction = np.arange(0,360,1) time = [datetime(2013,12,1), datetime(2013,12,2)] Hs = [3.,5.] Tp = [11.,13.] theta_p = [200,250]

ow = oceanwaves.OceanWaves(time = time)#, frequency = frequency, direction = direction) ow['_energy'].values = Hs ow_spc = ow.as_spectral(frequency,Tp = Tp) ow_dir = ow_spc.as_directional(direction, theta_peak = theta_p)

Gives me the following error

C:\ProgramData\Anaconda3\lib\site-packages\oceanwaves\oceanwaves.py:206: RuntimeWarning: invalid value encountered in greater if self._isvalid(frequency, mask=frequency>0) and spectral: Traceback (most recent call last):

File "", line 11, in ow_dir = ow_spc.as_directional(direction, theta_peak = theta_p)

File "C:\ProgramData\Anaconda3\lib\site-packages\oceanwaves\oceanwaves.py", line 883, in as_directional expand_dims=ix_direction

File "C:\ProgramData\Anaconda3\lib\site-packages\oceanwaves\utils.py", line 85, in expand_and_repeat 'dimensions (%d).' % (len(shape) - mtx.ndim, len(expand_dims)))

ValueError: Dimensionality of the target shape minus the number of matrix dimensions (2) should match the number of expanding dimensions (1).

Not sure why it won't allow me to enter 2 different values of theta_p?

Irrespective of that - if I leave theta_p alone and just do

ow_dir = ow_spc.as_directional(direction)

It works but if I go on to try to do

ow_dir.to_swan('TEST.SP2')

I get

C:\ProgramData\Anaconda3\lib\site-packages\oceanwaves\oceanwaves.py:206: RuntimeWarning: invalid value encountered in greater if self._isvalid(frequency, mask=frequency>0) and spectral: Traceback (most recent call last):

File "", line 14, in ow_dir.to_swan('TEST.SP2')

File "C:\ProgramData\Anaconda3\lib\site-packages\oceanwaves\swan.py", line 556, in call self.write(fpath)

File "C:\ProgramData\Anaconda3\lib\site-packages\oceanwaves\swan.py", line 563, in write k = self._key_lookup('_time')

AttributeError: 'SwanSpcWriter' object has no attribute '_key_lookup'

I had a look at the source code swan.py as this is where the error appears to be and I think line 563 should be

k = self.obj._key_lookup('_time') rather than k = self._key_lookup('_time')

When changing that it actually writes out my .SP2 file however it splits them in TEST_000.SP2 and TEST_001.SP2. Both files have the long header with the direction and frequency information but without any time info. Any idea what's happening or any suggestions what I could do differently?

Any help is much appreciated.

hoonhout commented 6 years ago

Hi,

Thanks for using the oceanwaves toolbox! I'm afraid both issues are bugs. And both scenarios are not tested. So, thanks for providing tests :-)

I will have to look deeper into these issues. This might take some time as I just switched jobs.

You can also look into these issues yourself and submit a pull request. I'm happy to guide you along. I agree with your patch regarding swan.py:563. I expect you to be able to solve the writing issues quite easy. The dimensionality issue might me tougher. Is the result of the as_spectral() method accurate?

Bas