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

Set crop parameter default to False #549

Merged
merged 2 commits into from
Oct 1, 2024
Merged

Set crop parameter default to False #549

merged 2 commits into from
Oct 1, 2024

Conversation

vikineema
Copy link
Collaborator

Proposed changes

Include a brief description of the changes being proposed, and why they are necessary.

Checklist (replace [ ] with [x] to check off)

  • Remove any unused Python packages from Load packages
  • Remove any unused/empty code cells
  • Remove any guidance cells (e.g. General advice)
  • Ensure that all code cells follow the PEP8 standard for code. The jupyterlab_code_formatter tool can be used to format code cells to a consistent style: select each code cell, then click Edit and then one of the Apply X Formatter options (YAPF or Black are recommended)
  • Include relevant tags in the first notebook cell and re-use tags if possible
  • Ensure appropriate colour schemes have been used to maximise accessibility for vision impairment. Test your images or learn more with Coblis or TPGI
  • Clear all outputs, run notebook from start to finish, and save the notebook in the state where all cells have been sequentially evaluated

Closes issues (optional)

  • Closes Issue #000

@vikineema vikineema merged commit cd7a921 into main Oct 1, 2024
1 check passed
@robbibt
Copy link
Contributor

robbibt commented Oct 1, 2024

Hey @vikineema! Any chance you could share any extra information about this change, e.g. reasons for turning it off by default? I'd love to know if you've had any bad experiences with this functionality or any feedback that I could use to improve it (it should make larger analyses a lot quicker).

@vikineema
Copy link
Collaborator Author

vikineema commented Oct 3, 2024

Hi @robbibt When the default for the crop parameter of the model_tides function is True a couple of the notebooks fail. This includes:

  • The Mangrove_Analysis.ipynb notebook in the Real_world_examples folder fails with the following error:
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[11], line 2
      1 # Calculate tides for each timestep in the satellite dataset
----> 2 ds = tidal_tag(ds=ds, tidepost_lat=tidepost_lat, tidepost_lon=tidepost_lon)
      4 # Print the output dataset with new `tide_m` variable
      5 print(ds)

File ~/.local/lib/python3.10/site-packages/deafrica_tools/coastal.py:595, in tidal_tag(ds, ebb_flow, swap_dims, tidepost_lat, tidepost_lon, return_tideposts, **model_tides_kwargs)
    593 model = "FES2014" if "model" not in model_tides_kwargs else model_tides_kwargs["model"]
    594 print(f"Modelling tides using {model} tidal model")
--> 595 tide_df = model_tides(
    596     x=tidepost_lon,
    597     y=tidepost_lat,
    598     time=ds.time,
    599     epsg="EPSG:4326",
    600     **model_tides_kwargs,
    601 )
    603 # If tides cannot be successfully modeled (e.g. if the centre of the
    604 # xarray dataset is located is over land), raise an exception
    605 if tide_df.tide_m.isnull().all():

File ~/.local/lib/python3.10/site-packages/deafrica_tools/coastal.py:227, in model_tides(x, y, time, model, directory, epsg, crop, method, extrapolate, cutoff)
    224     deltat = timescale.time.interpolate_delta_time(delta_file, t)
    226 elif model.format in ("FES-netcdf", "FES-ascii"):
--> 227     amp, ph = pyTMD.io.FES.extract_constants(
    228         lon,
    229         lat,
    230         model.model_file,
    231         type=model.type,
    232         crop=crop,
    233         version=model.version,
    234         method=method,
    235         extrapolate=extrapolate,
    236         cutoff=cutoff,
    237         scale=model.scale,
    238         compressed=model.compressed,
    239     )
    241     # Available model constituents
    242     c = model.constituents

