38892

Fortran: Open, form='(un)formatted', read

Question:

My data ('location_data.txt') look like :

100030002001000 39.772158 -75.527002 40 100030002001001 39.771498 -75.525601 150 100030002001002 39.771226 -75.526509 2 100030002001003 39.767265 -75.526035 0 100030002001004 39.769209 -75.527356 1 100030002001005 39.769644 -75.528116 0 100030002001006 39.767594 -75.527571 3 100030002001007 39.770331 -75.530489 0 100030002001008 39.769329 -75.529616 230 100030002001009 39.768497 -75.528902 0 100030002001010 39.769968 -75.524596 3 100030002001011 39.769757 -75.525916 0 100030002001012 39.768603 -75.525462 5 100030002001013 39.768216 -75.524161 0 100030002001014 39.768765 -75.522921 0 100030002001015 39.767254 -75.524572 77 100030002001016 39.767773 -75.523119 2 100030002001017 39.76735 -75.522105 0 100030002001018 39.768074 -75.521028 12 100030002001019 39.766908 -75.521198 0

The variables are

<ol><li>unique key (len=15),</li> <li>longitude</li> <li>latitude</li> <li>the number of workers.</li> </ol>

My goal here is to get the four vectors from the external data. I am writing a Fortran code as follows:

program location implicit none integer, parameter :: maxnr = 200000 integer :: nr, i, j, ios character(len=1) :: junkfornr ! My variable declaration character(len=15), dimension(:), allocatable :: key real, dimension(:), allocatable :: lat, lon integer, dimension(:), allocatable :: jobs ! Determine the total number of lines in my data file nr=0 open(10, file='location_data.txt', status='old', form='formatted') do i=1,maxnr read(10,*,iostat=ios) junkfornr if (ios/=0) exit if (i == maxnr) then write(*,*) "Error: Sorry, max. # of records exceeded..." stop endif nr = nr + 1 end do print*, "The number of rows of the data =", nr ! Create variables: key, lat, lon, jobs allocate(key(nr)) allocate(lat(nr)) allocate(lon(nr)) allocate(jobs(nr)) ! Read variables from the data file do i=1,nr read(10,*) key(i), lat(i), lon(i), jobs(i) end do print*, key close(10) end program location <hr />

If I use <strong>form='formatted'</strong> in the open statement, I've got the following message on the screen:

The number of rows of the data = 20 At line 41 of file location.f90 <unit=10, file = 'location_data.txt'> Fortran runtime error: Sequential READ or WRITE not allowed after EOF market, possibly use REWIND or BACKSPACE

And when I change <strong>form='formatted'</strong> to <strong>form='unformatted'</strong>, I have another erroneous result:

The number of rows of the data = 0

I have several barriers to tackle in order to get to the solution for a bigger data analysis in Fortran, and I think now it's the time to seek some help from you Fortran gurus. I would really appreciate it if you could help me with these errors.

Answer1:

I removed the form=unformatted part of the open statement and it got through that part no problem.

I received a different error after fixing the above. After allocating the variables, you try reading from the file <em>without rewinding it</em>. That is, the program last left the file at at the end, so there isn't anything more to read when you try to read it. The fix is simple: tell the program to jump back up to the beginning by using rewind:

allocate(jobs(nr)) rewind(10) do i=1,nr read(10,*) ... enddo

Recommend

  • Is Fortran return statement obsolete?
  • openMP: Assigning specific thread to a specific core
  • Installing R 3.3.2 in RHEL 6.8 without yum
  • Star * symbol found on second line of Fortran function declaration
  • Whats wrong with the following FORTRAN 77 code?
  • Converting Fortran IV to 77
  • Using ifelse with random variate generation in a function applied to a vector
  • Convert from DataFrame to JavaPairRDD
  • How to capture or listen to browser notifications?
  • Using Node cluster module with SailsJs: EADDRINUSE
  • Assignment of Allocatables of Different Shapes in Fortran [duplicate]
  • Passing array length to function
  • how tensorflow worker driver training process and cause variables update on ps job?
  • Downloading files from Google Storage using Spark (Python) and Dataproc
  • Objective C - Create a framework for my iphone apps?
  • Program crashes when run outside IDE
  • How to package a jar and all dependencies within a new jar with maven
  • Netezza Incremental load from Sql server using SSIS
  • LNK1104: cannot open file 'kernel32.lib'
  • Most efficient way to replace lowest list values in dataframe in R
  • Azure table store snapshot/backup capability
  • Memory error in python- how to use more memory
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • How to get current document uri in XSLT?
  • Differences in dis-assembled C code of GCC and Borland?
  • SharedPreferences or SQLite Database?
  • Spark fat jar to run multiple versions on YARN
  • Algorithm for a smudge tool?
  • How to delay loading a property with linq to sql external mapping?
  • Is there a javascript serializer for JSON.Net?
  • Where to put my custom functions in Wordpress?
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Buffer size for converting unsigned long to string
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • Checking variable from a different class in C#
  • Running Map reduces the dimensions of the matrices
  • Binding checkboxes to object values in AngularJs
  • java string with new operator and a literal