Regridding data from 2D grid to another - scipy

i'm working on a project involving netcdf data analisys. To correctly process the data i need to regrid it to a coarser level. The matrix is in fact multidimensional, but i only need spatial regridding, so basically i have to work with a lat/lon matrix.
The process i had in mind would consist, giving a source 2d grid (the one retrived from the netcdf) and a destination grid ( the one that i need for my calculations) the second of wich is coarser(have less points),in calculating a sort of weight matrix that contains the coefficients needed to compute each point of destination grid.
This will be helpful because i have a bunch of variables laying on the same grid, so if i calculate a weight matrix, the problem is reduced in a sequence of matrices multiplications.
basically what i need is a function f(lat_source,lon_source,lat_dest, lot_dest) that given the coords of the source/destinaiton grids return a matrix of weights that maps the dest lat/lon onto the source one.
is there something already implemented ? Or some reads that could point me in the right direction ?
I know there are different interpolation methods that could be used, but, for the sake of the argument, let's suppose a linear interpolation.

Related

Interpolating 3D points from input points corresponding to a closed surface

I have a list of scattered 3D points similar to the one below:
Using MATLAB, I want to interpolate further points from the surface that those original points correspond to, in order to obtain a more complete scatter. Note that there are no particular slices defined on this scattered data. That is, the z values of the point cloud are not discrete, so it's not possible to interpolate slice by slice.
I think that the ideal way to achieve this would be to somehow obtain the smooth closed surface which best matches the scattered data, and then sample it. But I have found no straightforward way to achieve this.
The scatterinterpolant class could be a simple option.
Use scatteredInterpolant to perform interpolation on a 2-D or 3-D
Scattered Data set. For example, you can pass a set of (x,y) points
and values, v, to scatteredInterpolant, and it returns a surface of
the form v = F(x, y). This surface always passes through the sample
values at the point locations. You can evaluate this surface at any
query point, (xq,yq), to produce an interpolated value, vq.
http://au.mathworks.com/help/matlab/math/interpolating-scattered-data.html
Scattered data consists of a set of points X and corresponding values
V, where the points have no structure or order between their relative
locations. There are various approaches to interpolating scattered
data. One widely used approach uses a Delaunay triangulation of the
points.

Coordinate normalization for NN input in matlab

I am trying to implement a classification NN in Matlab.
My inputs are clusters of coordinates from an image. (Corresponding to delaunay triangulation vertexes)
There are 3 clusters (results of the optics algorithm) in this format:
( Not all clusters are of the same size.). Elements represent coordinates in euclidean 2d space . So (110,12) is a point in my image and the matrix depicted represents one cluster of points.
Clustering was done on image edges. So coordinates refer to logical values (always 1s in this case) on the image matrix.(After edge detection there are 3 "dense" areas in an image, and these collections of pixels are used for classification). There are 6 target classes.
So, my question is how can I format them into single column vector inputs to use in a neural network?
(There is a relevant answer here but I would like some elaboration if possible. ( I am probably too tired right now from 12 hours of trying stuff and dont get it 100% :D :( )
Remember, there are 3 different coordinate matrices for each picture, so my initial thought was, create an nn with 3 inputs (of different length). But how to serialize this?
Here's a cluster with its tags on in case it helps:
For you to train the classifier, you need a matrix X where each row will correspond to an image. If you want to use a coordinate representation, this means all images will have to be of the same size, say, M by N. So, the row of an image will have M times N elements (features) and the corresponding feature values will be the cluster assignments. Class vector y will be whatever labels you have, that is one of the six different classes you mentioned through the comments above. You should keep in mind that if you use a coordinate representation, X can get very high-dimensional, and unless you have a large number of images, chances are your classifier will perform very poorly. If you have few images, consider using fractions of pixels belonging to clusters that I suggested in one of the comments: this can give you a shorter feature description that is invariant to rotation and translation, and may yield better classification.

Save the values of Surface function in MATLAB

I use the surf function in MATLAB in order to create a 3D map.
This map is created by 2 vectors and for each combination of their values I have a third one that creates the surface.
Is there a way to save for each iteration these sets of three values somehow?
What I want to achieve is to have a matrix which will be as a map for every possible combination of these values.
I've made some efforts with 3D matrices but I made it even more complex and I had many errors because of the dimensions.

Controlled random number/dataset generation in MATLAB

Say, I have a cube of dimensions 1x1x1 spanning between coordinates (0,0,0) and (1,1,1). I want to generate a random set of points (assume 10 points) within this cube which are somewhat uniformly distributed (i.e. within certain minimum and maximum distance from each other and also not too close to the boundaries). How do I go about this without using loops? If this is not possible using vector/matrix operations then the solution with loops will also do.
Let me provide some more background details about my problem (This will help in terms of what I exactly need and why). I want to integrate a function, F(x,y,z), inside a polyhedron. I want to do it numerically as follows:
$F(x,y,z) = \sum_{i} F(x_i,y_i,z_i) \times V_i(x_i,y_i,z_i)$
Here, $F(x_i,y_i,z_i)$ is the value of function at point $(x_i,y_i,z_i)$ and $V_i$ is the weight. So to calculate the integral accurately, I need to identify set of random points which are not too close to each other or not too far from each other (Sorry but I myself don't know what this range is. I will be able to figure this out using parametric study only after I have a working code). Also, I need to do this for a 3D mesh which has multiple polyhedrons, hence I want to avoid loops to speed things out.
Check out this nice random vectors generator with fixed sum FEX file.
The code "generates m random n-element column vectors of values, [x1;x2;...;xn], each with a fixed sum, s, and subject to a restriction a<=xi<=b. The vectors are randomly and uniformly distributed in the n-1 dimensional space of solutions. This is accomplished by decomposing that space into a number of different types of simplexes (the many-dimensional generalizations of line segments, triangles, and tetrahedra.) The 'rand' function is used to distribute vectors within each simplex uniformly, and further calls on 'rand' serve to select different types of simplexes with probabilities proportional to their respective n-1 dimensional volumes. This algorithm does not perform any rejection of solutions - all are generated so as to already fit within the prescribed hypercube."
Use i=rand(3,10) where each column corresponds to one point, and each row corresponds to the coordinate in one axis (x,y,z)

How to locate the center of the peaks of a mesh 3d plot automatically?

I have been working on the 3d plots for a while in matlab. I have plotted three matrices on the mesh plot with a 40 by 40 grid and I could see peaks manually and locate the peaks manually but I'm not able to find a method to locate the peaks automatically using logic of the 40 by 40 1600 point grid where we can compare each vertice and see which one has the maximum number of points and thus automatically get the vertices. If someone could help me how to read the data of the plot and thus define a logic to compare the vertices.
I argue that you are trying to find local maxima (and/or minima) in your matrix.
In this case, there is a function in the Cumputer Vision toolbox if you have it. If don't, you can simply create a function that iterates over elements of your matrix and takes a portion of it (let's say, 3x3, but could be also 5x5 or more). Then you check if the central element is the biggest of that small matrix, and if it is you add the coordinates (row and col indexes) in a vector.
You can also give the size of the sub-matrix as a parameter, to define how sensitive is your function to small peaks.