0

I opened a .nc raster file with terra::rast. It has 42 bands. The raster reads in upside down & needs to be flipped vertically. When I flip one band from the spatRast object, it flips properly. code below

library(terra)

s2file <- "S2A_MSI_2018_07_24_18_49_21_T10SFH_L2W.nc"
S2Rrs <- rast(s2file)

S2Rrs
#class       : SpatRaster 
#dimensions  : 5490, 5490, 41  (nrow, ncol, nlyr)
#resolution  : 1, 1  (x, y)
#extent      : 0.5, 5490.5, 0.5, 5490.5  (xmin, xmax, ymin, ymax)
#coord. ref. :  
#sources     : S2A_MSI_2018_07_24_18_49_21_T10SFH_L2W.nc:spm_nechad  
#              S2A_MSI_2018_07_24_18_49_21_T10SFH_L2W.nc:spm_nechad2016  
#              S2A_MSI_2018_07_24_18_49_21_T10SFH_L2W.nc:t_nechad  
#              ... and 38 more source(s)
#varnames    : spm_nechad (Suspended Particulate Matter) 
#              spm_nechad2016 (Suspended Particulate Matter) 
#              t_nechad (Water turbidity) 
#              ...
#names       : spm_nechad, spm_n~d2016, t_nechad, t_nechad2016, t_dogliotti, chl_re_gons, ... 
#unit        :     g m^-3,      g m^-3,      FNU,          FNU,         FNU,     mg m^-3, ... 

names(S2Rrs)
# [1] "spm_nechad"        "spm_nechad2016"    "t_nechad"         
# [4] "t_nechad2016"      "t_dogliotti"       "chl_re_gons"      
# [7] "chl_re_moses3b"    "chl_re_moses3b740" "chl_re_gons740"   
#[10] "chl_re_mishra"     "slh"               "a443_qaasw"       
#[13] "a490_qaasw"        "a560_qaasw"        "a665_qaasw"       
#[16] "bbp443_qaasw"      "bbp490_qaasw"      "bbp560_qaasw"     
#[19] "bbp665_qaasw"      "rhos_443"          "rhos_492"         
#[22] "rhos_560"          "rhos_665"          "rhos_704"         
#[25] "rhos_740"          "rhos_783"          "rhos_833"         
#[28] "rhos_865"          "rhos_1614"         "rhos_2202"        
#[31] "Rrs_443"           "Rrs_492"           "Rrs_560"          
#[34] "Rrs_665"           "Rrs_704"           "Rrs_740"          
#[37] "Rrs_783"           "Rrs_833"           "Rrs_865"          
#[40] "Rrs_1614"          "Rrs_2202"     

I then assign the CRS & extent

rastcrs <- "+proj=utm +zone=10 +datum=WGS84 +units=m +no_defs"
rextent <- c(600000, 709800, 4190220, 4300020)

When I try to flip the whole object, I get this error:

x <- flip(S2Rrs)
#Error: [flip] too few values for writing: 32863140 < 150360120
#In addition: There were 41 warnings (use warnings() to see them)

One layer plots like this:

Single band plot

The input file is 5G.

I haven't found any guidance for this online. I tried flipping one layer from the spatRast object. A single layer flips properly, and plots rightside up.

1 Answer 1

0

I can reproduce this with the current CRAN version of terra

library(terra)
#terra 1.7.55
s <- rast(system.file("ex/logo.tif", package="terra"))   
terraOptions(todisk=TRUE)
y <- flip(s*1, steps=2)
#Error: [flip] too many values for writing: 11817 > 11514

This is a bug that affects some large raster datasets that have to be processed in chunks. It has now been fixed in "terra" version 1.7.57 (currently the development version). You should be able to install this version of terra from the R-Universe with

install.packages('terra', repos='https://rspatial.r-universe.dev')
2
  • Thank you! That seems to work. However, the terra::flip step takes a really long time (over 20 min). And for some images still fails with an error: Error: [flip] incorrect start and/or nrows value. Any ideas?
    – Dulci
    Commented Oct 19, 2023 at 15:40
  • this error still occurs in terra ‘1.7.71’, Error: [flip] incorrect start and/or nrows. Raster has ~35m cells
    – Sam
    Commented May 23 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.