简体   繁体   中英

XArray: Create a new coordinate / dimension from an attribute

I'm using a library ( georinex ) that reads GPS RINEX data and returns an xarray in return.

The typical data I would get in return from parsing the rinex file from one GPS receiver station is for example this one:

<xarray.Dataset>Users\eballes\Desktop\share\226_day\225_highrate\00\abpo225a00.14n
Dimensions:           (sv: 12, time: 2)
Coordinates:
  * time              (time) datetime64[ns] 2014-08-13 2014-08-13T02:00:00
  * sv                (sv) <U3 'G01' 'G03' 'G07' 'G08' ... 'G27' 'G28' 'G32'
Data variables:
    SVclockBias       (time, sv) float64 nan nan nan ... 0.0003767 -0.0003641
    SVclockDrift      (time, sv) float64 nan nan nan ... 2.728e-12 8.413e-12
    SVclockDriftRate  (time, sv) float64 nan nan nan nan nan ... 0.0 0.0 0.0 0.0
    IODE              (time, sv) float64 nan nan nan nan ... 89.0 60.0 85.0 64.0
    Crs               (time, sv) float64 nan nan nan nan ... -29.56 100.2 78.56
    DeltaN            (time, sv) float64 nan nan nan ... 3.904e-09 5.106e-09
    M0                (time, sv) float64 nan nan nan nan ... 0.6369 1.585 -1.493
    Cuc               (time, sv) float64 nan nan nan ... 4.929e-06 4.02e-06
    Eccentricity      (time, sv) float64 nan nan nan ... 0.01919 0.01139
    Cus               (time, sv) float64 nan nan nan ... 9.466e-06 8.697e-06
    sqrtA             (time, sv) float64 nan nan nan ... 5.154e+03 5.154e+03
    Toe               (time, sv) float64 nan nan nan ... 2.664e+05 2.664e+05
    Cic               (time, sv) float64 nan nan nan ... -7.078e-08 -1.863e-08
    Omega0            (time, sv) float64 nan nan nan nan ... 2.154 1.175 -1.948
    Cis               (time, sv) float64 nan nan nan ... -4.619e-07 -1.434e-07
    Io                (time, sv) float64 nan nan nan ... 0.9641 0.9871 0.9473
    Crc               (time, sv) float64 nan nan nan nan ... 338.2 208.2 206.5
    omega             (time, sv) float64 nan nan nan ... 0.3215 -1.71 -0.1162
    OmegaDot          (time, sv) float64 nan nan nan ... -7.925e-09 -8.374e-09
    IDOT              (time, sv) float64 nan nan nan ... 5.997e-10 6.879e-10
    CodesL2           (time, sv) float64 nan nan nan nan nan ... 0.0 0.0 0.0 0.0
    GPSWeek           (time, sv) float64 nan nan nan ... 1.805e+03 1.805e+03
    L2Pflag           (time, sv) float64 nan nan nan nan nan ... 0.0 0.0 0.0 0.0
    SVacc             (time, sv) float64 nan nan nan nan nan ... 2.0 2.0 2.0 2.0
    health            (time, sv) float64 nan nan nan nan nan ... 0.0 0.0 0.0 0.0
    TGD               (time, sv) float64 nan nan nan ... -1.071e-08 -3.26e-09
    IODC              (time, sv) float64 nan nan nan nan ... 89.0 60.0 85.0 64.0
    TransTime         (time, sv) float64 nan nan nan ... 2.592e+05 2.592e+05
    FitIntvl          (time, sv) float64 nan nan nan nan nan ... 4.0 4.0 4.0 4.0
Attributes:
    version:    2.1
    svtype:     ['G']
    rinextype:  nav
    filename:   abpo225a00.14n

As you can see Coordinates are time and sv . However for my particular problem I would need to add the data variable TransTime as a Coordinate .

Unfortunately I am not able to get anything working and I haven't found any example useful for modifying the structure of an already existing xarray since I assume the most normal approach would be to create it directly with the correct coordinates.

The code up to this point is straightforward using this data after uncompress it:

  import georinex as gr
  nav = gr.load('abpo225a00.14n')

And is in this point where I should transform nav into something similar to:

<xarray.Dataset>Users\eballes\Desktop\share\226_day\225_highrate\00\abpo225a00.14n
Dimensions:           (sv: 12, time: 2)
Coordinates:
  * time              (time) datetime64[ns] 2014-08-13 2014-08-13T02:00:00
  * sv                (sv) <U3 'G01' 'G03' 'G07' 'G08' ... 'G27' 'G28' 'G32'
  * TransTime         (TransTime) <values from TransTime variable>

尝试

nav = nav.assign_coords(TransTime=nav.TransTime])

Technically from my question the accepted answer is correct. Unfortunately it was not enough for my problem which I couldn't really formulate correctly.

When assigning coordinates with the accepted answer the resulting Xarray had this shape:

Coordinates:
  * time              (time) datetime64[ns] 2014-08-13 2014-08-13T02:00:00
  * sv                (sv) <U3 'G01' 'G03' 'G07' 'G08' ... 'G27' 'G28' 'G32'
    TransTime         (sv, time) <values from TransTime variable>

which is not what I was actually looking for (or at least it was not something useful for me) since the new coordinate was depending on the other two indexes and was not indexed itself.

I realized that what I really wanted was not a new coordinate but a change of index. In the end what actually work for this goal was to go to the DataFrame level, remove the current indexes, create new indexes and come back to an xarray

nav = gr.load(file_path).to_dataframe().dropna(how='all')
nav = nav.reset_index(['time', 'sv'])
nav = nav.set_index(['sv','TransTime'])
nav = nav.to_xarray()

I hope this may be useful for somebody starting in XArray/Pandas that come across to this question not fully knowing what they want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM