About changing longitude array from 0 - 360 to -180 to 180 with Python xarray












1















I am a matlab user trying to use Python more for my computations recently. I am using xarray and would like to change my longitude array from 0 - 360 to -180 to 180 of a geophysical field. But when I do that:



df=xr.open_dataset(ecmwf_winds.nc)
u10=df['u10']
lon=df['longitude']
lon = np.where(lon > 180, lon-360, lon)
[X,Y]=np.meshgrid(lon,df.latitude)
plt.contourf(X,Y,u10)


the contourplot turns out to messy with gaps, which does not make sense. Can anyone please help me with it. I am not sure where I am doing wrong.










share|improve this question























  • you should covert the range of ALL you data to the new range, not just the ones above 180. so the line lon = np.where(lon > 180, lon-360, lon) should just be lon=lon-180

    – Gerges
    Nov 16 '18 at 21:20











  • Thank you, but it does not seem to work. Its something to do with the field. However, somehow got around it. Thank you again.

    – SMaj
    Nov 19 '18 at 19:17











  • @GergesDib that's not right at all. longitudes from (-180 to 180) are not (0 to 360) - 180. The formula the OP has is correct - the data simply has to be sorted before it can be plotted.

    – delgadom
    Nov 26 '18 at 1:45
















1















I am a matlab user trying to use Python more for my computations recently. I am using xarray and would like to change my longitude array from 0 - 360 to -180 to 180 of a geophysical field. But when I do that:



df=xr.open_dataset(ecmwf_winds.nc)
u10=df['u10']
lon=df['longitude']
lon = np.where(lon > 180, lon-360, lon)
[X,Y]=np.meshgrid(lon,df.latitude)
plt.contourf(X,Y,u10)


the contourplot turns out to messy with gaps, which does not make sense. Can anyone please help me with it. I am not sure where I am doing wrong.










share|improve this question























  • you should covert the range of ALL you data to the new range, not just the ones above 180. so the line lon = np.where(lon > 180, lon-360, lon) should just be lon=lon-180

    – Gerges
    Nov 16 '18 at 21:20











  • Thank you, but it does not seem to work. Its something to do with the field. However, somehow got around it. Thank you again.

    – SMaj
    Nov 19 '18 at 19:17











  • @GergesDib that's not right at all. longitudes from (-180 to 180) are not (0 to 360) - 180. The formula the OP has is correct - the data simply has to be sorted before it can be plotted.

    – delgadom
    Nov 26 '18 at 1:45














1












1








1








I am a matlab user trying to use Python more for my computations recently. I am using xarray and would like to change my longitude array from 0 - 360 to -180 to 180 of a geophysical field. But when I do that:



df=xr.open_dataset(ecmwf_winds.nc)
u10=df['u10']
lon=df['longitude']
lon = np.where(lon > 180, lon-360, lon)
[X,Y]=np.meshgrid(lon,df.latitude)
plt.contourf(X,Y,u10)


the contourplot turns out to messy with gaps, which does not make sense. Can anyone please help me with it. I am not sure where I am doing wrong.










share|improve this question














I am a matlab user trying to use Python more for my computations recently. I am using xarray and would like to change my longitude array from 0 - 360 to -180 to 180 of a geophysical field. But when I do that:



df=xr.open_dataset(ecmwf_winds.nc)
u10=df['u10']
lon=df['longitude']
lon = np.where(lon > 180, lon-360, lon)
[X,Y]=np.meshgrid(lon,df.latitude)
plt.contourf(X,Y,u10)


the contourplot turns out to messy with gaps, which does not make sense. Can anyone please help me with it. I am not sure where I am doing wrong.







python python-xarray






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 21:12









SMajSMaj

62




62













  • you should covert the range of ALL you data to the new range, not just the ones above 180. so the line lon = np.where(lon > 180, lon-360, lon) should just be lon=lon-180

    – Gerges
    Nov 16 '18 at 21:20











  • Thank you, but it does not seem to work. Its something to do with the field. However, somehow got around it. Thank you again.

    – SMaj
    Nov 19 '18 at 19:17











  • @GergesDib that's not right at all. longitudes from (-180 to 180) are not (0 to 360) - 180. The formula the OP has is correct - the data simply has to be sorted before it can be plotted.

    – delgadom
    Nov 26 '18 at 1:45



















  • you should covert the range of ALL you data to the new range, not just the ones above 180. so the line lon = np.where(lon > 180, lon-360, lon) should just be lon=lon-180

    – Gerges
    Nov 16 '18 at 21:20











  • Thank you, but it does not seem to work. Its something to do with the field. However, somehow got around it. Thank you again.

    – SMaj
    Nov 19 '18 at 19:17











  • @GergesDib that's not right at all. longitudes from (-180 to 180) are not (0 to 360) - 180. The formula the OP has is correct - the data simply has to be sorted before it can be plotted.

    – delgadom
    Nov 26 '18 at 1:45

















you should covert the range of ALL you data to the new range, not just the ones above 180. so the line lon = np.where(lon > 180, lon-360, lon) should just be lon=lon-180

– Gerges
Nov 16 '18 at 21:20





you should covert the range of ALL you data to the new range, not just the ones above 180. so the line lon = np.where(lon > 180, lon-360, lon) should just be lon=lon-180

– Gerges
Nov 16 '18 at 21:20













Thank you, but it does not seem to work. Its something to do with the field. However, somehow got around it. Thank you again.

– SMaj
Nov 19 '18 at 19:17





Thank you, but it does not seem to work. Its something to do with the field. However, somehow got around it. Thank you again.

– SMaj
Nov 19 '18 at 19:17













@GergesDib that's not right at all. longitudes from (-180 to 180) are not (0 to 360) - 180. The formula the OP has is correct - the data simply has to be sorted before it can be plotted.

– delgadom
Nov 26 '18 at 1:45





@GergesDib that's not right at all. longitudes from (-180 to 180) are not (0 to 360) - 180. The formula the OP has is correct - the data simply has to be sorted before it can be plotted.

– delgadom
Nov 26 '18 at 1:45












2 Answers
2






active

oldest

votes


















0














You need to assign the values as you've done and then also sort the resulting DataArray along the new coordinate values:



lon_name = 'longitude'  # whatever name is in the data

# Adjust lon values to make sure they are within (-180, 180)
ds['_longitude_adjusted'] = xr.where(
ds[lon_name] > 180,
ds[lon_name] - 360,
ds[lon_name])

# reassign the new coords to as the main lon coords
# and sort DataArray using new coordinate values
ds = (
ds
.swap_dims({lon_name: '_longitude_adjusted'})
.sel(**{'_longitude_adjusted': sorted(ds._longitude_adjusted)})
.drop(lon_name))

ds = ds.rename({'_longitude_adjusted': lon_name})





share|improve this answer


























  • It worked. Thank you very much.

    – SMaj
    Nov 26 '18 at 22:13



















0














Another faster approach and much simpler approach without using where would be



 df.coords['lon'] = (df.coords['lon'] + 180) % 360 - 180
df = df.sortby(df.lon)


Or it could be done in one line as



df['lon'] = ((df.lon + 180) % 360 - 180).sortby(df.lon)


Tip: For quick plotting you can use Xarrays inbuilt plotting function so you won't have to create a meshgrid.



df.u10.plot()
#or
df.u10.plt.contourf()





share|improve this answer


























  • Worked , thank you

    – SMaj
    Dec 4 '18 at 19:21











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345442%2fabout-changing-longitude-array-from-0-360-to-180-to-180-with-python-xarray%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to assign the values as you've done and then also sort the resulting DataArray along the new coordinate values:



lon_name = 'longitude'  # whatever name is in the data

# Adjust lon values to make sure they are within (-180, 180)
ds['_longitude_adjusted'] = xr.where(
ds[lon_name] > 180,
ds[lon_name] - 360,
ds[lon_name])

# reassign the new coords to as the main lon coords
# and sort DataArray using new coordinate values
ds = (
ds
.swap_dims({lon_name: '_longitude_adjusted'})
.sel(**{'_longitude_adjusted': sorted(ds._longitude_adjusted)})
.drop(lon_name))

ds = ds.rename({'_longitude_adjusted': lon_name})





share|improve this answer


























  • It worked. Thank you very much.

    – SMaj
    Nov 26 '18 at 22:13
















0














You need to assign the values as you've done and then also sort the resulting DataArray along the new coordinate values:



lon_name = 'longitude'  # whatever name is in the data

# Adjust lon values to make sure they are within (-180, 180)
ds['_longitude_adjusted'] = xr.where(
ds[lon_name] > 180,
ds[lon_name] - 360,
ds[lon_name])

# reassign the new coords to as the main lon coords
# and sort DataArray using new coordinate values
ds = (
ds
.swap_dims({lon_name: '_longitude_adjusted'})
.sel(**{'_longitude_adjusted': sorted(ds._longitude_adjusted)})
.drop(lon_name))

ds = ds.rename({'_longitude_adjusted': lon_name})





share|improve this answer


























  • It worked. Thank you very much.

    – SMaj
    Nov 26 '18 at 22:13














0












0








0







You need to assign the values as you've done and then also sort the resulting DataArray along the new coordinate values:



lon_name = 'longitude'  # whatever name is in the data

# Adjust lon values to make sure they are within (-180, 180)
ds['_longitude_adjusted'] = xr.where(
ds[lon_name] > 180,
ds[lon_name] - 360,
ds[lon_name])

# reassign the new coords to as the main lon coords
# and sort DataArray using new coordinate values
ds = (
ds
.swap_dims({lon_name: '_longitude_adjusted'})
.sel(**{'_longitude_adjusted': sorted(ds._longitude_adjusted)})
.drop(lon_name))

ds = ds.rename({'_longitude_adjusted': lon_name})





share|improve this answer















You need to assign the values as you've done and then also sort the resulting DataArray along the new coordinate values:



lon_name = 'longitude'  # whatever name is in the data

# Adjust lon values to make sure they are within (-180, 180)
ds['_longitude_adjusted'] = xr.where(
ds[lon_name] > 180,
ds[lon_name] - 360,
ds[lon_name])

# reassign the new coords to as the main lon coords
# and sort DataArray using new coordinate values
ds = (
ds
.swap_dims({lon_name: '_longitude_adjusted'})
.sel(**{'_longitude_adjusted': sorted(ds._longitude_adjusted)})
.drop(lon_name))

ds = ds.rename({'_longitude_adjusted': lon_name})






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 26 '18 at 23:19

























answered Nov 25 '18 at 20:34









delgadomdelgadom

812718




812718













  • It worked. Thank you very much.

    – SMaj
    Nov 26 '18 at 22:13



















  • It worked. Thank you very much.

    – SMaj
    Nov 26 '18 at 22:13

















It worked. Thank you very much.

– SMaj
Nov 26 '18 at 22:13





It worked. Thank you very much.

– SMaj
Nov 26 '18 at 22:13













0














Another faster approach and much simpler approach without using where would be



 df.coords['lon'] = (df.coords['lon'] + 180) % 360 - 180
df = df.sortby(df.lon)


Or it could be done in one line as



df['lon'] = ((df.lon + 180) % 360 - 180).sortby(df.lon)


Tip: For quick plotting you can use Xarrays inbuilt plotting function so you won't have to create a meshgrid.



df.u10.plot()
#or
df.u10.plt.contourf()





share|improve this answer


























  • Worked , thank you

    – SMaj
    Dec 4 '18 at 19:21
















0














Another faster approach and much simpler approach without using where would be



 df.coords['lon'] = (df.coords['lon'] + 180) % 360 - 180
df = df.sortby(df.lon)


Or it could be done in one line as



df['lon'] = ((df.lon + 180) % 360 - 180).sortby(df.lon)


Tip: For quick plotting you can use Xarrays inbuilt plotting function so you won't have to create a meshgrid.



df.u10.plot()
#or
df.u10.plt.contourf()





share|improve this answer


























  • Worked , thank you

    – SMaj
    Dec 4 '18 at 19:21














0












0








0







Another faster approach and much simpler approach without using where would be



 df.coords['lon'] = (df.coords['lon'] + 180) % 360 - 180
df = df.sortby(df.lon)


Or it could be done in one line as



df['lon'] = ((df.lon + 180) % 360 - 180).sortby(df.lon)


Tip: For quick plotting you can use Xarrays inbuilt plotting function so you won't have to create a meshgrid.



df.u10.plot()
#or
df.u10.plt.contourf()





share|improve this answer















Another faster approach and much simpler approach without using where would be



 df.coords['lon'] = (df.coords['lon'] + 180) % 360 - 180
df = df.sortby(df.lon)


Or it could be done in one line as



df['lon'] = ((df.lon + 180) % 360 - 180).sortby(df.lon)


Tip: For quick plotting you can use Xarrays inbuilt plotting function so you won't have to create a meshgrid.



df.u10.plot()
#or
df.u10.plt.contourf()






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 30 '18 at 16:50

























answered Nov 30 '18 at 16:18









Light_BLight_B

16310




16310













  • Worked , thank you

    – SMaj
    Dec 4 '18 at 19:21



















  • Worked , thank you

    – SMaj
    Dec 4 '18 at 19:21

















Worked , thank you

– SMaj
Dec 4 '18 at 19:21





Worked , thank you

– SMaj
Dec 4 '18 at 19:21


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345442%2fabout-changing-longitude-array-from-0-360-to-180-to-180-with-python-xarray%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

To store a contact into the json file from server.js file using a class in NodeJS

Marschland