Skip to content

Instantly share code, notes, and snippets.

@koldunovn
Created April 30, 2021 20:48
Show Gist options
  • Save koldunovn/82576e4ebcd4236b3f1990cf6c21ecc1 to your computer and use it in GitHub Desktop.
Save koldunovn/82576e4ebcd4236b3f1990cf6c21ecc1 to your computer and use it in GitHub Desktop.
Convert FESOM2 data to UGRID that QGIS3.16.6 can understand
import xarray as xr
import numpy as np
import pyfesom2 as pf
# we make it similar to this file
# ugrid = xr.open_dataset('./MDAL/tests/data/ugrid/ADCIRC/ADCIRC_BG_20190910_1t.nc')
data = xr.open_mfdataset('/Users/nkolduno/PYTHON/DATA/LCORE2/temp.fesom.*.nc')
mesh = pf.load_mesh('/Users/nkolduno/PYTHON/DATA/core2/')
elem2 = mesh.elem[mesh.no_cyclic_elem]
ds = xr.Dataset({'mesh_topology': ( np.array(-2147483647, dtype='int32')),
'elem': (["nelem", "three"], elem2.astype('int32')),
'variable': (['nod2'], data.temp[0,:,0].astype('float32')),
},
coords={'latitude': (['nod2'], mesh.y2),
'longitude': (['nod2'], mesh.x2),
})
ds.mesh_topology.attrs = {'long_name': 'mesh topology',
'standard_name': 'mesh_topology',
'dimension': 2,
'node_coordinates': 'longitude latitude',
'face_node_connectivity': 'elem',
'cf_role': 'mesh_topology',
'topology_dimension': 2}
ds.latitude.attrs = {'long_name': 'latitude',
'standard_name': 'latitude',
'units': 'degrees_north',
'positive': 'north'}
ds.longitude.attrs = {'long_name': 'longitude',
'standard_name': 'longitude',
'units': 'degrees_east',
'positive': 'east'}
ds.elem.attrs = {'long_name': 'elem',
'standard_name': 'face_node_connectivity',
'units': 'nondimensional',
'start_index': 0}
ds.attrs = {'Conventions': 'CF-1.6, UGRID-1.0'}
ds.to_netcdf('output.nc')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment