*IF DEF,C84_1A                                                             STMAX1A.2      
C ******************************COPYRIGHT******************************    GTS2F400.9649   
C (c) CROWN COPYRIGHT 1995, METEOROLOGICAL OFFICE, All Rights Reserved.    GTS2F400.9650   
C                                                                          GTS2F400.9651   
C Use, duplication or disclosure of this code is subject to the            GTS2F400.9652   
C restrictions as set forth in the contract.                               GTS2F400.9653   
C                                                                          GTS2F400.9654   
C                Meteorological Office                                     GTS2F400.9655   
C                London Road                                               GTS2F400.9656   
C                BRACKNELL                                                 GTS2F400.9657   
C                Berkshire UK                                              GTS2F400.9658   
C                RG12 2SZ                                                  GTS2F400.9659   
C                                                                          GTS2F400.9660   
C If no contract has been raised with this copy of the code, the use,      GTS2F400.9661   
C duplication or disclosure of it is strictly prohibited.  Permission      GTS2F400.9662   
C to do so must first be obtained in writing from the Head of Numerical    GTS2F400.9663   
C Modelling at the above address.                                          GTS2F400.9664   
C ******************************COPYRIGHT******************************    GTS2F400.9665   
C                                                                          GTS2F400.9666   
CLL  Routine: STMAX ----------------------------------------------------   STMAX1A.3      
CLL                                                                        STMAX1A.4      
CLL  Purpose: Computes the point-by-point maximum in time of a field       STMAX1A.5      
CLL           by comparing the field at the current time with the          STMAX1A.6      
CLL           maximum so far (STASH TEMPORAL service routine)              STMAX1A.7      
CLL                                                                        STMAX1A.8      
CLL  Tested under compiler:   cft77                                        STMAX1A.9      
CLL  Tested under OS version: UNICOS 5.1                                   STMAX1A.10     
CLL                                                                        STMAX1A.11     
CLL  Author:   S.Tett/T.Johns                                              STMAX1A.12     
CLL                                                                        STMAX1A.13     
CLL  Model            Modification history from model version 3.0:         STMAX1A.14     
CLL version  date                                                          STMAX1A.15     
CLL                                                                        STMAX1A.16     
CLL  Programming standard: UM Doc Paper 3, version 2 (7/9/90)              STMAX1A.17     
CLL                                                                        STMAX1A.18     
CLL  Logical components covered: D722                                      STMAX1A.19     
CLL                                                                        STMAX1A.20     
CLL  Project task: D7                                                      STMAX1A.21     
CLL                                                                        STMAX1A.22     
CLL  External documentation:                                               STMAX1A.23     
CLL    Unified Model Doc Paper C4 - Storage handling and diagnostic        STMAX1A.24     
CLL                                 system (STASH)                         STMAX1A.25     
CLL                                                                        STMAX1A.26     
C*L  Interface and arguments: ------------------------------------------   STMAX1A.27     
C                                                                          STMAX1A.28     

      SUBROUTINE STMAX(fieldin,result,size,masking,amdi)                    1STMAX1A.29     
C                                                                          STMAX1A.30     
      IMPLICIT NONE                                                        STMAX1A.31     
C                                                                          STMAX1A.32     
      INTEGER size             ! IN size of fieldin and result.            STMAX1A.33     
      REAL fieldin(size)       ! IN input field                            STMAX1A.34     
      REAL result(size)        ! OUT output field (maximum)                STMAX1A.35     
      LOGICAL masking          ! IN true if masked (ie. MDI possible)      STMAX1A.36     
      REAL amdi                ! IN missing data indicator                 STMAX1A.37     
C*----------------------------------------------------------------------   STMAX1A.38     
C                                                                          STMAX1A.39     
C Local variables                                                          STMAX1A.40     
C                                                                          STMAX1A.41     
      INTEGER i ! loop count                                               STMAX1A.42     
C-----------------------------------------------------------------------   STMAX1A.43     
CL 1.1 loop over array size, if either result or fieldin is amdi set       STMAX1A.44     
CL     result to amdi, else set result to maximum of fieldin and result    STMAX1A.45     
CL                                                                         STMAX1A.46     
      IF (masking) THEN                                                    STMAX1A.47     
        DO i=1,size                                                        STMAX1A.48     
          IF ((result(i).ne.amdi).and.(fieldin(i).ne.amdi)) THEN           STMAX1A.49     
            result(i)=max(result(i),fieldin(i))                            STMAX1A.50     
          ELSE                                                             STMAX1A.51     
            result(i)=amdi                                                 STMAX1A.52     
          ENDIF                                                            STMAX1A.53     
        ENDDO                                                              STMAX1A.54     
      ELSE                                                                 STMAX1A.55     
CL                                                                         STMAX1A.56     
CL 1.2 loop over array size, set result to maximum of fieldin and result   STMAX1A.57     
CL     without checking for missing data                                   STMAX1A.58     
CL                                                                         STMAX1A.59     
        DO i=1,size                                                        STMAX1A.60     
          result(i)=max(result(i),fieldin(i))                              STMAX1A.61     
        ENDDO                                                              STMAX1A.62     
      ENDIF                                                                STMAX1A.63     
C                                                                          STMAX1A.64     
      RETURN                                                               STMAX1A.65     
      END                                                                  STMAX1A.66     
*ENDIF                                                                     STMAX1A.67