Search code examples
pythonfortrancode-translation

How to convert old-style Fortran DO loop with conditionals to Python?


My question is about converting Fortran to Python language but I couldn't understand the working principle of this part of code. How can I rewrite the code in Python and which statement should I use instead of do if then write statement ?

#FOR GEAR CONVEX SIDE I = l, FOR GEAR CONCAVE SIDE I = 2.

DO 99999 I=1,2
IF(I .EQ. 1)THEN
WRITE (72,*)'GEAR CONVEX SIDE'
DLTA=DLTX
M21PRM=M21XPR
AXIL=AXILX
ELSE
WRITE(72,*)'GEAR CONCAVE SIDE'
DLTA=DLTV
M21PRM=M21VPR
END IF
WRITE (72, *)

AXIA=DEF/(AXIL*AXIL)
#CALCULATE GEAR BLADE ANGLE


IF(I .EQ. 2)THEN
PSIG=180. D00*CNST-PSIG
END IF
CSPSIG=DCOS(PSIG)
SNPSIG=DSIN(PSIG)
CTPSIG=CSPSIG/SNPSIG

##CALCULATE CUTTER TIP RADIUS

IF(I .EQ. 1)THEN
RG = (ADIA-W)/2.DO0
ELSE
RG = (ADIA+W)/2.D00
END IF

It is just a part of the main code and I couldn't understand the DO 9999 i=1,2 section and the following code (the relation of if, then and write).


Solution

  • Mother of god, it's Fortran 77. I feel your pain.

    DO 99999 I=1,2
    IF(I .EQ. 1)THEN
    WRITE (72,*)'GEAR CONVEX SIDE'
    .
    .
    .
    IF(I .EQ. 2)THEN
    .
    .
    

    is similar to

    for i in [1, 2]:
        if i == 1:
            print "GEAR CONVEX SIDE"
            .
            .
        if i == 2:
            .
            .
    

    As far as I understand this part of Fortran code, everything apart from for loop line has to be indented.

    If you are writing to file, as WRITE(72, *) might suggest, then you need to open a file before entering the loop and then write to it instead of just using print, something like this:

    file = open("filename", "w")
    for ...:
        if ...:
            file.write(" GEAR CONVEX SIDE")
        .
        .
        .