Write format specifiers

68 views
Skip to first unread message

Pratik Patel

unread,
Jul 13, 2022, 2:24:47 AMJul 13
to
Hi,
I want to create a file and write data in that file as shown below to use the 'pm3d' command in Gnuplot and plot a 3D contour graph. I want to add one blank space line when the value of the X variable changes. How to write data like this in a file?
Example :
X Y Z
1 1 2
1 2 5
1 3 10

2 1 5
2 2 8
2 3 13

3 1 10
3 2 13
3 3 18

Jakob Mendel

unread,
Jul 13, 2022, 3:07:02 AMJul 13
to
Am 13.07.22 um 08:24 schrieb Pratik Patel:
I onestly think you should start with the data structure holding the x,
y, and z values. How is the data being read in? Do you employ DO
loops, explicit or implicit?

If you employ DO loops it should be an easy task to adapt them to your
output needs.

Jakob

Pratik Patel

unread,
Jul 13, 2022, 3:18:33 AMJul 13
to
I have employed Do Loop implicitly and generated data.
There are 16k data points so can not be done manually.
I'm looking for some format that can be used with write command.

Pratik

John

unread,
Jul 13, 2022, 7:14:48 AMJul 13
to
If you mean you already have some arrays X,Y,Z of simple integers just about any
format would do. If you mean you want to do it in one line it is possible with the
MERGE intrinsic and an implied DO, but that would start to obfuscate the code.
As noted, if you could show a sample code it might be you can print the data as
you generate it and not have to create the data arrays, etc. Do you mean you just
want something like this?


program testit
implicit none
! generate some sample data
integer,parameter :: i=100
integer :: x(i),y(i),z(i), j
real :: r(i)
call random_number(r)
x=r*10
call random_number(r)
y=r*10
call random_number(r)
z=r*10

! write the arrays adding a line when the X value changes

write(*,'("X Y Z")')
do j=1,i
if(x(j).ne.x(max(1,j-1))) write(*,*)
write(*,'(*(g0,1x))')x(j),y(j),z(j)
enddo
end program testit

John

unread,
Jul 13, 2022, 7:32:30 AMJul 13
to
As noted, you could do the printing all with one line of code, but it becomes very unclear
what the line is doing versus a simple DO loop as per above.

program testit
implicit none
integer,parameter :: i=100
integer :: x(i),y(i),z(i), j
real :: r(i)
character(len=1) :: nl=new_line('a')
call random_number(r)
x=r*10
call random_number(r)
y=r*10
call random_number(r)
z=r*10

write(*,'(4(g0,1x))') ' X','Y','Z','',(nl(:merge(1,0,x(j).ne.x(max(1,j-1)))), x(j),y(j),z(j),j=1,i)

end program testit



Pratik Patel

unread,
Jul 13, 2022, 7:55:42 AMJul 13
to
Thank you so much, John. This code will work for me.
Message has been deleted

FortranFan

unread,
Jul 13, 2022, 9:00:12 AMJul 13
to
On Wednesday, July 13, 2022 at 7:55:42 AM UTC-4, Pratik Patel wrote:

> ..
> Thank you so much, John. This code will work for me.

@Pratik Patel,

You may also to keep in mind the Fortran-lang site and the Discourse there if you have further needs and interests with Fortran:
https://fortran-lang.org/
https://fortran-lang.discourse.group/

Robin Vowels

unread,
Jul 13, 2022, 9:18:16 AMJul 13
to
On Wednesday, July 13, 2022 at 4:24:47 PM UTC+10, Pratik Patel wrote:
> Hi,
> I want to create a file and write data in that file as shown below to use the 'pm3d'
> command in Gnuplot and plot a 3D contour graph. I want to add one blank space
> line when the value of the X variable changes. How to write data like this in a file?
.
Use a WRITE statement with format control.
.

Pratik Patel

unread,
Jul 13, 2022, 12:34:18 PMJul 13
to
On Wednesday, July 13, 2022, at 6:30:12 PM UTC+5:30, FortranFan wrote:
> On Wednesday, July 13, 2022, at 7:55:42 AM UTC-4, Pratik Patel wrote:
>
> > ..
> > Thank you so much, John. This code will work for me.
> @Pratik Patel,
>
> You may also keep in mind the Fortran-lang site and the Discourse there if you have further needs and interests with Fortran:
> https://fortran-lang.org/
> https://fortran-lang.discourse.group/
@FortranFan
Thank you so much for providing links. As I'm a beginner and will need to use this site frequently.
Reply all
Reply to author
Forward
0 new messages