Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
1 View

HOW TO READ DATA FROM INPUT FILE

Hi, I wrote a program.for 9 variables. I want to read 9 Variables from the data.dat  file. Also, I wrote 13 subroutines based on 9 variables. But, when I compiled my program, its only read first one variable for all subroutine. How can I fix it? for example my variables data are a, b, c, d, e,f, i, j, k respectively 12.5   6.7  24.8  9.1   0.28   25.1   8.0   0.3   9.7.

the program 1 subroutine wrote using a parameter and 2 subroutines wrote using b parameter. Same rules for remaining subroutines.

please, suggest me how can I read my data from a file correctly. Many thanks for the consideration.

!MAIN PROGRAM FOR ESTIMATING WQI SCORES 
IMPLICIT NONE
REAL:: SAL,TEMP,PH, DOX, TON, AMN, POS, CHL,NT
REAL:: SISAL,SITEMP, SIPH, SIDOX, SITON, SIAMN, SICHL, SINT,SIPOS 
REAL:: MSAL, MTEMP, MPH, MDOX, MTON,MAMN, MPOS, MCHL, MNT 
REAL:: WSAL, WTEMP,WPH,WDOX,WTON,WAMN,WPOS,WCHL,WNT 
REAL:: M, WQI,SUMW,SUMSIW   
OPEN(UNIT=1,FILE='DATA2007A.DAT',STATUS= 'OLD')
READ(1,*) SAL,TEMP,PH, DOX, TON, AMN, POS,CHL,NT
WRITE (*,100) SAL,TEMP,PH, DOX, TON, AMN, POS, CHL,NT
100 FORMAT (9F10.2)
CLOSE(1)
!--------------------------------------------------------------------------
!CALL SUBROUTINES FOR PARAMETERS SUBINDEX GENERATE
!---------------------------------------------------------------------------
CALL SALI(SAL,SISAL)          !CALL SUBROUTINE SALINITY SUBINDEX CALCULATOR
CALL SUBTEMP(SITEMP,TEMP)     !CALL SUBROUTINE TEMPERATURE SUBINDEX CALCULATOR
CALL SPH (PH, SIPH)           !CALL SUBROUTINE PH SUBINDEX CALCULATOR
CALL SDOX( DOX, SIDOX)        !CALL SUBROUTINE DISSOLVED OXYGEN SUBINDEX CALCULATOR
CALL TORN(TON, SITON)         !CALL SUBROUTINE TON SUBINDEX CALCULATOR
CALL SAMN(AMN, SIAMN)         !CALL SUBROUTINE AMMONIA SUBINDEX CALCULATOR
CALL SUBPOS(SIPOS, POS)       !CALL SUBROUTINE TOTAL PHOSPHATE SUBINDEX CALCULATOR
CALL SUBCHL(SICHL, CHL)       !CALL SUBROUTINE CHLOROPHYLL SUBINDEX CALCULATOR
CALL SUBNT(NT, SINT)          !CALL SUBROUTINE NITRATE SUBINDEX CALCULATOR            

OPEN (2, FILE ='SUB2007.DAT', STATUS = 'NEW')
WRITE(2,200) SISAL, SITEMP, SIPH, SIDOX,SITON, SIAMN, SIPOS, SICHL, SINT
200 FORMAT (F6.2)
CLOSE(2)
!-------------------------------------------------------------------------------------

CALL MULTISIWEIGHT(MSAL,WSAL)   !CALL SUBROUTINE FOR MULTIPLY SUBINDEX AND WEIGHT

CALL CF(TEMP,M)                  !CALL SUBROUTINE TEMP CORRECTION FACTORS


CALL WQIFS(WQI,SUMSIW,SUMW)     !CALL SUBROUTINE FOR WQI CALCULATOR



END PROGRAM

!==========================================================================
!SUBROUTINE FOR CALCULATING SALINITY SUB INDEX 
!==========================================================================
SUBROUTINE SALI(SAL, SISAL)
IMPLICIT NONE
REAL :: SAL, SISAL
OPEN (1, FILE = 'DATA2007A.DAT',  STATUS = 'OLD')
READ(*,*) SAL
CLOSE (1)
IF ((SAL .LE. 35.0) .AND. (SAL .GT. 20.0)) THEN
 SISAL= 100
 ELSE IF ((SAL .GT. 10.0) .AND. (SAL .LE. 20.0)) THEN
 SISAL = 75
ELSE IF ((SAL .LE. 10) .AND. (SAL .GT. 0.0)) THEN
   SISAL = 50.0
ELSE
SISAL = 0.0
END IF
RETURN
END SUBROUTINE

