schism-dev / pyschism

Python interface for handling the SCHISM model.
https://schism-dev.github.io/schism/master/getting-started/pre-processing-with-pyschism/overview.html
Apache License 2.0
25 stars 21 forks source link

Error writing bctides.in when using add_Z0 #122

Open VHernaman opened 8 months ago

VHernaman commented 8 months ago

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 commented 8 months ago

This looks like an issue introduced in https://github.com/schism-dev/pyschism/commit/d815c9ea6e03b4aab0614a986d6b6316385e02fb. I'm not sure if just adding Z0 to ALL_CONSTITUENTS is the right fix or not, but alternatively one can use https://github.com/schism-dev/pyschism/blob/fc41b51cc751d9b64a75c91fac48e6a7f0a27e93/pyschism/forcing/bctides/tides.py#L192 in the current version of the code instead.

mymyrere commented 7 months ago

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?