Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29273 Discussions

Output file naming in incremental order

sashi_balakrishnan
521 Views

Hi,

I am am calculating pressure distribution (say 50 by 50 matrix)on a surface for each time step. I would like to save the pressure values for each of the time step as text but would like the name tocorrespond to the time step, e.g. "pressure_t1.txt","pressure_t2.txt" etc. Can you please help me solve this problem? Thanking you in advance.

0 Kudos
3 Replies
ArturGuzik
Valued Contributor I
521 Views

Hi,

I'm not sure this is waht you want, but you can use "internal write" inside your loop (over time steps). Something like this below (not tested, just idea)

character (len=8) :: string

character (len=256) :: step_name, name

name='..../pressure_t_'

do i=1, max_steps

string=''

! internal write
WRITE (FMT='(I8)', UNIT=string, IOSTAT=stat_val) i

! check status
IF (stat_val /= 0) THEN

! handle error

END IF

! count blank(s) trailing
bl=scan(string(1:len_trim(string)), ' ', back=.true.)

! concatenate
step_name=name(1:len_trim(name))//string(bl+1:len_trim(string))//'.dat'

! open file with step_name

! calculations

! close file

end do

A.

0 Kudos
Les_Neilson
Valued Contributor II
521 Views
Quoting - arturguzik


! internal write
WRITE (FMT='(I8)', UNIT=string, IOSTAT=stat_val) i

You could also use a format of "(i0)" which will automatically remove leading spaces and then you can concatenate string directly.

One problem with this method is sorting of the file names (a directory listing or file explorer), itwill give all of the t1* files first ie t1, t10,t11,t12... then t2,t20,t21 etc)

A way round this is to write leading zeros to string. For example : If you know that you will have less than 1000 steps then you can use a format of (i3.3) when writing to string. This will give you t001, t002, t003 etc.

If you don't know directly the magnitude of the number of steps then youwould write some code to work out what the (In.n) format should be, based on the value of (your)max_steps.

Les

0 Kudos
sashi_balakrishnan
521 Views
Quoting - les_neilson

You could also use a format of "(i0)" which will automatically remove leading spaces and then you can concatenate string directly.

One problem with this method is sorting of the file names (a directory listing or file explorer), itwill give all of the t1* files first ie t1, t10,t11,t12... then t2,t20,t21 etc)

A way round this is to write leading zeros to string. For example : If you know that you will have less than 1000 steps then you can use a format of (i3.3) when writing to string. This will give you t001, t002, t003 etc.

If you don't know directly the magnitude of the number of steps then youwould write some code to work out what the (In.n) format should be, based on the value of (your)max_steps.

Les

Gentlemen,

Thank you both for your replies. I have managed to combine the suggestions and come up with the following code to achieve what I wanted to do!

CHARACTER(20) sFileName
WRITE(sFileName, "(A, i3.3, A)") 'pressure', ntime_count, '.txt'
open(101,file=sFileName)
write(101,906)((pressure(i,j),j=1,nyy ),i=1,nxx)
906 format (21(EN12.0),1X)

I really appreciated both your help.

Sashi

I really appreciated both your help.

Sashi

0 Kudos
Reply