Search code examples
inputfortran

Trying to figure out the structure of an input file based on a code


I'm sort of troubleshooting a code written in Fortran but I'm not yet familiar with it. I have the following code (a part), and basically, I have to 'reverse-engineer' the structure of the input file.

OPEN(10, FILE=TRIM(10)//'.txt', STATUS='OLD')
READ(10, *)     ! header

READ(10, *) NB_TEMP
ALLOCATE(TEMP(abs(NB_TEMP)))

IF (NB_TEMP <0) THEN
  NB_TEMP = ABS(NB_TEMP)
  READ(10, *) TEMP_0, TEMP_D
  TEMP(1) = TEMP_0
  DO I=2, NB_TEMP
    TEMP(I) = TEMP(I-1) + TEMP_D
  ENDDO
ELSEIF (NB_TEMP>0) THEN
  READ(10,*) TEMP(:)
ENDIF

READ(10, *) NB_PRS
ALLOCATE(PRS(ABS(NB_PRS)))
IF (NB_PRS<0) THEN
  NB_PRS = ABS(NB_PRS)
  READ(10, *) PRS_0, PRS_D
  PRS(1) = PRS_0*PI/180.
  DO I=2, NB_PRS
    PRS(I) = PRS(I-1) + PRS_D*PI/180.
  ENDDO
ELSEIF(NB_PRS>0) THEN
  READ(10,*) PRS(:)
  DO I=1, NB_PRS
    PRS(I) = PRS(I)*PI/180.
  ENDDO
ENDIF

So, I know I'm opening the .txt file first. Then, the first value I read is the "NB_TEMP". I'm not understanding what is happening at the second READ command. Does the program read the same values but set them differently, as "NB_PRS" I don't really have an error or anything - I'm simply trying to understand what this code does line-by-line and what would the structure be like for the input text file.

Thanks in advance!


Solution

  • Each read command reads a line from the file. Lets us examine them one by one

    1. READ(10, *) ! header

      Ignore the first line

    2. READ(10, *) NB_TEMP

      Read an integer value into NB_TEMP.

    3. READ(10, *) TEMP_0, TEMP_D

      Read two real values. This happens if NB_TEMP is negative, and they represent the starting value and the step for an arithmetic sequence stored into TEMP(:). This is asserted from TEMP(I) = TEMP(I-1) + TEMP_D.

    4. READ(10,*) TEMP(:)

      Read multiple real values and store them into TEMP(:) array. The should be NB_TEMP count values in one line here.

    and similarly for PRS where the program branches depending if the integer NB_PRS is positive or negative.


    Here are some valid inputs they way I interpret this code

    ! TEST INPUT #1, DEFINED TEMP and PRS
    6
        32.0000    40.0000    60.0000    90.0000    120.0000    130.0000    
    9
        0.00000     5.0000     15.0000        20.0000     25.0000     30.0000     45.0000     55.0000     60.0000
    
    ! TEST INPUT #2, SEQUENCE TEMP and  DEFINED PRS
    -6
        30.0000    10.0000
    9
        0.00000     5.0000     15.0000        20.0000     25.0000     30.0000     45.0000     55.0000     60.0000