Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: cannot reshape array of size 175089973 into shape (1853423616,2) #364

Open
GEOSCIENCELXG opened this issue Nov 20, 2024 · 1 comment

Comments

@GEOSCIENCELXG
Copy link

Dear pyTMDers,

I want to get the history tide signals at specific location.
like bellow code: but get some errors.

`import numpy as np
import matplotlib.pyplot as plt
import pyTMD.io
import pyTMD.predict
import pyTMD.tools
import timescale.time
import datetime

Set parameters

latitude = 30 # Latitude
longitude = 18 # Longitude
time = np.arange('2017-01-01', '2022-10-01', dtype='datetime64[h]') # Time series
data = 2.0 * np.sin(2 * np.pi * time.astype('float') / 24.0) + 1.0 + np.random.normal(size=len(time)) # Simulated data with tidal signal and noise

Set tide model parameters

model_dir = '/data1/NorthIceland/tidemodels'
model_name = 'TPXO9-atlas-v5-nc'

Get tide model

model = pyTMD.io.model(model_dir).elevation(model_name)

Extract tidal constants

amp, ph, D, c = pyTMD.io.OTIS.extract_constants([longitude], [latitude], model.grid_file,
model.model_file, model.projection, type=model.type,
crop=True, method='spline', grid=model.file_format)
DELTAT = np.zeros_like(time)

Compute solid and ocean tidal disturbances

cph = -1j * ph * np.pi / 180.0 # Compute complex phase
hc = amp * np.exp(cph) # Compute tidal amplitude

Use pyTMD for tide prediction

tidal_signal = np.zeros_like(time)
for i, t in enumerate(time):
ts_current = timescale.time.Timescale().from_datetime(t)
TIDE = pyTMD.predict.map(ts_current.tide[0], hc, c, deltat=DELTAT[i], corrections=model.corrections)
tidal_signal[i] = np.mean(TIDE) * 100 # In centimeters

Remove tidal signal from original data

data_corrected = data - tidal_signal

Plot the results

plt.figure(figsize=(10, 6))
plt.plot(time, data, label='Original Data')
plt.plot(time, tidal_signal, label='Tidal Signal')
plt.plot(time, data_corrected, label='Data After Removing Tidal Signal')
plt.xlabel('Time')
plt.ylabel('Height (m)')
plt.legend()
plt.show()
`
Traceback (most recent call last):
amp, ph, D, c = pyTMD.io.OTIS.extract_constants([longitude], [latitude], model.grid_file,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "miniforge3/envs/disstans/lib/python3.12/site-packages/pyTMD/io/OTIS.py", line 276, in extract_constants
xi,yi,hz,mz,iob,dt = read_otis_grid(grid_file)
^^^^^^^^^^^^^^^^^^^^^^^^^
File "miniforge3/envs/disstans/lib/python3.12/site-packages/pyTMD/io/OTIS.py", line 954, in read_otis_grid
iob=np.fromfile(fid, dtype=np.dtype('>i4'), count=2*nob).reshape(nob, 2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: cannot reshape array of size 175089973 into shape (1853423616,2)

anyone can help solve this problem?

@tsutterley
Copy link
Owner

hey @GEOSCIENCELXG,
Thanks for all the info! I believe what's going on is that TPXO9-atlas-v5-nc is a netCDF4 model and the OTIS reader wants binary files. So in this case I think you should be using io.ATLAS instead of io.OTIS. If you're using the latest version of the software, I've simplified the io functionality to add an accessor to io.model.

model = pyTMD.io.model(model_dir).elevation(model_name)
amp, ph, c = model.extract_constants(longitude, latitude)

Hope this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants