Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran Error Unexpected data declaration statement

  1. Mar 4, 2014 #1
    Fortran Error "Unexpected data declaration statement"

    Hi all, I'm writing a program to perform a linear inversion on a set of magnetic data. I'm fairly new to Fortran, but I have coded Fourier and wavelet transformation programs in it. I'm having a problem when trying to declare my variable types. I'm using the "dimension" command as I have in previous programs to define the size of multiple arrays at once. For some reason when it compiles I get the error "Unexpected data declaration statement." The code is more lengthy than this, so I just copied and pasted the parameter declaration into another file, yet the error still exists. Its as if my compiler does not recognize the dimension command, but if I try to compile a previous program that uses it, it does so fine. Also, even if I don't use the dimension command the compiler still finds errors. I'm sure something simple is wrong, I just cannot seem to find it.



    Code (Text):
    *This code will perform a linear inversion to a set of magnetic data
    *via the use of 2D prisms

    *>>>>>>>>>>>>>>>>>>>>>Declare Variable Definitions<<<<<<<<<<<<<<<<<<   
    *pi= pi
    *u_0= permeability of free space
    *D= Number of magnetic data
    *P= Number of parameters (prisms)
    *T= Total field anomaly
    *B_obs= Raw data in absolute intensity form
    *Bx= Bx to be used in filling matrix A
    *Bz= Bz to be used in filling matrix A
    *A= Forward problem matrix with physics


    *>>>>>>>>>>>>>>>>>>>>>>>>>Declare Variable Types<<<<<<<<<<<<<<<<<<<<<  

        parameter(pi=3.141592, u_0=1.25663706e-6)
        integer D=251
        integer P=25
        real, dimension(0:D-1) :: Inp_Data, T, B_obs
        real, dimension(0:P-1) :: Bx, Bz
        real, dimension(0:D-1,0:P-1):: A

    *>>>>>>>>>>>>>>>>>>>>>>>Quantify Various Variables<<<<<<<<<<<<<<<<<<

        end
    Another odd thing to me is if I define P and D in the parameter statement (as I have done in the past) the compiler treating them as real and not integer.

    Code (Text):
    A2P1test.f:28.24:

          real, dimension(0:D-1,0:P-1) :: A                                
                            1
    Error: Expression at (1) must be of INTEGER type, found REAL
    A2P1test.f:28.30:

          real, dimension(0:D-1,0:P-1) :: A                                
                                  1
    Error: Expression at (1) must be of INTEGER type, found REAL
    A2P1test.f:28.39:

          real, dimension(0:D-1,0:P-1) :: A                                
                                           1
    Error: The module or main program array 'a' at (1) must have constant shape
    A2P1test.f:26.24:

          real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                            1
    Error: Expression at (1) must be of INTEGER type, found REAL
    A2P1test.f:26.50:

          real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                                      1
    Error: The module or main program array 'b_obs' at (1) must have constant shape
    A2P1test.f:27.24:

          real, dimension(0:P-1) :: Bx, Bz                                  
                            1
    Error: Expression at (1) must be of INTEGER type, found REAL
    A2P1test.f:27.34:

          real, dimension(0:P-1) :: Bx, Bz                                  
                                      1
    Error: The module or main program array 'bx' at (1) must have constant shape
    A2P1test.f:27.24:

          real, dimension(0:P-1) :: Bx, Bz                                  
                            1
    Error: Expression at (1) must be of INTEGER type, found REAL
    A2P1test.f:27.38:

          real, dimension(0:P-1) :: Bx, Bz                                  
                                          1
    Error: The module or main program array 'bz' at (1) must have constant shape
    A2P1test.f:26.24:

          real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                            1
    Error: Expression at (1) must be of INTEGER type, found REAL
    A2P1test.f:26.40:

          real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                            1
    Error: The module or main program array 'inp_data' at (1) must have constant shape
    A2P1test.f:26.24:

          real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                            1
    Error: Expression at (1) must be of INTEGER type, found REAL
    A2P1test.f:26.43:

          real, dimension(0:D-1) :: Inp_Data, T, B_obs                      
                                               1
    Error: The module or main program array 't' at (1) must have constant shape
    Code (Text):
    *>>>>>>>>>>>>>>>>>>>>>>>>>Declare Variable Types<<<<<<<<<<<<<<<<<<<<<  

        parameter(pi=3.141592, u_0=1.25663706e-6, D=251, P=25)
    *   integer D=251
    *   integer P=25
        real, dimension(0:D-1) :: Inp_Data, T, B_obs
        real, dimension(0:P-1) :: Bx, Bz
        real, dimension(0:D-1,0:P-1) :: A
     
    Thank you for any insight on these problems,

    TamuKevin
     
  2. jcsd
  3. Mar 4, 2014 #2

    Mark44

    Staff: Mentor

    By default, variables whose names start with I, J, K, L, M, or N are considered to be integer-type variables, and variables that start with other letters in the alphabet are considered to be real-type variables.

    I haven't written any Fortran for many years, so I'm a little rusty on the syntax, but I believe your declarations and parameter statements are causing you problems.

    Since P and D would by default be floating point variables, to make them integer parameters, I believe that you need to first declare them as integers, and then give them values in a parameter statement, like so:
    Code (Text):

    integer P, D
    parameter (P = 25, D = 251)
     
    When you fix these two, that should take care of the error involving the arrays that are described as not having a constant shape.
     
  4. Mar 4, 2014 #3
    Mark, you nailed it. I knew it was something silly. Thank you for your help, I really appreciate it.
     
  5. Mar 4, 2014 #4

    Mark44

    Staff: Mentor

    Glad to be able to help!
     
  6. Mar 4, 2014 #5

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    I think the OP got caught by one of Fortran's interesting features ("interesting" as in " strange, and not very useful"...)

    All whitespace characters in statements are ignored, except in character strings. So the statement

    Code (Text):

    integer D=251
     
    is legal, but it is actually an assignment statement giving a value to the variable named
    Code (Text):

    integerD
     
    ... which is not what you wanted, of course.

    A similar "gotcha" is a typo like
    Code (Text):

    DO 10 I = 1.10
     
    which is legal, and sets the real variable
    Code (Text):

    DO10I
     
    to 1.1, instead of starting a DO loop. :redface:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook