Fortran Error Unexpected data declaration statement

  • Context: Fortran 
  • Thread starter Thread starter TamuKevin
  • Start date Start date
  • Tags Tags
    Data Error Fortran
Click For Summary

Discussion Overview

The discussion revolves around a Fortran programming issue related to variable declaration and array dimensions in a program intended for linear inversion of magnetic data. Participants are exploring the error messages encountered during compilation, particularly focusing on the "Unexpected data declaration statement" error and issues with variable types.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their experience with Fortran and the specific error they encounter when declaring variable types using the "dimension" command.
  • They express confusion over the compiler not recognizing the dimension command, despite it working in previous programs.
  • There is mention of the compiler treating defined parameters as real instead of integer, leading to further errors when declaring arrays.
  • Errors are noted regarding the need for constant shapes in array declarations, with specific error messages provided as examples.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the cause of the errors, and multiple competing views regarding the handling of variable types and array dimensions remain unresolved.

Contextual Notes

Limitations include potential misunderstandings of Fortran's handling of parameter types and array dimensions, as well as the specific requirements for constant shapes in array declarations.

TamuKevin
Messages
2
Reaction score
0
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.



[...~/documents/Assignment2] gfortran A2P1test.f
A2P1test.f:26.72:

real, dimension(0:D-1) :: Inp_Data, T, B_obs
1
Error: Unexpected data declaration statement at (1)
A2P1test.f:27.72:

real, dimension(0:P-1) :: Bx, Bz
1
Error: Unexpected data declaration statement at (1)
A2P1test.f:28.72:

real, dimension(0:D-1,0:P-1) :: A
1
Error: Unexpected data declaration statement at (1)

Code:
*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:
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:
*>>>>>>>>>>>>>>>>>>>>>>>>>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
 
Technology news on Phys.org
TamuKevin said:
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:
*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:
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:
*>>>>>>>>>>>>>>>>>>>>>>>>>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
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:
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.
 
Mark, you nailed it. I knew it was something silly. Thank you for your help, I really appreciate it.
 
Mark44 said:
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.

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:
integer D=251
is legal, but it is actually an assignment statement giving a value to the variable named
Code:
integerD
... which is not what you wanted, of course.

A similar "gotcha" is a typo like
Code:
DO 10 I = 1.10
which is legal, and sets the real variable
Code:
DO10I
to 1.1, instead of starting a DO loop. :redface:
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
3
Views
12K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K