wrf-python icon indicating copy to clipboard operation
wrf-python copied to clipboard

Wrfout to NetCDF

Open rajkumar8581 opened this issue 3 years ago • 7 comments

I have three wrfout files (sample) in the below link. These files are from NCAR wrf python tutorials.

https://actvet-my.sharepoint.com/:f:/g/personal/rajkumar_sivaprahasam_adpoly_ac_ae/EofPOyhq8UhPlq3VBrvqLJABVR0G8xj_y0gmKRgLW1wagA?e=0DvsZc

I got some code from wrf-py github as follows to read in the data for all timesteps:-


import glob from netCDF4 import Dataset from wrf import getvar, ALL_TIMES

list_of_paths = glob.glob(r'/home/rajkumar/wrf_python_tutorial/wrf_tutorial_data/wrfout_d0*') list_of_paths.sort() wrflist=[] for i in range(0, len(list_of_paths)-1): wrflist.append(Dataset(list_of_paths[i]))

HFX=getvar(wrflist, "HFX", timeidx=ALL_TIMES, method="join") print(HFX)


Now I am trying to do two things:-

  1. Write the data in classic NetCDF (CF compliant) for the variable HFX for all times( time series).
  2. Create a 2D Plot for the time series for the variable HFX (some thing like an area average for all timesteps)

Would be grateful if someone can help.

rajkumar8581 avatar Dec 16 '21 07:12 rajkumar8581

Hi @erogluorhan Do we have any solution for writing wrfout files to CF compliant netcdf file using python- I mean an alternative to postprocessing tools like ARWPost or UPP.

rajkumar8581 avatar Nov 19 '22 15:11 rajkumar8581

Hi @rajkumar8581 ,

When you use getvar as follows:

HFX=getvar(wrflist, "HFX", timeidx=ALL_TIMES, method="join")

HFX is of type xarray.DataArray, right? Then, you should be able to call HFX.to_netcdf() to write that out to netCDF file. Would this help?

erogluorhan avatar Nov 28 '22 16:11 erogluorhan

Hi @erogluorhan Thanks for your response. the lat long variables are supposed to be one dimensional arrays for CF compliance.

image

The problem with wrfout files is the xlat/xlong are 2D arrays like below. Basically this hinders use of to_netcdf.

image

Could you please ponder over this.

rajkumar8581 avatar Dec 01 '22 16:12 rajkumar8581

Also, I would like to check whether there an alternative to wrfout_to_cf.ncl in python please?

https://sundowner.colorado.edu/wrfout_to_cf/overview.html

rajkumar8581 avatar Dec 01 '22 16:12 rajkumar8581

@rajkumar8581 Hello, I have also encountered a similar problem. Have you resolved how to save the DataArray processed by getvar into NC file?

Shuang-Chen1016 avatar Jan 17 '24 07:01 Shuang-Chen1016

The lat long variables are supposed to be one dimensional arrays for CF compliance.

Are you sure? My reading of section 5.2: Two-dimensional latitude and longitude coordinate variables implies that two-dimensional latitude and longitude coordinate variables are compatible with CF. Could you elaborate on where you saw this?

Are you thinking perhaps of projection_x_coordinate and projection_y_coordinate as illustrated for a Lambert Conformal projection here?

You can get a decent start with the DX/DY values and the length of the relevant dimensions: if you set the appropriate values for false_easting and false_northing on the CRS, you can even get away without centering them on zero.

Just be aware: those projected coordinates have some odd characteristics if you try to reproject directly to another coordinate system.

The problem with wrfout files is the xlat/xlong are 2D arrays like below.

The thing about the conformal projections WRF uses, is that only in the case of the Mercator Projection does a one-dimensional XLAT/XLONG make sense. Consider a WRF run on a polar stereographic projection, say the upper-left plot below from the WRF user guide

image

What latitude would you select for the row of grid cells connecting the $120^\circ$ W label to the $120^\circ$ E label? It varies from under $40^\circ$ N to nearly $60^\circ$ N before heading back down below $40^\circ$ N: perhaps it is latitude that varies on this row of grid cells? The other option given one-dimensional latitude and longitude variables is longitude, so what longitude would you assign to this row of grid cells? I described the row by noting it sweeps out $120^\circ$ of longitude, so that is no easier a question.

The older COARDS conventions don't cover projected coordinate systems, but even the CF-1.0 conventions do: have you tried updating whatever tool is checking for CF-compliance? It would seem it supports only a subset of CF, or is a few years out of date.

Basically this hinders use of to_netcdf.

The actual problem I've run into with processed files is the tool putting a non-serializable variable into the dataset directly, instead of available through an accessor: here it looks like that would be ds.coords["wrf_projection"], which is a pyproj.Proj instance rather than a float or an int. del ds.coords["wrf_projection"] should allow the file to save (you may need to iterate if there's more than one of these, but the error from to_netcdf should say which variable or attribute is causing problems, even if how it says "I don't know how to save this to netCDF" is slightly opaque).

Also, I would like to check whether there an alternative to wrfout_to_cf.ncl in python please?

https://sundowner.colorado.edu/wrfout_to_cf/overview.html

There's the WRF NetCdf eXtract aNd Join script out of the Santander group, or the newer xWRF project's .xwrf.postprocess accessor. There's also the xESMF project for putting your data on a regular lat-lon grid, in case you can't upgrade the tool that's complaining about having to deal with coordinate systems and projected coordinates.

DWesl avatar May 22 '24 19:05 DWesl

已收到,谢谢

Shuang-Chen1016 avatar May 22 '24 19:05 Shuang-Chen1016