C
c
c     changes
c
c     jun 99  jh   --------------- version 7.0 -------------------------
c                  add version call
c
C     PROGRAM EQRISK(INPUT,OUTPUT,TAPE5=INPUT,TAPE6=OUTPUT)                A   1
C     ******************************************************************   A   2
C     NO RESPONSIBILITY IS ASSUMED BY THE AUTHORS OR BY THE UNIVERSITY     A   3
C     OF CALIFORNIA FOR ANY ERRORS, MISTAKES, OR MISREPRESENTATIONS        A   4
C     THAT MAY OCCUR FROM THE USE OF THIS COMPUTER PROGRAM.  ALL           A   5
C     SOFTWARE PROVIDED ARE IN 'AS IS' CONDITION. NO WARRANTIES OF ANY     A   6
C     KIND, WHETHER STATUTORY, WRITTEN, ORAL, EXPRESSED, OR IMPLIED        A   7
C     (INCLUDING WARRANTIES OF FITNESS AND MERCHANTABILITY) SHALL APPLY.   A   8
C                                                                          A   9
C     PUBLIC DISTRIBUTION OF THIS PROGRAM THROUGH NISEE WAS MADE POSSI-    A  10
C     BLE BY THE AUTHORS AND THE NATIONAL SCIENCE FOUNDATION WITH THE      A  11
C     STIPULATION THAT THE PROGRAM NEITHER BE SOLD IN WHOLE OR IN PART     A  12
C     FOR DIRECT PROFIT NOR ROYALTIES OR DEVELOPMENT CHARGES MADE FOR      A  13
C     ITS USE.  BY ACCEPTANCE OF DELIVERY OF THIS PROGRAM PACKAGE, THE     A  14
C     PURCHASER UNDERSTANDS THE RESTRICTIONS ON THE USE AND DISTRIBU-      A  15
C     TION OF THE PROGRAM.  THE FEE PAID TO NISEE REPRESENTS A CHARGE      A  16
C     FOR DUPLICATION, MAILING, AND DOCUMENTATION.  THE LEGAL OWNERSHIP    A  17
C     OF THE PROGRAM REMAINS WITH THE DEVELOPER.                           A  18
C                                                                          A  19
C     IF THIS PROGRAM HAS GIVEN YOU NEW ANALYSIS CAPABILITY RESULTING      A  20
C     IN INDIRECT PROFITS, YOU MAY WISH TO SUPPORT FURTHER WORK IN THIS    A  21
C     AREA BY GIVING AN UNRESTRICTED GRANT TO THE AUTHORS' UNIVERSITY.     A  22
C     ******************************************************************   A  23
C        MAINROUTINE                                                       A  24
C        R K MCGUIRE   U.S.G.S. JANUARY 1975                               A  25
C       PLANAR VERSION (CARTESIAN COORDINATES)                             A  26
C                                                                          A  27
C   INTERNAL SOURCE AREA SIDE AND CORNER NOTATION FOLLOWS......            A  28
C                                                                          A  29
C                  SIDE 4                                                  A  30
C     (X3,Y3)------------------(X4,Y4)                                     A  31
C        '                        '                                        A  32
C        '                        '                                        A  33
C        '                        '                                        A  34
C        '                        '                                        A  35
C SIDE 3 '                        ' SIDE 2                                 A  36
C        '                        '                                        A  37
C        '                        '                                        A  38
C        '                        '                                        A  39
C        '                        '                                        A  40
C     (X1,Y1)------------------(X2,Y2)                                     A  41
C                  SIDE 1                                                  A  42
C                                                                          A  43
C                                                                          A  44
      COMMON NRD,NWR,NLEI,TI(12),RSKTI(12)                                 A  45
      COMMON C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                               A  46
      COMMON NGS,NRS(20),AM0(21),AM1(21),LORS(21)                          A  47
      COMMON BETA(21),RATE(21),COEF(21),FDEPTH(21)                         A  48
      COMMON NSTEPO,NSTEPI,JCALC,JPRNT                                     A  49
      COMMON INDIC(4),AREA(20,11),X(20,11,2),Y(20,11,2)                    A  50
C..Attenuation choice in risk2 subroutine                                  t0000
      COMMON /atten/ IATTE                                                 t0001
      DIMENSION TITLE(20)                                                  A  51
      DIMENSION RISKS(9),ATI(12),BRISK(12),TIF(8)                          A  52
C         TO MODIFY LIMITS AS STATED IN PROGRAM DOCUMENTATION, CHANGE      A  53
C         COMMON DIMENSION STATEMENTS IN MAIN PROGRAM AND SUBROUTINES AS   A  54
C         FOLLOWS=                                                         A  55
C         (1) TO CHANGE MAX. NO. OF GROSS SOURCES (PRESENTLY 10)=          A  56
C             (A) CHANGE ALL 10'S TO NEW MAX.                              A  57
C             (B) CHANGE ALL 11'S IN SINGLE DIMENSION ARRAYS TO            A  58
C                   NEW MAXIMUM + 1.                                       A  59
C         (2) TO CHANGE MAX. NO. OF SUBSOURCES IN EACH GROSS SOURCE        A  60
C                 (PRESENTLY 10)=                                          A  61
C             (A) CHANGE ALL 11'S IN DOUBLE- AND TRIPLE-DIENSION ARRAYS    A  62
C                 TO NEW MAXIMUM + 1.                                      A  63
C         (3) TO CHANGE MAX. NO. IN THE LIST OF EXAMINED INTENSITIES       A  64
C                 (PRESENTLY 12)=                                          A  65
C             (A) CHANGE ALL 12'S TO NEW MAXIMUM.                          A  66
C         (4) TO CHANGE MAX. NO. OF INPUT RISK LEVELS (PRESENTLY 8)=       A  67
C             (A) CHANGE 9 TO NEW MAX. + 1                                 A  68
C             (B) CHANGE STATEMENT BELOW= 'RISKS(9)=0.0'                   A  69
C                 TO 'RISKS(NEW MAX. + 1) = 0.0'.                          A  70


c
c print version
c
      include 'version.inc'
      out_version_date='July 23, 2001'
      if (version_new) out_version_date=version_date
      call print_ver

      NRD=9                                                                A  71
      NWR=7                                                                A  72
  	  OPEN(NRD,FILE='DATA')
 	  OPEN(NWR,FILE='eqrisk.out',status='unknown')
C        READ TITLE                                                        A  73
      READ (NRD,900) (TITLE(I),I=1,20)                                     A  74
 900  FORMAT (20A4)                                                        A  75
      WRITE (NWR,910) (TITLE(I),I=1,20)                                    A  76
 910  FORMAT (10X,20A4)                                                    A  77
C          READ STEP SIZE AND SWITCH VALUES.                               A  78
      READ (NRD,921) NSTEP,JCALC,JPRNT,IATTE                               A  79
 921  FORMAT (4I10)                                                        A  80
      WRITE(NWR,922) NSTEP,JCALC,JPRNT,IATTE                               A  81
 922  FORMAT (/' NSTEP =',I5,' JCALC =',I5,' JPRNT =',I5,' IATTE =',I5)    A  82
C          NSTEPO IS STEP SIZE USED WHEN SITE IS OUTSIDE                   A  83
C                 (QUADRILATERAL) SOURCE BEING CONSIDERED.                 A  84
C          NSTEPI IS STEP SIZE USED WHEN SITE IS INSIDE                    A  85
C                 (QUADRILATERAL) SOURCE BEING CONSIDERED.                 A  86
      NSTEPO=NSTEP                                                         A  87
      NSTEPI=NSTEP                                                         A  88
C          READ NUMBER AND LIST OF EXAMINED INTENSITIES.                   A  89
      READ (NRD,901) NLEI,(TI(I),I=1,NLEI)                                 A  90
 901  FORMAT(I5,12F5.3)                                                    A  91
      WRITE (NWR,902) (TI(I),I=1,NLEI)                                     A  92
 902  FORMAT (/' LIST OF EXAMINED INTENSITIES ',12F8.2)                    A  93
 903  FORMAT(8F10.2)                                                       A  94
      DO 90 I=1,NLEI                                                       A  95
      ATI(I)=EXP(TI(I))                                                    A  96
  90  BRISK(I)=0.0                                                         A  97
C         READ LIFETIME AND RISKS DESIRED.                                 A  98
      READ (NRD,903) (RISKS(I),I=1,8)                                      A  99
      WRITE(NWR,919) (RISKS(I),I=1,8)                                      A 100
 919  FORMAT(/' RISKS DESIRED ',8F8.4)                                     A 101
C        READ ATTENUATION DATA                                             A 102
      READ (NRD,920) C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                       A 103
 920  FORMAT (8F10.2)                                                      A 104
      WRITE (NWR,905)C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                       A 105
 905  FORMAT(/' ATTENUATION DATA=   C1      C2      C3    '                A 106
     1,'SIGMA   RZERO    RONE     AAA     BBB',/16X,8F8.2)                 A 107
      IF (BBB+0.00001) 101,102,102                                         A 108
 101  WRITE (NWR,924)                                                      A 109
 924  FORMAT(//' INPUT ERROR=  THE VALUE OF BBB MUST BE POSITIVE,'         A 110
     1 ' BETWEEN 0.0 AND THE VALUE OF C2. EXECUTION STOPPED.')             A 111
      GO TO 890                                                            A 112
 102  IF (C2-BBB) 101,103,103                                              A 113
C         IF BBB=0.0, SET EQUAL TO A SMALL NUMBER                          A 114
 103  IF (BBB-0.00001) 104,105,105                                         A 115
 104  BBB=0.0000000001                                                     A 116
