Category Archives: Fortran

Fortran get_command_argument

Fortran 2003 is a major extension to Fortran 90/95 including many useful features, one significant feature is access to command arguments; this allows a program to take data from the execution command line and use this arguments as input information.

program cmdline
  implicit none

  character(len=*), parameter :: version = '1.0'
  character(len=32) :: arg
  integer :: i

  do i = 1, command_argument_count()
     call get_command_argument(i, arg)

     select case (arg)
     case ('-v', '--version')
        print '(2a)', 'cmdline version ', version
     case ('-h', '--help')
        call print_help()
     case default
        print '(a,a,/)', 'Unrecognized command-line option: ', arg
        call print_help()
     end select
  end do


  subroutine print_help()
    print '(a)', 'usage: cmdline [OPTIONS]'
    print '(a)', 'cmdline options:'
    print '(a)', '  -v, --version     print version information and exit'
    print '(a)', '  -h, --help        print usage information and exit'
  end subroutine print_help

end program cmdline


In my last year of college I started working in solar energy research, processing large amounts of data of global, diffuse and direct solar radiation. The processing of this information was very difficult, considering that a year of measurements means more than 500,000 data measured every minute. For each data had to calculate the position of the sun and do quality control of measurements, it was impossible to do it in Excel due to long processing times and freezes. It was then that I began to process information with Fortran, everything was much faster, did not fall and was able to process lots of information quickly. The problem was that was not possible to generate graphics and that was critical for me.

This is how I reach DISLIN, a library to generate incredible graphics from Fortran, very easy to use and export to all formats. With the time I even use Dislin to generate Graphics Users Interface (GUI) for Fortran software.

Below are images with more than 200,000 points that was realized in a couple of seconds with Fortran and DISLIN.


Make it’s a great Unix utility when you are compiling big programs, because automatically builds executable programs and libraries from source code by reading a file called makefile.

The text file named makefile must be in the source folder and it’s runs by the command make in the terminal.

Here a generic makefile:

F90 = gfortran
CFLAGS := -g -fbounds-check
NETCDF := -I/usr/include -L/usr/lib -lnetcdff -lnetcdf
OBJECTS = main.o subrutine.o function.o
.PHONY : clean

all : program1 program2

program1 : $(OBJECTS)
	$(F90) $(CFLAGS) -o Program1 $(OBJECTS) 

main.o : main.f90
	$(F90) $(CFLAGS) $(NETCDF) -c main.f90 

subrutine.o : subrutine.f90
	$(F90) $(CFLAGS) -c subrutine.f90

function.o : function.f90
	$(F90) $(CFLAGS) -c function.f90

program2 : prog2.o
	$(F90) $(CFLAGS) -o Program2 prog2.o

prog2.o : prog2.f90
	$(F90) $(CFLAGS) -c prog2.f90

	cp Program1 /Home/JP/
	cp Program2 /Home/JP/sub/
	rm –f *.o *.mod

Where is the Sun ?? (Solar position algorithms)

Accurate computation of solar position plays a fundamental role in solar energy applications, especially for concentrating systems. The required accuracy varies over a wide range, depending on the application: flat systems tolerate errors of a few degrees without significant losses, while high-concentration systems can require an accuracy of the order of 0.01º. More specific applications, such as the calibration of pyranometers ( Reda and Andreas, 2004 ), require an even greater accuracy.

Continue reading

How to compile a Fortran program

Most of the times the most difficult step it is the first one…

Every programing languages have differents statements to interpretate in code what you wants your programs do. To compile is the process to translate from text of source code written in a defined programming language to binary machine language, creating an executable program.

The process of develop a program have some main steps, define the flowchart of information, write the code, test and optimize.

The compilation commands are different if we are in the tests and debugging step or in the optimization step, because for tests of functionality the speed it is not an issue but it is possible to use the debugger and to easily understand where are the problems.

The basic structure of Fortran is:

program <name>

use <module_name>

implicit none


<executable statements>



end program <name>

Continue reading