CMAX: [Version 1.0 (6/22/93)]
CMAX: ----------------------------------------------------
CMAX: Transformation of BDPATCH from bdpatch.f
CMAX: ----------------------------------------------------
      SUBROUTINE bdpatch_v1(blat,blon,glat,glon,igindx,lgood,indx,os,ln,
     &    ng)
      include '/usr/include/cm/CMF_defs.h'

C......................................................................
C                                                                     .
C  Program:    BDPATCH                                                .
C  Language:   FORTRAN V (ANSI-77 STANDARD)                           .
C  Computer:   Cray YMP 4/832                                         .
C                                                                     .
C  Programmer: Y. BRIN    ("DECYBERIZATION" BY M.SEABLOM)             .
C  Date:       MARCH, 1990                                            .
C                                                                     .
C  PROGRAM BDPATCH SCANS FOR GOOD QUALITY OBSERVATIONS NEIGHBOURING A .
C  QUESTIONABLE ONE.                                                  .
C                                                                     .
C......................................................................
C**********************************************************************
C*                                                                    *
C*  N A S A   O P T I M U M   I N T E R P O L A T I O N   -   O 9 X   *
C*               Global Modeling and Simulation Branch                *
C*                   GODDARD SPACE FLIGHT CENTER                      *
C*                                                                    *
C**********************************************************************
C INPUT/OUTPUT ARGUMENTS.
cmax: Changed argument INDX to passed with index
      INTEGER os
      INTEGER ln
      REAL blat, blon, glat(lgood), glon(lgood), igindx(lgood), 
     &    indx(1-os+1:ln+1-os), buddist, budlat, sige, dlat, dlon, pi, 
     &    pi180, ptop, pint, gi, omg2re, radei, radei2, gradei, gradei2
     &    , g2, polefac, rtd, polelat
      INTEGER BWITHIN (20000)
      CHARACTER * 60 message
C MAXIMUM BATCH SIZE, MAXIMUM NUMBER OF NEIGHBORING POINTS.
      COMMON / BUDDY / MAXLEV, MAXLIST, MAXBATCH, MAXINDX, BUDDIST, BUDL
     &AT
C CONSTANTS. USE PI/180
      COMMON / CONST / SIGE (20), DLAT, DLON, PI, PI180, PTOP, PINT, GI,
     & OMG2RE, RADEI, RADEI2, GRADEI, GRADEI2, G2, POLEFAC, RTD, POLELAT
C LOCAL VARIABLES.
      REAL MINLAT, MAXLAT, MINLON, MAXLON, BUDLON, COSLAT
!hpf$ distribute bwithin(block)
!hpf$ distribute glat(block)
!hpf$ distribute glon(block)
!hpf$ distribute igindx(block)
!hpf$ distribute indx(block)
!hpf$ distribute sige(block)
      DATA BWITHIN / 20000 * 0 /
      message(1:60) = ' '
      ng = 0
C DEFINE BOUNDS OF THE BOX SURROUNDING THE GREAT CIRCLE AROUND BAD OB.
C ZONAL BOUNDS.
      minlat = blat - budlat
      maxlat = blat + budlat
C LONGITUDE BOUNDS (ESTIMATE FROM 'GBATCH' SUBROUTINE).
      coslat = max(cos(pi180 * (blat + sign(budlat,blat))),1.E-3)
      budlon = min(budlat / coslat,180.0)
      minlon = blon - budlon
      maxlon = blon + budlon
C DEFINE GOOD OBS WITHIN ZONAL BOUNDS FIRST.
      FORALL (i = 1:lgood, glat(i) .GE. minlat .AND. glat(i) .LE. maxlat
     &    ) bwithin(i) = 1
C CHECK ON LONGITUDE BOUNDS CONSIDERING (-180.0 - + 180.0)
C DISCONTINUITY AT THE INTERNATIONAL DATE LINE.
C IF BUDLON = 180 ALL LONGITUDES USED.
      IF (budlon .LT. 180.0) THEN
        IF (maxlon .LE. 180.0 .AND. minlon .GE. -180.0) THEN
          FORALL (i = 1:lgood, bwithin(i) .EQ. 1 .AND. glon(i) 
     &        .GE. minlon .AND. glon(i) .LE. maxlon) bwithin(i) = 1
          FORALL (i = 1:lgood, .NOT.(bwithin(i) .EQ. 1 .AND. glon(i) 
     &        .GE. minlon .AND. glon(i) .LE. maxlon)) bwithin(i) = 0
        ELSE
          IF (maxlon .GT. 180.0) THEN
            FORALL (i = 1:lgood, bwithin(i) .EQ. 1 .AND. glon(i) .LE. (
     &          maxlon - 360.0) .OR. glon(i) .GE. minlon) bwithin(i) = 1
            FORALL (i = 1:lgood, .NOT.(bwithin(i) .EQ. 1 .AND. glon(i) 
     &          .LE. (maxlon - 360.0) .OR. glon(i) .GE. minlon)) 
     &          bwithin(i) = 0
          ENDIF
          IF (minlon .LT. -180.0) THEN
            FORALL (i = 1:lgood, bwithin(i) .EQ. 1 .AND. glon(i) .GE. (
     &          minlon + 360.) .OR. glon(i) .LE. maxlon) bwithin(i) = 1
            FORALL (i = 1:lgood, .NOT.(bwithin(i) .EQ. 1 .AND. glon(i) 
     &          .GE. (minlon + 360.) .OR. glon(i) .LE. maxlon)) 
     &          bwithin(i) = 0
          ENDIF
        ENDIF
      ENDIF
C COMPRESS OUT ALL UNUSABLE (OUTSIDE THE BOX) REPORTS.
      DO i = 1 , lgood
        IF (bwithin(i) .EQ. 1) THEN
          ng = ng + 1
        ENDIF
      ENDDO
      ncount = 0
      DO i = 1 , lgood
        IF (bwithin(i) .EQ. 1) THEN
          ncount = ncount + 1
          igindx(ncount) = i
        ENDIF
      ENDDO
      IF (ng .GT. 0 .AND. ng .LE. maxindx) THEN
        FORALL (i = 1:ng) indx(i) = igindx(i)
      ENDIF
      IF (ng .GT. maxindx) THEN
        message = ' BDPATCH: Overflow of INDX (good reps). '
        PRINT *, ' INDX FOR GOOD REPS IN BDPATCH=',ng
        CALL flash(5,message,1)
      ENDIF
      RETURN
      END