!======================================================
!SUBROUTINE FOR CALCULATING TEMPERATURE SUBINDEX
!======================================================
SUBROUTINE SUBTEMP (SITEMP, TEMP)
IMPLICIT NONE
REAL :: SITEMP, TEMP

OPEN (1, FILE = 'DATA2007A.DAT', STATUS = 'OLD')

READ (*,*) TEMP
CLOSE (1)

IF ((TEMP .LE. 25.0) .AND. (TEMP .GT. 0.0)) THEN 
   SITEMP = 100
   ELSE
     SITEMP = 0.0
     END IF
     RETURN
     END SUBROUTINE
!=====================================================
!SUBROUTINE FOR CLACULATING SUBINDEX OF WATER PH
!=====================================================
SUBROUTINE SPH (PH, SIPH)
IMPLICIT NONE
REAL :: PH, SIPH
OPEN(1, FILE ='DATA2007A.DAT', STATUS = 'OLD')
READ (*,*) PH
CLOSE(1)

IF((PH .GE. 5.5) .AND. (PH .LE. 8.5)) THEN
  SIPH = 100
  ELSE
    SIPH= 0.0
    END IF
    RETURN
    END SUBROUTINE
!=================================================
!SUBROUTINE FOR DISSOLVED OXYGEN SUBINDEX
!=================================================
SUBROUTINE SDOX( DOX, SIDOX)
IMPLICIT NONE
REAL :: DOX, SIDOX

OPEN (1, FILE = 'DATA2007A.DAT', STATUS = 'OLD')
READ (*,*) DOX
CLOSE(1)

IF ((DOX .GE. 6.0) .AND. (DOX .GT. 0.0))THEN
  SIDOX = 100
  ELSE IF ((DOX .GE. 5.0) .AND. (DOX .LT. 6.0)) THEN
    SIDOX = 75
   ELSE IF ((DOX .GE. 3.0) .AND. (DOX .LT. 5.0)) THEN
      SIDOX = 50
      ELSE 
        SIDOX = 0.0
        END IF 
        RETURN 
        END SUBROUTINE
!=======================================================       
!SUBROUTINE FOR TOTAL ORGANIC NITROGEN (TON) SUBINDEX
!=======================================================
SUBROUTINE TORN(TON, SITON)
IMPLICIT NONE
REAL :: TON, SITON

OPEN(1, FILE = 'DATA2007A.DAT', STATUS = 'OLD')
READ(*,*)TON
CLOSE (1)

IF ((TON .LE. 1.0) .AND. (TON .GT. 0.0)) THEN
  SITON = 100.0
ELSE IF ((TON .GE. 2.0) .AND. (TON .LT. 3.0)) THEN
  SITON = 75.0
ELSE
  SITON = 0.0
END IF
RETURN
END SUBROUTINE
!====================================================
!SUBROUTINE FOR CALCULATING AMMONIA SUBINDEX
!====================================================
SUBROUTINE SAMN (AMN, SIAMN)
IMPLICIT NONE
REAL :: AMN, SIAMN

OPEN (1, FILE = 'DATA2007A.DAT', STATUS = 'OLD')
READ (*,*) AMN
CLOSE(1)

IF ((AMN .LE. 0.2) .AND. (AMN .GT. 0.0)) THEN
          SIAMN = 100.0
          ELSE IF((AMN .GE. 1.5) .AND. (AMN .LT. 2.0)) THEN
            SIAMN = 75.0
            ELSE IF ((AMN .GE. 2.0) .AND. (AMN .LT. 4.0)) THEN
              SIAMN = 50.0
              ELSE
                SIAMN = 0.0
              END IF 
      RETURN
   END SUBROUTINE
!=======================================================
!SUBROUTINE FOR CALCULATING PHOSPHATE SUBINDEX
!=======================================================
SUBROUTINE SUBPOS(SIPOS, POS)
IMPLICIT NONE
REAL :: SIPOS, POS
OPEN (1, FILE = 'DATA2007A.DAT', STATUS = 'OLD')
READ(*,*) POS
CLOSE(1)
IF ((POS .LE. 500) .AND. (POS .GT. 0.0)) THEN 
 SIPOS = 100
ELSE
 SIPOS = 0.0
END IF
RETURN
END SUBROUTINE
!======================================================
!SUBROUTINE FOR CHLOROPHILL
!======================================================
SUBROUTINE SUBCHL(SICHL, CHL)
IMPLICIT NONE
REAL :: SICHL, CHL
OPEN (1, FILE = 'DATA2007A.DAT', STATUS = 'OLD')
READ (*,*) CHL
CLOSE(1)
IF ((CHL .LE. 8) .AND. (CHL .GT. 0.0)) THEN
  SICHL = 100.0
  ELSE
    SICHL = 0.0
    END IF
    RETURN
    END SUBROUTINE
!======================================================
!SUBROUTINE FOR NITRATE SUBINDEX
!=======================================================
SUBROUTINE SUBNT(NT, SINT)
IMPLICIT NONE
REAL :: NT, SINT

OPEN(1, FILE = 'DATA2007A.DAT', STATUS = 'OLD')
READ(*,*) NT
CLOSE(1)
IF ((NT .LE. 50.0) .AND. (NT .GT. 0.0)) THEN
   SINT = 100.0
   ELSE
     SINT = 0.0
     END IF 
     RETURN
     END SUBROUTINE
!===============================================================     
!SUBROUTINE FOR MULTIPLICATING PARAMETER SUBINDEX AND WEIGHT
!===============================================================
SUBROUTINE MULTISIWEIGHT(MSAL,WSAL)
IMPLICIT NONE
REAL:: MSAL, MTEMP, MPH, MDOX, MTON,MAMN, MPOS, MCHL, MNT !MULTIPLICATION OF SUBINDEX AND WEIGHT VARIABLES
REAL:: SISAL, SITEMP,SIPH,SIDOX,SITON,SIAMN,SIPOS,SICHL,SINT ! PARAMETERS SUBINDEX VARIABLES
REAL:: WSAL, WTEMP,WPH,WDOX,WTON,WAMN,WPOS,WCHL,WNT !PARAMETERS WEIGHT VARIABLES

!READ PARAMETERS SUBINDEX VALUES

OPEN (3, FILE = 'SUB2007.DAT', STATUS = 'OLD')
READ (3,300) SISAL,SITEMP,SIPH,SIDOX,SITON,SIAMN,SIPOS,SICHL,SINT
300 FORMAT (F6.2)

CLOSE(3)
OPEN (4, FILE = 'WEIGHT.DAT', STATUS = 'OLD')
READ (4,400) WSAL,WTEMP,WPH,WDOX,WTON,WAMN,WPOS,WCHL,WNT
400 FORMAT (F6.2)
CLOSE(4)
       MSAL = SISAL*WSAL
       MTEMP = SITEMP*WTEMP
       MPH = SIPH*WPH
       MDOX = SIDOX*WDOX
       MTON = SITON*WTON
       MAMN = SIAMN*WAMN
       MPOS = SIPOS*WPOS
       MCHL = SICHL*WCHL
       MNT = SINT*WNT
OPEN(5, FILE ='MSUBIW.DAT', STATUS = 'NEW')
WRITE(5,500) MSAL,MTEMP,MPH,MDOX,MTON,MAMN,MPOS,MCHL,MNT
500 FORMAT (2X, F6.2)
CLOSE(5)
RETURN
END SUBROUTINE   
!===========================================================
!FOR CALCULATING TEMPERATURE CORRECTION FACTOR 
!===========================================================
  SUBROUTINE CF(TEMP,M)
  IMPLICIT NONE
  REAL::TEMP, M
!READ WATER TEMPERATURE FROM WQ DATA FILE
  OPEN (6, FILE = 'DATA2007A.DAT', STATUS = 'OLD')
  READ(6,*) TEMP

        IF ((TEMP .LT. 34) .AND. (TEMP .GT. 0.0)) THEN
         M = 0.5

        ELSE 
          M = 1.0
          END IF
  OPEN(10, FILE = 'TEMPCF.DAT', STATUS = 'NEW')
  WRITE(10,900) M
  900 FORMAT (F6.2)
  CLOSE(10)
          RETURN
          END SUBROUTINE
!=========================================================
!SUBROUTINE FOR CALCULATING FINAL WQI SCORES
!=========================================================
SUBROUTINE WQIFS(WQI,SUMSIW,SUMW)
IMPLICIT NONE
REAL :: MSAL,MTEMP,MPH,MDOX,MTON,MAMN,MPOS,MCHL,MNT,M,SUMW, SUMSW
REAL :: WSAL,WTEMP,WPH,WDOX,WTON,WAMN,WPOS,WCHL,WNT,SUMW,SUMSIW,WQI

!READ MULTIPLY VALUES OF PARAMETERS SUBINDEX AND WEIGHT
OPEN (7, FILE = 'MSUBIW.DAT', STATUS = 'OLD')
READ (7,600) MSAL, MTEMP, MPH, MDOX, MTON,MAMN, MPOS, MCHL, MNT
600 FORMAT (F6.2)
CLOSE(7)

