NCAR / wrf-python

A collection of diagnostic and interpolation routines for use with output from the Weather Research and Forecasting (WRF-ARW) Model.
https://wrf-python.readthedocs.io
Apache License 2.0
402 stars 152 forks source link

save slp to netcdf #215

Open fipoucat opened 1 year ago

fipoucat commented 1 year ago

I extracted slp from wrf output using wrf-python. I can print the output but want to save it in a netcdf file. I wonder how to do it within wrf-python?

SarahRoffe commented 8 months ago

I am in the same boat. @fipoucat, have you been able to save a netcdf file using wrf-python?

fipoucat commented 8 months ago

Still did not find a solution, hope someone will helP

leonardojimenez1990 commented 8 months ago

Hello, you can use the xarray library. This function and the link can help you. https://github.com/NCAR/wrf-python/issues/91

def write_xarray_to_netcdf(xarray_array, output_path,mode='w', format='NETCDF4', group=None, engine=None, encoding=None): """writes and xarray in a netcdf format outputfile Uses the xarray typical for wrf-python. The projection objects are transformed into strings to be able to use them as netcdf attributes :param xarray_array: xarray.DataArray :param output_path: str :param format: 'NETCDF4', 'NETCDF4_CLASSIC', 'NETCDF3_64BIT' or 'NETCDF3_CLASSIC' default: 'NETCDF4' :param group: str, default None :param engine: 'netcdf4', 'scipy' or 'h5netcdf' :param encoding: dict, default: None """ xarray_array_out = xarray_array.copy(deep=True)

coordinates are extracted from variable

del xarray_array_out.attrs['coordinates']
# wrf-python projection object cannot be processed
xarray_array_out.attrs['projection'] =

str(xarray_array_out.attrs['projection'])

xarray_array_out.to_netcdf(path=output_path, mode=mode, format=format,

group=group, engine=engine, encoding=encoding)

El mar, 23 ene 2024 a las 9:34, fipoucat @.***>) escribió:

Still did not find a solution, hope someone will helP

— Reply to this email directly, view it on GitHub https://github.com/NCAR/wrf-python/issues/215#issuecomment-1905967163, or unsubscribe https://github.com/notifications/unsubscribe-auth/AROHQY7CS6LCFV4BBE6L2RTYP6U5BAVCNFSM6AAAAAA3YJT72SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBVHE3DOMJWGM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

DWesl commented 2 months ago

I suspect they are running into encoding errors when they try: one would need to delete the variables with non-numeric data.

Mostly this means the coordinates in which wrf-python stores the Cartopy, Basemap, or Proj representation of the geospatial map projection. One might be able to ask the object for a string to save as a coordinate or a dictionary one could save as attributes of a variable so one could re-create the projection later.

EDIT: That is what you are saying, and the suggestion is the function from this comment, though without formatting due to being sent as an email. The important bit is ds.attrs["projection"] = str(ds.attrs["projection"])