How can I reverse Coordinate vector and apply transformation to all Data Variables?

1,434 views
Skip to first unread message

Andrew Joros

unread,
Nov 7, 2016, 6:48:20 AM11/7/16
to xarray
Hi all,

I have tried googling around without luck, and maybe it's because I'm not sure what proper keywords to enter, but here is my question:

I have the following Dataset in xarray (see below). I am looking to flip the "latitude" coordinate and consequently apply it to all the Data Variables. Naturally, latitude should go from largest to smallest value (90 to -90), and when I tried to use something like latitude[::-1], it doesn't apply that reversing function to the data variables themselves. I tested this by grabbing the value from TMP_2maboveground[0,50,50] before and after reversing the latitude vector. The values came back the same which can't be possible since they should be two different locations on Earth before and after reversing latitude vector.

In short, how can I reverse the latitude vector and apply it to all the Data Variables?

<xarray.Dataset>


Dimensions:              (latitude: 721, longitude: 1440, time: 1)


Coordinates:

* latitude             (latitude) float64 -90.0 -89.75 -89.5 -89.25 -89.0 ...

* longitude            (longitude) float64 0.0 0.25 0.5 0.75 1.0 1.25 1.5 ...

* time                 (time) datetime64[ns] 2016-11-10T12:00:00


Data variables:


   TMP_2maboveground    (time, latitude, longitude) float64 234.9 234.9 ...

   RH_2maboveground     (time, latitude, longitude) float64 96.8 96.8 96.8 ...

   TMAX_2maboveground   (time, latitude, longitude) float64 234.9 234.9 ...

   TMIN_2maboveground   (time, latitude, longitude) float64 234.3 234.3 ...

   UGRD_10maboveground  (time, latitude, longitude) float64 -1.17 -1.16 ...

   VGRD_10maboveground  (time, latitude, longitude) float64 -4.26 -4.27 ...

   PRATE_surface        (time, latitude, longitude) float64 1e-06 1e-06 ...

   DSWRF_surface        (time, latitude, longitude) float64 310.0 310.0 ...

   PRMSL_meansealevel   (time, latitude, longitude) float64 1.024e+05 ...


Attributes:


   Conventions: COARDS

   History: created by wgrib2

   GRIB2_grid_template: 0


 Thanks

Andy

Stephan Hoyer

unread,
Nov 7, 2016, 11:31:59 AM11/7/16
to xarray
I think ds.sel(latitude=slice(None, None -1)) should do the trick -- it will reverse the latitude axis on every variable.

Side note: StackOverflow is even better than the mailing list for questions like this, because it turns up better in search.

--
You received this message because you are subscribed to the Google Groups "xarray" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+unsubscribe@googlegroups.com.
To post to this group, send email to xar...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/4bc8256c-e884-449e-ad68-7e41ef6eee0a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Joros

unread,
Nov 7, 2016, 3:14:22 PM11/7/16
to xarray
Hmmm, it's not liking your hyphen in "None-1". TypeError: unsupported operand type(s) for -: 'NoneType' and 'int'

I will attempt to duplicate and post on StackOverflow as well.

Thanks Stephan


To unsubscribe from this group and stop receiving emails from it, send an email to xarray+un...@googlegroups.com.

erol512

unread,
Nov 7, 2016, 3:22:50 PM11/7/16
to xar...@googlegroups.com
Hi Andrew,

I believe Stephan meant to write ds.sel(latitude=slice(None, None, ::-1)). This should work.

Cheers,
Jeremy

To unsubscribe from this group and stop receiving emails from it, send an email to xarray+unsubscribe@googlegroups.com.

To post to this group, send email to xar...@googlegroups.com.
Message has been deleted

Andrew Joros

unread,
Nov 7, 2016, 3:27:30 PM11/7/16
to xarray
Unfortunately, that's returning "invalid syntax" as I think slice only takes 2 arguments, not 3 like you posted.
  File "<ipython-input-100-86b2cfc60863>", line 1
    tempDS.sel(lat=slice(None, None, ::-1))
                                     ^
SyntaxError: invalid syntax

Stephan Hoyer

unread,
Nov 7, 2016, 3:38:08 PM11/7/16
to xarray
I was missing a comma:
ds.sel(latitude=slice(None, None, -1)) 

To unsubscribe from this group and stop receiving emails from it, send an email to xarray+unsubscribe@googlegroups.com.

To post to this group, send email to xar...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages