GithubHelp home page GithubHelp logo

Comments (4)

fmaussion avatar fmaussion commented on June 3, 2024

Thanks for the report! Indeed I haven't looked much how salem behaves across hemispheres. I'll try to have a look soon.

Is your data defined on [0, 360] or [-180, 180]?

from salem.

ritviksahajpal avatar ritviksahajpal commented on June 3, 2024

Thanks for looking into this! The shapefile is define on -180 to 180 (and so is the data). The shapefile is here: https://www.dropbox.com/sh/yp22wdue7ri63z9/AADqm4zPwKNkykcneMwthm8Fa?dl=0

from salem.

fmaussion avatar fmaussion commented on June 3, 2024

OK, so there are a couple of things going on. If everything is in the -180 180 range, roi and subset work fine, as long as one uses no margin:

import xarray as xr
import numpy as np
import salem
from salem import get_demo_file, open_wrf_dataset
import matplotlib.pyplot as plt

borders = salem.read_shapefile(get_demo_file('world_borders.shp'), cached=True)
borders = borders.loc[borders['CNTRY_NAME'].isin(['Russia'])]

# make dummy dataset
lon = np.linspace(1, 359, 180) - 180
lat =  np.linspace(1, 89, 45)
data = lon * np.ones((len(lat), 1))
da = xr.DataArray(data, dims=['lat', 'lon'], coords={'lat': lat, 'lon':lon})

# plot
f = plt.figure(figsize=(12, 4))
roi = da.salem.roi(shape=borders)
roi.salem.quick_map(countries=False)

map1

f = plt.figure(figsize=(12, 4))
s = roi.salem.subset(shape=borders)
s.salem.quick_map(countries=False)

map2

With margin=2 we have an issue, as you noticed. I am not sure what to do in this case, though. The subset (with or without margin) isn't very useful...

The easiest way to make Russia "continuous" is to shift the data prior analysis:

sdata = np.roll(data, 90, axis=1)
slon = np.roll(lon, 90)
slon = np.where(slon < 0, slon + 360, slon)
sda = xr.DataArray(sdata, dims=['lat', 'lon'], coords={'lat': lat, 'lon':slon})

But then, the shapefile also has to be converted too :-(

def trafo_360(x, y, z=None):
    return np.where(x < 0, x+360, x), y
from shapely.ops import transform
sborders = borders.copy()
sborders['geometry'] = borders.geometry.apply(lambda geom: transform(trafo_360, geom))

# finally!
f = plt.figure(figsize=(12, 4))
roi = sda.salem.roi(shape=sborders)
s = roi.salem.subset(shape=sborders, margin=2)
s.salem.quick_map(countries=False)

map3

I guess that salem could take over the last step (the shapefile transformation), and could provide automatic tools to shift datasets (I've needed this several times).

from salem.

ritviksahajpal avatar ritviksahajpal commented on June 3, 2024

thank you so much! this is a very comprehensive answer and works for me.

from salem.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.