How to read a variable called “T” with xarray?

那年仲夏 提交于 2019-12-11 06:29:07

问题


All,

This might be a FAQ, but my Google-fu has failed me. Namely, I read in a file generated by a weather model I work on with xarray a la:

In [4]: data = xr.open_dataset("test_old.nc4")

In [5]: data
Out[5]: 
<xarray.Dataset>
Dimensions:  (lat: 49, lev: 48, lon: 96, time: 1)
Coordinates:
  * lon      (lon) float64 -180.0 -176.2 -172.5 -168.8 -165.0 -161.2 -157.5 ...
  * lat      (lat) float64 -90.0 -86.25 -82.5 -78.75 -75.0 -71.25 -67.5 ...
  * lev      (lev) float64 1e+03 975.0 950.0 925.0 900.0 875.0 850.0 825.0 ...
  * time     (time) datetime64[ns] 2000-04-15
Data variables:
    H        (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    O3       (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    OMEGA    (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    PHIS     (time, lat, lon) float64 2.605e+04 2.605e+04 2.605e+04 ...
    PS       (time, lat, lon) float64 6.984e+04 6.984e+04 6.984e+04 ...
    QI       (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    QL       (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    QV       (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    RH       (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    SLP      (time, lat, lon) float64 9.973e+04 9.973e+04 9.973e+04 ...
    T        (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    U        (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...
    V        (time, lev, lat, lon) float64 nan nan nan nan nan nan nan nan ...

So far, so good. (Note I've removed the attributes for space). Now, let's refer to RH, the relative humidity:

In [8]: data.RH
Out[8]: 
<xarray.DataArray 'RH' (time: 1, lev: 48, lat: 49, lon: 96)>
array([[[[             nan,              nan,              nan, ...,
                       nan,              nan,              nan],
         [             nan,              nan,              nan, ...,
                       nan,              nan,              nan],
         [             nan,              nan,              nan, ...,
                       nan,              nan,              nan],
         ..., 
         [  9.84245896e-01,   9.84482586e-01,   9.84114528e-01, ...,
            9.82491255e-01,   9.83228445e-01,   9.83820796e-01],
         [  9.84869719e-01,   9.86230493e-01,   9.87663150e-01, ...,
            9.81099427e-01,   9.82316971e-01,   9.83569324e-01],
         [  9.83583868e-01,   9.83583868e-01,   9.83583868e-01, ...,
            9.83583868e-01,   9.83583868e-01,   9.83583868e-01]],
<snip>
         [  8.91117509e-07,   8.92956564e-07,   8.92726121e-07, ...,
            8.90103763e-07,   8.89725982e-07,   8.90051581e-07],
         [  9.32031071e-07,   9.32695400e-07,   9.33462957e-07, ...,
            9.30619990e-07,   9.30997828e-07,   9.31466616e-07],
         [  9.39349945e-07,   9.39349945e-07,   9.39349945e-07, ...,
            9.39349945e-07,   9.39349945e-07,   9.39349945e-07]]]])
Coordinates:
  * lon      (lon) float64 -180.0 -176.2 -172.5 -168.8 -165.0 -161.2 -157.5 ...
  * lat      (lat) float64 -90.0 -86.25 -82.5 -78.75 -75.0 -71.25 -67.5 ...
  * lev      (lev) float64 1e+03 975.0 950.0 925.0 900.0 875.0 850.0 825.0 ...
  * time     (time) datetime64[ns] 2000-04-15
Attributes:
    long_name: relative_humidity_after_moist
    units: 1
    fmissing_value: 1e+15
    standard_name: relative_humidity_after_moist
    vmin: -1e+15
    vmax: 1e+15
    valid_range: [ -9.99999987e+14   9.99999987e+14]

Great! Now, what about T, the temperature:

In [12]: data.T
Out[12]: 
<xarray.Dataset>
Dimensions:  (lat: 49, lev: 48, lon: 96, time: 1)
Coordinates:
  * lon      (lon) float64 -180.0 -176.2 -172.5 -168.8 -165.0 -161.2 -157.5 ...
  * lat      (lat) float64 -90.0 -86.25 -82.5 -78.75 -75.0 -71.25 -67.5 ...
  * lev      (lev) float64 1e+03 975.0 950.0 925.0 900.0 875.0 850.0 825.0 ...
  * time     (time) datetime64[ns] 2000-04-15
Data variables:
    H        (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    O3       (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    OMEGA    (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    PHIS     (lon, lat, time) float64 2.605e+04 1.887e+04 3.46e+03 207.6 0.0 ...
    PS       (lon, lat, time) float64 6.984e+04 7.764e+04 9.496e+04 ...
    QI       (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    QL       (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    QV       (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    RH       (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    SLP      (lon, lat, time) float64 9.973e+04 9.937e+04 9.905e+04 ...
    T        (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    U        (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...
    V        (lon, lat, lev, time) float64 nan nan nan nan nan nan nan nan ...

Oh dear. I think what it's doing is a transpose. How exactly can one refer to a variable called "T" in an xarray dataset?


回答1:


Xarray supports attribute-style access for variables as a convenience feature for interactive use. But as you've noticed, this doesn't work to access a variable with the same name as a built-in Dataset method (in this case Dataset.T, which is the same as Dataset.transpose()).

The reliable way to access variables is to use dictionary-style access, data['T'].

data.get('T') also works, because Dataset supports Python's Mapping interface. Like dict.get, it's intended for accessing variables with a default value if the key is not found: data.get('not found') will return None.




回答2:


I think I figured it out:

data.get("T")

Might use that exclusively now.



来源:https://stackoverflow.com/questions/40270929/how-to-read-a-variable-called-t-with-xarray

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!