C        READ SOURCE DATA                                                  A 117
 105  READ (NRD,906) NGS,(NRS(I),I=1,NGS)                                  A 118
 906  FORMAT (24I3)                                                        A 119
      WRITE (NWR,907) NGS,(NRS(I),I=1,NGS)                                 A 120
 907  FORMAT(/' NO. OF GROSS SOURCES ',I5/' NO. OF SUBSOURCES IN GROSS S   A 121
     1OURCES ',16I5)                                                       A 122
      WRITE (NWR,918)                                                      A 123
 918  FORMAT (/' GROSS SOURCE   L/S',5X,'COEF',7X,                         A 124
     1'M0',8X,'M1',6X,'BETA',5X,'RATE/YR',3X,'FDEPTH')                     A 125
      NGS1=NGS+1                                                           A 126
      DO 110 I=1,NGS1                                                      A 127
      READ(NRD,904)LORS(I),COEF(I),AM0(I),AM1(I),BETA(I),RATE(I),          A 128
     1 FDEPTH(I)                                                           A 129
 904  FORMAT(I10,6F10.2)                                                   A 130
      IF (I-NGS1) 110,120,120                                              A 131
 110  WRITE (NWR,908)I,LORS(I),COEF(I),AM0(I),AM1(I),BETA(I),RATE(I),      A 132
     1 FDEPTH(I)                                                           A 133
 908  FORMAT (1X,I7,3X,I7,3F10.2,3F10.4)                                   A 134
C         WRITE BACKGROUND SEISMICITY                                      A 135
 120  WRITE (NWR,925) LORS(I),COEF(I),AM0(I),AM1(I),BETA(I),RATE(I),       A 136
     1 FDEPTH(I)                                                           A 137
 925  FORMAT(' (BACKGROUND)',I5,3F10.2,3F10.4/)                            A 138
      DO 200 II=1,NGS                                                      A 139
      NRSII=NRS(II)+1                                                      A 140
      DO 150 JJ=1,NRSII                                                    A 141
      READ (NRD,903) X(II,JJ,1),Y(II,JJ,1),X(II,JJ,2),Y(II,JJ,2)           A 142
      WRITE (NWR,909) II,X(II,JJ,1),Y(II,JJ,1),X(II,JJ,2),Y(II,JJ,2)       A 143
 909  FORMAT(' GROSS SOURCE',I3,' SUBSOURCE COORDINATE DATA ',4F10.2)      A 144
 150  CONTINUE                                                             A 145
 200  WRITE (NWR,932)                                                      A 146
 932  FORMAT(1X)                                                           A 147
C        CALCULATE AREA OF EACH SUBSOURCE AND GROSS SOURCE.                A 148
C        IF ARRAYS X( ) AND Y( ) CONTAIN LONGITUDE                         A 149
C          AND LATITUDE, CHANGE SUBROUTINE 'AREAS' TO                      A 150
C          CALCULATE CORRECT AREAS USING THESE PARAMETERS.                 A 151
C          SEE ALSO REQUIRED CHANGES BELOW.                                A 152
      DO 400 II=1,NGS                                                      A 153
      NTOT=NRS(II)+1                                                       A 154
      AREA(II,NTOT)=0.0                                                    A 155
      JJNRS=NRS(II)                                                        A 156
      DO 300 JJ=1,JJNRS                                                    A 157
	  CALL AREAS(X(II,JJ,1),Y(II,JJ,1),X(II,JJ,2),Y(II,JJ,2),
     @ X(II,JJ+1,1),Y(II,JJ+1,1),X(II,JJ+1,2),Y(II,JJ+1,2),AREA(II,JJ))
      WRITE (NWR,911) II,JJ, AREA(II,JJ)                                   A 160
 911  FORMAT(' GROSS SOURCE',I3,' SUBSOURCE',I3,' EXACT AREA',F10.1)       A 161
 300  AREA(II,NTOT)=AREA(II,NTOT)+AREA(II,JJ)                              A 162
 400  WRITE (NWR,913) II,AREA(II,NTOT)                                     A 163
 913  FORMAT(' GROSS SOURCE',I3,13X,' TOTAL AREA',F10.1/)                  A 164
C         COMPUTE BACKGROUND SEISMICITY                                    A 165
      IF(RATE(NGS1)-0.0000000001) 420,420,405                              A 166
C         RBACK IS RADIUS OUT TO WHICH RISK FROM                           A 167
C           BACKGROUND SEISMICITY IS CALCULATED.                           A 168
 405  RBACK=150.                                                           A 169
C        FOR BACKGROUND SEISMICITY, NSTEPI IS DOUBLED (AND                 A 170
C           THEN HALVED AFTER CALCULATIONS).                               A 171
      NSTEPI=2*NSTEPI                                                      A 172
      CALL CIRCLE(RBACK,NGS1,1.,BRISK)                                     A 173
      NSTEPI=NSTEPI/2                                                      A 174
      DO 410 I=1,NLEI                                                      A 175
 410  BRISK(I)=COEF(NGS1)*BRISK(I)/10000.                                  A 176
      WRITE(NWR,926)(TI(I),I=1,NLEI)                                       A 177
 926  FORMAT(/' RESULTS FOR BACKGROUND SEISMICITY'/13X,'INTENSITY=  ',     A 178
     1 F7.2,11(2X,F7.2))                                                   A 179
      WRITE(NWR,923)(ATI(I),I=1,NLEI)                                      A 180
      WRITE(NWR,927)(BRISK(I),I=1,NLEI)                                    A 181
 927  FORMAT(' (BACKGROUND) E(NO/YR)= ',12E9.3)                            A 182
C         LOOP ON SETS OF SITES FOR ANALYSIS                               A 183
 420  READ (NRD,914) NX,NY,XZERO,YZERO,XDELTA,YDELTA                       A 184
 914  FORMAT (2I5,4F10.2)                                                  A 185
      IF (NX) 850,850,430                                                  A 186
 430  DO 800 IY=1,NY                                                       A 187
      YNOT=YZERO+(IY-1)*YDELTA                                             A 188
      DO 700 IX=1,NX                                                       A 189
      XNOT=XZERO+(IX-1)*XDELTA                                             A 190
      DO 450 I=1,NLEI                                                      A 191
 450  RSKTI(I)=BRISK(I)                                                    A 192
      WRITE (NWR,915) XNOT,YNOT,(TI(I),I=1,NLEI)                           A 193
 915  FORMAT(///' RESULTS FOR SITE LOCATION ',2F10.2/                      A 194
     1 /13X,'INTENSITY=  ',F7.2,11(2X,F7.2))                               A 195
      WRITE (NWR,923) (ATI(I),I=1,NLEI)                                    A 196
 923  FORMAT (4X,'ANTILOG(INTENSITY)=  ',F7.2,11(2X,F7.2))                 A 197
      DO 600 II=1,NGS                                                      A 198
      JJNRS=NRS(II)                                                        A 199
      DO 500 JJ=1,JJNRS                                                    A 200
      INGS=II                                                              A 201
      INSS=JJ                                                              A 202
C         IF THE USER WISHES THIS PROGRAM TO WORK ON LONGITUDE AND         A 203
C         LATITUDE COORDINATES, SIMPLY INPUT THESE COORDINATES AS IN THE   A 204
C         CARTESIAN CASE' AT THIS POINT CALCULATE (IN UNITS                A 205
C         CONSISTENT WITH THE ATTENUATION FUNCTION) THE CARTESIAN          A 206
C         LOCATION OF X(II,JJ,1),Y(II,JJ,1) WITH RESPECT TO XNOT,YNOT,     A 207
C         AND SIMILARLY WITH THE OTHER 3 POINTS DESCRIBING THE SOURCE      A 208
C         AREA UNDER CONSIDERATION, AND PLACE THESE CALCULATED CARTESIAN   A 209
C         COORDINATES IN THE ARGUMENT LIST OF THE FOLLOWING CALL STATE-    A 210
C         MENT (WITH XNOT AND YNOT AS THE ORIGIN, I.E. (0.0,0.0)).         A 211
C         SEE ALSO CHANGES REQUIRED ABOVE FOR SUBROUTINE 'AREAS'.          A 212
       CALL RRISK(XNOT,YNOT,INGS,INSS,X(II,JJ,1),Y(II,JJ,1),X(II,JJ,
     @2),Y(II,JJ,2),X(II,JJ+1,1),Y(II,JJ+1,1),X(II,JJ+1,2),Y(II,JJ+1,2))
 500  CONTINUE                                                             A 215
 600  CONTINUE                                                             A 216
      IF (JPRNT) 615,615,610                                               A 217
 610  WRITE(NWR,927)(BRISK(I),I=1,NLEI)                                    A 218
 615  WRITE (NWR,916) (RSKTI(I),I=1,NLEI)                                  A 219
 916  FORMAT (' ALL SOURCES  E(NO/YR)= ',12E15.3)                          A 220
      DO 620 I=1,NLEI                                                      A 221
 620  RSKTI(I)=1.-EXP(-RSKTI(I))                                           A 222
      WRITE (NWR,931) (RSKTI(I),I=1,NLEI)                                  A 223
 931  FORMAT(' ALL SOURCES      RISK= ',12E15.3)                           A 224
	write(6,309) rskti(2),rskti(4)
309	format(2e12.6)
C         ESTIMATE INTENSITIES AT RISKS DESIRED.                           A 225
      RISKS(9)=0.0                                                         A 226
      IA=0                                                                 A 227
      IF(RISKS(1)-0.0000000001) 700,700,625                                A 228
 625  DO 630 IRK=1,8                                                       A 229
      IF (RISKS(IRK)-RSKTI(1)) 640,640,630                                 A 230
 630  TIF(IRK)=1000000.                                                    A 231
      GO TO 700                                                            A 232
 640  IA=IA+1                                                              A 233
      IF (IA-NLEI) 650,645,645                                             A 234
 645  TIF(IRK)=1000000.                                                    A 235
      IRK=IRK+1                                                            A 236
      IF (RISKS(IRK)-0.0000000001)680,680,645                              A 237
 650  IF(RISKS(IRK)-RSKTI(IA+1))640,655,655                                A 238
 655  TIF(IRK)=(ALOG(RSKTI(IA)/RISKS(IRK)))                                A 239
     1  /(ALOG(RSKTI(IA)/RSKTI(IA+1)))                                     A 240
      TIF(IRK)=TI(IA)+TIF(IRK)*(TI(IA+1)-TI(IA))                           A 241
 658  IRK=IRK+1                                                            A 242
      IF (RISKS(IRK)-0.0000000001)680,680,660                              A 243
 660  IF(RISKS(IRK)-RSKTI(IA+1)) 640,655,655                               A 244
 680  IRK=IRK-1                                                            A 245
      WRITE(NWR,928)(RISKS(I),I=1,IRK)                                     A 246
 928  FORMAT(/16X,' RISKS= ',8F9.6)                                        A 247
      WRITE(NWR,929)(TIF(I),I=1,IRK)                                       A 248
 929  FORMAT(13X,'INTENSITY= ',8F9.2)                                      A 249
      DO 685 I=1,IRK                                                       A 250
      IF (TIF(I)-999999.)683,685,685                                       A 251
 683  TIF(I)=EXP(TIF(I))                                                   A 252
 685  CONTINUE                                                             A 253
      WRITE (NWR,930)(TIF(I),I=1,IRK)                                      A 254
 930  FORMAT(4X,'ANTILOG(INTENSITY)= ',8F9.2)                              A 255
 700  CONTINUE                                                             A 256
 800  CONTINUE                                                             A 257
C     GO TO 420                                                            A 258
 850  WRITE (NWR,917)                                                      A 259
 917  FORMAT (/////'       END OF JOB ' //)                                A 260
 890  STOP 
      END                                                                  A 262
      SUBROUTINE RRISK(XNOT,YNOT,INGS,INSS,                                B   1
     1 X1,Y1,X2,Y2,X3,Y3,X4,Y4)                                            B   2
      COMMON NRD,NWR,NLEI,TI(12),RSKTI(12)                                 B   3
      COMMON C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                               B   4
      COMMON NGS,NRS(20),AM0(21),AM1(21),LORS(21)                          B   5
      COMMON BETA(21),RATE(21),COEF(21),FDEPTH(21)                         B   6
      COMMON NSTEPO,NSTEPI,JCALC,JPRNT                                     B   7
      COMMON INDIC(4),AREA(20,11)                                          B   8
      DIMENSION XL(4),YL(4),XR(4),YR(4),AA(4),BB(4)                        B   9
C         SUBROUTINE WHICH LOADS TEMPORARY ARRAYS WITH THIS                B  10
C           SUBSOURCE'S CORNERS AND DETERMINES IF THIS SITE IS             B  11
C           WITHIN OR WITHOUT THE SUBSOURCE.                               B  12
      XL(1)=X1                                                             B  13
      YL(1)=Y1                                                             B  14
      XR(1)=X2                                                             B  15
      YR(1)=Y2                                                             B  16
      XL(2)=X2                                                             B  17
      YL(2)=Y2                                                             B  18
      XR(2)=X4                                                             B  19
      YR(2)=Y4                                                             B  20
      XL(3)=X3                                                             B  21
      YL(3)=Y3                                                             B  22
      XR(3)=X1                                                             B  23
      YR(3)=Y1                                                             B  24
      XL(4)=X4                                                             B  25
      YL(4)=Y4                                                             B  26
      XR(4)=X3                                                             B  27
      YR(4)=Y3                                                             B  28
C        DETERMINE IF ANY SIDES ARE VERTICAL LINES                         B  29
      DO 200 II=1,4                                                        B  30
      DIF=XL(II)-XR(II)                                                    B  31
      IF (DIF) 140,180,160                                                 B  32
 140  IF (DIF+0.01) 190,190,180                                            B  33
 160  IF (DIF-0.01) 180,190,190                                            B  34
C        INDIC(I)=1  IMPLIES NOT A VERTICAL LINE                           B  35
C        INDIC(I)=2  IMPLIES A VERTICAL LINE (INFINITE SLOPE).             B  36
 180  INDIC(II)=2                                                          B  37
      AA(II)=0.                                                            B  38
      BB(II)=0.                                                            B  39
      GO TO 200                                                            B  40
 190  INDIC(II)=1                                                          B  41
      AA(II)=(YL(II)-YR(II))/(XL(II)-XR(II))                               B  42
      BB(II)=YL(II)-AA(II)*XL(II)                                          B  43
C          AA(II) IS SLOPE OF II'TH SIDE                                   B  44
C          BB(II) IS INTERCEPT OF II'TH SIDE                               B  45
  200  CONTINUE                                                            B  46
C        DETERMINE IF SITE IS INSIDE SOURCE AREA.                          B  47
      DO 220 II=1,4                                                        B  48
      IJ=5-II                                                              B  49
      IINDIC=INDIC(II)                                                     B  50
C                                                                          B  51
      IF(IINDIC.EQ.2)GO TO 215                                             B  52
C                                                                          B  53
 210  DIFNOT=YNOT-AA(II)*XNOT-BB(II)                                       B  54
      DIF=YL(IJ)-AA(II)*XL(IJ)-BB(II)                                      B  55
 211  IF (DIF) 214,214,212                                                 B  56
 212  IF (DIFNOT) 400,400,220                                              B  57
 214  IF (DIFNOT) 220,400,400                                              B  58
 215  DIF=XL(IJ)-XL(II)                                                    B  59
      DIFNOT=XNOT-XL(II)                                                   B  60
      GO TO 211                                                            B  61
 220  CONTINUE                                                             B  62
C        IF DO LOOP FINISHED, POINT LIES WITHIN AREA.                      B  63
      CALL INSIDE(XNOT,YNOT,INGS,INSS,XL,YL,XR,YR,AA,BB)                   B  64
      GO TO 900                                                            B  65
 400  CALL OUTSID(XNOT,YNOT,INGS,INSS,XL,YL,XR,YR,AA,BB)                   B  66
 900  RETURN                                                               B  67
      END                                                                  B  68
      SUBROUTINE OUTSID(XNOT,YNOT,INGS,INSS,XL,YL,XR,YR,AA,BB)             C   1
      COMMON NRD,NWR,NLEI,TI(12),RSKTI(12)                                 C   2
      COMMON C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                               C   3
      COMMON NGS,NRS(20),AM0(21),AM1(21),LORS(21)                          C   4
      COMMON BETA(21),RATE(21),COEF(21),FDEPTH(21)                         C   5
      COMMON NSTEPO,NSTEPI,JCALC,JPRNT                                     C   6
      COMMON INDIC(4),AREA(20,11)                                          C   7
      DIMENSION XL(4),YL(4),XR(4),YR(4),AA(4),BB(4),XC(4),YC(4)            C   8
      DIMENSION RSK(12)                                                    C   9
C         SUBROUTINE FOR CALCULATING RISK WHEN SITE IS OUTSIDE             C  10
C          (QUADRILATERAL) SOURCE AREA.                                    C  11
C          DEFINE DISTANCE VALUES TO SELECT STEP SIZE.                     C  12
C          (RC IS CLOSEST DISTANCE BETWEEN SITE AND SOURCE.)               C  13
C          RC BETWEEN 0.0 AND RZ1 INPLIES STEP SIZE = NSTEPO.              C  14
C          RC BETWEEN RZ1 AND RZ2 IMPLIES STEP SIZE = NSTEPO/2.            C  15
C          RC BETWEEN RZ2 AND RZ3 IMPLIES LUMP RISK                        C  16
C                AT CENTER OF SOURCE (DEFINED BY AVERAGING LOCATIONS       C  17
C                OF CORNER POINTS).                                        C  18
C          RC GREATER THAN RZ3 IMPLIES IGNORE SOURCE.                      C  19
      RZ1=100.0                                                            C  20
      RZ2=300.0                                                            C  21
      RZ3=500.0                                                            C  22
C          TO BY-PASS THIS ALGORITHM, SET RZ1 TO A LARGE NUMBER.           C  23
C          TO PRODUCE 0-1 ALGORITHM (DISREGARD, OR CALCULATE USING         C  24
C          NSTEPO), SET RZ1=RZ2=RZ3=DISTANCE WITHIN WHICH YOU              C  25
C          WISH TO CONSIDER RISK.                                          C  26
C      FIND CLOSEST (IC,RC) AND FARTHEST (IFAR,RF) POINTS AND DISTANCES.   C  27
      RC2=10000000000.                                                     C  28
      RF2=0.                                                               C  29
      DO 108 II=1,4                                                        C  30
      DIST=(XNOT-XL(II))*(XNOT-XL(II))+(YNOT-YL(II))*(YNOT-YL(II))         C  31
      IF (RC2-DIST) 104,104,102                                            C  32
 102  RC=SQRT(DIST)                                                        C  33
      RC2=DIST                                                             C  34
      IC=II                                                                C  35
 104  IF (DIST-RF2) 108,108,106                                            C  36
 106  RF=SQRT(DIST)                                                        C  37
      RF2=DIST                                                             C  38
      IFAR=II                                                              C  39
 108  CONTINUE                                                             C  40
      ICS=0                                                                C  41
C        SEE IF ANY SIDE LIES CLOSER THAN CLOSEST POINT                    C  42
      DO 150 II=1,4                                                        C  43
C       IS SLOPE INFINITEf                                                 C  44
      IINDIC=INDIC(II)                                                     C  45
C                                                                          C  46
      IF(IINDIC.EQ.1) GO TO 130                                            C  47
C                                                                          C  48
 121  XS=XL(II)                                                            C  49
      YS=YNOT                                                              C  50
      GO TO 145                                                            C  51
C       IS SLOPE ZEROf                                                     C  52
 130  IF (AA(II)-0.001) 131,140,140                                        C  53
 131  IF (AA(II)+0.001) 140,140,132                                        C  54
 132  XS=XNOT                                                              C  55
      YS=YL(II)                                                            C  56
      GO TO 145                                                            C  57
C        SLOPE IS NOT ZERO, SO CALCULATE NEAREST POINT.                    C  58
 140  XS=(YNOT+(XNOT/AA(II))-BB(II))/(AA(II)+(1./AA(II)))                  C  59
      YS=((XNOT-XS)/AA(II))+YNOT                                           C  60
 145  CALL BETWEN(XL(II),YL(II),XR(II),YR(II),XS,YS,INDIC(II),IANS)        C  61
      IF (IANS) 150,146,148                                                C  62
 146  NERROR=1                                                             C  63
      GO TO 800                                                            C  64
 148  DIST=(XNOT-XS)*(XNOT-XS)+(YNOT-YS)*(YNOT-YS)                         C  65
      IF (DIST-RC2) 149,150,150                                            C  66
 149  RC2=DIST                                                             C  67
      RC=SQRT(DIST)                                                        C  68
      ICS=II                                                               C  69
 150  CONTINUE                                                             C  70
      APPROX=0.0                                                           C  71
      DO 290 II=1,NLEI                                                     C  72
 290  RSK(II)=0.                                                           C  73
C         DETERMINE STEP SIZE FROM RZ1,RZ2, AND RZ3.                       C  74
      IF (RC-RZ1) 308,308,292                                              C  75
 292  IF (RC-RZ2) 294,294,296                                              C  76
 294  NSTEPX=NSTEPO/2                                                      C  77
      GO TO 310                                                            C  78
 296  IF (RC-RZ3) 298,298,850                                              C  79
C          IF RC IS BETWEEN RZ2 AND RZ3, CALCULATE RISK                    C  80
C          ASSUMING SEISMICITY IS LUMPED AT CENTER (AVERAGE                C  81
C          OF CORNER POINTS).                                              C  82
 298  XAVE=(XL(1)+XL(2)+XL(3)+XL(4))/4.                                    C  83
      YAVE=(YL(1)+YL(2)+YL(3)+YL(4))/4.                                    C  84
      R=SQRT((XAVE-XNOT)*(XAVE-XNOT)+(YAVE-YNOT)*(YAVE-YNOT))              C  85
      NTOT=NRS(INGS)+1                                                     C  86
      RATEI=RATE(INGS)*AREA(INGS,INSS)/AREA(INGS,NTOT)                     C  87
      DO 306 JJ=1,NLEI                                                     C  88
      IF (JCALC) 300,302,302                                               C  89
 300  CALL RISK1(TI(JJ),R,INGS,RISK)                                       C  90
      GO TO 304                                                            C  91
 302  CALL RISK2(TI(JJ),R,INGS,RISK)                                       C  92
 304  IF (RISK-0.0000000001) 600,600,305                                   C  93
 305  RSK(JJ)=COEF(INGS)*RISK*RATEI                                        C  94
 306  RSKTI(JJ)=RSKTI(JJ) + RSK(JJ)                                        C  95
      GO TO 600                                                            C  96
 308  NSTEPX=NSTEPO                                                        C  97
 310  AN=NSTEPX                                                            C  98
      STSIZE=(RF-RC)/AN                                                    C  99
C        STEP THRU SOURCE AREA.                                            C 100
      DO 500 ISTEP=1,NSTEPX                                                C 101
      AI=ISTEP                                                             C 102
      R=RC+(AI-0.5)*STSIZE                                                 C 103
      NPT=0                                                                C 104
      ANGLE=0.                                                             C 105
      SIGNAL=1.                                                            C 106
C        LOOP ON EACH SIDE                                                 C 107
      DO 400 II=1,4                                                        C 108
      IINDIC=INDIC(II)                                                     C 109
C                                                                          C 110
      IF(IINDIC.EQ.1) GO TO 330                                            C 111
C                                                                          C 112
C          SIDE II IS A VERTICAL LINE                                      C 113
C          DOES CIRCLE (RADIUS R) INTERSECT ITf                            C 114
 320  A=XL(II)-XNOT                                                        C 115
      IF (A) 322,322,323                                                   C 116
 322  IF (R+A) 400,400,324                                                 C 117
 323  IF (R-A) 400,400,324                                                 C 118
C          COMPUTE TWO INTERSECTION POINTS                                 C 119
 324  X1=XL(II)                                                            C 120
      B=SQRT(R*R-(X1-XNOT)*(X1-XNOT))                                      C 121
      Y1=YNOT+B                                                            C 122
      X2=XL(II)                                                            C 123
      Y2=YNOT-B                                                            C 124
      GO TO 341                                                            C 125
 330  A=1.+AA(II)*AA(II)                                                   C 126
      B=2.*(-XNOT+AA(II)*(BB(II)-YNOT))                                    C 127
      C=XNOT*XNOT+YNOT*YNOT+BB(II)*(BB(II)-2.*YNOT)-R*R                    C 128
      D=B*B-4.*A*C                                                         C 129
      IF (D) 400,400,340                                                   C 130
C        TWO INTERSECTIONS, CALCULATE FIRST INTERSECTION POINT.            C 131
 340  D=SQRT(D)                                                            C 132
      X1=(-B+D)/(2.*A)                                                     C 133
      Y1=AA(II)*X1 + BB(II)                                                C 134
      X2=(-B-D)/(2.*A)                                                     C 135
      Y2=AA(II)*X2+BB(II)                                                  C 136
C        SEE IF (X1,Y1) IS ON BOUNDARY                                     C 137
 341  CALL BETWEN(XL(II),YL(II),XR(II),YR(II),X1,Y1,INDIC(II),IANS)        C 138
      IF (IANS) 360,342,345                                                C 139
 342  NERROR=4                                                             C 140
      GO TO 800                                                            C 141
C        CALCULATE OTHER POINT, SEE IF IS ON BOUNDARY.                     C 142
 345  CALL BETWEN(XL(II),YL(II),XR(II),YR(II),X2,Y2,INDIC(II),IANS)        C 143
      IF (IANS) 348,346,350                                                C 144
 346  NERROR=5                                                             C 145
      GO TO 800                                                            C 146
C        STORE FIRST POINT                                                 C 147
 348  NPT=NPT+1                                                            C 148
      XC(NPT)=X1                                                           C 149
      YC(NPT)=Y1                                                           C 150
      GO TO 400                                                            C 151
C        SEE IF THIS SIDE IS CLOSEST TO POINT, IF SO, TREAT SPECIALLY.     C 152
 350  IF (II-ICS) 352,355,352                                              C 153
C          BOTH POINTS ARE ON BOUNDARY, CALCULATE ANGLE BETWEN THEM.       C 154
 352  SIGN=-1.                                                             C 155
      GO TO 357                                                            C 156
 355  SIGN=1.                                                              C 157
      SIGNAL=-1.                                                           C 158
 357  AD=SQRT((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2))                             C 159
      IF (AD-2.*R) 358,359,359                                             C 160
 358  ANGLE=SIGN*2.*ASIN(AD/(2.*R)) + ANGLE                               C 161
      GO TO 400                                                            C 162
 359  ANGLE=3.1415926536 +ANGLE                                            C 163
      GO TO 400                                                            C 164
C        SEE IF SECOND POINT ONLY IS ON BOUNDARY                           C 165
 360  CALL BETWEN(XL(II),YL(II),XR(II),YR(II),X2,Y2,INDIC(II),IANS)        C 166
      IF (IANS) 400,362,370                                                C 167
 362  NERROR=6                                                             C 168
      GO TO 800                                                            C 169
 370  NPT=NPT+1                                                            C 170
      XC(NPT)=X2                                                           C 171
      YC(NPT)=Y2                                                           C 172
 400  CONTINUE                                                             C 173
      IF(NPT.LT.1.OR.NPT.GT.4)GO TO 402                                    C 174
      GO TO (410,420,410,440),NPT                                          C 175
 402  CONTINUE                                                             C 176
      IF(SIGNAL)460,405,405                                                C 177
 405  NERROR=7                                                             C 178
      GO TO 800                                                            C 179
 410  NERROR=8                                                             C 180
      GO TO 800                                                            C 181
 420  AD=SQRT((XC(1)-XC(2))*(XC(1)-XC(2))+(YC(1)-YC(2))*(YC(1)-YC(2)))     C 182
      ANGLE=ANGLE + SIGNAL*2.*ASIN(AD/(2.*R))                             C 183
      GO TO 460                                                            C 184
C          FOUR INTERSECTION POINTS (EACH ON A DIFFERENT SIDE).            C 185
C          DETERMINE ANGLE BY FINDING CLOSEST 2 INTERSECTIONS TO           C 186
C          FARTHEST CORNER, CALCULATE ANGLE BETWEEN, AND ADD ANGLE         C 187
C          BETWEEN OTHER TWO INTERSECTIONS.                                C 188
 440  DIST1=10000000000.                                                   C 189
      I1=0                                                                 C 190
      I2=0                                                                 C 191
      I3=0                                                                 C 192
      I4=0                                                                 C 193
      DO 450 JJ=1,4                                                        C 194
      DIST=(XL(IFAR)-XC(JJ))*(XL(IFAR)-XC(JJ))                             C 195
     1  +(YL(IFAR)-YC(JJ))*(YL(IFAR)-YC(JJ))                               C 196
      IF (DIST-DIST1) 442,444,444                                          C 197
 442  DIST2=DIST1                                                          C 198
      DIST1=DIST                                                           C 199
      I4=I3                                                                C 200
      I3=I2                                                                C 201
      I2=I1                                                                C 202
      I1=JJ                                                                C 203
      GO TO 450                                                            C 204
 444  IF (DIST-DIST2) 445,446,446                                          C 205
 445  DIST2=DIST                                                           C 206
      I4=I3                                                                C 207
      I3=I2                                                                C 208
      I2=JJ                                                                C 209
      GO TO 450                                                            C 210
 446  I4=I3                                                                C 211
      I3=JJ                                                                C 212
 450  CONTINUE                                                             C 213
C           CALCULATE ANGLE BETWEEN 2 CLOSEST POINTS TO FARTHEST CORNER.   C 214
      AD=SQRT((XC(I1)-XC(I2))*(XC(I1)-XC(I2))                              C 215
     1  + (YC(I1)-YC(I2))*(YC(I1)-YC(I2)))                                 C 216
      ANGLE=2.*ASIN(AD/(2.*R))                                            C 217
C         CALCULATE ANGLE BETWEEN 2 POINTS FARTHEST FROM                   C 218
C         FARTHEST CORNER AND ADD TO PREVIOUS ANGLE.                       C 219
      AD=SQRT((XC(I3)-XC(I4))*(XC(I3)-XC(I4))                              C 220
     1  +(YC(I3)-YC(I4))*(YC(I3)-YC(I4)))                                  C 221
      ANGLE=2.*ASIN(AD/(2.*R)) + ANGLE                                    C 222
 460  CONTINUE                                                             C 223
C        ANGLE FOR THIS RADIUS NOW KNOWN, CALCULATE RISK.                  C 224
C        COMPUTE RATE OF EARTHQUAKES IN THIS ANNULAR SOURCE                C 225
      ANAREA=ANGLE*R*STSIZE                                                C 226
      APPROX=APPROX+ANAREA                                                 C 227
      NTOT=NRS(INGS)+1                                                     C 228
      RATEI=RATE(INGS)*ANAREA*AREA(INGS,INSS)/AREA(INGS,NTOT)              C 229
C        CALCULATE CONTRIBUTION TO RISK                                    C 230
      DO 480 JJ=1,NLEI                                                     C 231
      IF (JCALC) 470,470,475                                               C 232
 470  CALL RISK1(TI(JJ),R,INGS,RISK)                                       C 233
      GO TO 478                                                            C 234
 475  CALL RISK2(TI(JJ),R,INGS,RISK)                                       C 235
 478  IF (RISK-0.0000000001) 500,490,490                                   C 236
 490  RSK(JJ)=RSK(JJ)+RISK*RATEI                                           C 237
 480  CONTINUE                                                             C 238
 500  CONTINUE                                                             C 239
      ARERR=((APPROX-AREA(INGS,INSS))/AREA(INGS,INSS))*100.                C 240
      IF (ARERR-5.) 510,520,520                                            C 241
 510  IF (ARERR+5.) 520,520,540                                            C 242
 520  WRITE(NWR,903) ARERR,INGS,INSS                                       C 243
 903  FORMAT(10X,'CAUTION= NUMERICAL INTEGRATION ERROR IN AREA IS ',       C 244
     1 F6.2,' ( FOR (OUTSIDE) SOURCE ',2I3)                                C 245
C          NORMALIZE BY COMPUTED (APPROXIMATE) AREA                        C 246
 540  DO 550 JJ=1,NLEI                                                     C 247
      RSK(JJ)=COEF(INGS)*RSK(JJ)/APPROX                                    C 248
 550  RSKTI(JJ)=RSKTI(JJ)+RSK(JJ)                                          C 249
 600  IF (JPRNT) 850,850,610                                               C 250
C         PRINT RISKS FOR THIS SOURCE                                      C 251
 610  WRITE(NWR,902) INGS,INSS,(RSK(I),I=1,NLEI)                           C 252
 902  FORMAT(' SOURCE',2I3,' E(NO/YR)= ',12E15.3)                          C 253
      GO TO 850                                                            C 254
C        ERROR PRINTOUT                                                    C 255
 800  WRITE(NWR,901)NERROR,INGS,INSS,IC,RC,XNOT,YNOT,(XL(I),YL(I),I=1,4)   C 256
     1 , R,ANGLE,RC,RF,(XC(I),YC(I),I=1,4)                                 C 257
 901  FORMAT(' ***** ERROR',I4,' IN SUBROUTINE OUTSID. SOURCE NO.',2I3,    C 258
     1' DEBUG VALUES FOLLOW.....',/10X,2I5,5(/10X,4F14.6))                 C 259
 850  RETURN                                                               C 260
      END                                                                  C 261
      SUBROUTINE RISK1(TIC,REPIS,INGS,RISK)                                D   1
      COMMON NRD,NWR,NLEI,TI(12),RSKTI(12)                                 D   2
      COMMON C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                               D   3
      COMMON NGS,NRS(20),AM0(21),AM1(21),LORS(21)                          D   4
      COMMON BETA(21),RATE(21),COEF(21),FDEPTH(21)                         D   5
      COMMON NSTEPO,NSTEPI,JCALC,JPRNT                                     D   6
      COMMON INDIC(4),AREA(20,11)                                          D   7
C                                                                          D   8
C          SUBROUTINE TO CALCULATE RISK WHEN THE FOLLOWING SPECIAL         D   9
C          FORM OF ATTENUATION FUNCTION IS USED=                           D  10
C               I = C1 + C2*M + C3*ALOG(R+RZERO)                           D  11
C                                                                          D  12
      SIGG=SIG                                                             D  13
      RFOC=SQRT(REPIS*REPIS + FDEPTH(INGS)*FDEPTH(INGS))                   D  14
      IF (RFOC-RONE) 10,10,20                                              D  15
  10  R=RONE                                                               D  16
C         IF DIFFERENT STANDARD DEVIATION INSIDE RADIUS RONE IS            D  17
C         DESIRED, SET SIGG TO THIS STANDARD DEVIATION HERE.               D  18
      GO TO 30                                                             D  19
  20  R=RFOC                                                               D  20
  30  RLN=ALOG(R+RZERO)                                                    D  21
C                                                                          D  22
C          IS THIS LOOSE OR STRICT SOURCEf                                 D  23
C          IF STRICT, RISK COMPUTED IS THAT FOR A SINGLE EARTHQUAKE        D  24
C          WITH (EXPONENTIALLY-DISTRIBUTED) RANDOM MAGNITUDE               D  25
C          (OR INTENSITY) BETWEEN AM0 AND AM1.  IF A LOOSE                 D  26
C          SOURCE, RISK COMPUTED IS THAT FOR 'ANEQ' EARTHQUAKES            D  27
C          WITH (EXPONENTIALLY-DISTRIBUTED) RANDOM MAGNITUDE               D  28
C          (OR INTENSITY) BETWEEN 0.0 AND AM1, WITH 'ANEQ' CALCULATED      D  29
C          SO THAT THE EXPECTED NUMBER OF EVENTS BETWEEN AM0 AND AM1       D  30
C          IS UNITY.                                                       D  31
C                                                                          D  32
      IF (LORS(INGS)) 40,40,50                                             D  33
  40  AK=1./(1.-EXP(-BETA(INGS)*AM1(INGS)))                                D  34
      ANEQ=1./(1.-AK+AK*EXP(-BETA(INGS)*AM0(INGS)))                        D  35
      AMZ=0.0                                                              D  36
      GO TO 60                                                             D  37
  50  AK=1./(1.-EXP(-BETA(INGS)*(AM1(INGS)-AM0(INGS))))                    D  38
      ANEQ=1.                                                              D  39
      AMZ=AM0(INGS)                                                        D  40
C          CALCULATE MAGNITUDE 'AMSTAR' ASSOCIATED WITH MAX. INTENSITY     D  41
C          AT THIS DISTANCE (R)' IF LESS THAN AM1, EVALUATE RISK           D  42
C          FOR MAGNITUDES BETWEEN AMSTAR AND AM1 SEPARATELY.               D  43
  60  AMSTAR=(AAA-C1-C3*RLN)/(C2-BBB)                                      D  44
      IF (AM1(INGS)-AMSTAR) 65,65,70                                       D  45
C         NONE OF MAGNITUDE INTEGRATION LIES ABOVE AMSTAR.                 D  46
  65  CALL ERISK(AMZ,AM1(INGS),C1,C2,C3,RLN,SIGG,BETA(INGS),TIC,           D  47
     1 G1,G2,G3,G4,CON1,CON2,CON3)                                         D  48
      GO TO 77                                                             D  49
  70  IF (AMZ-AMSTAR) 80,75,75                                             D  50
C         ALL OF MAGNITUDE INTEGRATION LIES ABOVE AMSTAR.                  D  51
  75  CALL ERISK(AMZ,AM1(INGS),AAA,BBB,0.,RLN,SIGG,BETA(INGS),TIC,         D  52
     1  G1,G2,G3,G4,CON1,CON2,CON3)                                        D  53
  77  RISK=((1.-AK)*G1 + AK*G2 + AK*(G3-G4)*CON1*CON2)*ANEQ                D  54
      GO TO 100                                                            D  55
C         SOME OF MAGNITUDE INTEGRATION LIES ABOVE MSTAR, SOME BELOW.      D  56
  80  CALL ERISK(AMZ,AMSTAR,C1,C2,C3,RLN,SIGG,BETA(INGS),TIC,              D  57
     1  G1,G2,G3,G4,CON1,CON2,CON3)                                        D  58
      CALL ERISK(AMSTAR,AM1(INGS),AAA,BBB,0.,RLN,SIGG,BETA(INGS),TIC,      D  59
     1  GG1,GG2,GG3,GG4,CCON1,CCON2,CCON3)                                 D  60
      RISK=((1.-AK)*G1 +AK*G2 +AK*(G3-G4)*CON1*CON2                        D  61
     1  +(1.-AK)*(GG1-GG2) + AK*(GG3-GG4)*CCON1*CCON2                      D  62
     2  *EXP(+BETA(INGS)*(AMZ-AMSTAR)))*ANEQ                               D  63
 100  RETURN                                                               D  64
      END                                                                  D  65
      SUBROUTINE ERISK(AMZ,AMM,C1,C2,C3,RLN,SIG,BETA,TIC,                  E   1
     1  G1,G2,G3,G4,CON1,CON2,CON3)                                        E   2
C         EVALUATE RISK ASSOCIATED WITH EXPONENTIAL MAGNITUDE LAW          E   3
C             FOR MAGNITUDES BETWEEN AMZ AND AMM.                          E   4
      Z=(TIC-C1-C2*AMM-C3*RLN)/SIG                                         E   5
      CALL NDTR(Z,G1,D)                                                    E   6
      Z=Z-BETA*SIG/C2                                                      E   7
      CALL NDTR(Z,G3,D)                                                    E   8
      Z=(TIC-C1-C2*AMZ-C3*RLN)/SIG                                         E   9
      CALL NDTR(Z,G2,D)                                                    E  10
      Z=Z-BETA*SIG/C2                                                      E  11
      CALL NDTR(Z,G4,D)                                                    E  12
      IF (C2-0.001) 10,10,20                                               E  13
  10  CON1=100000000.                                                      E  14
      CON3=CON1                                                            E  15
      GO TO 30                                                             E  16
  20  CON1=((BETA*BETA*SIG*SIG)/(2.*C2*C2))+(BETA*AMZ)                     E  17
     1 +((C1-TIC)*BETA/C2)                                                 E  18
      CON3=CON1+BETA*(AMM-AMZ)                                             E  19
      CON1=EXP(CON1)                                                       E  20
      CON3=EXP(CON3)                                                       E  21
  30  CON2=BETA*C3/C2                                                      E  22
      R=EXP(RLN)                                                           E  23
      CON2=R**CON2                                                         E  24
      RETURN                                                               E  25
      END                                                                  E  26
      SUBROUTINE RISK2(TIC,REPIS,INGS,RISK)                                F   1
      COMMON NRD,NWR,NLEI,TI(12),RSKTI(12)                                 F   2
      COMMON C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                               F   3
      COMMON NGS,NRS(20),AM0(21),AM1(21),LORS(21)                          F   4
      COMMON BETA(21),RATE(21),COEF(21),FDEPTH(21)                         F   5
      COMMON NSTEPO,NSTEPI,JCALC,JPRNT                                     F   6
      COMMON INDIC(4),AREA(20,11)                                          F   7
C  Attenuation choice:                                                     t0000
      COMMON /atten/ IATTE                                                 t0001
C                                                                          F   8
C         SUBROUTINE TO CALCULATE RISK WHEN AN ATTENUATION FUNCTION        F   9
C         NON-SEPARABLE IN M AND R IS USED, OR WHEN NON-EXPONENTIAL        F  10
C         DENSITY FUNCTION ON EARTHQUAKE SIZE IS USED, REQUIRING           F  11
C         NUMERICAL INTEGRATION ON MAGNITUDE.                              F  12
C                                                                          F  13
C         THE EXAMPLE GIVEN HERE IS A SEPARABLE FUNCTION OF M AND R,       F  14
C         FOR WHICH SUBROUTINE RISK1 WOULD ACTUALLY BE USED IN             F  15
C         CALCULATIONS.  IT IS USED HERE FOR DEMONSTRATION PURPOSES        F  16
C         ONLY.                                                            F  17
C                                                                          F  18
C         AMINT IS MAGNITUDE INTERVAL USED FOR NUMERICAL INTEGRATION.      F  19
C         NO. OF MAGNITUDE STEPS (E.G.(AM1-AM0)/AMINT) MUST BE             F  20
C         A WHOLE NUMBER.                                                  F  21
C                                                                          F  22
      AMINT=0.05                                                           F  23
      RISK=0.0                                                             F  24
      SIGG=SIG                                                             F  25
      RFOC=SQRT(REPIS*REPIS + FDEPTH(INGS)*FDEPTH(INGS))                   F  26
      IF (RFOC-RONE) 80,80,90                                              F  27
  80  R=RONE                                                               F  28
C         IF DIFFERENT STANDARD DEVVIATION INSIDE RADIUS RONE IS           F  29
C          DESIRED, SET SIGG TO THIS STANDARD DEVIATION HERE.              F  30
      GO TO 100                                                            F  31
  90  R=RFOC                                                               F  32
 100  IF (LORS(INGS)) 110,110,150                                          F  33
C         LOOSE SOURCE, COMPUTE APPROPRIATE RATE, ASSUMING                 F  34
C         LOWEST MAGNITUDE = 4.0.                                          F  35
 110  AK=1./(1.-EXP(-BETA(INGS)*(AM1(INGS)-4.0)))                          F  36
      ANEQ=1./(1.-AK+AK*EXP(-BETA(INGS)*(AM0(INGS)-4.0)))                  F  37
      AMZ=4.0                                                              F  38
      GO TO 200                                                            F  39
 150  AMZ=AM0(INGS)                                                        F  40
      ANEQ=1.                                                              F  41
      AK=1./(1.-EXP(-BETA(INGS)*(AM1(INGS)-AM0(INGS))))                    F  42
C            COMPUTE NUMBER OF INTEGRATION STEPS                           F  43
C            (STEPSIZE = AMINT MAGNITUDE UNITS).                           F  44
 200  NMAGS=(AM1(INGS)-AMZ)/AMINT +0.1                                     F  45
C           LOOP ON MAGNITUDE, IN STEPS OF AMINT MAGNITUDE UNITS           F  46
      DO 500 IMAG=1,NMAGS                                                  F  47
      AM=IMAG                                                              F  48
      AMAG=AMZ+AM*AMINT-(AMINT/2.)                                         F  49
C         CALCULATE PROBABILITY OF EARTHQUAKE IN THIS MAGNITUDE STEP       F  50
      AMAGP=AMAG + (AMINT/2.)                                              F  51
      AMAGM=AMAG - (AMINT/2.)                                              F  52
      PROB=AK*(EXP(-BETA(INGS)*(AMAGM-AMZ))                                F  53
     1  -EXP(-BETA(INGS)*(AMAGP-AMZ)))                                     F  54
C           CALCULATE MEAN INTENSITY FOR THIS MAGNITUDE (AND DISTANCE).    F  55
C                                                                          F  56
C----------  USER=  INSERT YOUR MEAN FUNCTION HERE -------                 F  57
      go to (11,12,13) IATTE                                               t1000
 11   AMEAN = C1 + C2*AMAG + C3*ALOG(R+RZERO)                              F  58
	  go to 111
C..Joyner and Boore attenuation relation                                   t0000
 12     AMEAN1 = -1.02 + 0.249 * AMAG - ALOG10(R) - 0.00255*R 
  	amean2 = 10 ** (amean1)
 	amean = alog (amean2)
      go to 111
C..Campbell (1985) relation (see Walt Silva, 1986)                         t0011
C       AMEAN = -3.327 + 0.75 * AMAG - 1.14 *                              t0012
C    +          ALOG(R + 0.0531 * EXP(0.658 * AMAG))                       t0013
C..Campbell (1988) relation (see Joyner and Boore, 1988)                   t0021
 13     AMEAN = -2.817 + 0.702 * AMAG - 1.20 *                             t0022
     +          ALOG(R + 0.0921 * EXP (0.584 * AMAG))                      t0023
        go to 111
C..Correction 1: 6.88 = log (973)  ; conversion to cm/sec**2               t0002
 111    AMEAN = AMEAN + 6.88                                               t0003
      IF (AMEAN-AAA-BBB*AMAG) 220,220,210                                  F  59
 210  AMEAN=AAA+BBB*AMAG                                                   F  60
 220  CONTINUE                                                             F  61
C         CALCULATE STANDARDIZED VARIABLE                                  F  62
      Z = (TIC-AMEAN)/SIGG                                                 F  63
C           CALCULATE PROBABILITY OF EXCEEDING INTENSITY OF INTEREST       F  64
C --------  USER = INSERT YOUR (COMPLEMENTARY CUMULATIVE)                  F  65
C                  PROBABILITY FUNCTION HERE -------                       F  66
      CALL NDTR (Z,G,D)                                                    F  67
C           ACCUMULATE RISK                                                F  68
 500  RISK = RISK + G*PROB                                                 F  69
      RISK=RISK*ANEQ                                                       F  70
      RETURN                                                               F  71
      END                                                                  F  72
      SUBROUTINE BETWEN(X1,Y1,X2,Y2,XP,YP,INDIC,IANS)                      G   1
C         SUBROUTINE TO DETERMINE IF (XP,YP) LIES BETWEEN                  G   2
C            (X1,Y1) AND (X2,Y2).                                          G   3
C                                                                          G   4
      IF(INDIC.LT.1.OR.INDIC.GT.2)GO TO 300                                G   5
C                                                                          G   6
      GO TO (100,200),INDIC                                                G   7
 100  IF (X1-XP) 110,410,120                                               G   8
 110  IF (X2-XP) 420,420,410                                               G   9
 120  IF (X2-XP) 410,420,420                                               G  10
 200  IF (Y1-YP) 210,410,220                                               G  11
 210  IF (Y2-YP) 420,420,410                                               G  12
 220  IF (Y2-YP) 410,420,420                                               G  13
C        ERROR RETURN                                                      G  14
 300  IANS=0                                                               G  15
      GO TO 500                                                            G  16
C        (XP,YP) LIES BETWEEN END POINTS, I.E. ON SOURCE BOUNDARY          G  17
 410  IANS=1                                                               G  18
      GO TO 500                                                            G  19
C        (XP,YP) DOESN'T LIE BETWEEN END POINTS, I.E. IT'S OUTSIDE SOURC   G  20
 420  IANS=-1                                                              G  21
 500  RETURN                                                               G  22
      END                                                                  G  23
      SUBROUTINE NDTR(X,P,D)                                               H   1
C         X  IS NO. OF STANDARDIZED NORMAL DEVIATES.                       H   2
C         P  IS COMP. CUMULATIVE VALUE (OUTPUT).                           H   3
C         D  IS DENSITY VALUE (OUTPUT)                                     H   4
      IF (X) 1,2,2                                                         H   5
   1  AX=-X                                                                H   6
      GO TO 3                                                              H   7
   2  AX=X                                                                 H   8
   3  IF (AX-6.0) 5,4,4                                                    H   9
   4  P=1.                                                                 H  10
      D=0.                                                                 H  11
      GO TO 6                                                              H  12
   5  T=1./(1.0+.2316419*AX)                                               H  13
      D=0.3989423*EXP(-X*X/2.0)                                            H  14
      P = 1.0 - D*T*((((1.330274*T - 1.821256)*T + 1.781478)*T -           H  15
     1  0.3565638)*T + 0.3193815)                                          H  16
   6  IF (X) 8,7,7                                                         H  17
   7  P=1.0-P                                                              H  18
   8  RETURN                                                               H  19
      END                                                                  H  20
      SUBROUTINE AREAS(X1,Y1,X2,Y2,X3,Y3,X4,Y4,AREA)                       I   1
C      SUBROUTINE TO CALCULATE AREA OF ARBITRARY QUADRILATERAL,            I   2
C      WHERE (X1,Y1) AND X4,Y4) ARE OPPOSITE CORNERS.                      I   3
C         LOCATE INTERSECTIONS OF DIAGONALS=                               I   4
      IF (X4-X1-0.001) 20,30,30                                            I   5
  20  IF (X4-X1+0.001) 30,30,25                                            I   6
  25  XX=X1                                                                I   7
      A2=(Y3-Y2)/(X3-X2)                                                   I   8
      YY=(XX-X3)*A2 +Y3                                                    I   9
      DIST1=Y4-Y1                                                          I  10
      IF (DIST1) 26,27,27                                                  I  11
  26  DIST1=-DIST1                                                         I  12
  27  DIST2=SQRT((X3-X2)*(X3-X2) + (Y3-Y2)*(Y3-Y2))                        I  13
      GO TO 100                                                            I  14
  30  IF (X3-X2-0.001) 50,70,70                                            I  15
  50  IF (X3-X2+0.001) 70,70,65                                            I  16
  65  XX=X3                                                                I  17
      A1=(Y4-Y1)/(X4-X1)                                                   I  18
      YY=(XX-X4)*A1+Y4                                                     I  19
      DIST2=Y3-Y2                                                          I  20
      IF (DIST2) 66,67,67                                                  I  21
  66  DIST2=-DIST2                                                         I  22
  67  DIST1=SQRT((X4-X1)*(X4-X1) + (Y4-Y1)*(Y4-Y1))                        I  23
      GO TO 100                                                            I  24
  70  A1=(Y4-Y1)/(X4-X1)                                                   I  25
      A2=(Y3-Y2)/(X3-X2)                                                   I  26
      XX=(Y2-Y1+A1*X1-A2*X2)/(A1-A2)                                       I  27
      YY=A1*(XX-X1) + Y1                                                   I  28
      DIST1=SQRT((X4-X1)*(X4-X1) + (Y4-Y1)*(Y4-Y1))                        I  29
      DIST2=SQRT((X3-X2)*(X3-X2) + (Y3-Y2)*(Y3-Y2))                        I  30
C          CALCULATE LENGTH OF SIDES OF SUB-TRIANGLE                       I  31
 100  SIDE1=SQRT((XX-X1)*(XX-X1) + (YY-Y1)*(YY-Y1))                        I  32
      SIDE2=SQRT((XX-X2)*(XX-X2) + (YY-Y2)*(YY-Y2))                        I  33
      SIDE3=SQRT((X1-X2)*(X1-X2) + (Y1-Y2)*(Y1-Y2))                        I  34
C          SOLUTION ACCORDING TO C.R.C. HANDBOOK UNDER                     I  35
C               'MENSURATION FORMULAE' AND 'TRIGONOMETRIC FORMULAE'        I  36
      SS=(SIDE1+SIDE2+SIDE3)/2.                                            I  37
      SINANG=2.*SQRT(SS*(SS-SIDE1)*(SS-SIDE2)*(SS-SIDE3))/(SIDE1*SIDE2)    I  38
      AREA=0.5*DIST1*DIST2*SINANG                                          I  39
      RETURN                                                               I  40
      END                                                                  I  41
      SUBROUTINE CIRCLE(RC,INGS,FRAREA,RSK)                                J   1
      COMMON NRD,NWR,NLEI,TI(12),RSKTI(12)                                 J   2
      COMMON C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                               J   3
      COMMON NGS,NRS(20),AM0(21),AM1(21),LORS(21)                          J   4
      COMMON BETA(21),RATE(21),COEF(21),FDEPTH(21)                         J   5
      COMMON NSTEPO,NSTEPI,JCALC,JPRNT                                     J   6
      COMMON INDIC(4),AREA(20,11)                                          J   7
      DIMENSION RSK(12)                                                    J   8
C         SUBROUTINE TO CALCULATE RISK FROM A CIRCULAR                     J   9
C         SOURCE WITH CENTER AR SITE, RADIUS RC.                           J  10
      NRC=RC                                                               J  11
C         CHOOSE STEP SIZE=                                                J  12
C              STEP SIZE = NSTEPI ULESS RESULTING STEP SIZE IS             J  13
C              LESS THAN ONE KILOMETRE, IN WHICH CASE RC+1                 J  14
C              STEPS ARE USED.                                             J  15
      IF (NRC-NSTEPI) 10,12,12                                             J  16
  10  NSTEPX=NRC+1                                                         J  17
      GO TO 14                                                             J  18
  12  NSTEPX=NSTEPI                                                        J  19
  14  ANSTEP=NSTEPX                                                        J  20
      DO 90 II=1,NSTEPX                                                    J  21
      AI=II                                                                J  22
      R=((AI-0.5)*RC)/ANSTEP                                               J  23
      ANAREA=6.2831853072 *R*RC/ANSTEP                                     J  24
      RATEI=RATE(INGS)*ANAREA*FRAREA                                       J  25
      DO 80 JJ=1,NLEI                                                      J  26
      IF (JCALC) 50,50,60                                                  J  27
  50  CALL RISK1(TI(JJ),R,INGS,RISK)                                       J  28
      GO TO 70                                                             J  29
  60  CALL RISK2(TI(JJ),R,INGS,RISK)                                       J  30
  70  RSK(JJ)=RSK(JJ)+RISK*RATEI                                           J  31
  80  CONTINUE                                                             J  32
  90  CONTINUE                                                             J  33
      RETURN                                                               J  34
      END                                                                  J  35
      SUBROUTINE INSIDE(XNOT,YNOT,INGS,INSS,XL,YL,XR,YR,AA,BB)             K   1
      COMMON NRD,NWR,NLEI,TI(12),RSKTI(12)                                 K   2
      COMMON C1,C2,C3,SIG,RZERO,RONE,AAA,BBB                               K   3
      COMMON NGS,NRS(20),AM0(21),AM1(21),LORS(21)                          K   4
      COMMON BETA(21),RATE(21),COEF(21),FDEPTH(21)                         K   5
      COMMON NSTEPO,NSTEPI,JCALC,JPRNT                                     K   6
      COMMON INDIC(4),AREA(20,11)                                          K   7
      DIMENSION XL(4),YL(4),XR(4),YR(4),AA(4),BB(4),XC(4),YC(4)            K   8
      DIMENSION RSK(10)                                                    K   9
C         SUBROUTINE FOR CALCULATING RISK WHEN SITE IS INSIDE              K  10
C                 SOURCE AREA.                                             K  11
C           REFER TO DOCUMENTATION FOR ALGORITHM USED TO CHOOSE            K  12
C                 NUMBER OF INTEGRATION STEPS.                             K  13
      DO 50 II=1,NLEI                                                      K  14
  50  RSK(II)=0.                                                           K  15
      APPROX=0.                                                            K  16
      RC2=10000000000.                                                     K  17
      RF2=0.                                                               K  18
C          FIND CLOSEST SIDE AND FARTHEST POINT                            K  19
      DO 160 II=1,4                                                        K  20
      IINDIC=INDIC(II)                                                     K  21
C                                                                          K  22
      IF(IINDIC.EQ.1) GO TO 120                                            K  23
C                                                                          K  24
 110  XS=XL(II)                                                            K  25
      YS=YNOT                                                              K  26
      GO TO 140                                                            K  27
C          IS SLOPE ZEROf                                                  K  28
 120  IF (AA(II)-0.001) 121,125,125                                        K  29
 121  IF (AA(II)+0.001) 125,125,122                                        K  30
 122  XS=XNOT                                                              K  31
      YS=YL(II)                                                            K  32
      GO TO 140                                                            K  33
C        SLOPE NOT ZERO                                                    K  34
 125  XS=(YNOT+(XNOT/AA(II))-BB(II))/(AA(II)+1./AA(II))                    K  35
      YS=((XNOT-XS)/AA(II))+YNOT                                           K  36
C          CALCULATE SQUARE OF DISTANCE BETWEEN SITE AND CLOSEST POINT.    K  37
 140  DIST=(XNOT-XS)*(XNOT-XS)+(YNOT-YS)*(YNOT-YS)                         K  38
      IF (DIST-RC2) 151,152,152                                            K  39
 151  RC=SQRT(DIST)                                                        K  40
      RC2=DIST                                                             K  41
      ICLO=II                                                              K  42
C          CALCULATE DISTANCE BETWEEN SITE AND LEFT HAND POINT ON SIDE     K  43
 152  DIST=(XNOT-XL(II))*(XNOT-XL(II))+(YNOT-YL(II))*(YNOT-YL(II))         K  44
      IF (RF2-DIST) 154,160,160                                            K  45
 154  RF=SQRT(DIST)                                                        K  46
      RF2=DIST                                                             K  47
      IFAR=II                                                              K  48
 160  CONTINUE                                                             K  49
C          DETERMINE AZIMUTH OF FARTHEST POINT WITH RESPECT TO SITE        K  50
      AZIMF=ACOS((XL(IFAR)-XNOT)/RF)                                      K  51
      IF (YL(IFAR)-YNOT) 162,164,164                                       K  52
 162  AZIMF=6.2831853072 - AZIMF                                           K  53
 164  CONTINUE                                                             K  54
C      RC IS NOW DISTANCE FROM SITE TO CLOSEST SIDE.                       K  55
C      RF IS NOW DISTANCE FROM SITE TO FARTHEST CORNER.                    K  56
      IF (RC-0.01) 200,200,170                                             K  57
C        CALL SUBROUTINE CIRCLE TO CALCULATE RISK FROM CIRCULAR            K  58
C            SOURCE WITH RADIUS RC                                         K  59
 170  NTOT=NRS(INGS)+1                                                     K  60
      FRAREA=AREA(INGS,INSS)/AREA(INGS,NTOT)                               K  61
      CALL CIRCLE(RC,INGS,FRAREA,RSK)                                      K  62
      APPROX=3.1415926536*RC*RC                                            K  63
C       LOOP ON R TO CALCULATE RISK FROM RC TO RF                          K  64
 200  AN=NSTEPI                                                            K  65
C        PICK STEP SIZE BASED ON FRACTION OF AREA LEFT                     K  66
      FRLEFT=(AREA(INGS,INSS)-APPROX)/AREA(INGS,INSS)                      K  67
      NSTEPX=FRLEFT*AN + 1.                                                K  68
      AN=NSTEPX                                                            K  69
      STSIZE=(RF-RC)/AN                                                    K  70
      DO 500 ISTEP=1,NSTEPX                                                K  71
      AI=ISTEP                                                             K  72
      R=RC+(AI-0.5)*STSIZE                                                 K  73
      NPT=0                                                                K  74
      ANGLE=0.                                                             K  75
C         LOOP ON EACH SIDE                                                K  76
      DO 400 II=1,4                                                        K  77
      IINDIC=INDIC(II)                                                     K  78
C                                                                          K  79
      IF(IINDIC.EQ.1) GO TO 330                                            K  80
C                                                                          K  81
C         SIDE II IS VERTICAL, DOES CIRCLE (RADIUS R) INTERSECT ITf        K  82
 320  A=XL(II)-XNOT                                                        K  83
      IF (A) 322,322,323                                                   K  84
 322  IF (R+A) 400,400,324                                                 K  85
 323  IF (R-A) 400,400,324                                                 K  86
C         COMPUTE 2 INTERSECTION POINTS                                    K  87
 324  X1=XL(II)                                                            K  88
      B=SQRT(R*R-(X1-XNOT)*(X1-XNOT))                                      K  89
      Y1=YNOT+B                                                            K  90
      X2=XL(II)                                                            K  91
      Y2=YNOT-B                                                            K  92
      GO TO 341                                                            K  93
 330  A=1.+AA(II)*AA(II)                                                   K  94
      B=2.*(-XNOT+AA(II)*(BB(II)-YNOT))                                    K  95
      C=XNOT*XNOT+YNOT*YNOT+BB(II)*(BB(II)-2.*YNOT)-R*R                    K  96
      D=B*B-4.*A*C                                                         K  97
      IF (D) 400,400,340                                                   K  98
C          THERE ARE 2 INTERSECTION, CALCULATE THEIR COORDINATES.          K  99
 340  D=SQRT(D)                                                            K 100
      X1=(-B+D)/(2.*A)                                                     K 101
      Y1=AA(II)*X1+BB(II)                                                  K 102
      X2=(-B-D)/(2.*A)                                                     K 103
      Y2=AA(II)*X2+BB(II)                                                  K 104
C          SEE IF (X1,Y1) IS ON BOUNDARY                                   K 105
 341  CALL BETWEN(XL(II),YL(II),XR(II),YR(II),X1,Y1,INDIC(II),IANS)        K 106
      IF (IANS) 350,342,345                                                K 107
 342  NERROR=4                                                             K 108
      GO TO 800                                                            K 109
C       IS SECOND POINT ALSO ON BOUNDARYf                                  K 110
 345  CALL BETWEN(XL(II),YL(II),XR(II),YR(II),X2,Y2,INDIC(II),IANS)        K 111
      IF (IANS) 348,346,360                                                K 112
 346  NERROR=5                                                             K 113
      GO TO 800                                                            K 114
C         STORE FIRST POINT ONLY                                           K 115
 348  NPT=NPT+1                                                            K 116
      XC(NPT)=X1                                                           K 117
      YC(NPT)=Y1                                                           K 118
      GO TO 400                                                            K 119
C        SEE IF SECOND POINT ONLY IS ON BOUNDARY                           K 120
 350  CALL BETWEN(XL(II),YL(II),XR(II),YR(II),X2,Y2,INDIC(II),IANS)        K 121
      IF (IANS) 400,352,354                                                K 122
 352  NERROR=6                                                             K 123
      GO TO 800                                                            K 124
 354  NPT=NPT+1                                                            K 125
      XC(NPT)=X2                                                           K 126
      YC(NPT)=Y2                                                           K 127
      GO TO 400                                                            K 128
C         TWO INTERSECTION POINTS ON ONE SIDE BOTH LIE ON BOUNDARY,        K 129
C          CALCULATE ANGLE BETWEEN THEM.                                   K 130
 360  CONTINUE                                                             K 131
 380  AD=SQRT((X1-X2)*(X1-X2)+(Y1-Y2)*(Y1-Y2))                             K 132
      ANGLE=2.*ASIN(AD/(2.*R))+ANGLE                                      K 133
 400  CONTINUE                                                             K 134
      IF (NPT) 402,404,408                                                 K 135
 402  NERROR=7                                                             K 136
      GO TO 800                                                            K 137
 404  IF (ANGLE-0.001) 402,406,406                                         K 138
C        FOLLOWING IS FOR CASE OF NO SINGLE INTERSECTION POINTS'           K 139
C        ANGLE IS 2 * PI - ANGLE CALCULATED SO FAR.                        K 140
 406  PANGLE=6.2831853072-ANGLE                                            K 141
      GO TO 460                                                            K 142
 408  IF(NPT.LT.1.OR.NPT.GT.4) GO TO 409                                   K 143
      GO TO(402,410,402,440),NPT                                           K 144
 409  CONTINUE                                                             K 145
      NERROR=8                                                             K 146
      GO TO 800                                                            K 147
C         2 INTERSECTION POINTS' DETERMINE AZIMUTHS.                       K 148
 410  IF (XC(1)-XNOT-R) 414,413,411                                        K 149
 411  IF (XC(1)-XNOT-R-0.001) 413,413,412                                  K 150
 412  NERROR=18                                                            K 151
      GO TO 800                                                            K 152
 413  AZIM1=0.0                                                            K 153
      GO TO 418                                                            K 154
 414  IF (XC(1)-XNOT+R) 415,416,417                                        K 155
 415  IF (XC(1)-XNOT+R+0.001) 412,416,416                                  K 156
 416  AZIM1=3.1415926536                                                   K 157
      GO TO 420                                                            K 158
 417  AZIM1=ACOS((XC(1)-XNOT)/R)                                          K 159
 418  IF (YC(1)-YNOT) 419,420,420                                          K 160
 419  AZIM1=6.2831853072  - AZIM1                                          K 161
 420  IF (XC(2)-XNOT-R) 424,423,421                                        K 162
 421  IF (XC(2)-XNOT-R-0.001) 423,423,422                                  K 163
 422  NERROR=19                                                            K 164
      GO TO 800                                                            K 165
 423  AZIM2=0.0                                                            K 166
      GO TO 428                                                            K 167
 424  IF (XC(2)-XNOT+R) 425,426,427                                        K 168
 425  IF (XC(2)-XNOT+R+0.001) 422,426,426                                  K 169
 426  AZIM2=3.1415926536                                                   K 170
      GO TO 430                                                            K 171
 427  AZIM2=ACOS((XC(2)-XNOT)/R)                                          K 172
 428  IF (YC(2)-YNOT) 429,430,430                                          K 173
 429  AZIM2=6.2831853072 -AZIM2                                            K 174
 430  PANGLE=AZIM2-AZIM1                                                   K 175
      IF (PANGLE) 431,439,435                                              K 176
 431  IF (AZIM1-AZIMF) 432,439,433                                         K 177
 432  PANGLE=6.2831853072 +PANGLE -ANGLE                                   K 178
      GO TO 460                                                            K 179
 433  IF (AZIMF-AZIM2) 432,439,434                                         K 180
 434  PANGLE=-PANGLE-ANGLE                                                 K 181
      GO TO 460                                                            K 182
 435  IF (AZIM2-AZIMF) 436,439,437                                         K 183
 436  PANGLE=6.2831853072 -PANGLE -ANGLE                                   K 184
      GO TO 460                                                            K 185
 437  IF (AZIMF-AZIM1) 436,439,438                                         K 186
 438  PANGLE=PANGLE-ANGLE                                                  K 187
      GO TO 460                                                            K 188
 439  NERROR=9                                                             K 189
      GO TO 800                                                            K 190
C        FOUR INTERSECTION POINTS (EACH ON A DIFFERENT SIDE).              K 191
C        DETERMINE ANGLE BY FINDING CLOSEST 2 INTERSECTIONS TO             K 192
C        FARTHEST CORNER, CALCULATE ANGLE BETWEN, AND ADD ANGLE            K 193
C        BETWEEN OTHER TWO INTERSECTIONS.                                  K 194
 440  DIST1=10000000000.                                                   K 195
      I1=0                                                                 K 196
      I2=0                                                                 K 197
      I3=0                                                                 K 198
      I4=0                                                                 K 199
      DO 450 JJ=1,4                                                        K 200
      DIST=(XL(IFAR)-XC(JJ))*(XL(IFAR)-XC(JJ))                             K 201
     1   +(YL(IFAR)-YC(JJ))*(YL(IFAR)-YC(JJ))                              K 202
      IF (DIST-DIST1) 442,444,444                                          K 203
 442  DIST2=DIST1                                                          K 204
      DIST1=DIST                                                           K 205
      I4=I3                                                                K 206
      I3=I2                                                                K 207
      I2=I1                                                                K 208
      I1=JJ                                                                K 209
      GO TO 450                                                            K 210
 444  IF (DIST-DIST2) 445,446,446                                          K 211
 445  DIST2=DIST                                                           K 212
      I4=I3                                                                K 213
      I3=I2                                                                K 214
      I2=JJ                                                                K 215
      GO TO 450                                                            K 216
 446  I4=I3                                                                K 217
      I3=JJ                                                                K 218
 450  CONTINUE                                                             K 219
C        CALCULATE ANGLE BETWEEN 2 CLOSEST POINTS TO FARTHEST CORNER       K 220
      AD=SQRT((XC(I1)-XC(I2))*(XC(I1)-XC(I2))                              K 221
     1  + (YC(I1)-YC(I2))*(YC(I1)-YC(I2)))                                 K 222
      PANGLE=2.*ASIN(AD/(2.*R))                                           K 223
C         CALCULATE ANGLE BETWEEN 2 FARTHEST POINTS FROM                   K 224
C         FARTHEST CORNER AND ADD TO PREVIOUS ANGLE.                       K 225
      AD=SQRT((XC(I3)-XC(I4))*(XC(I3)-XC(I4))                              K 226
     1 + (YC(I3)-YC(I4))*(YC(I3)-YC(I4)))                                  K 227
      PANGLE=2.*ASIN(AD/(2.*R))+PANGLE                                    K 228
C       ANGLE FOR THIS RADIUS IS NOW KNOWN, CALCULATE RISK                 K 229
 460  CONTINUE                                                             K 230
      ANAREA=PANGLE*R*STSIZE                                               K 231
      APPROX=APPROX+ANAREA                                                 K 232
      NTOT=NRS(INGS)+1                                                     K 233
      RATEI=RATE(INGS)*ANAREA*AREA(INGS,INSS)/AREA(INGS,NTOT)              K 234
C       CALCULATE CONTRIBUTION TO RISK                                     K 235
      DO 480 JJ=1,NLEI                                                     K 236
      IF (JCALC) 470,470,475                                               K 237
 470  CALL RISK1(TI(JJ),R,INGS,RISK)                                       K 238
      GO TO 478                                                            K 239
 475  CALL RISK2(TI(JJ),R,INGS,RISK)                                       K 240
 478  IF (RISK-0.0000000001) 500,490,490                                   K 241
 490  RSK(JJ)=RSK(JJ) +RISK*RATEI                                          K 242
 480  CONTINUE                                                             K 243
 500  CONTINUE                                                             K 244
      ARERR=((APPROX-AREA(INGS,INSS))/AREA(INGS,INSS))*100.                K 245
      IF (ARERR-5.) 510,520,520                                            K 246
 510  IF (ARERR+5.) 520,520,540                                            K 247
 520  WRITE (NWR,903) ARERR,INGS,INSS                                      K 248
 903  FORMAT(10X,'CAUTION= NUMERICAL INTEGRATION ERROR IN AREA IS ',       K 249
     1 F6.2,' ( FOR (INSIDE) SOURCE ',2I3)                                 K 250
 540  DO 550 JJ=1,NLEI                                                     K 251
      RSK(JJ)=COEF(INGS)*RSK(JJ)/APPROX                                    K 252
 550  RSKTI(JJ)=RSKTI(JJ)+RSK(JJ)                                          K 253
      IF (JPRNT) 850,850,610                                               K 254
C         PRINT RISKS FOR THIS SOURCE.                                     K 255
 610  WRITE(NWR,902) INGS,INSS,(RSK(I),I=1,NLEI)                           K 256
 902  FORMAT(' SOURCE',2I3,' E(NO/YR)= ',12E10.3)                          K 257
      GO TO 850                                                            K 258
C          ERROR PRINTOUT                                                  K 259
 800  WRITE(NWR,901)NERROR,INGS,INSS,IFAR,NPT,XNOT,YNOT,(XL(I),YL(I),      K 260
     1 I=1,4),RC,RF,R,PANGLE,(XC(I),YC(I),I=1,4)                           K 261
 901  FORMAT (' ***** ERROR',I4,' IN SUBROUTINE INSIDE. SOURCE NO.',       K 262
     1 2I3,' DEBUG VALUES FOLLOW......',/10X,2I10,10(/10X,2F12.6))         K 263
 850  RETURN                                                               K 264
      END                                                                  K 265
