0

I have a folder with file-names ending .TIF, and I did a for loop

for i in *.TIF

In the loop, I want to get two types of files ending for example (B4.TIF and B5.TIF.

I did it like this but, it doesn't get the last four letters of the file name, instead it adds them at the end.

do ...... ${i}_B4.TIF -B ${i}_B5.TIF

My desired results are to get those file names in order to do some calculations

these are the examples of the files in the folder

LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF  
LC08_L1TP_195020_20171107_20171121_01_T1_B4.TIF

LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF  
LC08_L1TP_195020_20171107_20171121_01_T1_B5.TIF

and this is the script I am using to do the calculations

for i in *.TIF   
do 
    gdal_calc.py -A ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF --type=Float32
--outfile=~/Geodata/goteborg/croped/ndvi/${i}_NDVI.TIF --calc="(B-A)/(B+A)"

done

The final output will be subtracting the B4 and B5. so this ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF should get the two file names like this:

LC08_L1TP_195020_20170224_20170301_01_T1_B4.TIF - 
LC08_L1TP_195020_20170224_20170301_01_T1_B5.TIF
  • Please list an example input and output (just a few lines). – ctrl-alt-delor Nov 14 '18 at 10:20
  • 1
    Why don't you loop over ./*B[45]_SA.TIF? Or do you need both files i the same iteration of the loop. Please explain more about what it is you're doing. – Kusalananda Nov 14 '18 at 10:23
  • I need both files in the same iteration. see the example and the script in my last edit – MKJ Nov 14 '18 at 10:24
  • I don't see an _SA in the examples, is this the problem. – ctrl-alt-delor Nov 14 '18 at 10:27
  • Your example command takes two inputs and produces one output, but your list of example files doesn't have any "matches" between the B4 and B5 set. Please indicate which pairs of input files you want to use together, and how you decide the output filename each time. – JigglyNaga Nov 14 '18 at 10:30
1

Loop over the files that constitute one half of the pairs, and for each such file, construct the filename of the corresponding pair and of the output file:

for b4_file in ./*_B4.TIF; do
    b5_file="${b4_file%_B4.TIF}_B5.TIF"
    ndvi_file="$HOME/Geodata/goteborg/croped/ndvi/${b4_file%_B4.TIF}_NDVI.TIF"

    if [ ! -f "$b5_file" ]; then
        printf 'Expected to find "%s" to go with "%s", but did not\n' "$b5_file" "$b4_file" >&2
        continue
    fi

    # do whatever it is you need to do with "$b4_file" and "$b5_file"

    gdal_calc.py -A "$b4_file" -B "$b5_file" --type=Float32 \
        --outfile="$ndvi_file --calc="(B-A)/(B+A)"

done
0

First note there is no-such thing as a file extension on Unix. . is just another character. So here I search for some ½ of your files, strip the end of the name off, ready to put it back on (in two different ways).

This will sort of work, but may have some latent bugs.

for f in *_B5.TIF   
do 
    i="$(echo "$f" | sed -r -e 's/(.*)_B5.TIF/\1/')"
    … ${i}_B4_SA.TIF -B ${i}_B5_SA.TIF …
done

This should be more robust

find . -iname "*_B5.TIF" -print0 | sed -0r -e 's/(.*)_B5.TIF/\1/' | xargs -0 -i … {}_B4_SA.TIF -B {}_B5_SA.TIF …

Please test, as I have not (may be some small error). And there is no error checking. Your program must check for miss-matches.

0

As you're using Bash, you can use Parameter expansion to chop the ending and save the common part of the filename in a new variable:

for f in *_B4.TIF ; do
    i="${f%_B4.TIF}"
    gdal_calc.py -A "${i}_B4.TIF" -B "${i}_B5.TIF" --outfile=".../${i}_NDVI.TIF"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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