SURFEX v8.1
General documentation of Surfex
fm_writ.F90
Go to the documentation of this file.
1 ! ######spl
2  SUBROUTINE fm_writ(HFILEM,HRECFM,HFIPRI,KLENG,KFIELD,KGRID,&
3  KLENCH,HCOMMENT,KRESP)
4  USE parkind1, ONLY : jprb
5  USE yomhook , ONLY : lhook, dr_hook
6 ! ###########################################################
7 !
8 !!**** *FM_WRIT* - routine to write a single data article into a "FM"-file
9 !!
10 !! PURPOSE
11 !! -------
12 !
13 ! The purpose of FMWRIT is to write one article into a Meso-nh data file.
14 ! This routine only holds for a LFI-file (not namelist).
15 !
16 !!** METHOD
17 !! ------
18 !!
19 !! The unformatted write operation is actually performed by the routine
20 !! LFIECR. You need to indicate the file name without the ".lfi"
21 !! suffix, the data array and the
22 !! length of this array. Furthermore, you have to give a name for the article
23 !! you are writing (string) which you better choose by convention.
24 !! FMWRIT also appends the grid-indicator (KGRID) at the beginning of
25 !! the LFI logical article (IWORK(1)) ; then the length of the comment
26 !! string (KLENCH) ; then the comment string itself which is first
27 !! converted into integer type using ICHAR.
28 !! Finally, it writes the data (integer or
29 !! real) itself (rest of array IWORK). We stress that the length KLENG
30 !! that the user has to indicate is the length of the real data array
31 !! WITHOUT taking the other fields into account.
32 !!
33 !! EXTERNAL
34 !! --------
35 !!
36 !! FMLOOK,LFIECR,ICHAR
37 !!
38 !! IMPLICIT ARGUMENTS
39 !! ------------------
40 !!
41 !! MODULE: MODD_FMDECLAR contains management parameters and
42 !! storage arrays to move information around at the
43 !! level of all "FM"-routines.
44 !!
45 !! REFERENCE
46 !! ---------
47 !!
48 !! see the Technical Specifications Report for the Meso-nh project
49 !! (in French)
50 !!
51 !! AUTHOR
52 !! ------
53 !!
54 !! C. FISCHER *METEO-FRANCE*
55 !!
56 !! MODIFICATIONS
57 !! -------------
58 !!
59 !! original 06/94
60 !! modified by V. Masson 16/09/96 (prints if error occurs)
61 !----------------------------------------------------------------------------
62 !
63 !* 0. DECLARATIONS
64 ! ------------
65 !
66 USE modd_fmdeclar
67 
68 IMPLICIT NONE
69 !
70 !* 0.1 Declarations of arguments
71 !
72 CHARACTER(LEN=*) ,INTENT(IN) ::HFILEM ! file name
73 CHARACTER(LEN=*) ,INTENT(IN) ::HRECFM ! name of the article to be written
74 
75 CHARACTER(LEN=*) ,INTENT(IN) ::HFIPRI ! file for prints in FM
76 
77 INTEGER, INTENT(IN) ::KLENG ! length of the data field
78 INTEGER(KIND=8),DIMENSION(1:KLENG),INTENT(IN) ::KFIELD ! array containing the data field
79 INTEGER, INTENT(IN) ::KGRID ! C-grid indicator (u,v,w,T)
80 INTEGER, INTENT(IN) ::KLENCH ! length of comment string
81 
82 CHARACTER(LEN=KLENCH), INTENT(IN) ::HCOMMENT ! comment string)
83 
84 INTEGER, INTENT(OUT)::KRESP ! return-code if problems araised
85 
86 !
87 !* 0.2 Declarations of local variables
88 !
89 INTEGER::IRESP,ITOTAL,INUMBR,J,IROW,IFMFNL,ILUPRI
90 INTEGER(KIND=8),DIMENSION(:),ALLOCATABLE::IWORK
91 INTEGER,DIMENSION(1:JPXKRK)::ICOMMENT
92 CHARACTER(LEN=JPFINL)::YFNLFI
93 CHARACTER(LEN=LEN(HFILEM))::YINTFN
94 !
95 !* 0.3 Taskcommon for logical units
96 !
97 COMMON/TASKWRIT/ILUPRI,INUMBR,IRESP
98 !DIR$ TASKCOMMON TASKWRIT
99 !
100 !----------------------------------------------------------------------------
101 !
102 !* 1.1 THE NAME OF LFIFM
103 !
104 REAL(KIND=JPRB) :: ZHOOK_HANDLE
105 IF (lhook) CALL dr_hook('FM_WRIT',0,zhook_handle)
106 iresp = 0 ; irow = 0 ; ilupri = 6
107 ifmfnl=jpfinl-4
108 
109 irow=len(hfilem)
110 
111 IF (irow.EQ.0) THEN
112  iresp=-64
113  GOTO 1000
114 ELSEIF (irow.GT.ifmfnl) THEN
115  iresp=-65
116  GOTO 1000
117 ENDIF
118 yintfn=adjustr(hfilem)
119 yfnlfi=yintfn//'.lfi'
120 yfnlfi=adjustl(yfnlfi)
121 
122 !
123 !* 1.2 WE LOOK FOR THE FILE'S LOGICAL UNIT
124 !
125 CALL FMLOOK(YFNLFI,HFIPRI,INUMBR,IRESP)
126 IF (iresp.NE.0) GOTO 1000
127 
128 !
129 !* 2. GRID INDICATOR, COMMENT AND DATA ARE PUT TOGETHER
130 !
131 IF (kleng.LE.0) THEN
132  iresp=-40
133  GOTO 1000
134 ELSEIF (kleng.GT.jpxfie) THEN
135  iresp=-43
136  GOTO 1000
137 ELSEIF ((kgrid.LT.0).OR.(kgrid.GT.8)) THEN
138  iresp=-46
139  GOTO 1000
140 ENDIF
141 
142 itotal=kleng+1+klench+1
143 IF(ALLOCATED(iwork)) DEALLOCATE(iwork)
144 ALLOCATE(iwork(itotal))
145 
146 iwork(1)=kgrid
147 
148 SELECT CASE (klench)
149 CASE(:-1)
150  iresp=-55
151  GOTO 1000
152 CASE(0)
153  iwork(2)=klench
154  iwork(3:kleng+2)=kfield(1:kleng)
155 CASE(1:JPXKRK)
156  DO j=1,klench
157  icomment(j)=ichar(hcomment(j:j))
158  ENDDO
159  iwork(2)=klench
160  iwork(3:klench+2)=icomment(1:klench)
161  iwork(klench+3:itotal)=kfield(1:kleng)
162 CASE(JPXKRK+1:)
163  iresp=-57
164  GOTO 1000
165 END SELECT
166 
167 !
168 ! no compressing of any kind: the data is pure binary
169 !
170 !* 3. UNFORMATTED, DIRECT ACCESS WRITE OPERATION
171 !
172 CALL LFIECR(IRESP,INUMBR,HRECFM,IWORK,ITOTAL)
173 IF (iresp.NE.0) GOTO 1000
174 
175 DEALLOCATE(iwork)
176 !
177 !* 4. MESSAGE PRINTING WHATEVER THE ISSUE WAS
178 !
179 1000 CONTINUE
180 
181 IF (iresp.NE.0) THEN
182 yfnlfi=adjustl(hfipri)
183 DO j=1,jpnxlu
184  IF (cnamfi(j).EQ.yfnlfi) THEN
185  ilupri=j
186  EXIT
187  ENDIF
188 ENDDO
189 WRITE (ilupri,*) ' exit from FMWRIT with IRESP:',iresp
190 WRITE (ilupri,*) ' | HFILEM = ',hfilem
191 WRITE (ilupri,*) ' | HRECFM = ',hrecfm
192 WRITE (ilupri,*) ' | KLENG = ',kleng
193 WRITE (ilupri,*) ' | KGRID = ',kgrid
194 WRITE (ilupri,*) ' | KLENCH = ',klench
195 ENDIF
196 kresp=iresp
197 
198 IF (lhook) CALL dr_hook('FM_WRIT',1,zhook_handle)
199 RETURN
200  IF (lhook) CALL dr_hook('FM_WRIT',1,zhook_handle)
201  END SUBROUTINE fm_writ
integer, parameter jpfinl
integer, parameter jpnxlu
integer, parameter jprb
Definition: parkind1.F90:32
subroutine fm_writ(HFILEM, HRECFM, HFIPRI, KLENG, KFIELD, KGRID, KLENCH, HCOMMENT, KRESP)
Definition: fm_writ.F90:4
logical lhook
Definition: yomhook.F90:15
integer, parameter jpxfie