File [/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py:246](https://sandbox.digitalearth.africa/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py#line=245), in extract_constants(ilon, ilat, model_files, **kwargs)
    243 # crop tide model data to (buffered) bounds
    244 # or adjust longitudinal convention to fit tide model
    245 if kwargs['crop'] and np.any(kwargs['bounds']):
--> 246     hc, lon, lat = _crop(hc, lon, lat,
    247         bounds=kwargs['bounds'],
    248         buffer=4*dlon
    249     )
    250 elif (np.min(ilon) < 0.0) & (np.max(lon) > 180.0):
    251     # input points convention (-180:180)
    252     # tide model convention (0:360)
    253     ilon[ilon<0.0] += 360.0

File [/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py:954](https://sandbox.digitalearth.africa/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py#line=953), in _crop(input_matrix, ilon, ilat, bounds, buffer)
    952 # slices for cropping axes
    953 rows = slice(yind[0], yind[-1]+1)
--> 954 cols = slice(xind[0], xind[-1]+1)
    955 # crop matrix
    956 temp = input_matrix[rows, cols]

IndexError: index 0 is out of bounds for axis 0 with size 0
  • The Tidal_modelling.ipynb notebook in the Real_world_examples folder fails with the following error:
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[7], line 2
      1 # Model tide heights
----> 2 ds_tidal = tidal_tag(ds)
      4 # Print output data
      5 print(ds_tidal)

File ~/.local/lib/python3.10/site-packages/deafrica_tools/coastal.py:595, in tidal_tag(ds, ebb_flow, swap_dims, tidepost_lat, tidepost_lon, return_tideposts, **model_tides_kwargs)
    593 model = "FES2014" if "model" not in model_tides_kwargs else model_tides_kwargs["model"]
    594 print(f"Modelling tides using {model} tidal model")
--> 595 tide_df = model_tides(
    596     x=tidepost_lon,
    597     y=tidepost_lat,
    598     time=ds.time,
    599     epsg="EPSG:4326",
    600     **model_tides_kwargs,
    601 )
    603 # If tides cannot be successfully modeled (e.g. if the centre of the
    604 # xarray dataset is located is over land), raise an exception
    605 if tide_df.tide_m.isnull().all():

File ~/.local/lib/python3.10/site-packages/deafrica_tools/coastal.py:227, in model_tides(x, y, time, model, directory, epsg, crop, method, extrapolate, cutoff)
    224     deltat = timescale.time.interpolate_delta_time(delta_file, t)
    226 elif model.format in ("FES-netcdf", "FES-ascii"):
--> 227     amp, ph = pyTMD.io.FES.extract_constants(
    228         lon,
    229         lat,
    230         model.model_file,
    231         type=model.type,
    232         crop=crop,
    233         version=model.version,
    234         method=method,
    235         extrapolate=extrapolate,
    236         cutoff=cutoff,
    237         scale=model.scale,
    238         compressed=model.compressed,
    239     )
    241     # Available model constituents
    242     c = model.constituents

File [/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py:246](https://sandbox.digitalearth.africa/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py#line=245), in extract_constants(ilon, ilat, model_files, **kwargs)
    243 # crop tide model data to (buffered) bounds
    244 # or adjust longitudinal convention to fit tide model
    245 if kwargs['crop'] and np.any(kwargs['bounds']):
--> 246     hc, lon, lat = _crop(hc, lon, lat,
    247         bounds=kwargs['bounds'],
    248         buffer=4*dlon
    249     )
    250 elif (np.min(ilon) < 0.0) & (np.max(lon) > 180.0):
    251     # input points convention (-180:180)
    252     # tide model convention (0:360)
    253     ilon[ilon<0.0] += 360.0

File [/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py:954](https://sandbox.digitalearth.africa/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py#line=953), in _crop(input_matrix, ilon, ilat, bounds, buffer)
    952 # slices for cropping axes
    953 rows = slice(yind[0], yind[-1]+1)
--> 954 cols = slice(xind[0], xind[-1]+1)
    955 # crop matrix
    956 temp = input_matrix[rows, cols]

IndexError: index 0 is out of bounds for axis 0 with size 0
  • The Intertidal_elevation.ipynb notebook in the Real_world_examples folder fails with the following error:
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[11], line 2
      1 # Calculate tides for each timestep in the satellite dataset
----> 2 landsat_ds = tidal_tag(ds=landsat_ds, tidepost_lat=None, tidepost_lon=None)
      4 # Print the output dataset with new `tide_m` variable
      5 print(landsat_ds)

File ~/.local/lib/python3.10/site-packages/deafrica_tools/coastal.py:595, in tidal_tag(ds, ebb_flow, swap_dims, tidepost_lat, tidepost_lon, return_tideposts, **model_tides_kwargs)
    593 model = "FES2014" if "model" not in model_tides_kwargs else model_tides_kwargs["model"]
    594 print(f"Modelling tides using {model} tidal model")
--> 595 tide_df = model_tides(
    596     x=tidepost_lon,
    597     y=tidepost_lat,
    598     time=ds.time,
    599     epsg="EPSG:4326",
    600     **model_tides_kwargs,
    601 )
    603 # If tides cannot be successfully modeled (e.g. if the centre of the
    604 # xarray dataset is located is over land), raise an exception
    605 if tide_df.tide_m.isnull().all():

File ~/.local/lib/python3.10/site-packages/deafrica_tools/coastal.py:227, in model_tides(x, y, time, model, directory, epsg, crop, method, extrapolate, cutoff)
    224     deltat = timescale.time.interpolate_delta_time(delta_file, t)
    226 elif model.format in ("FES-netcdf", "FES-ascii"):
--> 227     amp, ph = pyTMD.io.FES.extract_constants(
    228         lon,
    229         lat,
    230         model.model_file,
    231         type=model.type,
    232         crop=crop,
    233         version=model.version,
    234         method=method,
    235         extrapolate=extrapolate,
    236         cutoff=cutoff,
    237         scale=model.scale,
    238         compressed=model.compressed,
    239     )
    241     # Available model constituents
    242     c = model.constituents

File [/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py:246](https://sandbox.digitalearth.africa/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py#line=245), in extract_constants(ilon, ilat, model_files, **kwargs)
    243 # crop tide model data to (buffered) bounds
    244 # or adjust longitudinal convention to fit tide model
    245 if kwargs['crop'] and np.any(kwargs['bounds']):
--> 246     hc, lon, lat = _crop(hc, lon, lat,
    247         bounds=kwargs['bounds'],
    248         buffer=4*dlon
    249     )
    250 elif (np.min(ilon) < 0.0) & (np.max(lon) > 180.0):
    251     # input points convention (-180:180)
    252     # tide model convention (0:360)
    253     ilon[ilon<0.0] += 360.0

File [/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py:954](https://sandbox.digitalearth.africa/usr/local/lib/python3.10/dist-packages/pyTMD/io/FES.py#line=953), in _crop(input_matrix, ilon, ilat, bounds, buffer)
    952 # slices for cropping axes
    953 rows = slice(yind[0], yind[-1]+1)
--> 954 cols = slice(xind[0], xind[-1]+1)
    955 # crop matrix
    956 temp = input_matrix[rows, cols]

IndexError: index 0 is out of bounds for axis 0 with size 0

@robbibt
Copy link
Contributor

robbibt commented Oct 3, 2024

Neat, thankyou! Will investigate 🙂

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

Successfully merging this pull request may close these issues.

3 participants