tsutterley / pyTMD

Python-based tidal prediction software
https://pytmd.readthedocs.io
MIT License
135 stars 40 forks source link

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

Open GEOSCIENCELXG opened 6 days ago

GEOSCIENCELXG commented 6 days ago

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 commented 6 days ago

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.