Guide for good coding
Contents
How to create a new module/subroutine
Subroutines should be integrated inside a module. A good writing habit is to add the line "IMPLICIT NONE", in the beginning of the module and subroutine.
First you can define all the variable that belongs to the module and could be used at another place. A good thing is also to create a subroutine that allocate and initialise module variables. If the variable is only modified inside the module it should be taged as protected. If the variable belongs only to the module it can be specified to be private.
You can now start the definition of the subroutine. Start by variable and function from other module if necessary. Comment the purpose of the subroutine directly. Declare the variables, first the one pass in arguments. Use the intent to specify the status of the variable inside the subroutine. Continue with the local and saved variables. Then you can define local variables. Remember to comment all of them, you can specify the dimension if the variable is an allocatable variable as well as the units and everything that seems appropriate.
You can finally write the code.
1 MODULE aquarium_mod
2
3 IMPLICIT NONE
4
5 INTEGER :: water ! Define what is the variable, the dimension, units etc..
6 REAL,SAVE,ALLOCATABLE,DIMENSION(:) :: kre1 ! Another comment (°)
7
8 CONTAINS
9
10 c===== subroutine 1
11
12 SUBROUTINE ini_aquarium(ngrid)
13 c=======================================================================
14 c subject:
15 c --------
16 c Initialise module s variable
17 c
18 c author: Someone
19 c=======================================================================
20 IMPLICIT NONE
21 INTEGER, INTENT(IN) :: ngrid
22
23 allocate(kre1(ngrid+1))
24
25 END SUBROUTINE ini_aquarium
26
27 c===== subroutine 2
28
29 SUBROUTINE end_aquarium
30 c=======================================================================
31 c subject:
32 c --------
33 c Deallocate module s variable
34 c
35 c author: Someone
36 c=======================================================================
37
38 IMPLICIT NONE
39
40 IF (allocated(kre1)) deallocate(kre1)
41
42 END SUBROUTINE end_aquarium
43
44 c===== subroutine 3
45
46 SUBROUTINE aquarium(kre, grass, other_things
47 & really_anything)
48
49 use another_module_mod, only: fct, variable
50
51 c=======================================================================
52 c subject:
53 c --------
54 c What is the subroutine doing
55 c
56 c author: Someone smart
57 c ------
58 c update: Someone smarter, march 2012:
59 c - I change something here
60 c
61 c=======================================================================
62
63 IMPLICIT NONE
64
65 c-----------------------------------------------------------------------
66 c
67 c Declarations :
68 c --------------
69 c
70 c Input/Output
71 c ------------
72
73 REAL, INTENT(IN) :: something(ngrid,nlayer) ! 2D tabular used as an input only (W/m^2)
74 REAL, INTENT(OUT) :: kre(ngrid) ! 1D tabular output by the subroutine
75 REAL, INTENT(INOUT) :: grass(ngrid,nlayer) ! Variable that is modified by the subroutine
76 ! it is an input and output
77 LOGICAL,INTENT(IN) :: really_anything ! Boolean variable
78
79 c Local saved variables :
80 c -----------------
81 INTEGER, PARAMETER :: nb_kre = 6 ! Number of kre
82 INTEGER, SAVE :: rock ! Another variable
83 LOGICAL,SAVE :: firstcall=.true. ! Another variable
84
85 c Local variables :
86 c -----------------
87 REAL tabular(ngrid) ! Local var
88 INTEGER i ! Loop var
89
90 c Beginning of the code :
91 c -----------------
92
93 IF (firstcall) THEN
94 ! identify where are the kre
95 DO i=1,nb_kre
96 kre(i)=i
97 write(*,*) "the Kre ", i, "is behind the rock"
98 ENDDO !end of the loop nb_kre
99 ENDIF !end firstcall
100
101 END SUBROUTINE aeropacity
102
103 END MODULE aeropacity_mod