// File: /workspace/github_c_makefile_star2_O2/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 %rsi, %r14
movq %rdi, %r13
xorl %ecx, %ecx
leaq 0x14(%rsp), %rbp
leaq 0xc(%rsp), %rbx
leaq 0x34(%rsp), %r15
movq %rdi, 0x20(%rsp)
movslq (%r13), %rax
cmpq %rax, %rcx
jge 0x27f4
movl 0x14(%r14), %eax
movl %eax, 0x2bf24(%r13,%rcx,4)
movss 0x668a4(%r13,%rcx,4), %xmm0
movss %xmm0, 0x1c(%rsp)
movq %rcx, 0x28(%rsp)
movss 0x75304(%r13,%rcx,4), %xmm0
movss %xmm0, 0x18(%rsp)
xorl %r13d, %r13d
movss 0x45cd(%rip), %xmm0     # 0x6d1c
movss %xmm0, 0x8(%rsp)
movslq 0x2c0(%r14), %rax
cmpq %rax, %r13
jge 0x27cb
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 0x2db4
movss 0xc(%rsp), %xmm0
movss 0x8(%rsp), %xmm1
ucomiss %xmm0, %xmm1
jbe 0x27c6
movl 0x2c4(%r14,%r13,4), %r12d
movss %xmm0, 0x8(%rsp)
incq %r13
jmp 0x2755
movss 0x454d(%rip), %xmm0     # 0x6d20
ucomiss 0x8(%rsp), %xmm0
movq 0x20(%rsp), %r13
movq 0x28(%rsp), %rcx
jbe 0x27ec
movl %r12d, 0x2bf24(%r13,%rcx,4)
incq %rcx
jmp 0x2706
movq 0x77dd(%rip), %rax      # 0x9fd8
movq (%rax), %rcx
leaq 0x4523(%rip), %rdi      # 0x6d28
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
xorl %r12d, %r12d
movq 0x77de(%rip), %r15      # 0x9ff8
leaq 0x4542(%rip), %r14      # 0x6d63
xorl %ebx, %ebx
movslq (%r13), %rcx
cmpq %rcx, %r12
jge 0x291c
movl 0x4(%r13,%r12,4), %edx
cmpl 0x2bf24(%r13,%r12,4), %edx
jge 0x2844
incq %r12
jmp 0x2823
movslq %ebx, %rax
movl %edx, 0x4(%r13,%rax,4)
movl 0xea64(%r13,%r12,4), %ecx
movl %ecx, 0xea64(%r13,%rax,4)
movl 0x1d4c4(%r13,%r12,4), %r8d
movl %r8d, 0x1d4c4(%r13,%rax,4)
movl 0x3a984(%r13,%r12,4), %esi
movl %esi, 0x3a984(%r13,%rax,4)
movss 0x57e44(%r13,%r12,4), %xmm0
movss %xmm0, 0x57e44(%r13,%rax,4)
movss 0x668a4(%r13,%r12,4), %xmm1
movss %xmm1, 0x668a4(%r13,%rax,4)
movss 0x75304(%r13,%r12,4), %xmm1
movss %xmm1, 0x75304(%r13,%rax,4)
movss 0x83d64(%r13,%r12,4), %xmm1
movss %xmm1, 0x83d64(%r13,%rax,4)
movss 0x927c4(%r13,%r12,4), %xmm1
movss %xmm1, 0x927c4(%r13,%rax,4)
movss 0xa1224(%r13,%r12,4), %xmm1
movss %xmm1, 0xa1224(%r13,%rax,4)
movl 0x2bf24(%r13,%r12,4), %esi
movl %esi, 0x2bf24(%r13,%rax,4)
movq (%r15), %rdi
cvtss2sd %xmm0, %xmm0
movq %r14, %rsi
movb $0x1, %al
callq 0x10f0
incl %ebx
jmp 0x283f
movq (%r15), %rdi
movl %ecx, %edx
subl %ebx, %edx
leaq 0x444c(%rip), %rsi      # 0x6d76
movl %ebx, %r8d
xorl %eax, %eax
callq 0x10f0
movl %ebx, (%r13)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq