[python #AQS-174406]: python/netcdf data array questions
- Subject: [python #AQS-174406]: python/netcdf data array questions
- Date: Wed, 22 Jul 2020 16:17:05 -0600
Hello! Apologies for the delayed response, and thanks for reaching out to us.
Our sample netCDF files in these examples are often organized and labelled
based on their original GRIB collections, with the 'lat' and 'lon' variables
added per request when accessed via our NetCDF Subset Service (NCSS) on a
THREDDS Data Server (TDS). I was able to adapt the differential advection code
to your provided NARR dataset with minor changes. Notably that our custom
`metpy.xarray.sel` method does not identify your 'plevel' vertical coordinate,
and so you won't be able to use units for the `da.metpy.sel(vertical=level)`
code as demonstrated. However, if you specify these levels as just the values
desired (with no units, but make sure you know you're providing the right
values!) and use the built-in xarray `dataarray.sel` method you should be fine.
Here is a snippet of my adaptation:
# Get lat/lon data from file
lats = ds.latitude.data
lons = ds.longitude.data
# Calculate variable dx, dy values for use in calculations
dx, dy = mpcalc.lat_lon_grid_deltas(lons, lats)
# Get 700-hPa data and smooth
level = 700
hght_700 = mpcalc.smooth_n_point(ds['HGT'].sel(plevel=level).squeeze(), 9)
and the code should work from there. For your specific file you will then need
to specify which time you intend to analyze and cut out the datetime
converstion we provide (`vtime = ds.time.values` should do.) Either way, you
should be able to adapt our code to work for your specific file, with no need
to reorganize completely.
You can also re-define your 'plevel' vertical coordinate, externally or
internally with xarray, to be in-line with CF conventions
and the code should work as originally provided. Just in case it helps, you
can rename any variable, coordinate, dimension, etc. in an xarray
Dataset/DataArray using Dataset.rename(), DataArray.rename(),
Dataset.rename_vars(), and/or Dataset.rename_dims() (link to one:
You can also modify the attributes of datasets and dataarrays if needed!
I will take some extra time to make sure I can fully help you with your
original ticket as well. You are on the right track as far as handling NaNs,
with the most "right" methods of filling (interpolation, fill, etc.) depending
on how many missing values, where they're located, and what is appropriate in
your use-case. Those xarray methods allow you to repeat values or insert your
own fill values, while there are other tools for using interpolation to fill
Just in case it will help: if you are already using xarray to access your GRIB
or netCDF files in Python, you can use it to subset your data in many powerful
ways! The excellent xarray documentation has a section just for this at
http://xarray.pydata.org/en/stable/indexing.html# demonstrating how you can use
slices, interpolation and lookups, and positional/named indexing. There are
more demonstrations in the examples as well. The differential_advection code
you are following demonstrates using `.sel()` to subset our data along our
vertical coordinate, for example. You can use this to select slices along
dimensions or use `.where()` to mask data using criteria of non-dimension
variables, and more.
In order for us to help you with your cartopy plotting issue, can you provide
us the following information?
* Versions of Python and cartopy installed. Any other package versions you
can include can be helpful too
* Where you've installed these from (pip, conda, etc.; looks like you're
using conda, but just in case!)
* A longer code sample including the plotting code, if possible
Thanks again for your patience, and I hope this helps and/or points you in the
right direction. Please don't hesitate to follow up, and I can get back to you
more quickly if so.
All the best,
> I started a bit more simple on this approach where I will try to follow
> posted example:
> 1) I confirmed that I can run the code with the slight adjustment of
> downloading the NARR.nc file first and then calling locally
> 2) I want to run the same code on my NARR_"date".nc files as a test. So I
> completed some wgrib and wgrib2 gymnastics to get what I feel is
> similarly organized data:
> shown here for ref:
> This won't work on existing code of course because it has different labels.
> QUESTION on the best approach to remedy:
> Should I edit the Differential_Temperature_Advection.py code to read in the
> data properly based on the different labels OR is there a simple way to get
> the nc data in the right format with the right labels?
Ticket ID: AQS-174406
Department: Support Python
NOTE: All email exchanges with Unidata User Support are recorded in the Unidata
inquiry tracking system and then made publicly available through the web. If
you do not want to have your interactions made available in this way, you must
let us know in each email you send to us.