簡體   English   中英

Fortran讀取不同長度的數據

[英]Fortran read data with different length

我有一個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

所以我認為我需要一個超出do循環的條件? 我通過以下方式獲取數據:

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)

我希望你能幫助我。

一種方法是將每一行作為字符串讀取,檢查結尾並相應地進行處理:

 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM