Page MenuHomeHEPForge

swap_columns.f90
No OneTemporary

swap_columns.f90

!------------------------------------------------------------
program swap_columns
!------------------------------------------------------------
! Assumes the file has 'skip' lines of comment at the beginning
! and then 3 columns of numbers. This program will swap the second
! and third columns.
!
! You need to add the variable 'skip' and the old and new filenames by hand.
!
! compile and run using:
! gfortran swap_columns.f90 -o swap_columns.exe
! ./swap_columns.exe
!------------------------------------------------------------
implicit none
character(LEN=150) :: description
double precision :: Mh, expect, obs
integer :: i,skip
integer :: ndatalines ! number of datalines in file
!----------------------------------------------------------------
! Edit this bit!
skip=5 ! number of lines to skip before starting to read in the data
open(10,file='sample/swap_columns_demo_bad_order2.txt')
open(11,file='sample/swap_columns_demo_correct_order.txt')
!E.g.
!skip=5
!open(10,file='../HiggsBounds_KW/Expt_tables/D0tables/D0_H-tautau_2.2fb_5740bad.txt')
!open(11,file='../HiggsBounds_KW/Expt_tables/D0tables/D0_H-tautau_2.2fb_5740.txt')
!----------------------------------------------------------------
ndatalines=getfilelength(10)-skip
! read in the lines of comments and write most of them to the top pf the new file
do i=1,skip
read(10,'(a)')description
! we want to leave out any comment line with the substring 'col',
! since it's probably describing what's in each column,
! which is exactly what we're changing!
if(index(trim(description),'col',.True.).eq.0)then
write(11,'(a)')trim(description)
else
write(11,*)
endif
enddo
! read in the lines of data, swap the 2nd and 3rd columns and
! write to new file.
do i=1,ndatalines
read(10,*)Mh, expect, obs
write(11,'(1D16.3,2D16.8)')Mh, obs, expect
enddo
write(*,*)'Need to edit the comments at the top of the new file by hand.'
close(10)
close(11)
contains
!----------------------------------------------------------------
function getfilelength(fileid)
implicit none
!-------------------------------------output
integer fileid
!-----------------------------------internal
integer :: getfilelength
integer :: n,ios,m
character(len=100) :: temp
!-------------------------------------------
m = 0 ; temp="" !this will count the number of lines in the file, including the last one,
do ! even if it doesn't end in a newline character
read(fileid,'(a)',iostat=ios)temp
if(ios.lt.0) exit
m = m + 1
enddo
rewind(fileid)
getfilelength=m
end function getfilelength
!****************************************************
end program swap_columns

File Metadata

Mime Type
text/plain
Expires
Wed, May 14, 10:30 AM (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5067236
Default Alt Text
swap_columns.f90 (2 KB)

Event Timeline