Skip to content
Prev 20315 / 29559 Next

How to parallize my code?

Yes, but why you are quering each point individually (one by one) in a
loop, and why you are writing every single data point in a different
file?  Loops are very slow in R, and reading/writing from/to a
disk file is also a very slow process.  Do it several hundred thousand
times and it is no surprise that this takes forever.  I do not have time to examine
your code either (next time provide a minimally reproducible example,
not a long script), but it seems that you have some netCDF files with
environmental data and a shapefile with locations, and you want to get
the environmental data for each location.  If this is what you want to
do, you should:

a) Read the shapefiles into a SpatialPoints object.
b) Use the raster package and read the netCDF rasters into a rasterbrick
object.
c) Use the over function and do an overlay between the points and the
rasterbrick.  You will get a SpaitalPointsDataFrame object, with the
data associated to each point.
d) Save the data into a single file.

This should take some time, say 20-30 minutes perhaps, maybe much less, depending on your
machine speed.

Julian


ping yang writes: