2015-02-11 169 views
1

我有一個Fortran問題。我想讀取不同長度的數據。 它們首先:Fortran讀取不同長度的數據

<TITLE>University of Wyoming - Radiosonde Data</TITLE> 
<LINK REL="StyleSheet" HREF="/resources/select.css" TYPE="text/css"> 
<BODY BGCOLOR="white"> 
    <H2>08190 Barcelona Observations at 12Z 11 Feb 2015</H2> 
<PRE> 
     ----------------------------------------------------------------------------- 
     PRES HGHT TEMP DWPT RELH MIXR DRCT SKNT THTA THTE THTV 
     hPa  m  C  C  % g/kg deg knot  K  K  K 
     ----------------------------------------------------------------------------- 
     1012.0  98 14.0 -1.0  36 3.53  0  0 286.2 296.5 286.8 

從該點文件是例如77線長。但其他人只有55,並在達到終點時,這部分來自

</PRE><H3>Station information and sounding indices</H3><PRE> 
         Station number: 8190 

所以我想我需要它運行了做循環的條件? 我得到我的數據有:

wget 'http://weather.uwyo.edu/cgi-bin/sounding?region=europe& TYPE=TEXT%3ALIST&YEAR=2015&MONTH=02&FROM=1112&TO=1112&STNM=08190' -0 data.dat 
open(33, file=infilename, form='formatted',& 
access='sequential',action='read') 

open(34, file=outfilename, form='formatted',& 
access='sequential',action='write') 


read(33,'(11/)') 
do i=1,77 
read(33, '(f7.1,2x,i5,2x,a5,2x,a5,4x,a3,3x,f4.2,4x,a3,4x,a3)')  pres,height,tmp,tmp_dew,rel_hum,mixing,wind_dir,wind_speed 

write(34,'(f7.1,2x,i5,2x,a5,2x,a5,4x,a3,3x,f4.2,4x,a3,4x,a3)') pres,height,tmp,tmp_dew,rel_hum,mixing,wind_dir,wind_speed 

end do 

close(33) 
close(34) 

我希望你能幫助我。

回答

0

一種方法是閱讀每行一個字符串,檢查結束,過程相應:

character*1000 wholeline 
do while(.true.) 
    read(33,'(a)')wholeline 
    if (index(wholeline,'</PRE>').ne.0)exit 
    read(wholeline,*)pres,height,tmp,tmp,dew ... 
enddo 

您也能夠或許更簡單,僅讀和出口上的錯誤..

do while(.true.) 
    read(33,*,err=100)pres,height,tmp,tmp,dew ... 
enddo 
100 continue 

我不是故意拋出錯誤的粉絲,如果它可以避免的話。

順便說一句,你不需要那種凌亂的格式,列表定向將爲這個例子工作正常。實際上,如果您只是在將數據傳輸到另一個文件,只需將該字符串重寫爲:write(34,'(a)')trim(wholeline)