Fortran Error Unexpected data declaration statement

In summary, The user is trying to declare variable types in their Fortran program, but is encountering an "Unexpected data declaration statement" error when using the "dimension" command. Even when removing the dimension command, the compiler still finds errors. The user cannot seem to find the issue and is confused as to why the compiler is treating variables as real instead of integer.
  • #1
TamuKevin
2
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
  • #2
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.
 
  • #3
Mark, you nailed it. I knew it was something silly. Thank you for your help, I really appreciate it.
 
  • #5
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:
 

What is "Fortran Error Unexpected data declaration statement"?

"Fortran Error Unexpected data declaration statement" is an error message that may appear when using the Fortran programming language. It indicates that there is a problem with a data declaration statement, such as a variable or array, that is not expected in that particular location in the code.

What causes this error to occur?

This error can occur for a variety of reasons, such as using an incorrect syntax or trying to declare a variable in the wrong place in the code. It can also happen if there are missing or extra characters in the data declaration statement.

How can I fix this error?

To fix this error, you will need to carefully review the data declaration statement that is causing the error and make sure it is correct. Check for any missing or extra characters, and make sure the statement is in the correct location in the code. You may also need to consult the Fortran language documentation for proper syntax and usage.

Are there any common mistakes that can lead to this error?

Yes, there are a few common mistakes that can cause this error. These include using incorrect syntax, forgetting to declare a variable before using it, and trying to declare a variable in a subroutine or function instead of in the main program.

Can I prevent this error from happening in the future?

Yes, you can prevent this error by carefully reviewing your code and double-checking all data declaration statements for accuracy and correct syntax. It is also helpful to have a good understanding of the Fortran language and its rules for declaring data.

Similar threads

  • Programming and Computer Science
Replies
15
Views
207
  • Programming and Computer Science
Replies
4
Views
619
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
2
Views
314
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
11K
Back
Top