读取二维数组
我正在尝试从 Fortran 90 中的外部文件读取二维数组 (30.7),因为
READ(*,*)Fname
OPEN(UNIT=10, FILE=Fname, ACTION="READ")
DO i=1,30
READ(10,*)(indices(i,j),j=1,7)
END DO
数据有一些缺失值表示为空白。当我将数据读入数组 (,*)(indices(i,j))
时,它会为丢失的数据分配 0.00
。如何克服这个问题呢?附上以下数据。
337.60,220.40,0.00,0.00,200.42,216.61,261.04
323.00,249.20,65.30,0.00,201.93,210.91,309.98
116.80,474.80,0.00,0.00,203.43,215.76,234.93
72.10,505.90,0.00,0.00,204.93,215.72,233.47
148.30,771.70,0.00,0.00,206.44,217.00,239.05
90.70,287.20,0.00,0.00,207.94,215.43,216.85
337.20,334.50,10.20,0.00,209.45,226.85,306.57
142.50,142.80,0.00,0.00,210.95,240.09,240.31
279.10,289.60,51.80,0.00,212.45,227.75,262.30
273.60,337.70,0.00,0.00,213.96,256.86,223.66
332.40,425.60,0.00,0.00,215.46,238.36,237.63
45.70,299.30,0.00,0.00,216.96,223.92,241.41
49.10,529.40,0.00,0.00,218.47,235.81,282.17
185.30,331.80,38.00,0.00,219.97,235.81,309.29
552.90,454.80,0.00,0.00,221.47,224.60,269.09
176.20,441.60,0.00,0.00,222.98,232.44,293.95
170.00, ,0.00, , , ,327.96
200.00, ,0.00, , , ,291.69
241.20,156.00,0.00,0.00,227.49,235.55,278.66
118.00,383.20,3.20,0.00,228.99,269.28,325.31
62.00,189.70,0.00,0.00,230.50,248.73,266.95
400.20,244.20,0.00,0.00,232.00,239.70,271.27
163.70,826.60,0.00,500.00,233.50,245.06,294.98
250.40,236.60,0.00,0.00,235.01,261.72,288.24
51.30,684.20,0.00,0.00,236.51,245.06,237.37
412.50,128.90,0.00,500.00,238.01,245.16,268.66
452.00, , , , , ,314.68
481.00,155.50,0.00,0.00,241.02,278.72,348.44
162.20,201.90,0.00,500.00,242.52,250.36,255.58
171.80,152.00,0.00,500.00,244.03,246.85,339.06
I'm trying to read a two dimensional array (30.7) from a external file in Fortran 90 as
READ(*,*)Fname
OPEN(UNIT=10, FILE=Fname, ACTION="READ")
DO i=1,30
READ(10,*)(indices(i,j),j=1,7)
END DO
Data has some missing values represented as blank. When I read data into array (,*)(indices(i,j))
, it assigns 0.00
for missing data. How to over come this problem? Attaching the data below.
337.60,220.40,0.00,0.00,200.42,216.61,261.04
323.00,249.20,65.30,0.00,201.93,210.91,309.98
116.80,474.80,0.00,0.00,203.43,215.76,234.93
72.10,505.90,0.00,0.00,204.93,215.72,233.47
148.30,771.70,0.00,0.00,206.44,217.00,239.05
90.70,287.20,0.00,0.00,207.94,215.43,216.85
337.20,334.50,10.20,0.00,209.45,226.85,306.57
142.50,142.80,0.00,0.00,210.95,240.09,240.31
279.10,289.60,51.80,0.00,212.45,227.75,262.30
273.60,337.70,0.00,0.00,213.96,256.86,223.66
332.40,425.60,0.00,0.00,215.46,238.36,237.63
45.70,299.30,0.00,0.00,216.96,223.92,241.41
49.10,529.40,0.00,0.00,218.47,235.81,282.17
185.30,331.80,38.00,0.00,219.97,235.81,309.29
552.90,454.80,0.00,0.00,221.47,224.60,269.09
176.20,441.60,0.00,0.00,222.98,232.44,293.95
170.00, ,0.00, , , ,327.96
200.00, ,0.00, , , ,291.69
241.20,156.00,0.00,0.00,227.49,235.55,278.66
118.00,383.20,3.20,0.00,228.99,269.28,325.31
62.00,189.70,0.00,0.00,230.50,248.73,266.95
400.20,244.20,0.00,0.00,232.00,239.70,271.27
163.70,826.60,0.00,500.00,233.50,245.06,294.98
250.40,236.60,0.00,0.00,235.01,261.72,288.24
51.30,684.20,0.00,0.00,236.51,245.06,237.37
412.50,128.90,0.00,500.00,238.01,245.16,268.66
452.00, , , , , ,314.68
481.00,155.50,0.00,0.00,241.02,278.72,348.44
162.20,201.90,0.00,500.00,242.52,250.36,255.58
171.80,152.00,0.00,500.00,244.03,246.85,339.06
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 DO 循环之前,为索引数组分配一个唯一值,您知道某些值不会出现在数据集中。示例:
读取文件后,索引中的缺失值仍将具有值-99。然后您可以计算每列中 -99 的数量。
Before your DO loop, assign a unique value to the indices array, some value that you know won't occur in the data set. Example:
The missing values in indices will still have the value -99 after reading the file. You can then count the number of -99s in each column.
namelist 方法可以跳过未提供的值,但其格式非常特殊。否则,如果您想跳过未提供的值并且空白不解释为零,您将必须提供自己的额外处理级别。 Fortran 方法将数字输入中的空格解释为零。我认为你基本上必须编写自己的解析器。将这些行读入一个字符串,然后通过逗号(或空格?)将它们分成子字符串。如果子字符串为空,则保持值不变——这就是您想要的吗?否则,使用“内部”读取将字符串中的值读入数字中。这并不简单,但你想做一些非标准的事情。查看 Fortran 提供的字符串内在函数,它们将使处理更容易。
The namelist method can skip non-provided values, but that has a very particular format. Otherwise if you want to skip non-provided values and have blanks not interpreted as zero you will have to provide your own additional level of processing. Interpreting blanks as zeros on numerical input is the Fortran way. I think that you basically have to write your own parser. Read the lines into a string, then split them into sub-strings via commas (or blanks too?). If the sub-string is blank leave the value unchanged -- that is what you want? Otherwise use an "internal" read to read the values from the string into the number. Not trivial, but you want to do something non-standard. Look through the string intrinsics provided with Fortran, they will make the processing easier.
尝试: open(unit=10, file=... Blank=blank)
不知道它是否有效,但 Blank="zero" 将您的空格重置为 0...假设我的解决方案会做相反的事情;)
try: open(unit=10, file=... blank=blank)
Don't know if it works, but blank="zero" resets your blank spaces to 0... supose my solution will do the opposite ;)