[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in perform_PCA/ Issue with data formatting #27

Closed
nmileva opened this issue Mar 12, 2024 · 3 comments
Closed

Error in perform_PCA/ Issue with data formatting #27

nmileva opened this issue Mar 12, 2024 · 3 comments

Comments

@nmileva
Copy link
nmileva commented Mar 12, 2024

When I perform the dimensionality reduction, as shown here:

PCA_Output <- perform_PCA(Input_Image_File = Input_Image_File, 
                          Input_Mask_File = Input_Mask_File,
                          Output_Dir = Output_Dir, 
                          TypePCA = TypePCA, 
                          FilterPCA = FilterPCA,
                          nbCPU = nbCPU, 
                          MaxRAM = MaxRAM, 
                          Continuum_Removal = Continuum_Removal)

I get the following error:

[1] "Extract pixels from the images to perform PCA on a subset"
Error in `[[<-.data.frame`(`*tmp*`, "sampleIndex", value = 1:0) : 
  replacement has 2 rows, data has 0

When I try to run the code with the test image provided, it works. So, I assume the problem is with the way the data is formatted. I tried both with ENVI and .tif file formats but the error persists. Do you have any tips and tricks for formatting the data?

Here is a subset of the Sentinel-2 tile I have used, together with the .hdr file: https://megastore.rz.uni-augsburg.de/get/9jgRjuA97J/

I use R version 4.3.3, biodivMapR version 1.14.0.

@jbferet
Copy link
Owner
jbferet commented Mar 15, 2024

Dear @nmileva
thank you for raising this issue.
it seems that the tif file you sent does not match with the information provided in the hdr file, indeed. I tried to generate a hdr file matching with the tif, but it did not work.
Right now, the easiest way I found to deal with your problem is to convert your tif image into a ENVI format. However, the hdr file created does not allow to correctly read the original tif file, so I guess there is a question of data encoding or driver which is not correctly identfied.
You can perform the conversion to ENVI file with terra

rasttif <- terra::rast('S2A_T33UUQ_20220627_test_subset.tif')
dir.create('ENVI')
terra::writeRaster(x = rasttif, filename = 'ENVI/S2A_T33UUQ_20220627_test_subset', filetype = 'ENVI')

then, once you get the resulting binary and hdr file, you can update the information required for biodivMapR to run properly:

HDR <- biodivMapR::read_ENVI_header(HDRpath = 'ENVI/S2A_T33UUQ_20220627_test_subset.hdr')
HDR$wavelength <- c(496.6, 560, 664.5, 703.9, 740.2, 782.5, 835.1, 864.8, 1613.7, 2202.4)
HDR$`band names` <- c('B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12')
biodivMapR::write_ENVI_header(HDR = HDR, HDRpath = 'ENVI/S2A_T33UUQ_20220627_test_subset.hdr')

Sorry, this is not very practical. A new version of biodivMapR should be released soon, with major updates so that there will not be any problem to run it with any raster format you want...
I hope this helps.

Best regards,
jb

@jbferet
Copy link
Owner
jbferet commented Mar 15, 2024

PS: the initial error may come from the range of values which is not correctly read and does not correspond to standard S2 reflectance values when using the hdr file you used. Then if you updated the mask with radiometric filtering, it will mask everything in your image, and you will get empty pixel values, I think

@nmileva
Copy link
Author
nmileva commented Mar 19, 2024

Thanks! This worked. As you suggested, I needed to adjust also the radiometric filters to the range of reflectance values I have.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants