NOAA-ORR-ERD / gridded

A single API for accessing / working with gridded model results on multiple grid types
https://noaa-orr-erd.github.io/gridded/index.html
The Unlicense
64 stars 14 forks source link

tutorial page empty #51

Open SBFRF opened 4 years ago

SBFRF commented 4 years ago

Sorry to be annoying, but would be nice to have the tutorial page populated as well.

https://noaa-orr-erd.github.io/gridded/tutorial.html

k-a-mendoza commented 1 month ago

Gosh yeah this is so basic.

I would love to see a tutorial on converting numpy array data to UGRIDS, especially due to its relevance to my own work. However, without tutorials, I have no idea how to go from the basic in-memory scipy/numpy meshing stack to outside conventions.

Here are some basic grid types that I can envision tutorials for. Someone more knowledgeable than I should try to build these out.

(1) Regular Square Grid, 2D

import numpy as np

def make_mesh():
    x = np.linspace(-10,10,num=21)
    y = np.linspace(-10,10,num=21)
    xx, yy = np.meshgrid(x,y)
    return xx,yy 

def some_scalar_function(x,y):
    field = np.cos(xx/np.pi) * np.sin(yy/np.pi)
    return field

mesh_x, mesh_y  = make_mesh()
field = some_scalar_function(mesh_x, mesh_y)
"""
Do your thing Gridded..
"""

(2) Logically rectilinear Grid, 2D

import numpy as np

def make_mesh():
    x = np.logspace(0,2,num=21)
    y = np.linspace(-10,10,num=21)
    xx, yy = np.meshgrid(x,y)
    return xx,yy 

def some_scalar_function(x,y):
    field = np.cos(xx/np.pi) * np.sin(yy/np.pi)
    return field

mesh_x, mesh_y  = make_mesh()
field = some_scalar_function(mesh_x, mesh_y)
"""
Do your thing Gridded..
"""

(3) Deformed Logically Rectilinear Grid, 2D

import numpy as np

def make_mesh():
    x = np.linspace(-10,10,num=21)
    y = np.linspace(-10,10,num=21)
    xx, yy = np.meshgrid(x,y)
    return xx,yy 

def deform_grid(x,y):
    x0 = 0
    y0 = 0
    sig_x = np.std(x)
    sig_y = np.std(y)

    factor = 0.3
    ratio   = 0.5
    x+= factor*(1-ratio) * np.exp( ((x-x0)/sig_x)**2 + ((y-y0)/sig_y)**2)
    y+= factor*(1-ratio) * np.exp( ((x-x0)/sig_x)**2 + ((y-y0)/sig_y)**2)
    return x, y

def some_scalar_function(x,y):
    field = np.cos(xx/np.pi) * np.sin(yy/np.pi)
    return field

mesh_x, mesh_y  = make_mesh()
mesh_x, mesh_y = deform_grid(mesh_x, mesh_y)
field = some_scalar_function(mesh_x, mesh_y)
"""
Do your thing Gridded..
"""

(4) Square Grid, 3D

import numpy as np

def make_mesh():
    x = np.linspace(-10,10,num=21)
    y = np.linspace(-10,10,num=21)
    z = np.linspace(-10,10,num=21)
    xx, yy, zz = np.meshgrid(x,y,z)
    return xx,yy, zz

def some_scalar_function(x,y,z):
    field = np.cos(x/np.pi) * np.sin(y/np.pi)* np.cos(z/np.pi)
    return field

mesh_x, mesh_y, mesh_z  = make_mesh()
field = some_scalar_function(mesh_x, mesh_y, mesh_z)
"""
Do your thing Gridded..
"""

(5) Logically Rectilinear, Deformed Grid, 3D

import numpy as np

def make_mesh():
    x = np.linspace(-10,10,num=21)
    y = np.linspace(-10,10,num=21)
    z = np.linspace(-10,10,num=21)
    xx, yy, zz = np.meshgrid(x,y,z)
    return xx,yy, zz

def deform_grid(x,y,z):
    x0 = 0
    y0 = 0
    z0 = 0

    sig_x = np.std(x)
    sig_y = np.std(y)
    sig_z = np.std(z)

    factor = 0.3

    x+= factor * np.exp( ((x-x0)/sig_x)**2 + ((y-y0)/sig_y)**2 + (z-z0)/sig_z)**2)
    y+= factor * np.exp( ((x-x0)/sig_x)**2 + ((y-y0)/sig_y)**2+ (z-z0)/sig_z)**2)
    z+= factor * np.exp( ((x-x0)/sig_x)**2 + ((y-y0)/sig_y)**2+ (z-z0)/sig_z)**2)
    return x, y

def some_scalar_function(x,y,z):
    field = np.cos(x/np.pi) * np.sin(y/np.pi)* np.cos(z/np.pi)
    return field

mesh_x, mesh_y, mesh_z  = make_mesh()
mesh_x, mesh_y, mesh_z = deform_grid(mesh_x, mesh_y, mesh_z)
field = some_scalar_function(mesh_x, mesh_y, mesh_z)
"""
Do your thing Gridded..
"""
ChrisBarker-NOAA commented 1 month ago

Thanks for the specific suggestions -- I'll see what I can do.

Unfortunately, not all the code is working for all of this -- in practice, we've only needed the reading and working with the gridded data parts in our work, and so the writing gridded data parts are incomplete.

Would you be willing to help fill lout missing pieces if we get it started?

k-a-mendoza commented 1 month ago

yeah I can do some of it. I'm less familiar with the target conventions but I could code up some facades and unit tests for those parts.

ChrisBarker-NOAA commented 1 month ago

Thanks -- I'm going to the SciPy sprints this weekend -- I'll try to carve out some time to work on this then.

-CHB

k-a-mendoza commented 1 month ago

Have fun! Let me know if there is a particular branch you'd prefer to work on. It might make sense for one of us to put in and assign TODOS

ChrisBarker-NOAA commented 1 month ago

sure -- I'll create a branch when I start.