Skip to content

Instantly share code, notes, and snippets.

@bekozi
Created November 8, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bekozi/c5c7bf43745a1880af62b3bc6f005863 to your computer and use it in GitHub Desktop.
Save bekozi/c5c7bf43745a1880af62b3bc6f005863 to your computer and use it in GitHub Desktop.
from shapely.geometry import Point
import ocgis
path = '/path/to/data.nc'
lat_variable = 'XLAT'
lon_variable = 'XLONG'
lat_dimension = 'south_north'
lon_dimension = 'west_east'
data_variables = ['T2']
# Load the field from disk and modify it before passing it to operations. Operations can take a request dataset
# a field.
field = ocgis.RequestDataset(path, variable=data_variables).get()
# Remove the time dimension from the spatial coordinate variables.
for cv in [lat_variable, lon_variable]:
var = field[cv]
new_dimensions = var.dimensions[1:]
var.reshape(new_dimensions)
# Update the field's metadata so it knows where the x/y coordinate variables are.
field.set_x(field[lon_variable], dimension=lon_dimension)
field.set_y(field[lat_variable], dimension=lat_dimension)
# Create and set the coordinate system.
lcc = ocgis.CRS(
proj4='+proj=lcc +a=6370000.0m +b=6370000.0m +lon_0=97.00w +lat_1=33.00n +lat_2=45.00n +lat_0=40.00n +units=m +no_defs',
name='rpolcc')
field.set_crs(lcc)
# Run operations and write to netCDF.
geom = [{'geom': Point(20, 2), 'crs': lcc}]
ops = ocgis.OcgOperations(dataset=field, geom=geom, output_format='nc')
ret = ops.execute()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment