// File: /workspace/github_c_makefile_star2_O3/usgs[P]nshmp-haz-catalogs/src/agrid/adaptive/get_eqs_fm_catalog.c
// Function: assign_variable_completeness_eqs
void assign_variable_completeness_eqs(struct eqcat* cat, struct compl_levels* compl_vals)
/*--------------------------------------------------------------------------*/
{
  int cnt, cnt1;
  int year_mindist;
  float loneq, lateq, lonpt, latpt;
  float dist, az, baz, mindist;
 
// loop over all earthquakes and assign the completeness year from closest grid point  
// default to 
  for(cnt=0; cnt<cat->nv; cnt++) {
    cat->year_compl[cnt]=compl_vals->yr_comp[0];
    loneq=cat->lon[cnt];
    lateq=cat->lat[cnt];
    mindist=9999.0;
    for(cnt1=0; cnt1<compl_vals->nVarComp; cnt1++) {
      lonpt=compl_vals->lon_arr[cnt1];
      latpt=compl_vals->lat_arr[cnt1];
      delaz_(&lateq,&loneq,&latpt,&lonpt,&dist,&az,&baz);
      if ( dist<mindist ) {
        mindist=dist;
        year_mindist=compl_vals->yrCompl_arr[cnt1];
      }
    }
    if (mindist<200) cat->year_compl[cnt]=year_mindist;
//    fprintf(stdout,"compl-yr %.2f %.2f %d\n", loneq, lateq, cat->year_compl[cnt]);
  }
  fprintf(stdout,"Assigned variable completeness to earthquakes in catalog.\n");

// remove events that occur before variable completeness year
  cnt1=0;
  for(cnt=0; cnt<cat->nv; cnt++) {
    if ( (cat->year[cnt]) >= (cat->year_compl[cnt]) ) {
      cat->year[cnt1]=cat->year[cnt];
      cat->month[cnt1]=cat->month[cnt];
      cat->day[cnt1]=cat->day[cnt];
      cat->lineno[cnt1]=cat->lineno[cnt];
      cat->mag[cnt1]=cat->mag[cnt];
      cat->lon[cnt1]=cat->lon[cnt];
      cat->lat[cnt1]=cat->lat[cnt];
      cat->depth[cnt1]=cat->depth[cnt];
      cat->neff[cnt1]=cat->neff[cnt];
      cat->mag_sigma[cnt1]=cat->mag_sigma[cnt];
      cat->year_compl[cnt1]=cat->year_compl[cnt];
      fprintf(stderr,"%4d %4d %4d %8.4f\n", cat->year[cnt1], cat->month[cnt1], cat->day[cnt1], cat->mag[cnt1]);
      cnt1++;
    }
/*    else {
     fprintf(stderr,"Year discrepancy: %d %d, complete\n",cat->year[cnt], cat->year_compl[cnt]);
    } */
  }
  fprintf(stderr,"Reduced catalog by %d events (%d orig; %d variable-completeness).\n", cat->nv-cnt1, cat->nv, cnt1);
  cat->nv=cnt1;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, %r13
cmpl $0x0, (%rdi)
jle 0x1631
movq %rsi, %r14
xorl %ecx, %ecx
leaq 0x14(%rsp), %rbp
leaq 0xc(%rsp), %rbx
leaq 0x34(%rsp), %r15
movq %r13, 0x20(%rsp)
movl 0x14(%r14), %eax
movl %eax, 0x2bf24(%r13,%rcx,4)
movss 0x668a4(%r13,%rcx,4), %xmm0
movss %xmm0, 0x1c(%rsp)
movss 0x75304(%r13,%rcx,4), %xmm0
movss %xmm0, 0x18(%rsp)
cmpl $0x0, 0x2c0(%r14)
jle 0x1621
movq %rcx, 0x28(%rsp)
xorl %r13d, %r13d
movss 0x57a2(%rip), %xmm0     # 0x6d1c
movss %xmm0, 0x8(%rsp)
movss 0x14c344(%r14,%r13,4), %xmm0
movss %xmm0, 0x14(%rsp)
movss 0x1f2384(%r14,%r13,4), %xmm0
movss %xmm0, 0x10(%rsp)
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
leaq 0x18(%rsp), %rdi
leaq 0x1c(%rsp), %rsi
leaq 0x10(%rsp), %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %r15, %r9
callq 0x3574
movss 0xc(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x15e5
movl 0x2c4(%r14,%r13,4), %r12d
movss %xmm0, 0x8(%rsp)
incq %r13
movslq 0x2c0(%r14), %rax
cmpq %rax, %r13
jl 0x1580
movss 0x5724(%rip), %xmm0     # 0x6d20
ucomiss 0x8(%rsp), %xmm0
jbe 0x1617
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rcx
movl %r12d, 0x2bf24(%r13,%rcx,4)
jmp 0x1621
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rcx
incq %rcx
movslq (%r13), %rax
cmpq %rax, %rcx
jl 0x1530
movq 0x89a0(%rip), %rax      # 0x9fd8
movq (%rax), %rcx
leaq 0x56e6(%rip), %rdi      # 0x6d28
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x11a0
movl (%r13), %ecx
xorl %r14d, %r14d
testl %ecx, %ecx
jle 0x176d
xorl %r15d, %r15d
movq 0x8992(%rip), %r12      # 0x9ff8
leaq 0x56f6(%rip), %rbx      # 0x6d63
xorl %r14d, %r14d
movl 0x4(%r13,%r15,4), %edx
cmpl 0x2bf24(%r13,%r15,4), %edx
jge 0x168f
incq %r15
movslq %ecx, %rax
cmpq %rax, %r15
jl 0x1670
jmp 0x176d
movslq %r14d, %r14
movl %edx, 0x4(%r13,%r14,4)
movl 0xea64(%r13,%r15,4), %ecx
movl %ecx, 0xea64(%r13,%r14,4)
movl 0x1d4c4(%r13,%r15,4), %r8d
movl %r8d, 0x1d4c4(%r13,%r14,4)
movl 0x3a984(%r13,%r15,4), %eax
movl %eax, 0x3a984(%r13,%r14,4)
movss 0x57e44(%r13,%r15,4), %xmm0
movss %xmm0, 0x57e44(%r13,%r14,4)
movss 0x668a4(%r13,%r15,4), %xmm1
movss %xmm1, 0x668a4(%r13,%r14,4)
movss 0x75304(%r13,%r15,4), %xmm1
movss %xmm1, 0x75304(%r13,%r14,4)
movss 0x83d64(%r13,%r15,4), %xmm1
movss %xmm1, 0x83d64(%r13,%r14,4)
movss 0x927c4(%r13,%r15,4), %xmm1
movss %xmm1, 0x927c4(%r13,%r14,4)
movss 0xa1224(%r13,%r15,4), %xmm1
movss %xmm1, 0xa1224(%r13,%r14,4)
movl 0x2bf24(%r13,%r15,4), %eax
movl %eax, 0x2bf24(%r13,%r14,4)
movq (%r12), %rdi
cvtss2sd %xmm0, %xmm0
movq %rbx, %rsi
movb $0x1, %al
callq 0x1100
incl %r14d
movl (%r13), %ecx
jmp 0x167f
movq 0x8884(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
movl %ecx, %edx
subl %r14d, %edx
leaq 0x55f3(%rip), %rsi      # 0x6d76
movl %r14d, %r8d
xorl %eax, %eax
callq 0x1100
movl %r14d, (%r13)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq