NCL

ncl : fatal:syntax error: line in file before or near do

이석사 중 2024. 7. 12. 11:56
728x90

정말 오랜만에 글을 써보네요

 

종강을 하고 국가 사업에 참여연구원으로써 매진을 하다보니 너무 오랫동안 아무것도 쓰지 못한 것 같습니다

 

물론 하는 일이 공개하면 안되는 것들이 대부분이어서 그렇기도 하지만 오래만에 쓸 수 있는 내용이 나왔습니다

 

오늘은 제목에서 나와있는 에러를 해결해보겠습니다

 

 


일단 이번에 발생한 에러는 아래와 같습니다

fatal:syntax error: line 45 in file JWBI.ncl before or near do

 

아무리 봐도 do와 들여쓰기 수준을 맞춰서 썼음에도 불구하고 계속 발생했습니다

 

이 에러가 발생하는 이유는 보통 2가지 에러가 대표적입니다

 

  1. 해당 에러가 발생한 라인 앞에 부분 코드에서 괄호가 잘 닫히지 않은 경우

 

  2. 해당 에러가 발생한 라인 앞에 부분 코드에서 if나 do가 정상적으로 end 되지 않은 경우

 

이렇게 2가지가 대표적입니다

(공식적인건 아니구 제가 써보면서 느껴본 점이에요...)

 

일단 제가 짠 전체적인 코드는 다음과 같습니다

 

dir = systemfunc("ls ../../KIMEXP/4.0.01")
  
file_name = (/ systemfunc("ls ../../KIMEXP/4.0.01/" + tostring(dir(0)) + "/*.nc"), \
               systemfunc("ls ../../KIMEXP/4.0.01/" + tostring(dir(1)) + "/*.nc"), \
               systemfunc("ls ../../KIMEXP/4.0.01/" + tostring(dir(2)) + "/*.nc"), \
               systemfunc("ls ../../KIMEXP/4.0.01/" + tostring(dir(3)) + "/*.nc") /)

res = True

res@gsnMaximize = True
res@gsnFrame    = True
res@gsnDraw     = True

res@cnLinesOn = True
res@cnFillOn = False
res@cnLineThicknessF = 0.05

res@gsnLeftString = "np3ne090"
res@gsnRightString = ""
res@tiMainString = "P~B~sfc~N~ & Temperature & wind of Day 9"

do i = 0, 3
  do j = 0, 30
    if (i.eq.0) then
      wks = gsn_open_wks("png", "./JWBI_DOUBLE_FALSE_day" + tostring(j))
    else if (i.eq.1) then
      wks = gsn_open_wks("png", "./JWBI_DOUBLE_TRUE_day" + tostring(j))
    else if (i.eq.2) then
      wks = gsn_open_wks("png", "./JWBI_SINGLE_FALSE_day" + tostring(j))
    else if (i.eq.3) then
      wks = gsn_open_wks("png", "./JWBI_SINGLE_TRUE_day" + tostring(j))
    end if

    ncfile = addfile(file_name(i,j), "r")

    p = ncfile->pds(0,:)

    res@sfXArray = ncfile->lons
    res@sfYArray = ncfile->lats

    plot = gsn_csm_contour_map(wks, p, res)

    draw(plot)
    frame(wks)
  end do
end do

 

여기서 중요한 점은 ncl은 if문을  end 시킬 때 if + else if 의 수 만큼 end if를 달아줘야 한다는 점 입니다

 

저의 경우는 if는 1개, else if는 3개가 있습니다

 

그러니까 총 4개의 end if가 있어야 하는데 저는 1개 밖에 없기 때문에 발생한 에러입니다

 

ncl은 else if를 사용하면 새로운 if문을 여는 것으로 간주한다는 것을 알게 된 것 같습니다

 

수정한 코드입니다

 

dir = systemfunc("ls ../../KIMEXP/4.0.01")
  
file_name = (/ systemfunc("ls ../../KIMEXP/4.0.01/" + tostring(dir(0)) + "/*.nc"), \
               systemfunc("ls ../../KIMEXP/4.0.01/" + tostring(dir(1)) + "/*.nc"), \
               systemfunc("ls ../../KIMEXP/4.0.01/" + tostring(dir(2)) + "/*.nc"), \
               systemfunc("ls ../../KIMEXP/4.0.01/" + tostring(dir(3)) + "/*.nc") /)

res = True

res@gsnMaximize = True
res@gsnFrame    = True
res@gsnDraw     = True

res@cnLinesOn = True
res@cnFillOn = False
res@cnLineThicknessF = 0.05

res@gsnLeftString = "np3ne090"
res@gsnRightString = ""
res@tiMainString = "P~B~sfc~N~ & Temperature & wind of Day 9"

do i = 0, 3
  do j = 0, 30
    if (i.eq.0) then
      wks = gsn_open_wks("png", "./JWBI_DOUBLE_FALSE_day" + tostring(j))
    else if (i.eq.1) then
      wks = gsn_open_wks("png", "./JWBI_DOUBLE_TRUE_day" + tostring(j))
    else if (i.eq.2) then
      wks = gsn_open_wks("png", "./JWBI_SINGLE_FALSE_day" + tostring(j))
    else if (i.eq.3) then
      wks = gsn_open_wks("png", "./JWBI_SINGLE_TRUE_day" + tostring(j))
    end if
    end if
    end if
    end if


    ncfile = addfile(file_name(i,j), "r")

    p = ncfile->pds(0,:)

    res@sfXArray = ncfile->lons
    res@sfYArray = ncfile->lats

    plot = gsn_csm_contour_map(wks, p, res)

    draw(plot)
    frame(wks)
  end do
end do

 

이렇게 하니 정상적으로 실행이 됐습니다

728x90