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

Helper for outputting a NetCDF "grid file" #3997

Open
glwagner opened this issue Dec 16, 2024 · 7 comments
Open

Helper for outputting a NetCDF "grid file" #3997

glwagner opened this issue Dec 16, 2024 · 7 comments

Comments

@glwagner
Copy link
Member

After learning a bit about how people work with NetCDF (😺 ) and discussing with @NoraLoose it seems there may be scope for a helper function that writes out a grid file. Something like:

write_grid_to_netcdf!(filename, grid)

Also internally, this functionality could be added to NetCDFOutputWriter, so that users could write something like

ow = NetCDFOutputWriter(model, grid; filename, with_grid_metrics=true)

and include all the same grid metrics that are saved when using write_grid_to_netcdf!.

Under the hood, I think this is accomplished by creating a special NetCDFOutputWriter and treating the grid metrics like output variables. One possible difference is that unlike other kinds of output the grid metrics do not have a time dimension. So we also need to generalize the scope of outputs for NetCDFOutputWriter to outputs that don't have a time dimension? My NetCDF knowledge is a little hazy so please let me know if this isn't right.

@ali-ramadhan @tomchor

@glwagner glwagner changed the title Helper for outputting a "grid file" to NetCDF Helper for outputting a NetCDF "grid file" Dec 16, 2024
@glwagner
Copy link
Member Author

glwagner commented Dec 16, 2024

For extending output utilities to variables that are not time-dependent I think this is just a matter of extending define_output_variable!, which currently assumes that time is a dimension here:

defVar(dataset, name, FT, (dims..., "time"); deflatelevel, attrib)

So perhaps just a kwarg time_dependent=true will do.

save_output! may also have to be generalized:

ds[name][colons..., time_index:time_index] = data

@glwagner
Copy link
Member Author

Each grid will need to define a function that returns a list of all the possible metrics too. Because they are different for each grid. Or do we need to save every location even if they are redundant for some grids?

@NoraLoose
Copy link

NoraLoose commented Dec 16, 2024

Thanks for opening this issue! I don't think the metrics need to be different for different grids. Here is a list of metrics that would be helpful to have in the grid file, assuming we are on a C-grid:

  • x-spacing:
    • centered at T-points
    • centered at u-points
    • centered at v-points
    • centered at vorticity points
  • y-spacing:
    • centered at T-points
    • centered at u-points
    • centered at v-points
    • centered at vorticity points
  • z-spacing (even though the user can easily infer this from zC and zF in case of a z-coordinate system)
    • dzC
    • dzF
  • grid cell area of
    • T-cells
    • u-cells
    • v-cells
    • vorticity cells
  • grid cell volumes
  • masks

The grid cell area could be skipped if we simply have

grid area of P-cell = x-spacing at P-points * y-spacing at P-points

Is the above equation true for all grid types?

Is it also worth outputting the grid cell volume if there are partial cells?

Am I forgetting any useful metrics @tomchor?

@glwagner
Copy link
Member Author

glwagner commented Dec 16, 2024

Is the above equation true for all grid types?

I don't think that is true for a curvilinear grid, for example for LatitudeLongitudeGrid:

@inline Azᶜᶜᵃ(i, j, k, grid::LatitudeLongitudeGrid) = @inbounds grid.radius^2 * deg2rad(grid.Δλᶜᵃᵃ[i]) * (hack_sind(grid.φᵃᶠᵃ[j+1]) - hack_sind(grid.φᵃᶠᵃ[j]))

Azᶜᶜᵃ is our notation for the cell area with a z-normal vector, at the staggered location "Center, Center, Any" in "x, y, z". The third direction is "Any" because the z-areas do not depend on the vertical position, because we make the thin shell approximation.

@glwagner
Copy link
Member Author

Is it also worth outputting the grid cell volume if there are partial cells?

Yes, although in principle because we make the thin shell approximation, one can derive the volume from area * dz. Still though I think it is better to output everything rather than miss a few things and create headaches / complicate user scripts that do analysis.

@tomchor
Copy link
Collaborator

tomchor commented Dec 17, 2024

Is it also worth outputting the grid cell volume if there are partial cells?

I think so. It's simple and it makes users' lives easier :)

Am I forgetting any useful metrics @tomchor?

Not that I can tell. Although I'd standardize and output the same variables for all directions. Oceananigans is very general, so I don't think we should assume a priori that any simulation treats the vertical direction any differently from the others. There'll be some redundancy in the output for lots of simulations, but given how cheap it is to output these metrics, that's okay I think.

@glwagner
Copy link
Member Author

Hmm so the missing metrics are the w locations perhaps then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants