Skip to content

Commit

Permalink
Merge pull request #100 from schism-dev/unittest/bctides
Browse files Browse the repository at this point in the history
Unittest/bctides
  • Loading branch information
cuill authored Oct 12, 2023
2 parents 45795d2 + f5fa0c2 commit fb11bf4
Show file tree
Hide file tree
Showing 34 changed files with 1,460 additions and 2,391 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ hgrid = Hgrid.open('hgrid.gr3')
hgrid.write("/path/to/output/file.2dm", fmt='2dm')
```
---
## Online manual

https://schism-dev.github.io/schism/master/getting-started/pre-processing-with-pyschism/overview.html

## References

If you used this software as part of your work, please use the following citation format.
Expand Down
152 changes: 113 additions & 39 deletions examples/Bctides/gen_bctides.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
from time import time
import os
import argparse
from datetime import datetime, timedelta
import logging
import pathlib
import f90nml
import json

import numpy as np

from pyschism.mesh import Hgrid
from pyschism.forcing.bctides import Bctides, iettype, ifltype, isatype, itetype
from pyschism.forcing.source_sink.nwm import NationalWaterModel, NWMElementPairings
from pyschism.forcing.nws import NWS2, GFS, HRRR
from pyschism.forcing.bctides import Bctides

logging.basicConfig(
format = "[%(asctime)s] %(name)s %(levelname)s: %(message)s",
force=True,
)
logging.getLogger("pyschism").setLevel(logging.DEBUG)

def list_of_strings(arg):
return arg.split(',')

if __name__ == "__main__":

Expand All @@ -21,50 +30,115 @@
database = 'tpxo'
~/.local/share/tpxo/
'''
#setup logging
logging.basicConfig(
format = "[%(asctime)s] %(name)s %(levelname)s: %(message)s",
force=True,
)
logging.getLogger("pyschism").setLevel(logging.DEBUG)
parser = argparse.ArgumentParser(description="Create bctides.in for SCHISM with command-line arguments! e.g. python test_bctides.py hgrid.ll 2014-12-01 397 '[[5,5,4,4],[5,5,4,4],[0,1,1,2]]' major fes2014")

#Add arguments
parser.add_argument('hgrid', type=str, help='hgrid.ll (lon/lat) file')
parser.add_argument('start_date', type=datetime.fromisoformat, help='model startdate')
parser.add_argument('rnday', type=float, help='model rnday')
parser.add_argument('bctypes', type=str, help="JSON format for Flags for each open boundary, '[[5,5,4,4],[5,5,4,4],[0,1,1,2]]'")
parser.add_argument('constituents', type=list_of_strings, help="Choose tidal constituents to be included, major, minor, or list of constituents ['K1', 'O1', 'M2']")
parser.add_argument('database', type=str, help='Tidal datbase: tpxo or fes2014')

start_date = datetime(2017, 12, 1)
rnday = 396
end_date = start_date + timedelta(days=61)
outdir = './'
hgrid = Hgrid.open("./hgrid.gr3", crs="epsg:4326")
#Parse the command-line arguments
args = parser.parse_args()
hgrid_filename = args.hgrid
start_date = args.start_date
rnday = args.rnday
bctypes = args.bctypes
constituents = args.constituents
database = args.database

#elevation
iet3 = iettype.Iettype3(constituents='major', database='fes2014')
iet4 = iettype.Iettype4()
iet5 = iettype.Iettype5(iettype3=iet3, iettype4=iet4)
# Parse the JSON string into a Python data structure
try:
flags = json.loads(bctypes)
print("Parsed bctype list:", flags)
except json.JSONDecodeError:
raise TypeError("Invalid JSON format for bctype list.")

#velocity
ifl1 = ifltype.Ifltype1()
ifl3 = ifltype.Ifltype3(constituents='major', database='fes2014')
ifl4 = ifltype.Ifltype4()
ifl5 = ifltype.Ifltype5(ifltype3=ifl3, ifltype4=ifl4)
#earth tidal potential
add_earth_tidal_potential = input("Would you like to add earth tidal potential? Y/N: ")
if add_earth_tidal_potential.lower() == "y":
earth_tidal_potential = True
else:
earth_tidal_potential = False

#salinity
isa4 = isatype.Isatype4()
isa2 = isatype.Isatype2(5, 1)
#Check if constant values needed
ethconst = []
vthconst = []
tthconst = []
sthconst = []
tobc = []
sobc = []
relax = []

#temperature
ite4 = itetype.Itetype4()
ite2 = itetype.Itetype2(0, 1)
for ibnd, flag in enumerate(flags):
iettype, ifltype, itetype, isatype = [i for i in flag]
if iettype == 2:
val = input(f"Elevation value at boundary {ibnd+1}: ")
ethconst.append(float(val))
else:
ethconst.append(np.nan)

if ifltype == 2:
val = input(f"Discharge value (negative for inflow) at boundary {ibnd+1}: ")
vthconst.append(float(val))
elif ifltype == -4:
val = input(f"Relaxation constants (between 0 and 1) for inflow at boundary {ibnd+1}: ")
relax.append(float(val))
val = input(f"Relaxation constants (between 0 and 1) for outflow at boundary {ibnd+1}: ")
relax.append(float(val))

else:
vthconst.append(np.nan)

if itetype == 2:
val = input(f"Temperature value at boundary {ibnd+1}: ")
tthconst.append(float(val))
val = input(f"Nuding factor (between 0 and 1) for temperature at boundary {ibnd+1}: ")
tobc.append(float(val))
elif itetype == 1 or itetype == 3 or itetype == 4:
tthconst.append(np.nan)
val = input(f"Nuding factor (between 0 and 1) for temperature at boundary {ibnd+1}: ")
tobc.append(float(val))
else:
tthconst.append(np.nan)
tobc.append(np.nan)

if isatype == 2:
val = input(f"Salinity value at boundary {ibnd+1}: ")
sthconst.append(float(val))
val = input(f"Nuding factor (between 0 and 1) for salinity at boundary {ibnd+1}: ")
sobc.append(float(val))
elif isatype == 1 or isatype == 3 or isatype == 4:
sthconst.append(np.nan)
val = input(f"Nuding factor (between 0 and 1) for salinity at boundary {ibnd+1}: ")
sobc.append(float(val))
else:
sthconst.append(np.nan)
sobc.append(np.nan)

outdir = './'
hgrid = Hgrid.open(hgrid_filename, crs="epsg:4326")

bctides=Bctides(
hgrid,
iettype={'1': iet5, '2': iet5},
ifltype={'1': ifl5, '2': ifl5, '3': ifl1},
isatype={'1': isa4, '2':isa4, '3': isa2},
itetype={'1': ite4, '2':ite4, '3': ite2}
hgrid = hgrid,
flags = flags,
constituents = constituents,
database = database,
add_earth_tidal = earth_tidal_potential,
ethconst = ethconst,
vthconst = vthconst,
tthconst = tthconst,
sthconst = sthconst,
tobc = tobc,
sobc = sobc,
relax = relax,
)

bctides.write(
outdir,
start_date=start_date,
end_date=end_date,
bctides=True,
overwrite=True
rnday=rnday,
overwrite=True,
)
31 changes: 31 additions & 0 deletions examples/Sflux/gen_sflux_hrrr2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from time import time
from datetime import datetime
import pathlib

from pyschism.mesh.hgrid import Hgrid
from pyschism.forcing.nws.nws2.hrrr2 import AWSZarrInventory

from pyschism.dates import nearest_cycle

'''
Please use it with caution, some variables may have missing data, see:
https://mesowest.utah.edu/html/hrrr/zarr_documentation/html/zarr_variables.html
'''

if __name__ == "__main__":
t0 = time()
startdate = datetime(2023, 10, 3)

#bbox from hgrid
#hgrid = Hgrid.open('./hgrid.gr3', crs='epsg:4326')
#bbox = hgrid.bbox
#or
##bbox from list [xmin, ymin, xmax, ymax]
bbox = [-98, 7.8, -52.986, 42.09]

outdir = './'

hrrr = AWSZarrInventory(bbox=bbox)
hrrr.write(outdir=outdir, start_date=startdate, rnday=2, air=True, prc=True, rad=False)
print(f'It took {(time() - t0)/60} mins')

6 changes: 3 additions & 3 deletions pyschism/cmd/bctides.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def add_bctides_options_to_parser(parser):
common.add_vgrid_to_parser(parser)
common.add_tidal_constituents_to_parser(parser)
common.add_tidal_database_to_parser(parser)
common.add_baroclinic_database_to_parser(parser)
#common.add_baroclinic_database_to_parser(parser)
common.add_bctides_options_to_parser(parser)
common.add_ibctype_to_parser(parser)
parser.add_argument("--parallel-download", action='store_true')
#common.add_ibctype_to_parser(parser)
#parser.add_argument("--parallel-download", action='store_true')


# add_nudge_to_parser(parser)
Expand Down
2 changes: 1 addition & 1 deletion pyschism/cmd/bootstrap/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ def add_bootstrap_options_to_parser(parser):
)
parser.add_argument('--Z0', type=float)
parser.add_argument("--cutoff-depth", type=float, default=50.0)
common.add_ibctype_to_parser(parser)
#common.add_ibctype_to_parser(parser)
Loading

0 comments on commit fb11bf4

Please sign in to comment.