Richard Rublev
Richard Rublev

Reputation: 8172

Write data to columns in a loop

I want to create an input file for one code. It looks like this

SITE   FREQ   DATA TYPE      DATUM              ERROR
     1     1     1    2.01562    0.217000E-01
     1     1     2    44.8114     2.86600    
     1     1     5    2.02486    0.217000E-01
     1     1     6    44.0423     2.86600    
     1     2     1    2.03421    0.217000E-01
     1     2     2    53.5181     2.86600    
     1     2     5    2.01103    0.217000E-01
     1     2     6    43.6452     2.86600    
     1     3     1    1.88711    0.217000E-01
     1     3     2    51.5582     2.86600    
     1     3     5    2.00536    0.217000E-01
     1     3     6    43.4296     2.86600    
     1     4     1    1.85939    0.217000E-01
     1     4     2    49.8675     2.86600    
     1     4     5    2.04246    0.217000E-01
     1     4     6    41.5948     2.86600    
     1     5     1    1.86721    0.217000E-01
     1     5     2    42.6603     2.86600    
     1     5     5    2.02059    0.217000E-01
     1     5     6    44.6032     2.86600    
     1     6     1    1.90233    0.217000E-01
     1     6     2    34.9367     2.86600    
     1     6     5    2.02904    0.217000E-01
     1     6     6    45.5312     2.86600    
     2     1     1    2.02998    0.217000E-01
     2     1     2    46.3565     2.86600    
     2     1     5    2.07089    0.217000E-01
     2     1     6    47.8481     2.86600    
     2     2     1    1.94406    0.217000E-01
     2     2     2    52.9107     2.86600    
     2     2     5    1.94073    0.217000E-01
     2     2     6    47.7353     2.86600    
     2     3     1    1.77228    0.217000E-01
     2     3     2    53.3664     2.86600    
     2     3     5    1.93717    0.217000E-01

I have thought of something like this

do i=1,74
  do j=1,4
    write(50,)num1,s1(i),dt(j),v1(i),er1
  end do
end do

But the data type takes value 1,2,5,6 not 1 to 4. How to solve this?

Upvotes: 0

Views: 146

Answers (2)

zdim
zdim

Reputation: 66883

Use one loop as you normally would (drop the loop over j) and print corresponding dt values (1,2,5,6) as

merge( dt(mod(i,4)), dt(size(dt)), mod(i,4)/=0 )

This is Fortran's ternary, producing either dt(mod(i,4)) if mod(i,4)/=0 or dt(size(dt)) when mod returns zero. The merge is F95. This does run mod every time through. Alternatively you can make a 74-long vector with repeating 1,2,5,6 in which case you have an extra vector.

Upvotes: 1

High Performance Mark
High Performance Mark

Reputation: 78316

I'm not sure that I fully understand the question but unless I'm wide of the mark I'd frame a solution along these lines:

integer, dimension(4) :: aux = [1,2,5,6]
...
do i=1,74
  do j=1,4
    write(50,)num1,s1(i),dt(aux(j)),v1(i),er1
  end do
end do

Upvotes: 0

Related Questions