Skip to content

Commit

Permalink
Merge pull request #120 from schism-dev/examples
Browse files Browse the repository at this point in the history
Updated example scripts and bugfix for TPXO environment variables
  • Loading branch information
cuill committed Feb 3, 2024
2 parents ac3bcfc + 5d453fc commit fc41b51
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 31 deletions.
4 changes: 4 additions & 0 deletions examples/Bctides/gen_bctides.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from pyschism.mesh import Hgrid
from pyschism.forcing.bctides import Bctides

##Optional: set up environment variable for TPXO files
#os.environ['TPXO_ELEVATION'] = '/path-to-your-hfile/h_tpxo9.v1.nc'
#os.environ['TPXO_VELOCITY'] = '/path-to-your-ufile/u_tpxo9.v1.nc'

logging.basicConfig(
format = "[%(asctime)s] %(name)s %(levelname)s: %(message)s",
force=True,
Expand Down
48 changes: 39 additions & 9 deletions examples/HYCOM/download_hycom.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,46 @@
logger.setLevel(logging.INFO)

if __name__ == '__main__':
hgrid = Hgrid.open('./hgrid.gr3', crs='epsg:4326')
date=datetime(2018, 1, 1)
outdir = './'

##example 1 - download data for IC
#rnday = 1

#hgrid = Hgrid.open('./hgrid.gr3', crs='epsg:4326')
#hycom = DownloadHycom(hgrid=hgrid)

#t0 = time()
#hycom.fetch_data(date, rnday=1, bnd=False, nudge=False, outdir=outdir)
#print(f'It took {(time()-t0)/60} mins to download')

#example 2 - download data for bnd
rnday = 10

xmin = -55
xmax = -50
ymin = 0
ymax = 53
bbox = Bbox.from_extents(xmin, ymin, xmax, ymax)

hycom = DownloadHycom(bbox=bbox)

#xmin = -65
#xmax = -50
#ymin = 0
#ymax = 53
#bbox = Bbox.from_extents(xmin, ymin, xmax, ymax)
hycom = DownloadHycom(hgrid=hgrid)
t0 = time()
hycom.fetch_data(date, rnday=1, bnd=False, nudge=False, sub_sample=3, outdir='./')
hycom.fetch_data(date, rnday=rnday, bnd=True, nudge=False, fmt='schism', outdir=outdir)
print(f'It took {(time()-t0)/60} mins to download')

#"""

"""example 3 - download data for nudge
rnday = 20
xmin = -65
xmax = -50
ymin = 0
ymax = 53
bbox = Bbox.from_extents(xmin, ymin, xmax, ymax)
hycom = DownloadHycom(bbox=bbox)
hycom.fetch_data(date, rnday=rnday, bnd=False, nudge=True, outdir=outdir)
print(f'It took {(time()-t0)/60} mins to download')
"""
8 changes: 4 additions & 4 deletions examples/Sflux/gen_sflux_hrrr3.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
#rndelta = end -start
#rnday = rndelta.total_seconds() / 86400.
start = datetime(2023, 10, 26)
rnday = 2
rnday = 10
hgrid = Hgrid.open('./hgrid.gr3', crs='epsg:4326')
#outdir = path = pathlib.Path('./HRRR_2017_2')
#bbox = Bbox.from_extents(-162, 60.79, -143, 69.1)


#pscr - to save downloaded grib2 files and files will be deleted after nc files are created
pscr='/sciclone/pscr/lcui01/HRRR/'
hrrr = HRRR(level=2, region='alaska', pscr=pscr, bbox=bbox)

hrrr = HRRR(level=2, region='conus', pscr=pscr, bbox=hgrid.bbox)
hrrr.write(start_date=start, rnday=rnday, air=True, prc=True, rad=True)
print(f'It took {(time()-t0)/60} mins to process {rnday} days!')
6 changes: 6 additions & 0 deletions pyschism/forcing/bctides/tpxo.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,11 @@ def h(self):
if self._h_file is None:
self._h_file = pathlib.Path(
appdirs.user_data_dir('tpxo')) / TPXO_ELEVATION
elif type(self._h_file) is str:
self._h_file = pathlib.Path(self._h_file)
if not self._h_file.exists():
raise_missing_file(self._h_file, TPXO_ELEVATION)
logger.info(f'h_file is {self._h_file}')
self._h = Dataset(self._h_file)
return self._h

Expand All @@ -114,8 +117,11 @@ def uv(self):
if self._u_file is None:
self._u_file = pathlib.Path(
appdirs.user_data_dir('tpxo')) / TPXO_VELOCITY
elif type(self._u_file) is str:
self._u_file = pathlib.Path(self._u_file)
if not self._u_file.exists():
raise_missing_file(self._u_file, TPXO_VELOCITY)
logger.info(f'u_file is {self._u_file}')
self._uv = Dataset(self._u_file)
return self._uv

Expand Down
36 changes: 18 additions & 18 deletions pyschism/forcing/hycom/hycom2schism.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,16 +804,16 @@ def fetch_data(self, start_date, rnday=1, fmt='schism', bnd=False, nudge=False,

time_idx, lon_idx1, lon_idx2, lat_idx1, lat_idx2, x2, y2, isLonSame = get_idxs(date, database, self.bbox)

url_ssh = f'https://tds.hycom.org/thredds/dodsC/{database}?lat[{lat_idx1}:{sub_sample}:{lat_idx2}],' + \
f'lon[{lon_idx1}:{sub_sample}:{lon_idx2}],depth[0:1:-1],time[{time_idx}],' + \
f'surf_el[{time_idx}][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}],' + \
f'water_u[{time_idx}][0:1:39][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}],' + \
f'water_v[{time_idx}][0:1:39][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}],' + \
f'water_temp[{time_idx}][0:1:39][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}],' + \
f'salinity[{time_idx}][0:1:39][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}]'

foutname = f'hycom_{date.strftime("%Y%m%d")}.nc'
if fmt == 'schism':
url_ssh = f'https://tds.hycom.org/thredds/dodsC/{database}?lat[{lat_idx1}:{sub_sample}:{lat_idx2}],' + \
f'lon[{lon_idx1}:{sub_sample}:{lon_idx2}],depth[0:1:-1],time[{time_idx}],' + \
f'surf_el[{time_idx}][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}],' + \
f'water_u[{time_idx}][0:1:39][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}],' + \
f'water_v[{time_idx}][0:1:39][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}],' + \
f'water_temp[{time_idx}][0:1:39][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}],' + \
f'salinity[{time_idx}][0:1:39][{lat_idx1}:{sub_sample}:{lat_idx2}][{lon_idx1}:{sub_sample}:{lon_idx2}]'

foutname = f'hycom_{date.strftime("%Y%m%d")}.nc'
#foutname = f'TS_{i+1}.nc'
logger.info(f'filename is {foutname}')
ds = xr.open_dataset(url_ssh)
Expand Down Expand Up @@ -862,15 +862,15 @@ def fetch_data(self, start_date, rnday=1, fmt='schism', bnd=False, nudge=False,
os.symlink(src, dst)

elif fmt == 'hycom':
url=f'https://tds.hycom.org/thredds/dodsC/{database}?lat[{lat_idx1}:1:{lat_idx2}],' + \
f'lon[{lon_idx1}:1:{lon_idx2}],depth[0:1:-1],time[{time_idx}],' + \
f'surf_el[{time_idx}][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}],' + \
f'water_temp[{time_idx}][0:1:39][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}],' + \
f'salinity[{time_idx}][0:1:39][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}],' + \
f'water_u[{time_idx}][0:1:39][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}],' + \
f'water_v[{time_idx}][0:1:39][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}]'

foutname = f'hycom_{date.strftime("%Y%m%d")}.nc'
#url=f'https://tds.hycom.org/thredds/dodsC/{database}?lat[{lat_idx1}:1:{lat_idx2}],' + \
# f'lon[{lon_idx1}:1:{lon_idx2}],depth[0:1:-1],time[{time_idx}],' + \
# f'surf_el[{time_idx}][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}],' + \
# f'water_temp[{time_idx}][0:1:39][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}],' + \
# f'salinity[{time_idx}][0:1:39][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}],' + \
# f'water_u[{time_idx}][0:1:39][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}],' + \
# f'water_v[{time_idx}][0:1:39][{lat_idx1}:1:{lat_idx2}][{lon_idx1}:1:{lon_idx2}]'

#foutname = f'hycom_{date.strftime("%Y%m%d")}.nc'
ds = xr.open_dataset(url)
ds.to_netcdf(foutname, 'w')

Expand Down

0 comments on commit fc41b51

Please sign in to comment.