# Create a cdo / paraview compatible grid file from a raw geos file#

Copyright 2021 Florian Ziemen ziemen@dkrz.de. This program is free software: you can redistribute it and/or modify it under the terms of the BSD-3-Clause license (see bottom for details).

[ ]:

import netCDF4 as nc
import numpy as np

[2]:

infile = nc.Dataset("geos.nc")

[3]:

vars = infile.variables

[4]:

corner_lons = vars["corner_lons"]
corner_lats = vars["corner_lats"]
xlen = corner_lons.shape[2] - 1
ylen = corner_lons.shape[1] - 1
lats = vars["lats"]
lons = vars["lons"]

[10]:

clon_bnds = np.array(
(
corner_lons[:, :-1, 1:],
corner_lons[:, 1:, 1:],
corner_lons[:, 1:, :-1],
corner_lons[:, :-1, :-1],
)
).transpose(1, 2, 3, 0)
clat_bnds = np.array(
(
corner_lats[:, :-1, 1:],
corner_lats[:, 1:, 1:],
corner_lats[:, 1:, :-1],
corner_lats[:, :-1, :-1],
)
).transpose(1, 2, 3, 0)

[13]:

try:
ds.close()
except:
pass
ds = nc.Dataset("geos_grid.nc", "w", format="NETCDF4")
nv = ds.createDimension("Ydim", ylen)
nv = ds.createDimension("Xdim", xlen)
nv = ds.createDimension("nf", 6)
nv = ds.createDimension("nv", 4)
clon = ds.createVariable("clon", "f8", ("nf", "Ydim", "Xdim"))
clat = ds.createVariable("clat", "f8", ("nf", "Ydim", "Xdim"))
lml = ds.createVariable("lml", "f4", ("nf", "Ydim", "Xdim"))
fclon_bnds = ds.createVariable("clon_bnds", "f8", ("nf", "Ydim", "Xdim", "nv"))
fclat_bnds = ds.createVariable("clat_bnds", "f8", ("nf", "Ydim", "Xdim", "nv"))
for x in [clon, clat, lml]:
x.coordinates = "clat clon"
clon.bounds = "clon_bnds"
clat.bounds = "clat_bnds"
clon.standard_name = "longitude"
clat.standard_name = "latitude"

clon[:] = lons[:] * np.pi / 180.0
clat[:] = lats[:] * np.pi / 180.0
lml[:] = clat[:] * clon[:]
fclon_bnds[:] = clon_bnds[:] * np.pi / 180
fclat_bnds[:] = clat_bnds[:] * np.pi / 180
ds.close()

[30]:

import matplotlib.pyplot as plt

plt.plot(clon_bnds[0, 0], clat_bnds[0, 0])
print(clon_bnds[0, 0])

[305.02597 305.02597 305.      305.     ]


Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.