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

Error writing bctides.in when using add_Z0 #122

Open
VHernaman opened this issue Mar 7, 2024 · 2 comments
Open

Error writing bctides.in when using add_Z0 #122

VHernaman opened this issue Mar 7, 2024 · 2 comments

Comments

@VHernaman
Copy link

Hi, since v0.0.10 there has been support for adding Z0 in bctides.in, but now using 0.1.15 I get an error when I write bctides.in having added Z0. The error is:
KeyError: 'Z0'

From the code I can't see 0.1.15 requires a different way of handling it, so not sure if this is a bug? I see Z0 is still accounted for in tides.py, so maybe it is a bug with bctides.write?

#For example, I instantiate an instance of bctides and then use add_Z0 to add Z0 to my list of constituents
bctides.tides.add_Z0(0.9)
#Check that Z0 has been added:
bctides.tides.get_active_forcing_constituents()
#returns
['M2', 'S2', 'N2', 'K2', 'K1', 'O1', 'P1', 'Q1', 'Z0']

#write it out
bctides.write(
outdir,
start_date=start_date,
rnday=30,
overwrite=True,
)

#But I get this error

KeyError Traceback (most recent call last)
Cell In[36], line 1
----> 1 bctides.write(
2 outdir,
3 start_date=start_date,
4 rnday=30,
5 overwrite=True,
6 )

File /srv/envs/cmextremes/.venv/lib/python3.11/site-packages/pyschism/forcing/bctides/bctides.py:265, in Bctides.write(self, output_directory, start_date, rnday, overwrite)
263 raise IOError("path exists and overwrite is False")
264 with open(bctides, "w") as f:
--> 265 f.write(str(self))

File /srv/envs/cmextremes/.venv/lib/python3.11/site-packages/pyschism/forcing/bctides/bctides.py:93, in Bctides.str(self)
91 if self.nbfr > 0:
92 for constituent in self.tides.get_active_forcing_constituents():
---> 93 forcing = self.tides(self.start_date, self.rnday, constituent)
94 f.append(
95 " ". join([
96 f"{constituent}\n",
(...)
101 ])
102 )
104 #get amplitude and phase for each open boundary

File /srv/envs/cmextremes/.venv/lib/python3.11/site-packages/pyschism/forcing/bctides/tides.py:119, in Tides.call(self, start_date, rnday, constituent)
111 """Returns the tidal characteristic values for a given time period.
112 """
113 _logger.debug(
114 f'Computing tidal factors for start_date={start_date} and '
115 f'constituent {constituent}')
116 return (self.get_tidal_species_type(constituent),
117 self.get_tidal_potential_amplitude(constituent), # TPK
118 self.get_orbital_frequency(constituent), # FF*
--> 119 self.get_nodal_factor(start_date, rnday, constituent), # Amig*
120 self.get_greenwich_factor(start_date, rnday, constituent))

File /srv/envs/cmextremes/.venv/lib/python3.11/site-packages/pyschism/forcing/bctides/tides.py:263, in Tides._manage_dates..decorator(self, start_date, rnday, constituent)
262 def decorator(self, start_date, rnday, constituent):
--> 263 val = f(self, start_date, rnday, constituent)
264 if hasattr(self, 'start_date_utc'):
265 del(self.start_date_utc)

File /srv/envs/cmextremes/.venv/lib/python3.11/site-packages/pyschism/forcing/bctides/tides.py:276, in Tides.get_nodal_factor(self, start_date, rnday, constituent)
271 @_manage_dates
272 def get_nodal_factor(self, start_date: datetime,
273 rnday: Union[float, timedelta],
274 constituent: str):
275 if constituent not in ALL_CONSTITUENTS:
--> 276 return self._nodal_factors[constituent]
277 if start_date.tzinfo is not None and
278 start_date.tzinfo.utcoffset(start_date) is not None:
279 self.start_date_utc = start_date.astimezone(timezone(timedelta(0)))

KeyError: 'Z0'

@SorooshMani-NOAA
Copy link
Contributor

This looks like an issue introduced in d815c9e. I'm not sure if just adding Z0 to ALL_CONSTITUENTS is the right fix or not, but alternatively one can use

def add_constituent(
in the current version of the code instead.

@mymyrere
Copy link

Hi,
I think it is similar problem, I see in the newer version a line has been added:
https://github.com/schism-dev/pyschism/blob/main/pyschism/forcing/bctides/tides.py#L275
but if the constituent is L2 for exemple, it is not in ALL_CONSTITUENTS but it is still define in:
https://github.com/schism-dev/pyschism/blob/main/pyschism/forcing/bctides/tides.py#L351

should line 275 be deleted?

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

3 participants