X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 11 Jun 2019 04:13 PM by  Ben Castellani
band math for cycle
 1 Replies
Sort:
You are not authorized to post a reply.
Author Messages

Gabriele Amato



New Member


Posts:
New Member


--
05 Jun 2019 03:21 AM
    Dear all,
    I'd like to perform the same band math expression (to calculate NDVI) on three different Sentinel-2 images, located in different folders.
    I'd also like to save them with different names that maintain a part of the names of the original files.
    I wrote this code where first I list the paths of the images ( in Files=[... ) and then I run a for cycle to perform the NDVI of each of them and to save the results with different names, which recall the name of the original images:

    pro idl_bandmath
    compile_opt idl2, hidden
    on_error, 2

    Files=[ 'D:\\Sentinel-2\\PROVA - S2A_R079_2A\\S2A_MSIL2A_20180513T095031_N0207_R079_T33SUC_20180513T115532.SAFE\\MTD_MSIL2A.xml', 'D:\\Sentinel-2\\PROVA - S2A_R079_2A\\S2A_MSIL2A_20180523T095031_N0208_R079_T33SUC_20180524T183727.SAFE\\MTD_MSIL2A.xml', 'D:\\Sentinel-2\\PROVA - S2A_R079_2A\\S2A_MSIL2A_20180602T095031_N0208_R079_T33SUC_20180602T142826.SAFE\\MTD_MSIL2A.xml' ]


    for f=0,n_elements(Files)-1 do begin

    task_1 = ENVITask('PixelwiseBandMathRaster')

    stringa = '{' + $
    ' "url": "xxxxx",' + $
    ' "factory": "URLRaster",' + $
    ' "dataset_index": 0' + $
    '}'
    stringa2=stringa.replace("xxxxx",Files[0])

    string_file_time=files[f].Substring(strlen(Files[0])-70,strlen(Files[0])-22)

    task_1.input_raster = IDLHydrate(TYPE='ENVIRASTER', JSON_Parse(stringa2 ))

    task_1.expression = '(float(b4) - float(b3)) / (float(b4) + float(b3))'
    temp_name = "D:\Sentinel-2\PROVA - S2A_R079_2A\OUTPUT\S2A_MSIL2A_20180602T095031_N0208_R079_T33SUC_20180602T142826_ndvi_prova3"
    temp_name= temp_name.Substring(0,51) + string_file_time + "_ndvi_prova6"
    task_1.output_raster_uri =temp_name
    task_1.Execute
    endfor

    end


    The code actually creates three files with correct names (thus taking part of the names of the corresponding original images) but all of them are the NDVI of the first image listed in Files, thus they result all the same. I don't understand why it does not calculate the NDVI of each of the images but only of the first one (three times).
    Any help will be appreciated, thanks a lot!

    Ben Castellani



    Basic Member


    Posts:130
    Basic Member


    --
    11 Jun 2019 04:13 PM
    In many places of your code, you are indexing Files with [0], which will only use the first Sentinel file. You probably should replace Files[0] in your code with Files[f] to obtain the desired results.

    Hope this helps.

    You are not authorized to post a reply.