// File: /workspace/github_c_makefile_star2_O2/usgs[P]nshmp-haz-catalogs/src/agrid/adaptive/get_eqs_fm_catalog.c
// Function: get_eqs_fm_catalog
int get_eqs_fm_catalog(char *fnm, struct eqcat* catn, struct compl_levels* compl, struct boundaries* reg, struct rates* seisr)
/*--------------------------------------------------------------------------*/
{
  FILE *fpcat;
  int cnt=0, cnt1, lineno=1;
//  int ix, iy, n;
  int yr, mon, day, hr, min;
  float mag, lon, lat, dep, sec, mag_adj, msig, neff,mconv;
  char buff[BUFFLEN];

// open file
  if( (fpcat = fopen(fnm, "r"))==NULL) {
    fprintf(stderr,"Earthquake catalog file not found, %s\n", fnm);
    return 0;
  }
  
// loop through catalog and select earthquakes that meet magnitude completeness thresholds and
// occur within the region
  fprintf(stderr,"Regional boundaries for eqs: min/max lon, lat: %d %d\n", reg->nsy, reg->nsx);
  while( fgets(buff,BUFFLEN,fpcat) ) {
//    sscanf(buff,"%f %f %f %f %d %d %d %d %d %f %f %f", &mag, &lon, &lat, &dep, &yr, &mon, &day, &hr, &min, &sec, &msig, &neff);
    sscanf(buff,"%f %f %f %f %d %d %d %d %d %f %f %f %f", &mconv, &lon, &lat, &dep, &yr, &mon, &day, &hr, &min, &sec, &msig, &mag, &neff);
    if ( (lon>=reg->minlon) && (lon<=reg->maxlon) &&  (lat>=reg->minlat) && (lat<=reg->maxlat) ) {
      for(cnt1=0; cnt1<compl->nlevels; cnt1++) {
//        fprintf(stderr,"mag/yr completeness level: %f %d\n", compl->mag_comp[cnt1], compl->yr_comp[cnt1]);
//        fprintf(stderr,"mag/yr completeness level: %f %d %d\n", compl->mag_comp[cnt1], compl->yr_comp[cnt1], compl->end_yr);
        if (compl->nlevels>1) fprintf(stderr,"There may be a problem with selection of earthquakes from catalog for multiple completeness levels.\n");
        mag_adj=compl->mag_comp[cnt1]-compl->dMag/2;
        if ( (mag>=mag_adj) && (yr>=compl->yr_comp[cnt1]) && (yr<=compl->end_yr) ) {
          catn->mag[cnt]=mag;
          catn->lon[cnt]=lon;
          catn->lat[cnt]=lat;
          catn->depth[cnt]=dep;
          catn->year[cnt]=yr;
          catn->month[cnt]=mon;
          catn->day[cnt]=day;
          catn->lineno[cnt]=lineno;
          catn->mag_sigma[cnt]=msig;
          catn->neff[cnt]=neff;
//          fprintf(stderr,"sel: %f %f %f %d %d %d %d\n", mag, lon, lat, yr, mon, day, lineno);
//          fprintf(stderr,"sel: %f %f %f %d %d %d %d\n", catn->mag[cnt], catn->lon[cnt], catn->lat[cnt], catn->year[cnt], catn->month[cnt], catn->day[cnt], catn->lineno[cnt]);
//          iy=(int)((reg->maxlat-lat)/reg->dlat);
//          ix=(int)((lon-reg->minlon)/reg->dlon);
//          n=iy*reg->nsx+ix+1;
//          seisr->asum_cnt[n]++;
          cnt++;
        }
      }
    }
    lineno++;
  }
  catn->nv=cnt;
  fclose(fpcat);

// check earthquake numbers fit within arrays
  if (catn->nv>NEQ) {
    fprintf(stderr,"Number of earthquakes in catalog (%d) - %s - greater than array size, NEQ (%d).", catn->nv, fnm, (int)NEQ);
    fprintf(stderr,"Modify NEQ and re-start code.\n");
    exit(1);
  }
  else {
    fprintf(stderr,"Selected %d earthquakes from catalog, %s (%d events, total)\n", catn->nv, fnm, lineno);
  }
  
  if ( (compl->variableCompleteness) == 1 ) {
    if ( (compl->nlevels) > 1 ) {
      fprintf(stderr,"ERROR: Code doesn't currently allow for variable completeness and multiple completeness levels.\n");
      exit(1);
    }
    assign_variable_completeness_eqs(catn, compl);
  }

  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rcx, %r12
movq %rdx, %rbx
movq %rsi, %r15
movq %rdi, %r14
leaq 0x3f60(%rip), %rsi      # 0x68cb
callq 0x1150
movq 0x7681(%rip), %rcx      # 0x9ff8
movq (%rcx), %rdi
movq %rax, 0x40(%rsp)
testq %rax, %rax
jne 0x299c
leaq 0x442e(%rip), %rsi      # 0x6db9
xorl %ebp, %ebp
movq %r14, %rdx
xorl %eax, %eax
callq 0x10f0
jmp 0x2c05
movq %r15, 0x38(%rsp)
movq %r14, 0x30(%rsp)
movl (%r12), %ecx
movq %r12, 0x48(%rsp)
movl 0x4(%r12), %edx
leaq 0x4425(%rip), %rsi      # 0x6de0
xorl %eax, %eax
callq 0x10f0
pushq $0x1
popq %rbp
leaq 0x60(%rsp), %r12
xorl %r13d, %r13d
movq 0x40(%rsp), %r14
movq %r12, %rdi
movl $0x12c, %esi            # imm = 0x12C
movq %r14, %rdx
callq 0x10d0
testq %rax, %rax
je 0x2bb1
subq $0x8, %rsp
movq %r12, %r15
movq %r12, %rdi
leaq 0x441a(%rip), %rsi      # 0x6e16
leaq 0x58(%rsp), %rdx
leaq 0x18(%rsp), %rcx
leaq 0x14(%rsp), %r8
leaq 0x24(%rsp), %r9
xorl %eax, %eax
leaq 0x1c(%rsp), %r10
pushq %r10
leaq 0x30(%rsp), %r10
pushq %r10
leaq 0x30(%rsp), %r10
pushq %r10
leaq 0x74(%rsp), %r10
pushq %r10
leaq 0x80(%rsp), %r10
pushq %r10
leaq 0x8c(%rsp), %r10
pushq %r10
leaq 0x5c(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
leaq 0x74(%rsp), %r10
pushq %r10
callq 0x1110
addq $0x50, %rsp
movss 0x10(%rsp), %xmm0
movq 0x48(%rsp), %rax
ucomiss 0xc(%rax), %xmm0
jae 0x2a76
movq %r15, %r12
jmp 0x2a99
movss 0x10(%rax), %xmm1
ucomiss %xmm0, %xmm1
movq %r15, %r12
jb 0x2a99
movss 0xc(%rsp), %xmm0
ucomiss 0x14(%rax), %xmm0
jb 0x2a99
movss 0x18(%rax), %xmm1
ucomiss %xmm0, %xmm1
jae 0x2aa0
incl %ebp
jmp 0x29d2
xorl %r14d, %r14d
movq 0x38(%rsp), %r12
movslq 0xc(%rbx), %rax
cmpq %rax, %r14
jge 0x2ba7
cmpl $0x2, %eax
jge 0x2b86
movss 0x64(%rbx), %xmm1
mulss 0x4259(%rip), %xmm1     # 0x6d24
addss 0x3c(%rbx,%r14,4), %xmm1
movss 0x20(%rsp), %xmm0
ucomiss %xmm1, %xmm0
jb 0x2b7e
movl 0x2c(%rsp), %eax
cmpl 0x14(%rbx,%r14,4), %eax
jl 0x2b7e
cmpl 0x10(%rbx), %eax
jg 0x2b7e
movslq %r13d, %r13
movss %xmm0, 0x57e44(%r12,%r13,4)
movss 0x10(%rsp), %xmm0
movss %xmm0, 0x668a4(%r12,%r13,4)
movss 0xc(%rsp), %xmm0
movss %xmm0, 0x75304(%r12,%r13,4)
movss 0x1c(%rsp), %xmm0
movss %xmm0, 0x83d64(%r12,%r13,4)
movl %eax, 0x4(%r12,%r13,4)
movl 0x28(%rsp), %eax
movl %eax, 0xea64(%r12,%r13,4)
movl 0x24(%rsp), %eax
movl %eax, 0x1d4c4(%r12,%r13,4)
movl %ebp, 0x3a984(%r12,%r13,4)
movss 0x18(%rsp), %xmm0
movss %xmm0, 0xa1224(%r12,%r13,4)
movss 0x14(%rsp), %xmm0
movss %xmm0, 0x927c4(%r12,%r13,4)
incl %r13d
incq %r14
jmp 0x2aa8
movq 0x746b(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x42a6(%rip), %rdi      # 0x6e3d
pushq $0x64
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
jmp 0x2abe
movq 0x40(%rsp), %r14
jmp 0x2a71
movq 0x38(%rsp), %r15
movl %r13d, (%r15)
movq %r14, %rdi
callq 0x1080
movl (%r15), %edx
movq 0x742d(%rip), %rax      # 0x9ff8
movq (%rax), %rdi
cmpl $0x3a99, %edx           # imm = 0x3A99
jge 0x2c19
leaq 0x4334(%rip), %rsi      # 0x6f11
movq 0x30(%rsp), %rcx
movl %ebp, %r8d
xorl %eax, %eax
callq 0x10f0
pushq $0x1
popq %rbp
cmpl $0x1, (%rbx)
jne 0x2c05
cmpl $0x2, 0xc(%rbx)
jge 0x2c56
movq %r15, %rdi
movq %rbx, %rsi
callq 0x26dc
movl %ebp, %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4282(%rip), %rsi      # 0x6ea2
movq 0x30(%rsp), %rcx
movl $0x3a98, %r8d           # imm = 0x3A98
xorl %eax, %eax
callq 0x10f0
movq 0x73bf(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x42af(%rip), %rdi      # 0x6ef2
pushq $0x1e
popq %rsi
pushq $0x1
popq %rdx
callq 0x1190
pushq $0x1
popq %rdi
callq 0x1180
movq 0x739b(%rip), %rax      # 0x9ff8
movq (%rax), %rcx
leaq 0x42e7(%rip), %rdi      # 0x6f4e
pushq $0x60
jmp 0x2c45
nop