-
I'm trying to resample and reproject some data from a global, regular latlon grid to much smaller LAEA grid, which is essentially going from a global gris defined in degrees to a smaller region defined in meters. The problem comes in here: result = kd_tree.resample_nearest(grid_def, da.values, area_def, radius_of_influence=50000, epsilon=0.5) Code example import os
os.environ["PROJ_LIB"] = "/root/anaconda3/envs/exp/share/proj"
import json
import xarray as xr
import numpy as np
from pyresample.geometry import AreaDefinition, GridDefinition
from pyresample import kd_tree, save_quicklook
def prj4_string_to_dict(pj4str):
ret = {}
segs = pj4str.split(' ')
for s in segs:
if s == '': continue
if '+units' in s:
ret['units'] = s.split('=')[-1]
elif '+proj' in s:
ret['proj'] = s.split('=')[-1]
elif '+lat_0' in s:
ret['lat_0'] = s.split('=')[-1]
elif '+lon_0' in s:
ret['lon_0'] = s.split('=')[-1]
elif '+a' in s:
ret['R'] = s.split('=')[-1]
elif '+ellps' in s:
ret['ellps'] = s.split('=')[-1]
elif '+over' in s:
ret['over'] = s.split('=')[-1]
else:
print(f'No logic for {s}')
print(segs)
return ret
# Load the data array from the netCDF file
da = xr.open_dataarray('gfs_orography.nc', engine='netcdf4')
lon2d, lat2d = np.meshgrid(da.coords['lon'].values, da.coords['lat'].values)
# there it is required that the coordinates arrays are the same shape, which necessitates the following:
grid_def = GridDefinition(lons=lon2d, lats=lat2d)
# Load the region projection info from a JSON file
with open('rois.json', 'r') as json_file:
region_config = json.load(json_file)
for roi, info in region_config.items():
x_len = info["xLen"]
y_len = info["yLen"]
resolution = info['defaultBin']
projString = info['projection']
# Calculate coordinates for the sector polygon
x_min, x_max = -x_len / 2, x_len / 2
y_min, y_max = -y_len / 2, y_len / 2
area_extent = (x_min, y_min, x_max, y_max)
area_id = regionName
description = regionName
proj_id = regionName
projection = prj4_string_to_dict(projString)
width = x_len
height = y_len
area_def = AreaDefinition(area_id, description, proj_id, projection, width, height, area_extent)
print(area_def)
result = kd_tree.resample_nearest(grid_def, da.values, area_def, radius_of_influence=50000, epsilon=0.5) Error: Exception has occurred: _ArrayMemoryError (note: full exception trace is shown but execution is paused at: <module>)
Unable to allocate 33.8 TiB for an array with shape (37158912000000,) and data type bool
File "/home/synrad/sw/mlde-rts/shapefile_extract_slice.py", line 74, in <module> (Current frame)
result = kd_tree.resample_nearest(grid_def,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
numpy.core._exceptions._ArrayMemoryError: Unable to allocate 33.8 TiB for an array with shape (37158912000000,) and data type bool Why is it pulling so much memory? Can I define the source grid by just using 1D coords? Is there a way to do that? The shape of the source data is: 741, 1400 and the target region is: extent: (-4032000.0, -2304000.0, 4032000.0, 2304000.0) in meters. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 38 replies
-
Side suggestion: You can get rid of the projection string -> dict conversion. AreaDefinitions understand anything pyproj CRS objects understand so WKT, EPSG, PROJ.4 strings, PROJ.4 dictionaries, or CRS objects themselves should all work. For your GridDefinition, you don't need to define it as a GridDefinition. If these are equally spaced pixels (which they are) then creating an AreaDefinition with the equivalent CRS (projection) and calculating extents in degrees should be fine. Note the extents are the outer edges of the pixels, not the centers of the pixels. This should at least save you the memory for the creation of the GridDefinition, but...what is the width and height of your target Area? Are you sure on the size of your input data? Can you print out all the shapes involved in this code? |
Beta Was this translation helpful? Give feedback.
-
I've gotten the NN to work very well, but ran into a problem with gradient_search for bilinear interpolation. In my case I'm going from from pyresample.geometry import AreaDefinition
from pyresample import kd_tree, gradient
from pyresample import load_area
def gradient_bilinear(source_def, target_def, data):
resampler = gradient.create_gradient_search_resampler(source_def, target_def)
regrid = resampler(data)
return regrid
print(f'Extract parameters from the definition: {regionName}')
targ_def = load_area('/home/rois.yaml', regionName)
# convert to a double
data = da_src.astype('float64')
result = gradient_bilinear(src_def, targ_def, data.values) But got the following error: Exception has occurred: TypeError
'ResampleBlocksGradientSearchResampler' object is not callable
File "sector_reprojection.py", line 32, in gradient_bilinear
regrid = resampler(data)
^^^^^^^^^^^^^^^ Am I doing this correct? Not much help in the docs on this method. |
Beta Was this translation helpful? Give feedback.
And no, pyresample needs to compute all the coordinates for these areas so it can: