Fortran无法读取文本文件 [英] Fortran non advancing reading of a text file

查看:323
本文介绍了Fortran无法读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有信息头的文本文件,后面跟着只有数字的行,这些数据是要读取的数据。

我不知道有多少

下面是一个例子:

<$ p $

p> 文件句柄:65536
总数#科学数据集:1
文件描述:
此文件包含一个北半球极地立体地形图, 。该地图使用在卫星服务部门(SSD)的交互处理分部(IPB)指导下开发的NOAA / NESDIS交互式多传感器雪和冰映射系统(IMS)制作。欲了解更多信息,请联系:布鲁斯拉姆齐先生bramsay@ssd.wwb.noaa.gov。

数据集#1
数据标签:
Northern Hemisphere冰图
坐标系:极坐标立体图
数据类型:BYTE
格式:I3
尺寸:1024 1024
最小值/最大值:0 165
单位:8位标志
尺寸#0
暗淡标签:经度
暗淡格式:设备坐标
暗淡单位:像素
尺寸#1
暗淡标签:Latitude
Dim格式:Device Coordinates
Dim Units:Pixels
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0

..........................



我使用以下方式打开文件:

  open(newunit = U,file = ValFile,STATUS =' OLD',ACCESS ='SEQUENTIAL',ACTION ='READ')

然后,一行一行地测试行的类型:标题行或数据行:

  ios = 0 
do while (.NOT。 is_iostat_end(ios))
read(U,'(A)',iostat = ios,advance ='NO')line!不应该前进到下一行
if(is_iostat_end(ios))stop在数据段之前到达的文件结束。
tol = getTypeOfLine(line,nValues)! nValues = 1024,需要测试行是否是数据。
if(tol> 0)then!如果该行保存数据。
退出!退出循环
else
read(U,'(A)',iostat = ios,advance ='YES')line!我们提前到下一行
end如果
end do

但是第一个阅读循环,总是前进到下一行,这是一个问题。



退出上述循环后,输入一个新的循环读取数据:

p>

  read(U,'(1024I1)',iostat = ios)值(c,:) 

1024组数据可以包含一些行,但每组数据都是矩阵值中的一行。



问题在于第二个循环没有读取测试循环中读取的最后一行(这是第一行数据)。

可能的解决方案是读取测试循环中的行,而不会前进到下一行。我用这个,advance ='no',但它仍然进入下一行,为什么?。

如果在从文件读取文件以满足读取语句的输出项目列表中的项目时遇到当前记录的末尾,则继续读取仍然会将文件位置设置为下一个记录的开始位置 - 非前进不会意思是永不前进。您可以使用分配给iostat说明符中指定变量的值来读取语句,以查看是否已达到当前记录的结尾 - 使用IS_IOSTAT_EOR内部函数或针对来自ISO_FORTRAN_ENV的等效值进行测试。



(上面隐含的一个事实是,非前进读取仍然继续对应于实际读取的项目的文件位置......因此,一旦 getTypeOfLine 程序决定它有一行数据,至少有一行数据已经被读取,除非你重新定位文件后面的数据读取语句将会错过该部分。)


I have a text file with a header of information followed by lines with just numbers, which are the data to be read.

I don't know how many lines are there in the header, and it is a variable number.

Here is an example:

filehandle:                65536
total # scientific data sets:        1
file description:
This file contains a Northern Hemisphere polar stereographic map of snow and ice coverage at 1024x1024 resolution.  The map was produced using the NOAA/NESDIS Interactive MultisensorSnow and Ice Mapping System (IMS) developed under the directionof the Interactive Processing Branch (IPB) of the Satellite Services Division (SSD).  For more information, contact:  Mr. Bruce Ramsay at bramsay@ssd.wwb.noaa.gov.

Data Set #       1
Data Label:                          
Northern Hemisphere 1024x1024 Snow & Ice Chart
Coordinate System:                   Polar Stereographic
Data Type:                           BYTE
Format:                              I3
Dimensions:                                  1024        1024
Min/Max Values:                         0 165
Units:                               8-bit Flag
Dimension #       0
  Dim Label:                         Longitude
  Dim Format:                        Device Coordinates
  Dim Units:                         Pixels
Dimension #       1
  Dim Label:                         Latitude
  Dim Format:                        Device Coordinates
  Dim Units:                         Pixels
1    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0
2    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0
3    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0
0    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0
0    0    0    0    0    0    0    0    0    0    0    0    0    0    0
 0

..........................

I open the file using:

open(newunit=U, file = ValFile, STATUS = 'OLD', ACCESS = 'SEQUENTIAL', ACTION = 'READ')

Then, I read the file line by line and test for the type of line: header line or data line:

ios = 0
do while ( .NOT. is_iostat_end(ios)   )
    read(U, '(A)', iostat = ios, advance = 'NO') line ! Shouldn't advance to next line
    if (is_iostat_end(ios)) stop "End of file reached before data section."
    tol = getTypeOfLine(line, nValues) ! nValues = 1024, needed to test if line is data.
    if ( tol > 0 ) then   ! If the line holds data.
        exit    ! Exits the loop
    else
        read(U, '(A)', iostat = ios, advance = 'YES') line ! We advance to the next line
    end if
end do

But the first read in the loop, always advances to the next line, and this is a problem.

After exiting the above loop, enter a new loop to read the data:

read(U, '(1024I1)', iostat = ios) Values(c,:)

The 1024 set of data can span some lines, but each set is a row in the matrix "Values".

The problem is that this second loop doesn't read the last line read in the testing loop (which is the first line of data).

A possible solution is to read the lines in the testing loop, without advancing to the next line. I used for this, advance='no', but it still advances to the next line, Why?.

解决方案

A non-advancing read will still set the file position to before start of the next record if the end of the current record is encountered while reading from the file to satisfy the items in the output item list of the read statement - non-advancing doesn't mean "never-advancing". You can use the value assigned to the variable nominated in an iostat specifier for the read statement to see if the end of the current record was reached - use the IS_IOSTAT_EOR intrinsic or test against the equivalent value from ISO_FORTRAN_ENV.

(Implicit in the above is the fact that a non-advancing read still advances over the file positions that correspond to items actually read... hence once that getTypeOfLine procedure decides that it has a line of data at least part of that line has already been read. Unless you reposition the file subsequent "data" read statements will miss that part.)

这篇关于Fortran无法读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