Merge remote-tracking branch 'origin/release-v4.6.1'
[WRF.git] / var / external / bufr / ufbrep.f
blobee59ea329cc63bb48c911f76898ef14aeedc7c44
1 SUBROUTINE UFBREP(LUNIO,USR,I1,I2,IRET,STR)
3 C$$$ SUBPROGRAM DOCUMENTATION BLOCK
5 C SUBPROGRAM: UFBREP
6 C PRGMMR: WOOLLEN ORG: NP20 DATE: 1994-01-06
8 C ABSTRACT: THIS SUBROUTINE WRITES OR READS SPECIFIED VALUES TO OR
9 C FROM THE CURRENT BUFR DATA SUBSET WITHIN INTERNAL ARRAYS, WITH THE
10 C DIRECTION OF THE DATA TRANSFER DETERMINED BY THE CONTEXT OF
11 C ABS(LUNIO) (I.E., IF ABS(LUNIO) POINTS TO A BUFR FILE THAT IS OPEN
12 C FOR INPUT, THEN DATA VALUES ARE READ FROM THE INTERNAL DATA SUBSET;
13 C OTHERWISE, DATA VALUES ARE WRITTEN TO THE INTERNAL DATA SUBSET).
14 C THE DATA VALUES CORRESPOND TO MNEMONICS WHICH ARE EITHER:
15 C 1) PART OF A REGULAR (I.E., NON-DELAYED) REPLICATION SEQUENCE
16 C OR
17 C 2) REPLICATED BY BEING DIRECTLY LISTED MORE THAN ONCE WITHIN AN
18 C OVERALL SUBSET DEFINITION
20 C THE DIFFERENCE IN THE WAY UFBREP WORKS AS COMPARED TO UFBINT IS IN
21 C THE WAY THE MNEMONIC STRING IS INTERPRETED TO DEFINE WHICH ELEMENTS
22 C ARE PROCESSED AND IN WHAT ORDER. UFBREP INTERPRETS THE FIRST
23 C MNEMONIC IN THE STRING AS A "PIVOT". THIS MEANS THE 2ND DIMENSION
24 C OF THE DATA RETURNED (AS INDICATED BY ARGUMENT I2) IS DEFINED BY
25 C OCCURRENCES OF THE PIVOT ELEMENT FOUND WITHIN THE OVERALL SUBSET
26 C DEFINITION. FOR EXAMPLE, IF THE SUBSET DEFINITION CONTAINS THE
27 C FOLLOWING SEQUENCE OF MNEMONICS:
28 C {..,A,..,B,..,C,..,D,..,A,..,C,..,D,..,B,..
29 C A,..,B,..,D,..,C,..,A,..,C,..,B,..,D,..},
30 C THEN READING A SUBSET VIA UFBREP WITH STR = "A B C D" RETURNS THE
31 C FOLLOWING 4X4 MATRIX OF VALUES IN USR, USING A AS THE "PIVOT"
32 C MNEMONIC SINCE IT WAS THE FIRST MNEMONIC IN THE STRING:
33 C ( A1, B1, C1, D2,
34 C A2, B2, C2, D2,
35 C A3, B3, C3, D3,
36 C A4, B4, C4, D4 )
37 C NOTE THAT, WHEN USING UFBREP, THE ORDER OF THE NON-PIVOT MNEMONICS
38 C BETWEEN EACH PIVOT IS IMMATERIAL, I.E., IN THE ABOVE EXAMPLE, UFBREP
39 C FINDS ALL OF THE OCCURRENCES OF MNEMONICS B, C AND D BETWEEN EACH
40 C PIVOT BECAUSE IT SEARCHES INDEPENDENTLY FOR EACH ONE BETWEEN
41 C SUCCESSIVE PIVOTS.
43 C IN CONTRAST, NOTE THERE IS ALSO A SEPARATE SUBROUTINE UFBSTP WHICH
44 C IS SIMILAR TO UFBREP, EXCEPT THAT UFBSTP ALWAYS STEPS FORWARD WHEN
45 C SEARCHING FOR EACH SUCCESSIVE NON-PIVOT MNEMONIC, RATHER THAN
46 C SEARCHING INDEPENDENTLY FOR EACH ONE BETWEEN SUCCESSIVE PIVOTS.
47 C SO IN THE ABOVE EXAMPLE WITH STR="A B C D" AND STARTING FROM EACH
48 C SUCCESSIVE PIVOT MNEMONIC A, UFBSTP WOULD SEARCH FORWARD FOR THE
49 C NEXT OCCURRENCE OF MNEMONIC B, THEN IF FOUND SEARCH FORWARD FROM
50 C THERE FOR THE NEXT OCCURRENCE OF C, THEN IF FOUND SEARCH FORWARD
51 C FROM THERE FOR THE NEXT OCCURRENCE OF D, ETC. UP UNTIL REACHING
52 C THE NEXT OCCURRENCE OF THE PIVOT MNEMONIC A (OR THE END OF THE DATA
53 C SUBSET), WITHOUT EVER DOING ANY BACKTRACKING. SO IN THE ABOVE
54 C EXAMPLE UFBSTP WOULD RETURN THE FOLLOWING 4x4 MATRIX OF VALUES IN
55 C ARRAY USR, WHERE XX DENOTES A "MISSING" VALUE:
56 C ( A1, B1, C1, D2,
57 C A2, B2, XX, XX,
58 C A3, B3, C3, XX,
59 C A4, B4, XX, XX )
61 C PROGRAM HISTORY LOG:
62 C 1994-01-06 J. WOOLLEN -- ORIGINAL AUTHOR
63 C 1998-07-08 J. WOOLLEN -- REPLACED CALL TO CRAY LIBRARY ROUTINE
64 C "ABORT" WITH CALL TO NEW INTERNAL BUFRLIB
65 C ROUTINE "BORT"
66 C 1999-11-18 J. WOOLLEN -- THE NUMBER OF BUFR FILES WHICH CAN BE
67 C OPENED AT ONE TIME INCREASED FROM 10 TO 32
68 C (NECESSARY IN ORDER TO PROCESS MULTIPLE
69 C BUFR FILES UNDER THE MPI)
70 C 2003-05-19 J. WOOLLEN -- DISABLED THE PARSING SWITCH WHICH CONTROLS
71 C CHECKING FOR IN THE SAME REPLICATION GROUP,
72 C UFBREP DOES NOT NEED THIS CHECK, AND IT
73 C INTERFERES WITH WHAT UFBREP CAN DO
74 C OTHERWISE
75 C 2003-11-04 S. BENDER -- ADDED REMARKS/BUFRLIB ROUTINE
76 C INTERDEPENDENCIES
77 C 2003-11-04 D. KEYSER -- MAXJL (MAXIMUM NUMBER OF JUMP/LINK ENTRIES)
78 C INCREASED FROM 15000 TO 16000 (WAS IN
79 C VERIFICATION VERSION); UNIFIED/PORTABLE FOR
80 C WRF; ADDED DOCUMENTATION (INCLUDING
81 C HISTORY); OUTPUTS MORE COMPLETE DIAGNOSTIC
82 C INFO WHEN ROUTINE TERMINATES ABNORMALLY OR
83 C UNUSUAL THINGS HAPPEN; CHANGED CALL FROM
84 C BORT TO BORT2 IN SOME CASES
85 C 2004-08-18 J. ATOR -- ADDED SAVE FOR IFIRST1 AND IFIRST2 FLAGS
86 C 2009-03-31 J. WOOLLEN -- ADD DOCUMENTATION
87 C 2009-04-21 J. ATOR -- USE ERRWRT
89 C USAGE: CALL UFBREP (LUNIO, USR, I1, I2, IRET, STR)
90 C INPUT ARGUMENT LIST:
91 C LUNIO - INTEGER: ABSOLUTE VALUE IS FORTRAN LOGICAL UNIT NUMBER
92 C FOR BUFR FILE
93 C - IF BUFR FILE OPEN FOR OUTPUT AND LUNIO IS LESS
94 C THAN ZERO, UFBREP TREATS THE BUFR FILE AS THOUGH
95 C IT WERE OPEN FOR INPUT
96 C USR - ONLY IF BUFR FILE OPEN FOR OUTPUT:
97 C REAL*8: (I1,I2) STARTING ADDRESS OF DATA VALUES
98 C WRITTEN TO DATA SUBSET
99 C I1 - INTEGER: LENGTH OF FIRST DIMENSION OF USR OR THE
100 C NUMBER OF BLANK-SEPARATED MNEMONICS IN STR (FORMER
101 C MUST BE AT LEAST AS LARGE AS LATTER)
102 C I2 - INTEGER:
103 C - IF BUFR FILE OPEN FOR INPUT: LENGTH OF SECOND
104 C DIMENSION OF USR
105 C - IF BUFR FILE OPEN FOR OUTPUT: NUMBER OF "LEVELS"
106 C OF DATA VALUES TO BE WRITTEN TO DATA SUBSET
107 C STR - CHARACTER*(*): STRING OF BLANK-SEPARATED TABLE B
108 C MNEMONICS IN ONE-TO-ONE CORRESPONDENCE WITH FIRST
109 C DIMENSION OF USR
110 C - IF BUFR FILE OPEN FOR INPUT: THERE ARE THREE
111 C "GENERIC" MNEMONICS NOT RELATED TO TABLE B,
112 C THESE RETURN THE FOLLOWING INFORMATION IN
113 C CORRESPONDING USR LOCATION:
114 C 'NUL' WHICH ALWAYS RETURNS BMISS ("MISSING")
115 C 'IREC' WHICH ALWAYS RETURNS THE CURRENT BUFR
116 C MESSAGE (RECORD) NUMBER IN WHICH THIS
117 C SUBSET RESIDES
118 C 'ISUB' WHICH ALWAYS RETURNS THE CURRENT SUBSET
119 C NUMBER OF THIS SUBSET WITHIN THE BUFR
120 C MESSAGE (RECORD) NUMBER 'IREC'
122 C OUTPUT ARGUMENT LIST:
123 C USR - ONLY IF BUFR FILE OPEN FOR INPUT:
124 C REAL*8: (I1,I2) STARTING ADDRESS OF DATA VALUES
125 C READ FROM DATA SUBSET
126 C IRET - INTEGER:
127 C - IF BUFR FILE OPEN FOR INPUT: NUMBER OF "LEVELS" OF
128 C DATA VALUES READ FROM DATA SUBSET (MUST BE NO
129 C LARGER THAN I2)
130 C - IF BUFR FILE OPEN FOR OUTPUT: NUMBER OF "LEVELS"
131 C OF DATA VALUES WRITTEN TO DATA SUBSET (SHOULD BE
132 C SAME AS I2)
134 C REMARKS:
135 C THIS ROUTINE CALLS: BORT BORT2 ERRWRT STATUS
136 C STRING UFBRP
137 C THIS ROUTINE IS CALLED BY: None
138 C Normally called only by application
139 C programs.
141 C ATTRIBUTES:
142 C LANGUAGE: FORTRAN 77
143 C MACHINE: PORTABLE TO ALL PLATFORMS
145 C$$$
147 INCLUDE 'bufrlib.prm'
149 COMMON /MSGCWD/ NMSG(NFILES),NSUB(NFILES),MSUB(NFILES),
150 . INODE(NFILES),IDATE(NFILES)
151 COMMON /USRINT/ NVAL(NFILES),INV(MAXSS,NFILES),VAL(MAXSS,NFILES)
152 COMMON /ACMODE/ IAC
153 COMMON /QUIET / IPRT
155 CHARACTER*(*) STR
156 CHARACTER*128 BORT_STR1,BORT_STR2,ERRSTR
157 REAL*8 USR(I1,I2),VAL
159 DATA IFIRST1/0/,IFIRST2/0/
161 SAVE IFIRST1, IFIRST2
163 C----------------------------------------------------------------------
164 C----------------------------------------------------------------------
166 IRET = 0
168 C CHECK THE FILE STATUS AND I-NODE
169 C --------------------------------
171 LUNIT = ABS(LUNIO)
172 CALL STATUS(LUNIT,LUN,IL,IM)
173 IF(IL.EQ.0) GOTO 900
174 IF(IM.EQ.0) GOTO 901
175 IF(INODE(LUN).NE.INV(1,LUN)) GOTO 902
177 IO = MIN(MAX(0,IL),1)
178 IF(LUNIO.NE.LUNIT) IO = 0
180 IF(I1.LE.0) THEN
181 IF(IPRT.GE.0) THEN
182 CALL ERRWRT('+++++++++++++++++++++WARNING+++++++++++++++++++++++')
183 ERRSTR = 'BUFRLIB: UFBREP - 3rd ARG. (INPUT) IS .LE. 0, ' //
184 . 'SO RETURN WITH 5th ARG. (IRET) = 0; 6th ARG. (STR) ='
185 CALL ERRWRT(ERRSTR)
186 CALL ERRWRT(STR)
187 CALL ERRWRT('+++++++++++++++++++++WARNING+++++++++++++++++++++++')
188 CALL ERRWRT(' ')
189 ENDIF
190 GOTO 100
191 ELSEIF(I2.LE.0) THEN
192 IF(IPRT.EQ.-1) IFIRST1 = 1
193 IF(IO.EQ.0 .OR. IFIRST1.EQ.0 .OR. IPRT.GE.1) THEN
194 CALL ERRWRT('+++++++++++++++++++++WARNING+++++++++++++++++++++++')
195 ERRSTR = 'BUFRLIB: UFBREP - 4th ARG. (INPUT) IS .LE. 0, ' //
196 . 'SO RETURN WITH 5th ARG. (IRET) = 0; 6th ARG. (STR) ='
197 CALL ERRWRT(ERRSTR)
198 CALL ERRWRT(STR)
199 IF(IPRT.EQ.0 .AND. IO.EQ.1) THEN
200 ERRSTR = 'Note: Only the first occurrence of this WARNING ' //
201 . 'message is printed, there may be more. To output all ' //
202 . 'such messages,'
203 CALL ERRWRT(ERRSTR)
204 ERRSTR = 'modify your application program to add ' //
205 . '"CALL OPENBF(0,''QUIET'',1)" prior to the first call ' //
206 . 'to a BUFRLIB routine.'
207 CALL ERRWRT(ERRSTR)
208 ENDIF
209 CALL ERRWRT('+++++++++++++++++++++WARNING+++++++++++++++++++++++')
210 CALL ERRWRT(' ')
211 IFIRST1 = 1
212 ENDIF
213 GOTO 100
214 ENDIF
216 C INITIALIZE USR ARRAY PRECEEDING AN INPUT OPERATION
217 C --------------------------------------------------
219 IF(IO.EQ.0) THEN
220 DO J=1,I2
221 DO I=1,I1
222 USR(I,J) = BMISS
223 ENDDO
224 ENDDO
225 ENDIF
227 C PARSE OR RECALL THE INPUT STRING - READ/WRITE VALUES
228 C ----------------------------------------------------
230 IA2 = IAC
231 IAC = 1
232 CALL STRING(STR,LUN,I1,IO)
234 C CALL THE MNEMONIC READER/WRITER
235 C -------------------------------
237 CALL UFBRP(LUN,USR,I1,I2,IO,IRET)
238 IAC = IA2
240 IF(IO.EQ.1 .AND. IRET.LT.I2) GOTO 903
242 IF(IRET.EQ.0) THEN
243 IF(IO.EQ.0) THEN
244 IF(IPRT.GE.1) THEN
245 CALL ERRWRT('+++++++++++++++++++++WARNING+++++++++++++++++++++++')
246 ERRSTR = 'BUFRLIB: UFBREP - NO SPECIFIED VALUES READ IN, ' //
247 . 'SO RETURN WITH 5th ARG. (IRET) = 0; 6th ARG. (STR) ='
248 CALL ERRWRT(ERRSTR)
249 CALL ERRWRT(STR)
250 CALL ERRWRT('+++++++++++++++++++++WARNING+++++++++++++++++++++++')
251 CALL ERRWRT(' ')
252 ENDIF
253 ELSE
254 IF(IPRT.EQ.-1) IFIRST2 = 1
255 IF(IFIRST2.EQ.0 .OR. IPRT.GE.1) THEN
256 CALL ERRWRT('+++++++++++++++++++++WARNING+++++++++++++++++++++++')
257 ERRSTR = 'BUFRLIB: UFBREP - NO SPECIFIED VALUES WRITTEN OUT, ' //
258 . 'SO RETURN WITH 5th ARG. (IRET) = 0; 6th ARG. (STR) ='
259 CALL ERRWRT(ERRSTR)
260 CALL ERRWRT(STR)
261 CALL ERRWRT('MAY NOT BE IN THE BUFR TABLE(?)')
262 IF(IPRT.EQ.0) THEN
263 ERRSTR = 'Note: Only the first occurrence of this WARNING ' //
264 . 'message is printed, there may be more. To output all ' //
265 . 'such messages,'
266 CALL ERRWRT(ERRSTR)
267 ERRSTR = 'modify your application program to add ' //
268 . '"CALL OPENBF(0,''QUIET'',1)" prior to the first call ' //
269 . 'to a BUFRLIB routine.'
270 CALL ERRWRT(ERRSTR)
271 ENDIF
272 CALL ERRWRT('+++++++++++++++++++++WARNING+++++++++++++++++++++++')
273 CALL ERRWRT(' ')
274 IFIRST2 = 1
275 ENDIF
276 ENDIF
277 ENDIF
279 C EXITS
280 C -----
282 100 RETURN
283 900 CALL BORT('BUFRLIB: UFBREP - BUFR FILE IS CLOSED, IT MUST BE'//
284 . ' OPEN')
285 901 CALL BORT('BUFRLIB: UFBREP - A MESSAGE MUST BE OPEN IN BUFR '//
286 . 'FILE, NONE ARE')
287 902 CALL BORT('BUFRLIB: UFBREP - LOCATION OF INTERNAL TABLE FOR '//
288 . 'BUFR FILE DOES NOT AGREE WITH EXPECTED LOCATION IN INTERNAL '//
289 . 'SUBSET ARRAY')
290 903 WRITE(BORT_STR1,'("BUFRLIB: UFBREP - MNEMONIC STRING READ IN IS'//
291 . ': ",A)') STR
292 WRITE(BORT_STR2,'(18X,"THE NUMBER OF ''LEVELS'' ACTUALLY '//
293 . 'WRITTEN (",I3,") LESS THAN THE NUMBER REQUESTED (",I3,") - '//
294 . 'INCOMPLETE WRITE")') IRET,I2
295 CALL BORT2(BORT_STR1,BORT_STR2)