Code Monkey home page Code Monkey logo

pygeogrids's People

Contributors

aplocon avatar christophreimer avatar claytharrison avatar cpaulik avatar d-chung avatar mazweg avatar mvreugde avatar s-scherrer avatar sebhahn avatar tracyscanlon avatar wpreimes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pygeogrids's Issues

Wronge shape order when storing netcdf file

Grid shape is expected to be given as (size_lat, size_lon) when creating a grid:

class BasicGrid(object):
"""
Grid that just has lat,lon coordinates and can find the
nearest neighbour. It can also yield the gpi, lat, lon
information in order.
Parameters
----------
lon : numpy.array
longitudes of the points in the grid
lat : numpy.array
latitudes of the points in the grid
geodatum : basestring
Name of the geodatic datum associated with the grid
gpis : numpy.array, optional
if the gpi numbers are in a different order than the
lon and lat arrays an array containing the gpi numbers
can be given
if no array is given here the lon lat arrays are given
gpi numbers starting at 0
subset : numpy.array, optional
if the active part of the array is only a subset of
all the points then the subset array which is a index
into lon and lat can be given here.
setup_kdTree : boolean, optional
if set (default) then the kdTree for nearest neighbour
search will be built on initialization
shape : tuple, optional
The shape of the grid array in 2-d space.
e.g. for a 1x1 degree global regular grid the shape would be (180,360).
if given the grid can be reshaped into the given shape
this indicates that it is a regular grid and fills the
attributes self.lon2d and self.lat2d which
define the grid only be the meridian coordinates(self.lon2d) and
the coordinates of the circles of latitude(self.lat2d).
The shape has to be given as (lat2d, lon2d)
It it is not given the shape is set to the length of the input
lon and lat arrays.

When storing the grid, the shape is passed to the netcdf writer:

global_attrs['shape'] = grid.shape

But later in save_lonlat() where it is used to structure the variables, latsize has to be the second, resp. lonsize has to be the first element, therefore it won't work.

latsize = global_attrs['shape'][1]
lonsize = global_attrs['shape'][0]

Suggested fix: Change

latsize = global_attrs['shape'][1]
lonsize = global_attrs['shape'][0]

to

latsize = global_attrs['shape'][0]
lonsize = global_attrs['shape'][1]

Subset from non-binary masks in netcdf file

Loading a grid from netcdf allows selecting a subset_flag to reduce the grid based on a single maskin the netcdf file. If we allow not only flags, i.e. subset==1, but variables, we could reduce grids from files directly based on e.g. landcover classes or climate classes (e.g. from esa cci landcover). Should be easy to implement, any objections?

Index error in function gpi2lonlat

Retrieving lonlat information from a gpi fails in the function gpi2lonlat in this line , not sure if this is also related to #39

e.g. gp = 2412975 of the WARP grid cannot be found in if the activearrlon and activearrlat are just the land points.

If I set self.gpidirect to True, the function works.

Allow BasicGrid serialization

For some reason Basic Grid can not be pkled, but CellGrid can. This can lead to issues with eg parallelization frameworks and should be fixed.

import cloudpickle
from pygeogrids.grids import CellGrid, BasicGrid

cloudpickle.dumps(CellGrid([1,2,3], [1,2,3], [1,2,3]))
print("Cellgrid could be pickled")
cloudpickle.dumps(BasicGrid([1,2,3], [1,2,3]))
Traceback (most recent call last):
  File "/home/wpreimes/miniforge3/envs/smecv_gapfill/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3553, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-7762823bd9a7>", line 6, in <module>
    cloudpickle.dumps(BasicGrid([1,2,3], [1,2,3]))
  File "/home/wpreimes/miniforge3/envs/smecv_gapfill/lib/python3.10/site-packages/cloudpickle/cloudpickle.py", line 1479, in dumps
    cp.dump(obj)
  File "/home/wpreimes/miniforge3/envs/smecv_gapfill/lib/python3.10/site-packages/cloudpickle/cloudpickle.py", line 1245, in dump
    return super().dump(obj)
  File "<stringsource>", line 2, in pykdtree.kdtree.KDTree.__reduce_cython__
TypeError: no default __reduce__ due to non-trivial __cinit__
``

Is the documentation published somewhere ?

I was going to write my own grid management python package and I discovered this one, do you happen to publish the documentation somewhere?

I would be glad to contribute if there are synergies.

grid.find_nearest_gpi() max_dist ignored

grid.find_nearest_gpi() takes a keyword max_dist, but this is ignored in the next line when calling ´´find_k_nearest_gpi()´´ and set to np.Inf

def find_nearest_gpi(self, lon, lat, max_dist=np.Inf):

gpi, distance = self.find_k_nearest_gpi(lon, lat, max_dist=np.Inf, k=1)

I guess L382 should be changed to max_dist=max_dist

Also the unit of max_dist is not clear from the docstring

Netcdf loading as masked array

load_grid with newer netcdf versions creates masked arrays, e.g. when calling grid.get_grid_points().
I think this is not intended and due to changes in the netcdf4 library in the back. We don't utilize masked arrays and in most cases they should be compatible, still should we fix that to restore the original behavior?

Error when searching nearest neighbour with custom gpis

I get an error when using the new find_nearest_gpi function (with max_dist) from #66

>> import pygeogrids
>>pygeogrids.__version__
'0.3.1'
>> grid = pygeogrids.BasicGrid(lon=[16,17], lat=[45,46], gpis=[100,200])
>> grid.find_nearest_gpi(0,0)
(100, 5082354.193805456)
>> grid.find_nearest_gpi(0,0, max_dist=1000)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\wpreimes\Anaconda3\envs\ismn\lib\site-packages\pygeogrids\grids.py", line 400, in find_nearest_gpi
    gpi, distance = self.find_k_nearest_gpi(lon, lat, max_dist=max_dist, k=1)
  File "C:\Users\wpreimes\Anaconda3\envs\ismn\lib\site-packages\pygeogrids\grids.py", line 441, in find_k_nearest_gpi
    gpi = self.activegpis[ind]
IndexError: arrays used as indices must be of integer (or boolean) type

I think the function should return an empty array instead.

It works when I dont set the gpis manually when creating the grid

>> grid = pygeogrids.BasicGrid(lon=[16,17], lat=[45,46])
>> grid.find_nearest_gpi(0,0, max_dist=1000)
(array([], dtype=float64), array([], dtype=float64))

At some point the GPI is used for looking up the lon/lat I think, which only works for default GPIs (starting at 0)

@s-scherrer can you fix this?

numpy searchsorted does not really search in an array

the methods gpi2cell gpi2lonlatusenumpy.searchsorted` but this function does not really search for the occurrence of the gpi in the array but just for the position of where to insert the gpi. This is fine as long as the gpi is actually in the grid. We need a better check/search algorithm for this.

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.