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

ValueError while trying to filter with fixed factor, using MOM5 #162

Closed
marimpacheco opened this issue Sep 13, 2023 · 12 comments
Closed

ValueError while trying to filter with fixed factor, using MOM5 #162

marimpacheco opened this issue Sep 13, 2023 · 12 comments
Labels
question Further information is requested

Comments

@marimpacheco
Copy link

Hi,

I want to use gcm-filters to filter a lat/lon(3S-46S/132W-68W) section of the GFDL CM2.6 model (MOM5, 0.1°) using a filter with fixed factor (spatially-varying and anisotropic filter), and with a 15° longitude, 5° latitude cut-off wavelength. I want to filter tracers (temp, o2, ph).

Since MOM5 is NE-oriented, I selected the dxu, dxt, dyu and dyt accordingly so dxu would be located southwest of dxt (not sure if that is necessary? or a wrong way to proceed?).

Then I followed all the steps in the example and choosing as filter scale:

dx_max = max(dxu.max(),dyu.max(),dxt.max(),dyt.max())
dx_max = dx_max.values

filter_scale = 150 * dx_max # 15 degrees in longitude; each cell is 0.1 degree
kappa_w = dxt * dxt / (dx_max * dx_max)
kappa_s =  (1/900) * dyt * dyt / (dx_max * dx_max) # 5 degrees in latitude

filter_fixed_factor = gcm_filters.Filter(
    filter_scale=filter_scale,
    dx_min=dx_min,
    filter_shape=gcm_filters.FilterShape.GAUSSIAN,
    grid_type=gcm_filters.GridType.IRREGULAR_WITH_LAND,
    grid_vars={
        'wet_mask': wet_mask, 
        'dxw': dxt, 'dyw': dyt, 'dxs': dxu, 'dys': dyu, 'area': area, 
        'kappa_w': kappa_w, 'kappa_s': kappa_s
    }
)
filter_fixed_factor
Filter(filter_scale=1665563.4834155159, dx_min=array(7591.0808314), filter_shape=<FilterShape.GAUSSIAN: 1>, transition_width=3.141592653589793, ndim=2, n_steps=242, grid_type=<GridType.IRREGULAR_WITH_LAND: 5>)

However, when I try to run the filtering, I get the error: ValueError: At least one place in the domain must have either kappa_w = 1.0 or kappa_s = 1.Otherwise the filter's scale will not be equal to filter_scale anywhere in the domain.

But:

kappa_w.max()
1

If I add the (1/900) * to the kappa_s and remove it from kappa_w, it works.

How should I solve this? Thanks!

@marimpacheco marimpacheco added the question Further information is requested label Sep 13, 2023
@NoraLoose
Copy link
Member

Hi @marimpacheco, thanks for reaching out! I think I came across the same problem a while ago, which is why I opened PR #161. Let's merge the PR and see if this solves your problem.

@marimpacheco
Copy link
Author

Hi Nora, I reinstalled gcm-filters, but unfortunately the problem remains

@iangrooms
Copy link
Member

@marimpacheco did you install directly from the website? The changes have not been pushed to conda or pip yet, so if you re-installed from there you wouldn't have gotten the updates

@marimpacheco
Copy link
Author

Ah right, from the commit it works, thanks!

@marimpacheco
Copy link
Author

Hi, I have a question on how to adjust the dxw/dyw/dxs/dys values using MOM5 with the grid_type=gcm_filters.GridType.IRREGULAR_WITH_LAND, since I think I was taking the wrong approach.

In issue #24 @NoraLoose mentioned we should set:
dxw = 0.5 * (np.roll(dxt, 1, axis=1) + np.roll(np.roll(dxt, 1, axis=1), 1, axis=0)), and similarly for dyw, dxs, dys (assuming that your coordinates have the order (j,i)).
I assume the same equation applies to dyw, dyt, but I am not sure for dxs and dys.
Is that something like:
dxs = 0.5 * ( dxu + np.roll(dxu, -1, axis=0)) ?
Thanks!

@NoraLoose
Copy link
Member

Hi @marimpacheco!

If you have MOM5 data, I recommend using the grid types gcm_filters.GridType.MOM5U and gcm_filters.GridType.MOM5T. That way, you don't have to compute any of the grid data; instead you can just input the native grid information that comes with the model output.

Let me know if that is helpful, or if you still want to use the gcm_filters.GridType.IRREGULAR_WITH_LAND grid instead.

@marimpacheco
Copy link
Author

I need to use the IRREGULAR_WITH_LAND in order to use a fixed factor filter. gcm_filters.GridType.MOM5T and gcm_filters.GridType.MOM5U do not have kappa_w and kappa_s. Or is there another way?

@NoraLoose
Copy link
Member

True, in that case the IRREGULAR_WITH_LAND grid type is the one to go.

I recommend to use the python package xgcm to compute the necessary grid information.

import xgcm
    
coords = {
    "X": {"center": "xt_ocean", "right": "xu_ocean"},
    "Y": {"center": "yt_ocean", "right": "yu_ocean"}
}
    
metrics = {
    ('X',): ['dxu', 'dxt'], # X distances
    ('Y',): ['dyu', 'dyt'], # Y distances
}
    
grid = Grid(ds_grid, coords=coords, metrics=metrics, periodic=['X', 'Y'])

where ds_grid contains the grid variables of your MOM5 configuration.

Next, you can usegrid.interp to create dxw from dxt etc.

If you run into more issues, feel free to share a minimal example notebook. Hope this helps!

@NoraLoose
Copy link
Member

Another (easier) option is to use a simple fixed factor filter with REGULAR_WITH_LAND_AREA_WEIGHTED. This grid type only requires grid variables that should be directly available from the MOM5 output.

https://gcm-filters.readthedocs.io/en/latest/examples/example_filter_types.html#simple-fixed-factor-filter

@marimpacheco
Copy link
Author

Ah ok, thank you very much! I'll try with xgcm :)

@iangrooms
Copy link
Member

@marimpacheco did xgcm resolve your problem? Can we close this issue?

@marimpacheco
Copy link
Author

Yes, it did, thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants