I don't understand what you're asking about fortran code, but getting sets of 3 files in sorted order in the shell is not hard. This doesn't do anything very useful, but may be a suitable example to show you what needs to be done to meet your requirements:
#!/bin/ksh
ls *.txt|sort -t"_" -k2n,2|while read f1 && read f2 && read f3
do echo processing $f1, $f2, and $f3
done
Note that this won't work if any of your *.txt filenames contain whitespace characters, and will silently ignore the last one or two *.txt files if the number of *.txt files is not an integral multiple of 3. In a directory containing the files:
I assume that you want your Fortran code to be able to look at its command line arguments rather than recompiling your source with different values for input and output filenames derived from shell variables at compile time.
If your Fortran compiler complies with the 2003 Fortran standard, try accessing the comand line arguments as specified here: GET_COMMAND_ARGUMENT