Convert UTM to latlon rasterBrick R - r

library(raster)
library(ncdf)
library(rgdal)
I am facing some issues converting UTM to latlon in a rasterBrick object. My code is below:
Example file can be found here:
https://www.dropbox.com/s/yv4opmrx1v4whpt/2013_000_CaPA_continental_v2.3-analyse_final_10km_6h_APCP.nc.7z?dl=0
rasnc<- brick('file.nc', varname = "APCP_surface")
rasnc
#class : RasterBrick
#dimensions : 824, 935, 770440, 1460 (nrow, ncol, ncell, nlayers)
#resolution : 10000, 10000 (x, y)
#extent : -5000, 9345000, -5000, 8235000 (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
wgs84.p4s <- "+proj=longlat +datum=NAD83 +ellps=GRS80 +no_defs"
# reprojecting
rx <- projectRaster(from=rasnc, crs=wgs84.p4s,method="ngb")
The above does not convert from UTM to latlon. I even tried to write the file to GeoTIFF and then re-project but it still did not work.
writeRaster(rasnc, file="myfil.tif", format="GTiff", overwrite=TRUE)
Basically I am trying to:
read a netCDF file.nc using brick
convert the UTM coordinates to latlon
crop the RasterBrick object to ext <- extent(-141.01807,-52.61941,41.68144.0,83.13550)
display any of the layers using levelplot function.
The time series is 6-hourly from Jan-01-2013 to dec-01-2013
It is true my use of the older raster package version was the source of the problem as suggested by #RobertH. rasnc has no coord.ref. From the webpage (http://weather.gc.ca/grib/grib2_RDPA_ps10km_e.html) of the data modellers, it is stated a polar-stereographic (PS) grid covering North America and adjacent waters with a 10 km resolution at 60 degrees north is used. How can I get the right PS for rasnc after which I will apply projectRaster to rasnc to get latlon coordinates

