Skip to content

Commit

Permalink
Add missing_value option to save_data function
Browse files Browse the repository at this point in the history
  • Loading branch information
koldunovn committed Feb 29, 2024
1 parent 6ab4ddb commit 331c219
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/fint/fint.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ def save_data(
lon,
lat,
out_path,
missing_value=None,
):
"""
Saves the interpolated data to a NetCDF file.
Expand Down Expand Up @@ -477,6 +478,10 @@ def save_data(
else:
out_time = np.atleast_1d(data.time.data[timesteps])

if missing_value is not None:
interpolated3d = np.where(np.isnan(interpolated3d), missing_value, interpolated3d)


out1 = xr.Dataset(
{variable_name: (["time", "depth", "lat", "lon"], interpolated3d)},
coords={
Expand Down Expand Up @@ -678,6 +683,12 @@ def fint(args=None):
Valid units are 'D' (days), 'h' (hours), 'm' (minutes), 's' (seconds). \
To substract timedelta, put argument in quotes, and prepend ' -', so SPACE and then -, e.g. ' -10D'.",
)
parser.add_argument(
"--missing_value",
default=None,
type=float,
help="Missing value for the output file. Default is None.",
)

args = parser.parse_args()

Expand Down Expand Up @@ -1065,6 +1076,7 @@ def fint(args=None):
lon,
lat,
out_path,
missing_value=args.missing_value,
)
if args.rotate:
save_data(
Expand All @@ -1079,6 +1091,7 @@ def fint(args=None):
lon,
lat,
out_path2,
missing_value=args.missing_value,
)


Expand Down
9 changes: 9 additions & 0 deletions test/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tasks:
- task: 3d_nodes_interfaces
- task: 2d_elements
- task: timedelta
- task: missing_value
- task: cleanup

download_test_data:
Expand Down Expand Up @@ -153,6 +154,14 @@ tasks:
- fint ${FILE} ${MESH} ${INFL} -t 0:10 --timedelta '1h'
- fint ${FILE} ${MESH} ${INFL} -t 0:10 --timedelta ' -1h'

missing_value:
deps:
- download_test_data
env:
FILE: "./test/data/ssh.fesom.1948.nc"
cmds:
- fint ${FILE} ${MESH} ${INFL} -t 0:3 --missing_value -9999

cleanup:
cmds:
- python -c "import os; [os.remove(f) for f in os.listdir() if f.endswith('.nc')]"
Expand Down

0 comments on commit 331c219

Please sign in to comment.