简体   繁体   English

Fortran读取不同长度的数据

[英]Fortran read data with different length

I have a Fortran problem. 我有一个Fortran问题。 I want to read in data with different length. 我想读取不同长度的数据。 they begin with: 他们开始于:

 <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

from this point the file is for example 77 lines long. 从这一点来看,该文件的长度例如为77行。 but others have only 55 , and the end is reached when this part comes 但其他人只有55岁,而当这部分到

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

So I think I need a condition which runs out of the do loop? 所以我认为我需要一个超出do循环的条件? I get my data with : 我通过以下方式获取数据:

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)

I hope you can help me. 我希望你能帮助我。

one approach is to read each line as a string, check for the end and process accordingly: 一种方法是将每一行作为字符串读取,检查结尾并相应地进行处理:

 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 

You can also perhaps more simply just read and exit on an error.. 您也可以更简单地读取并退出错误。

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

I'm not a fan of deliberately throwing errors if it can be avoided though. 如果可以避免,我不喜欢故意抛出错误。

As an aside you do not need that messy format, list directed will work fine for this example. 顺便说一句,您不需要那种凌乱的格式,对于此示例,定向列表将可以正常工作。 In fact if all you are doing is transferring the data to another file, simply rewrite the string as: write(34,'(a)')trim(wholeline) 实际上,如果您要做的只是将数据传输到另一个文件,只需将字符串重写为: write(34,'(a)')trim(wholeline)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM