PGI User Forum
 SearchSearch   MemberlistMemberlist     RegisterRegister   ProfileProfile    Log inLog in 

CUDA-x86.

unwanted trailing commas when writing a .csv file

 
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling
View previous topic :: View next topic  
Author Message
DaveE



Joined: 28 Aug 2012
Posts: 2

PostPosted: Tue Aug 28, 2012 2:21 pm    Post subject: unwanted trailing commas when writing a .csv file Reply with quote

Hello

I have an output formatting problem.

I compile with the following lines

pgcc -c *.c
pgf77 -Mbounds -w -Bstatic *.f utilit.o -o flex.2012.1


My results array is called datacsv and is filled in correctly. I'm trying to write a .csv file to be read by a plotting program. The number of parameters fluctuates depending on user input to the program. Here is the format statement and the write statement:


Code:
  316 format(1p g20.10,99999(",",g20.10))

        do k = 1 , nslices
           write(32,316) (datacsv(listpar(i,j),k),j = 1 , npid(i))
        enddo


The numbers appear in the file just fine separated by commas but a trailing comma appears in every line. The plotting program does not like that trailing comma. What would be the correct format to prevent the trailing commas?

Thanks for your time
Dave
Back to top
View user's profile
mkcolg



Joined: 30 Jun 2004
Posts: 6125
Location: The Portland Group Inc.

PostPosted: Thu Aug 30, 2012 12:29 pm    Post subject: Reply with quote

Hi Dave,

The problem is the "99999". When this format is being processed, the run time is expecting 99999 data items or until one too many items have been printed. It doesn't know that it's past the end until the "g20.10" is processed, in which case the extra "," has already been printed. You'll probably need to explicitly write out each element and use "ADVANCE='NO'" so a new line is not added. Something like:

Code:
% cat csv.f90
      program test
     integer :: j
        integer, parameter :: N=4
        real*8 :: datacsv(N)
        datacsv = 1.0
  316 format(1p g20.10,99999(",",g20.10))
       write(*,316) (datacsv(j),j = 1 , N)
 
  317 format(1p g20.10)
  318 format((g20.10,","))
       
       do j=1,N-1
         write(*,318,ADVANCE='NO') datacsv(j)
       enddo
       write(*,317) datacsv(N)       
    
       end program test
% pgf90 csv.f90
% a.out
     1.000000000    ,     1.000000000    ,     1.000000000    ,     1.000000000    ,
     1.000000000    ,     1.000000000    ,     1.000000000    ,     1.000000000 


Hope this helps,
Mat
Back to top
View user's profile
DaveE



Joined: 28 Aug 2012
Posts: 2

PostPosted: Fri Aug 31, 2012 2:03 pm    Post subject: Reply with quote

Mat

Never heard of that one.

So I had to rearrange some code and switch to pg90 because pg77 didn't like the ADVANCE stuff.
Now I have

Code:

  317 format(1p g20.10                 )
  318 format((  g20.10,      ","       ))

        do k = 1 , nslices
           npidim1 = npid(i) - 1
           do j = 1 , npidim1
              write(32,318,ADVANCE='NO')  datacsv(listpar(i,     j ),k)
c             write(32,318             )  datacsv(listpar(i,     j ),k)
           enddo
           write(32,317)                  datacsv(listpar(i,npid(i)),k)
        enddo


and out comes

Code:

     0.000000000    ,    0.3000000000E-01,     0.000000000   
     25.00024414    ,     1.220000000    ,     1.000000000   
     50.00048828    ,     2.410000000    ,     3.000000000   
     75.00073242    ,     3.610000000    ,     4.000000000   
     100.0000000    ,     4.800000000    ,     5.000000000   



It works. Now I don't need to spend 10 minutes with the text editor after each run.
Thanks for your help.

Dave
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    PGI User Forum Forum Index -> Programming and Compiling All times are GMT - 7 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group