The problem is that rasnc reports that it has a lon/lat coordinate reference system (#coord. ref. : +proj=longlat +datum=WGS84); which looks incorrect. If you know what it should be, (you say UTM, but what is the zone?), assign it:
crs(rasnc) <- '+proj=utm +zone=??? +datum=WGS84'
and then things should work.
The question is, how did this go wrong?
I get:
library(raster)
r <- brick("2013_000_CaPA_continental_v2.3-analyse_final_10km_6h_APCP.nc")
#Loading required namespace: ncdf4
#r
#class : RasterBrick
#dimensions : 824, 935, 770440, 1460 (nrow, ncol, ncell, nlayers)
#resolution : 10000, 10000 (x, y)
#extent : -5000, 9345000, -5000, 8235000 (xmin, xmax, ymin, ymax)
#coord. ref. : NA
which is correct as this file does not provide coordinate reference system information.
First update your version of raster (you are using ncdf instead of ncdf4, so we can see it is old); and do not leave out code (if you set the coord. ref. system yourself)

Related

Merge (mosaic) of rasters changes resolution

I'm merging two MODIS DSR tiles using a R script that I developed, these are the products:
https://drive.google.com/drive/folders/1RG3JkXlbaotBax-h5lEMT7lEn-ObwWsD?usp=sharing
So, I open both products (tile h15v05 and tile h16v05) from same date (2019180), then I open each SDS and merge them together (00h from h15v05 with 00h from h16v05 and so on...)
Visualisation on Panoply (using the merge option) of the two products:
Purple square is the location of the division line that separates the two tiles.
With my code I obtain a plot with pixels with different resolution (and different min/max values) and I don't understand why:
I suspect that the results obtained are due to:
1- Changing from Sinusoidal CRS to longlat WGS84 CRS;
2- Using resample (method ngb) to work with mosaic.
My code is extensive, but here are some parts of it:
# Open scientific dataset as raster
SDSs <- sds(HDFfile)
SDS <- SDSs[SDSnumber]
crs(SDS) <- crs("+proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs")
SDSreprojected <- project(SDS, DesiredCRS)
SDSasRaster <- as(SDSreprojected, "Raster")
# Resample SDS based on a reference SDS (SDS GMT_1200_DSR of a first product), I need to do this to be able to use mosaic
SDSresampled <- resample(SDSasRaster,ResampleReference_Raster,method='ngb')
# Create mosaic of same SDS, but first convert stack to list to use mosaic
ListWith_SameSDS_OfGroupFiles <- as.list(StackWith_SameSDS_OfGroupFiles)
ListWith_SameSDS_OfGroupFiles.mosaicargs <- ListWith_SameSDS_OfGroupFiles
ListWith_SameSDS_OfGroupFiles.mosaicargs$fun <- mean
SDSmosaic <- do.call(mosaic, ListWith_SameSDS_OfGroupFiles.mosaicargs)
# Save SDSs mosaic stack to netCDF
writeRaster(StackWith_AllMosaicSDSs_OfGroupFiles, NetCDFpath, overwrite=TRUE, format="CDF", varname= "DSR", varunit="w/m2", longname="Downward Shortwave Radiation", xname="Longitude", yname="Latitude", zname="TimeGMT", zunit="GMT")
Does anyone have an idea of what could be the cause of this mismatch between results?
print(ResampleReference_Raster)
class : RasterLayer
dimensions : 1441, 897, 1292577 (nrow, ncol, ncell)
resolution : 0.01791556, 0.006942043 (x, y)
extent : -39.16222, -23.09196, 29.99652, 40 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : MCD18A1.A2019180.h15v05.061.2020343034815
values : 227.5543, 970.2346 (min, max)
print(SDSasRaster)
class : RasterLayer
dimensions : 1399, 961, 1344439 (nrow, ncol, ncell)
resolution : 0.01515284, 0.007149989 (x, y)
extent : -26.10815, -11.54627, 29.99717, 40 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : MCD18A1.A2019180.h16v05.061.2020343040755
values : 0, 0 (min, max)
print(SDSmosaic)
class : RasterLayer
dimensions : 1441, 897, 1292577 (nrow, ncol, ncell)
resolution : 0.01791556, 0.006942043 (x, y)
extent : -39.16222, -23.09196, 29.99652, 40 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84 +no_defs
source : memory
names : layer
values : 0, 62.7663 (min, max)
Also, some of the islands were ignored by the script (bottom right)...
sorry I didn't reply earlier. So I think you're right that this issue is extent to which you are resampling. I think you might be able to get around this by creating a dummy raster that has the extent of the raster you want to resample, but has the resolution of the raster you want to mosaic to.Try:
dummy<-raster(ext = SDSasRaster#extent, resolution=ResampledReference_Raster#res, crs=SDSasRaster#crs)
SDS2<-resample(SDSasRaster, dummy, method="ngb")
Final<-moasic(SDS2, ResampledReference_Raster, fun=mean)

Reprojecting list of raster stacks

I have a big list of raster stacks and I want to reproject and then clip them in R. I have done the same procedure in ArcGIS using batch processing which was significantly faster.
It is not going well in R. Any suggestion to improve the process?
my rstack.lst consist of 19 raster stacks like this:
class : RasterStack
dimensions : 4800, 7200, 34560000, 46 (nrow, ncol, ncell, nlayers)
resolution : 463.3127, 463.3127 (x, y)
extent : 11119505, 14455357, -5559753, -3335852 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=sinu +lon_0=0 +x_0=0 +y_0=0 +a=6371007.181 +b=6371007.181 +units=m +no_defs
names : ...
I am using this code:
for (i in 1:length(rstack.lst)){
rstack.lst[[i]] <- projectRaster(rstack.lst[[i]], crs = m.crs) # reproject
rstack.lst[[i]] <- crop(rstack.lst[[i]], brdshp) # crop
rstack.lst[[i]] <- mask(rstack.lst[[i]], brdshp) # mask
print (i)
}
not even one I is printed after two hours!

Identify CRS in raster file

I would like to identify the correct coordinate reference system for the following ASCII raster file:
class : RasterLayer
dimensions : 2160, 4320, 9331200 (nrow, ncol, ncell)
resolution : 0.0833333, 0.0833333 (x, y)
extent : -180, 179.9999, -90, 89.99993 (xmin, xmax, ymin, ymax)
coord. ref. : NA
data source : C:/popc_0AD.asc
names : popc_0AD
I tried to guess the correct projection by setting the CRS to some of the common formats and plotting it, as suggested in related posts. But I am still not sure about the correct setting. As far as I am concerned raster and related packages do not entail any function able to estimate missing CRS information. Do you have any idea what this raster file's CRS could be or how to find out?
The extent suggests coordinates are not projected. This seems to be the extent of Earth in degrees.
Then, you may want to use EPSG 4326, which is also crs="+proj=longlat +datum=WGS84 +no_defs":
library(raster)
r <- raster("0AD_lu/cropland0AD.asc")
projection(r) <- "+proj=longlat +datum=WGS84 +no_defs"
However, it is much better to use dataset correctly built with the coordinates reference system. It is never recommended to guess it... But I know that having clean metadata is not always possible...
You have
r <- raster(nrow=2160, ncol=4320, xmn=-180, xmx=179.9999, ymn=-90, ymx=89.99993, crs=NA)
Sébastien Rochette already pointed out that this is surely lon/lat and that you can set the CRS to relfect that
crs(r) <- "+proj=longlat +datum=WGS84"
It seems to me that the extent is a bit suspect. It looks like it is supposed to be a global raster, but that there has been some loss of precision. If so, you may correct that like this:
extent(r) <- c(-180, 180, -90, 90)
To get
r
#class : RasterLayer
#dimensions : 2160, 4320, 9331200 (nrow, ncol, ncell)
#resolution : 0.08333333, 0.08333333 (x, y)
#extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

Make raster stack with different extent

I am in trouble making raster stack which have slightly different extent. The answer (1st one) given here is useful but did not help in my case. For example, I want to make a raster stack using bio2 raster for Australia and this Australian raster. The second raster comes for Australia only and the first one is global. So I cropped the global bio2 raster to the same extent of Australian raster using crop() function, but the resultant raster extent (i.e., bio2.au) is slightly different (therefore, I cannot make raster using the cropped raster and the Australian raster, awc). Sample code is below:
library(raster)
awc <- raster("path to Australian raster")
bio2.g <- raster("path to Bio2 global raster")
# crop bio2.g to the same extent of awc
bio2.au <- crop(bio2.g, extent(awc))
# make a raster stack
st <- stack(awc, bio2.au)
Error in compareRaster(x) : different extent
I have also tried using quick=TRUE within the stack() function. But in this case the cell values in awc is lost. Note: the size of awc raster is 4gb.
# first make a list of rasters saved in the computer
li <- list.files("path to file", pattern = ".tif$", full.names = TRUE)
st <- stack(li, quick=TRUE)
st[[1]] # no cell values for awc
Your suggestions will be highly appreciated. My ultimate goal is to crop several bioclim rasters to the same extent of Australian raster awc and stack them together so that raster cell values are not lost.
Edit (after comment of #Cobin):
Below is the attribute of each raster
# global raster (bigger raster)
> r
class : RasterLayer
dimensions : 21600, 43200, 933120000 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:\Worldclim2_Bioclim\wc2.0_bio_30s_02.tif
names : wc2.0_bio_30s_02
values : 0, 37.06667 (min, max)
# Australian raster (smaller raster)
> r1
class : RasterLayer
dimensions : 43201, 49359, 2132358159 (nrow, ncol, ncell)
resolution : 0.0008333333, 0.0008333333 (x, y)
extent : 112.8921, 154.0246, -44.00042, -7.999583 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:\SoilAWC5cm.EV1.tif
names : SoilAWC5cm.EV1
values : 2.997789, 27.86114 (min, max)
# new raster, after crop() function is applied
> r2 <- crop(r,extent(r1))
> r2
class : RasterLayer
dimensions : 4320, 4936, 21323520 (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333 (x, y)
extent : 112.8917, 154.025, -44, -8 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\Users\Anwar\AppData\Local\Temp\Rtmpmg9fyF\raster\r_tmp_2018-11-23_164300_11308_65747.grd
names : wc2.0_bio_30s_02
values : 1.933333, 18.15833 (min, max)
# rebuild r2 to match r1
> r22 <- raster(vals=values(r2),ext=extent(r1), nrows=dim(r1)[1],ncols=dim(r1)[2])
Error in setValues(r, vals) :
length(values) is not equal to ncell(x), or to 1
I suppose that the extent of two raster are differet though the raster masked by crop function.You
should check the both of awc and bio.au extent base on same reolution, rows and columns. Because I couldn't download data from
hyperlink, I give an example of my own data.
r <- raster('/big_raster')
r1 <- raster('/small_raster')
r2 <- crop(r,extent(r1))
r1
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.0833333, 0.0833333 (x, y)
extent : 89.2185, 102.3018, 30.96238, 37.12905 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : D:\D\temp\Rtest\modis8km.tif
names : modis8km
values : -32768, 32767 (min, max)
r2
class : RasterLayer
dimensions : 74, 157, 11618 (nrow, ncol, ncell)
resolution : 0.08333333, 0.08333333 (x, y)
extent : 89.25, 102.3333, 31, 37.16667 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names : g201401a
values : -32768, 7789 (min, max)
Though r1 and r1 with same resolution and dimension, the extent have tiny offset. It cause stack error.
stack(r1,r2)
Error in compareRaster(x) : different extent
So, you should rebuid the r2 to match r1:
r22 <- raster(vals=values(r2),ext=extent(r1),crs=crs(r1),
nrows=dim(r1)[1],ncols=dim(r1)[2])
Now stack(r22,r1) will be successful.

Sentinel-2 R gdal raster

I would like to make a piece of code I have more efficient.
Right now I download Sentinel-2 data in jp2 format via the open acceshub. The jp2 files that I download have, for some reason, a wrong extent. Right now I correct this in the following way (in which file is the filename of the jp2):
r = raster(file)
extent(r) = new_extent
writeRaster(r, file)
This method, however, writes the entire raster (which takes ages) whereas I only changed a minor detail.
Is there a neat way using gdal or the raster package to do this more efficiently?
If I print the raster I see:
class : RasterLayer
dimensions : 1830, 1830, 3348900 (nrow, ncol, ncell)
resolution : 60, 60 (x, y)
extent : 499980, 609780, 6690240, 6800040 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=55 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : /home/daniel/R/farmhack/x.jp2
names : x
values : 0, 65535 (min, max)
I do not know what this extent means.

Resources