How to interpolate 2D array from a coarser resolution to finer resolution
Suppose that I have an emission data with shape (21600,43200)
,
which corresponds to the lat
and lon
,i.e,
lat = np.arange(21600)*(-0.008333333)+90
lon = np.arange(43200)*0.00833333-180
And I also have a scaling factor with shape of (720,1440,7)
,which corresponds to lat
, lon
, day of week
, and
lat = np.arange(720)*0.25-90
lon = np.arange(1440)*0.25-180
For now, I want to apply the factor to the emission data and I think I need to interpolate the factor on (720,1440)
to (21600,43200)
. After that I can multiply the interpolated factor with the emission data to get the new emission output.
But I have a difficulty on the interpolation method.
Could anyone give me some suggestions?
numpy scipy netcdf python-xarray
add a comment |
Suppose that I have an emission data with shape (21600,43200)
,
which corresponds to the lat
and lon
,i.e,
lat = np.arange(21600)*(-0.008333333)+90
lon = np.arange(43200)*0.00833333-180
And I also have a scaling factor with shape of (720,1440,7)
,which corresponds to lat
, lon
, day of week
, and
lat = np.arange(720)*0.25-90
lon = np.arange(1440)*0.25-180
For now, I want to apply the factor to the emission data and I think I need to interpolate the factor on (720,1440)
to (21600,43200)
. After that I can multiply the interpolated factor with the emission data to get the new emission output.
But I have a difficulty on the interpolation method.
Could anyone give me some suggestions?
numpy scipy netcdf python-xarray
1
scipy.interpolate.interp2d
should solve your problem... as long as you can fit everything in memory (if you can't, you could compute the interpolation by pieces).
– jdehesa
Nov 20 '18 at 12:14
add a comment |
Suppose that I have an emission data with shape (21600,43200)
,
which corresponds to the lat
and lon
,i.e,
lat = np.arange(21600)*(-0.008333333)+90
lon = np.arange(43200)*0.00833333-180
And I also have a scaling factor with shape of (720,1440,7)
,which corresponds to lat
, lon
, day of week
, and
lat = np.arange(720)*0.25-90
lon = np.arange(1440)*0.25-180
For now, I want to apply the factor to the emission data and I think I need to interpolate the factor on (720,1440)
to (21600,43200)
. After that I can multiply the interpolated factor with the emission data to get the new emission output.
But I have a difficulty on the interpolation method.
Could anyone give me some suggestions?
numpy scipy netcdf python-xarray
Suppose that I have an emission data with shape (21600,43200)
,
which corresponds to the lat
and lon
,i.e,
lat = np.arange(21600)*(-0.008333333)+90
lon = np.arange(43200)*0.00833333-180
And I also have a scaling factor with shape of (720,1440,7)
,which corresponds to lat
, lon
, day of week
, and
lat = np.arange(720)*0.25-90
lon = np.arange(1440)*0.25-180
For now, I want to apply the factor to the emission data and I think I need to interpolate the factor on (720,1440)
to (21600,43200)
. After that I can multiply the interpolated factor with the emission data to get the new emission output.
But I have a difficulty on the interpolation method.
Could anyone give me some suggestions?
numpy scipy netcdf python-xarray
numpy scipy netcdf python-xarray
edited Nov 20 '18 at 18:00


tel
7,34121431
7,34121431
asked Nov 20 '18 at 11:36


Allen ZhangAllen Zhang
178
178
1
scipy.interpolate.interp2d
should solve your problem... as long as you can fit everything in memory (if you can't, you could compute the interpolation by pieces).
– jdehesa
Nov 20 '18 at 12:14
add a comment |
1
scipy.interpolate.interp2d
should solve your problem... as long as you can fit everything in memory (if you can't, you could compute the interpolation by pieces).
– jdehesa
Nov 20 '18 at 12:14
1
1
scipy.interpolate.interp2d
should solve your problem... as long as you can fit everything in memory (if you can't, you could compute the interpolation by pieces).– jdehesa
Nov 20 '18 at 12:14
scipy.interpolate.interp2d
should solve your problem... as long as you can fit everything in memory (if you can't, you could compute the interpolation by pieces).– jdehesa
Nov 20 '18 at 12:14
add a comment |
2 Answers
2
active
oldest
votes
Here's a complete example of the kind of interpolation you're trying to do. For example purposes I used emission
data with shape (10, 20)
and scale
data with shape (5, 10)
. It uses scipy.interpolate.RectBivariateSpline
, which is the recommended method for interpolating on regular grids:
import scipy.interpolate as sci
def latlon(res):
return (np.arange(res)*(180/res) - 90,
np.arange(2*res)*(360/(2*res)) - 180)
lat_fine,lon_fine = latlon(10)
emission = np.ones(10*20).reshape(10,20)
lat_coarse,lon_coarse = latlon(5)
scale = np.linspace(0, .5, num=5).reshape(-1, 1) + np.linspace(0, .5, num=10)
f = sci.RectBivariateSpline(lat_coarse, lon_coarse, scale)
scale_interp = f(lat_em, lon_em)
with np.printoptions(precision=1, suppress=True, linewidth=9999):
print('original emission data:n%sn' % emission)
print('original scale data:n%sn' % scale)
print('interpolated scale data:n%sn' % scale_interp)
print('scaled emission data:n%sn' % (emission*scale_interp))
which outputs:
original emission data:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
original scale data:
[[0. 0.1 0.1 0.2 0.2 0.3 0.3 0.4 0.4 0.5]
[0.1 0.2 0.2 0.3 0.3 0.4 0.5 0.5 0.6 0.6]
[0.2 0.3 0.4 0.4 0.5 0.5 0.6 0.6 0.7 0.8]
[0.4 0.4 0.5 0.5 0.6 0.7 0.7 0.8 0.8 0.9]
[0.5 0.6 0.6 0.7 0.7 0.8 0.8 0.9 0.9 1. ]]
interpolated scale data:
[[0. 0. 0.1 0.1 0.1 0.1 0.2 0.2 0.2 0.2 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5]
[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0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7]
[0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8]
[0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8 0.8 0.8]
[0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.8 0.9 0.9]
[0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]]
Notes
The interpolation methods in
scipy.interpolate
expect both x and y to be strictly increasing, so you'll have to make sure that youremission
data is arranged in a grid such that:
lat = np.arange(21600)*0.008333333 - 90
instead of:
lat = np.arange(21600)*(-0.008333333) + 90
like you have above. You can flip your
emission
data like so:
emission = emission[::-1, :]
add a comment |
If you're just looking for nearest neighbor or linear interpolation, you can use xarray's native da.interp
method:
scaling_interped = scaling_factor.interp(
lon=emissions.lon,
lat=emissions.lat,
method='nearest') # or 'linear'
note that this will dramatically increase the size of the array. Assuming these are 64-bit floats, the result will be approximately (21600*43200*7)*8/(1024**3)
or 48.7 GB
. You could cut the in-memory size by a factor of 7 by chunking the array by day of week and doing the computing out of core with dask.
If you want to use an interpolation scheme other than nearest or linear, use the method suggested by tel.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392177%2fhow-to-interpolate-2d-array-from-a-coarser-resolution-to-finer-resolution%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
Here's a complete example of the kind of interpolation you're trying to do. For example purposes I used emission
data with shape (10, 20)
and scale
data with shape (5, 10)
. It uses scipy.interpolate.RectBivariateSpline
, which is the recommended method for interpolating on regular grids:
import scipy.interpolate as sci
def latlon(res):
return (np.arange(res)*(180/res) - 90,
np.arange(2*res)*(360/(2*res)) - 180)
lat_fine,lon_fine = latlon(10)
emission = np.ones(10*20).reshape(10,20)
lat_coarse,lon_coarse = latlon(5)
scale = np.linspace(0, .5, num=5).reshape(-1, 1) + np.linspace(0, .5, num=10)
f = sci.RectBivariateSpline(lat_coarse, lon_coarse, scale)
scale_interp = f(lat_em, lon_em)
with np.printoptions(precision=1, suppress=True, linewidth=9999):
print('original emission data:n%sn' % emission)
print('original scale data:n%sn' % scale)
print('interpolated scale data:n%sn' % scale_interp)
print('scaled emission data:n%sn' % (emission*scale_interp))
which outputs:
original emission data:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
original scale data:
[[0. 0.1 0.1 0.2 0.2 0.3 0.3 0.4 0.4 0.5]
[0.1 0.2 0.2 0.3 0.3 0.4 0.5 0.5 0.6 0.6]
[0.2 0.3 0.4 0.4 0.5 0.5 0.6 0.6 0.7 0.8]
[0.4 0.4 0.5 0.5 0.6 0.7 0.7 0.8 0.8 0.9]
[0.5 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 0.6 0.6]
[0.1 0.2 0.2 0.2 0.2 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6]
[0.2 0.2 0.2 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7]
[0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8]
[0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8 0.8 0.8]
[0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.8 0.9 0.9]
[0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]]
Notes
The interpolation methods in
scipy.interpolate
expect both x and y to be strictly increasing, so you'll have to make sure that youremission
data is arranged in a grid such that:
lat = np.arange(21600)*0.008333333 - 90
instead of:
lat = np.arange(21600)*(-0.008333333) + 90
like you have above. You can flip your
emission
data like so:
emission = emission[::-1, :]
add a comment |
Here's a complete example of the kind of interpolation you're trying to do. For example purposes I used emission
data with shape (10, 20)
and scale
data with shape (5, 10)
. It uses scipy.interpolate.RectBivariateSpline
, which is the recommended method for interpolating on regular grids:
import scipy.interpolate as sci
def latlon(res):
return (np.arange(res)*(180/res) - 90,
np.arange(2*res)*(360/(2*res)) - 180)
lat_fine,lon_fine = latlon(10)
emission = np.ones(10*20).reshape(10,20)
lat_coarse,lon_coarse = latlon(5)
scale = np.linspace(0, .5, num=5).reshape(-1, 1) + np.linspace(0, .5, num=10)
f = sci.RectBivariateSpline(lat_coarse, lon_coarse, scale)
scale_interp = f(lat_em, lon_em)
with np.printoptions(precision=1, suppress=True, linewidth=9999):
print('original emission data:n%sn' % emission)
print('original scale data:n%sn' % scale)
print('interpolated scale data:n%sn' % scale_interp)
print('scaled emission data:n%sn' % (emission*scale_interp))
which outputs:
original emission data:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
original scale data:
[[0. 0.1 0.1 0.2 0.2 0.3 0.3 0.4 0.4 0.5]
[0.1 0.2 0.2 0.3 0.3 0.4 0.5 0.5 0.6 0.6]
[0.2 0.3 0.4 0.4 0.5 0.5 0.6 0.6 0.7 0.8]
[0.4 0.4 0.5 0.5 0.6 0.7 0.7 0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4 0.5 0.5 0.5 0.5 0.6 0.6]
[0.1 0.2 0.2 0.2 0.2 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6]
[0.2 0.2 0.2 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7]
[0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8]
[0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8 0.8 0.8]
[0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.8 0.9 0.9]
[0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]]
Notes
The interpolation methods in
scipy.interpolate
expect both x and y to be strictly increasing, so you'll have to make sure that youremission
data is arranged in a grid such that:
lat = np.arange(21600)*0.008333333 - 90
instead of:
lat = np.arange(21600)*(-0.008333333) + 90
like you have above. You can flip your
emission
data like so:
emission = emission[::-1, :]
add a comment |
Here's a complete example of the kind of interpolation you're trying to do. For example purposes I used emission
data with shape (10, 20)
and scale
data with shape (5, 10)
. It uses scipy.interpolate.RectBivariateSpline
, which is the recommended method for interpolating on regular grids:
import scipy.interpolate as sci
def latlon(res):
return (np.arange(res)*(180/res) - 90,
np.arange(2*res)*(360/(2*res)) - 180)
lat_fine,lon_fine = latlon(10)
emission = np.ones(10*20).reshape(10,20)
lat_coarse,lon_coarse = latlon(5)
scale = np.linspace(0, .5, num=5).reshape(-1, 1) + np.linspace(0, .5, num=10)
f = sci.RectBivariateSpline(lat_coarse, lon_coarse, scale)
scale_interp = f(lat_em, lon_em)
with np.printoptions(precision=1, suppress=True, linewidth=9999):
print('original emission data:n%sn' % emission)
print('original scale data:n%sn' % scale)
print('interpolated scale data:n%sn' % scale_interp)
print('scaled emission data:n%sn' % (emission*scale_interp))
which outputs:
original emission data:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
original scale data:
[[0. 0.1 0.1 0.2 0.2 0.3 0.3 0.4 0.4 0.5]
[0.1 0.2 0.2 0.3 0.3 0.4 0.5 0.5 0.6 0.6]
[0.2 0.3 0.4 0.4 0.5 0.5 0.6 0.6 0.7 0.8]
[0.4 0.4 0.5 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.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6]
[0.1 0.2 0.2 0.2 0.2 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6]
[0.2 0.2 0.2 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7]
[0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8]
[0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8 0.8 0.8]
[0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.8 0.9 0.9]
[0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]]
Notes
The interpolation methods in
scipy.interpolate
expect both x and y to be strictly increasing, so you'll have to make sure that youremission
data is arranged in a grid such that:
lat = np.arange(21600)*0.008333333 - 90
instead of:
lat = np.arange(21600)*(-0.008333333) + 90
like you have above. You can flip your
emission
data like so:
emission = emission[::-1, :]
Here's a complete example of the kind of interpolation you're trying to do. For example purposes I used emission
data with shape (10, 20)
and scale
data with shape (5, 10)
. It uses scipy.interpolate.RectBivariateSpline
, which is the recommended method for interpolating on regular grids:
import scipy.interpolate as sci
def latlon(res):
return (np.arange(res)*(180/res) - 90,
np.arange(2*res)*(360/(2*res)) - 180)
lat_fine,lon_fine = latlon(10)
emission = np.ones(10*20).reshape(10,20)
lat_coarse,lon_coarse = latlon(5)
scale = np.linspace(0, .5, num=5).reshape(-1, 1) + np.linspace(0, .5, num=10)
f = sci.RectBivariateSpline(lat_coarse, lon_coarse, scale)
scale_interp = f(lat_em, lon_em)
with np.printoptions(precision=1, suppress=True, linewidth=9999):
print('original emission data:n%sn' % emission)
print('original scale data:n%sn' % scale)
print('interpolated scale data:n%sn' % scale_interp)
print('scaled emission data:n%sn' % (emission*scale_interp))
which outputs:
original emission data:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
original scale data:
[[0. 0.1 0.1 0.2 0.2 0.3 0.3 0.4 0.4 0.5]
[0.1 0.2 0.2 0.3 0.3 0.4 0.5 0.5 0.6 0.6]
[0.2 0.3 0.4 0.4 0.5 0.5 0.6 0.6 0.7 0.8]
[0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2 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6]
[0.1 0.2 0.2 0.2 0.2 0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6]
[0.2 0.2 0.2 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7]
[0.3 0.3 0.3 0.3 0.4 0.4 0.4 0.4 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8]
[0.3 0.3 0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.8 0.8 0.8 0.8]
[0.4 0.4 0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.8 0.9 0.9]
[0.4 0.5 0.5 0.5 0.5 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]]
Notes
The interpolation methods in
scipy.interpolate
expect both x and y to be strictly increasing, so you'll have to make sure that youremission
data is arranged in a grid such that:
lat = np.arange(21600)*0.008333333 - 90
instead of:
lat = np.arange(21600)*(-0.008333333) + 90
like you have above. You can flip your
emission
data like so:
emission = emission[::-1, :]
edited Nov 20 '18 at 18:13
answered Nov 20 '18 at 17:57


teltel
7,34121431
7,34121431
add a comment |
add a comment |
If you're just looking for nearest neighbor or linear interpolation, you can use xarray's native da.interp
method:
scaling_interped = scaling_factor.interp(
lon=emissions.lon,
lat=emissions.lat,
method='nearest') # or 'linear'
note that this will dramatically increase the size of the array. Assuming these are 64-bit floats, the result will be approximately (21600*43200*7)*8/(1024**3)
or 48.7 GB
. You could cut the in-memory size by a factor of 7 by chunking the array by day of week and doing the computing out of core with dask.
If you want to use an interpolation scheme other than nearest or linear, use the method suggested by tel.
add a comment |
If you're just looking for nearest neighbor or linear interpolation, you can use xarray's native da.interp
method:
scaling_interped = scaling_factor.interp(
lon=emissions.lon,
lat=emissions.lat,
method='nearest') # or 'linear'
note that this will dramatically increase the size of the array. Assuming these are 64-bit floats, the result will be approximately (21600*43200*7)*8/(1024**3)
or 48.7 GB
. You could cut the in-memory size by a factor of 7 by chunking the array by day of week and doing the computing out of core with dask.
If you want to use an interpolation scheme other than nearest or linear, use the method suggested by tel.
add a comment |
If you're just looking for nearest neighbor or linear interpolation, you can use xarray's native da.interp
method:
scaling_interped = scaling_factor.interp(
lon=emissions.lon,
lat=emissions.lat,
method='nearest') # or 'linear'
note that this will dramatically increase the size of the array. Assuming these are 64-bit floats, the result will be approximately (21600*43200*7)*8/(1024**3)
or 48.7 GB
. You could cut the in-memory size by a factor of 7 by chunking the array by day of week and doing the computing out of core with dask.
If you want to use an interpolation scheme other than nearest or linear, use the method suggested by tel.
If you're just looking for nearest neighbor or linear interpolation, you can use xarray's native da.interp
method:
scaling_interped = scaling_factor.interp(
lon=emissions.lon,
lat=emissions.lat,
method='nearest') # or 'linear'
note that this will dramatically increase the size of the array. Assuming these are 64-bit floats, the result will be approximately (21600*43200*7)*8/(1024**3)
or 48.7 GB
. You could cut the in-memory size by a factor of 7 by chunking the array by day of week and doing the computing out of core with dask.
If you want to use an interpolation scheme other than nearest or linear, use the method suggested by tel.
answered Nov 26 '18 at 2:18
delgadomdelgadom
812718
812718
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53392177%2fhow-to-interpolate-2d-array-from-a-coarser-resolution-to-finer-resolution%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
scipy.interpolate.interp2d
should solve your problem... as long as you can fit everything in memory (if you can't, you could compute the interpolation by pieces).– jdehesa
Nov 20 '18 at 12:14