!CALCULATE SUM OF MULTIPL RESULTS OF SUBINDEX AND WEIGHT
    SUMSIW = MSAL+MTEMP+MPH+MDOX+MTON+MAMN+MPOS+MCHL+MNT
    OPEN (13, FILE = 'SUMSIW.DAT', STATUS = 'NEW')
    WRITE (13,610) SUMSIW                         !SUM OF THE MULTILICATION OF SUBINDEX AND WEIGHT VALUES
    610 FORMAT (F6.2)
    CLOSE(13)

!READ PARAMETERS WEIGHT VALUES
OPEN(8, FILE = 'WEIGHT.DAT', STATUS = 'OLD')
READ(8,700) WSAL,WTEMP,WPH,WDOX,WTON,WAMN,WPOS,WCHL,WNT
700 FORMAT (F6.2)
CLOSE(8)

!OPEN TEMP CORRECTION FACTOR

OPEN(12, FILE = 'TEMPCF.DAT', STATUS = 'OLD')
READ(12,950) M
950 FORMAT (F6.2)
CLOSE(12)

!CALCULATE SUM OF PARAMETERS WEIGHT VALUES
      
  SUMW = WSAL+WTEMP+WPH+WDOX+WTON+WAMN+WPOS+WCHL+WNT

OPEN(15, FILE = 'SUMWEIGHT.DAT', STATUS = 'NEW')
WRITE(15,710) SUMW                             !SUM OF PARAMETERS WEIGHT VALUES
710 FORMAT (F6.2)
CLOSE(15)

OPEN(16, FILE ='SUMSIW.DAT', STATUS = 'OLD')
READ(16,350) SUMSIW
350 FORMAT (F6.2)
CLOSE(16)
OPEN (17, FILE = 'SUMWEIGHT.DAT', STATUS = 'OLD')
READ (17,360) SUMW
360 FORMAT (F6.2)
CLOSE(17)
!CALCULATE FINAL WQI SCORES
 WQI = (SUMSIW/SUMW)*M                         !WATER QUALITY INDEX 

OPEN (9, FILE = 'WQI.DAT', STATUS = 'NEW')
WRITE(9, 800) WQI
800 FORMAT (F6.1)
CLOSE(9)
     
RETURN

END SUBROUTINE

 

0 Kudos
4 Replies
Highlighted
1 View

Could you please place your

Could you please place your program into the copy buffer, then in this forum compose a comment and use the "{...} code" button to open a dialog box. In the dialog box is a pull-down control. Click on the pull-down and then select Fortran. Then in the edit box, paste the copy of your program. For the data file, do the same except that the selection in the pull-down should be Plain Text.

That will help us understand your problem.

Your issue appears to relate to mis-coordination between the

   formatting of the data file
   incorrect mode for OPEN
   incorrect READ
   incorrect FORMAT for READ

Jim Dempsey

0 Kudos
Highlighted
Beginner
1 View

Many thanks,

Many thanks, jimdempseyatthecove (Blackbelt) for your valuable reply. Already I added my code into my original post. Again thanks for your support.

0 Kudos
Highlighted
1 View

For future reference, please

For future reference, please use the "{...} code" button to insert your code into your comment. This does two things:

1) The displayed text contains auto-generated line numbers. These numbers can be used by readers to reference in their suggestions "on line number nn you should use xxx". As posted above we have no means to do that other than copying some section of text about the location and of which there may be several similar instances in your comment.

2) It make it easy for others here to copy your source file, and paste it into their development system.

! open and read prior run data
OPEN(UNIT=1,FILE='DATA2007A.DAT',STATUS= 'OLD')
READ(1,*) SAL,TEMP,PH, DOX, TON, AMN, POS,CHL,NT
...
! open and write updated run data
OPEN (2, FILE ='SUB2007.DAT', STATUS = 'NEW')
WRITE(2,200) SISAL, SITEMP, SIPH, SIDOX,SITON, SIAMN, SIPOS, SICHL, SINT
200 FORMAT (F6.2)
CLOSE(2)
...

In the above, READ(1,*) is a list directed read requiring field delimiters (,), whereas later WRITE(2,200) ... is a formatted write using fixed field widths (no commas, and may or may not contain white space between data).

You can correct this by either using the same FORMAT statement on the READ as on the WRITE, or changing the FORMAT used on the WRITE to include the commas:

200 FORMAT(5(F6.2,", "),F6.2) ! five followed by ", ", sixth without ", "

Be consistent with your usage.

Jim Dempsey

0 Kudos
Highlighted
Beginner
1 View

Dear jimdempseyatthecove

Dear jimdempseyatthecove (Blackbelt), Many thanks for your comments. If possible please can you define more precisely. Actually I am a new programmer in FORTRAN. This is my first program. Again many thanks for your supports and precious guidelines. 

0 Kudos