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

Add a notebook about (gaussian) grid/bounds/weights generation #61

Open
jypeter opened this issue Nov 27, 2020 · 0 comments
Open

Add a notebook about (gaussian) grid/bounds/weights generation #61

jypeter opened this issue Nov 27, 2020 · 0 comments

Comments

@jypeter
Copy link
Member

jypeter commented Nov 27, 2020

A colleague (@dmr-dj) told me this morning that he had no easy way to create cell boundaries for a gaussian grid. I sent him the following lines, that could probably be integrated into a new notebook/tutorial, since grid/bounds/weights generation are an asset of cdms2 (not sure if you can find this in netcdf4 and xarray)

>>> dir(cdms2)
['AbstractAxis', ...'createAxis', 'createDataset', 'createEqualAreaAxis', 'createGaussianAxis', 'createGaussianGrid', 'createGenericGrid', 'createGlobalMeanGrid', 'createRectGrid', 'createUniformGrid', 'createUniformLatitudeAxis', 'createUniformLongitudeAxis', 'createVariable', 'createZonalGrid', ... ]

>>> help(cdms2.createGaussianGrid)

createGaussianGrid(nlats, xorigin=0.0, order='yx')
    createGaussianGrid(nlats, xorigin=0.0)
    Create a Gaussian grid, with shape (nlats, 2*nlats).

    Parameters
    ----------
         nlats:
              is the number of latitudes.

         xorigin:
              is the origin of the longitude axis

         order:
              is either "yx" or "xy"

>>> gr32 = cdms2.createGaussianGrid(32)
>>> lat32 = gr32.getLatitude()
>>> lat32
   id: latitude
   Designated a latitude axis.
   units:  degrees_north
   Length: 32
   First:  85.7605871204438
   Last:   -85.7605871204438
   Other axis attributes:
      axis: Y
   Python id:  0x2b9a4bcc2438

>>> lat32[:]
array([ 85.76058712,  80.26877907,  74.74454037,  69.21297617,
        63.67863556,  58.14295405,  52.60652603,  47.06964206,
        41.53246125,  35.99507841,  30.45755396,  24.91992863,
        19.38223135,  13.84448373,   8.30670286,   2.76890301,
        -2.76890301,  -8.30670286, -13.84448373, -19.38223135,
       -24.91992863, -30.45755396, -35.99507841, -41.53246125,
       -47.06964206, -52.60652603, -58.14295405, -63.67863556,
       -69.21297617, -74.74454037, -80.26877907, -85.76058712])
>>> lat32.getBounds()
array([[ 9.00000000e+01,  8.32076896e+01],
       [ 8.32076896e+01,  7.76092666e+01],
       [ 7.76092666e+01,  7.20479751e+01],
       [ 7.20479751e+01,  6.64972690e+01],
       [ 6.64972690e+01,  6.09510468e+01],
...
       [-7.76092666e+01, -8.32076896e+01],
       [-8.32076896e+01, -9.00000000e+01]])
>>> lat32_w, lon32_w = gr32.getWeights()
>>> lat32_w.sum(), lon32_w.sum()
(1.0, 1.0)

>>> len(lat32_w), len(lon32_w)
(32, 64)
>>> weights32 = np.outer(lat32_w, lon32_w)
>>> weights32.shape
(32, 64)
>>> weights32.sum()
1.0

>>> weights32[0, :]
array([5.48328907e-05, 5.48328907e-05, 5.48328907e-05, 5.48328907e-05,
...
       5.48328907e-05, 5.48328907e-05, 5.48328907e-05, 5.48328907e-05])
>>> weights32[:, 0]
array([5.48328907e-05, 1.27143709e-04, 1.98375510e-04, 2.67764554e-04,
       3.34655453e-04, 3.98422338e-04, 4.58469480e-04, 5.14236115e-04,
...
       5.14236115e-04, 4.58469480e-04, 3.98422338e-04, 3.34655453e-04,
       2.67764554e-04, 1.98375510e-04, 1.27143709e-04, 5.48328907e-05]) 
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

No branches or pull requests

1 participant