# Translating Fortran to Julia

**URL:** <https://discourse.julialang.org/t/translating-fortran-to-julia/27518>\
**Category:** General Usage\
**Tags:** fortran\
**Created:** [August 14, 2019, 7:04am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518 "2019-08-14T07:04:06Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 14, 2019, 7:04am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518/1 "2019-08-14T07:04:06Z")

</div>

I’m translating some old Fortran code to Julia. It’s pretty straightforward, but I only picked up Fortran to be able to translate this project. I see at some points things like

```julia
c OPENING FILES FOR OUTPUT WRITING
     OPEN(2, file='pv.txt')
     REWIND(2)
     WRITE(2, *)"lambda1",19,20,21

```

What would be the equivalent in Julia?

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [August 14, 2019, 9:17am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518/2 "2019-08-14T09:17:56Z")

</div>

I am old enough to remember programming in Fortran using magnetic tapes.  
The REWIND statement rewinds the tape to the start. So you will effectively be overwriting that file.  
Just open a file for writing as normal, not as an append.  
open(“pv.txt”, “w”)

Stop me before I start talking about mounting ring out / ring in

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 14, 2019, 9:53am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518/3 "2019-08-14T09:53:47Z")

</div>

haha, I have heard about those magnetic tapes you talk about.

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [August 14, 2019, 10:55am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518/4 "2019-08-14T10:55:31Z")

</div>

The equivalent of REWIND in C stdlib and also Julia is the `seek` function. Without the REWIND (or `seek`) the writing would start wherever the tape had been left. Perhaps this is not relevant any more for modern I/O. But just in case I would translate the REWIND to `seek(io, 0)` .

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [August 15, 2019, 4:48am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518/5 "2019-08-15T04:48:43Z")

</div>

Could I help with your translation project? I even have this book on my bookshelf, and no it is not a spoof!

> **[A structured approach to FORTRAN 77 programming by T. M. R. Ellis | Open Library](https://openlibrary.org/books/OL3780511M/A_structured_approach_to_FORTRAN_77_programming)**
>
> A structured approach to FORTRAN 77 programming by T. M. R. Ellis, 1982, Addison-Wesley edition, in English

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 15, 2019, 5:12am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518/6 "2019-08-15T05:12:37Z")

</div>

Oh, thank you!

It’s around 20k lines of code but very straightforward, as it is mainly manipulation of matrices and vectors.

I’m checking that matrices that are constructed inside subroutines are preallocated (Fortran does that in the declaration of variables section of a subroutine).

The main time-consuming task is changing the indexation of matrices from `()` to `[]`. It is time consuming because one has to interpret whether some parenthesis are actually grouping operations, rather than indexing or calling functions.

I’m also changing spelled-out greek letters to unicode (alpha to α), but that’s just me.

@Raf posted this rough translator: [https://gist.github.com/rafaqz/fede683a3e853f36c9b367471fde2f56](https://gist.github.com/rafaqz/fede683a3e853f36c9b367471fde2f56), but it doesn’t catch all translation instances (and I think it doesn’t work with Julia 1+).

If that is something you are interested in, I uploaded some of the files to my GitHub: [https://github.com/amrods/Fortran](https://github.com/amrods/Fortran)

---

<div class="post-metadata">

**Author:** ![Modatu](https://avatars.discourse-cdn.com/v4/letter/m/4bbf92/32.png) [@Modatu](https://discourse.julialang.org/u/Modatu)\
**Post date:** [August 15, 2019, 7:17am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518/7 "2019-08-15T07:17:04Z")

</div>

> [@stephancb](#):
>
> Perhaps this is not relevant any more for modern I/O.

Sorry for being off-topic.  
Actually, I think it is. We used a lot of tapes for long-term backup (see: [LTO](https://en.wikipedia.org/wiki/Linear_Tape-Open))  
As far as I know it is pretty common for backup solutions.

Cheers,  
Florian

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [August 15, 2019, 10:22am UTC](https://discourse.julialang.org/t/translating-fortran-to-julia/27518/8 "2019-08-15T10:22:48Z")

</div>

Yes, there should be still tape drives around. According to my memory the FORTRAN rewind (on VAX VMS, Norsk Data SINTRAN, etc.) would rewind the tape drive and subsequent writes would then overwrite files that were on the tape before, starting with the first one, and make the full length of the tape available for writing. To do this in Julia, one would need to use an external utility like [mt](https://linux.die.net/man/1/mt). However, the file name `'pv.txt'` suggests, that this is intended for ordinary files on a disk, and then the `REWIND` just after the `OPEN` has no effect. Just don’t open for appending, as already said. The equivalent of the `WRITE` is probably `println` (`write` would be in case of `OPEN( …, FORMAT=‘unformatted’) in FORTRAN).
