2014-06-11 2 views
-2

코드는 do-loop로 구성되어 있으며 실행되는 동안 데이터 배열을 작성합니다. 이 배열을 파일에 새 열로 추가해야합니다.Fortran : 열 당 열을 파일에 추가하십시오.

첫번째 컬럼 (파장)에 고정되고, 두 번째는 제 런 생성된다

OPEN (unit=11,file=filename // '.csv') 
WRITE(11,'(i4,A1,f10.6)') (lambda(ii),tab,resv(ii), ii=1,nw) 
CLOSE(11) 

, λ는 파장 (4 자리)이며, 탭 문자로 선언 (9)과 RESV있다 내 데이터 (플로팅). 배열은 nw=2000 개의 항목으로 구성됩니다.

스크립트를 처음 실행하면 MS Excel에 .csv 으로로드 할 수있는 좋은 결과를 얻을 수 있습니다. 그러나 스크립트는 루프의 시작 부분으로 돌아가서 새 데이터를 계산하고 변경된 "resv"항목을 저장합니다 새 열로 내가

WRITE(11,'(T17,i4,A1,f10.6)') (lambda(ii),tab,resv(ii), ii=1,nw) 

처럼 갈 때

는 그러나 새로운 데이터는 실제로 열 (17)에 저장되어 있지만 모든 데이터가 전에 제거되고 있습니다! Fortran에 새 열을 "추가"한다고 어떻게 말할 수 있습니까?

+1

* 프로그램에서 파일을 편집 * 할 수 없습니다. 이전 데이터로 파일의 이름을 바꾸고 이전 파일을 읽고 이전 데이터와 새 데이터로 새 파일을 작성합니다. 작업이 끝나면 이전 파일을 삭제하십시오. – cup

+0

이것은 http://stackoverflow.com/questions/24157423/fortran-add-column-to-file-ie-skip-a-varying-amount-of-horizontal-spaces와 너무 유사합니다. 업데이트/수정하십시오. 원래의 질문 – steabert

+0

물론 동일합니다. 동일한 코드에는 두 가지 문제가 있습니다. 마지막 질문에 대한 대답은 여기에 오기까지 필요했습니다. 그러나 그것은 최종 해결책이 아닙니다. 이것이 내 실제 문제입니다. – offeltoffel

답변

0

다른 변형을 시도해보십시오. addcolumn의 코드를 살펴보십시오. 단 한 줄의 길이는 1024자를 초과 할 수 없습니다. 필요에 맞게 배열의 크기를 늘릴 수 있습니다.

module mod_helper 
    integer, parameter:: AMAX = 10 
    integer, dimension(AMAX):: coldata 
    integer:: revision 
    contains 
    subroutine init 
     revision = 0 
     do ii = 1, AMAX 
      coldata(ii) = ii 
     end do 
    end subroutine init 

    subroutine increment(howmuch) 
     integer, intent(in):: howmuch 
     coldata = coldata + howmuch 
    end subroutine 

    subroutine addcolumn(csvname) 
     character*(*), intent(in) csvname 
     integer(kind=2):: status 
     integer:: prevlen 
     character(1024):: prev, oldname, newname 
     integer, parameter:: oldfile = 20, newfile = 30 

     write(oldname, "(A,I3.3,'.csv')") csvname, revision 
     revision = revision + 1 
     write(newname, "(A,I3.3,'.csv')") csvname, revision 
     if (revision .gt. 1) then 
      ! Open the old file 
      open(oldfile, file=oldname, access="sequential", status="old") 
     end if 
     open(newfile, file=newname, access="sequential", status="new", action="write") 
     prev = ' ' 
     do ii = 1, AMAX 
      if (revision .gt. 1) then 
       ! read previous contents as a string 
       read(oldfile, "(A)") prev 
       prevlen = len(trim(prev)) 
      else 
       prevlen = 1 
      end if 
      ! write previous and new contents 
      write(newfile, "(A, I4, ',')") prev(1:prevlen), coldata(ii) 
     end do 
     ! delete the previous file 
     if (revision .gt. 1) close(oldfile, status='delete') 
     close(newfile) 
    end subroutine 
    end module 

    program main 
    use mod_helper 

    call init 
    call addcolumn('col') 
    call increment(1) 
    call addcolumn('col') 
    call increment(20) 
    call addcolumn('col') 
    end program 
관련 문제