// File: /workspace/github_c_makefile_star2_O3/esheldon[P]xshear/src/lens.c
// Function: lcat_read
LensCatalog* lcat_read(const ShearConfig* config, const char* lens_url) {

    int nread=0, expected=0;
    if (config->Dlens_input) {
        expected=6;
    } else {
        expected=5;
    }

    wlog("Reading lenses from %s\n", lens_url);
    FILE* stream=open_url(lens_url, "r");

    wlog("counting lines....");
    size_t nlens = count_lines_and_rewind(stream);

    wlog("%lu\n", nlens);

    wlog("    creating lcat...");

    LensCatalog* lcat=lcat_new(nlens);
    wlog("OK\n");

    wlog("    reading data...");
    double ra_rad,dec_rad;
    for (size_t i=0; i<nlens; i++) {

        Lens* lens = &lcat->data[i];

        if (config->Dlens_input) {
            nread=fscanf(stream,"%ld %lf %lf %lf %lf %ld",
                    &lens->index,&lens->ra,&lens->dec,&lens->z,&lens->da,&lens->maskflags);
        } else {
            nread=fscanf(stream,"%ld %lf %lf %lf %ld",
                    &lens->index,&lens->ra,&lens->dec,&lens->z,&lens->maskflags);
        }
        
        if (nread != expected) {
            wlog("Failed to read row %lu from %s\n", i, lens_url);
            wlog("nread=%d index=%d ra=%lf dec=%lf z=%lf maskflags=%d\n", nread, lens->index, lens->ra, lens->dec, lens->z, lens->maskflags);
            exit(EXIT_FAILURE);
        }

        ra_rad = lens->ra*D2R;
        dec_rad = lens->dec*D2R;

        lens->sinra = sin(ra_rad);
        lens->cosra = cos(ra_rad);
        lens->sindec = sin(dec_rad);
        lens->cosdec = cos(dec_rad);

        // add sin(lam),cos(lam),sin(eta),cos(eta)
        eq2sdss_sincos(lens->ra,lens->dec,
                       &lens->sinlam, &lens->coslam,
                       &lens->sineta, &lens->coseta);

    }
    fclose(stream);
    wlog("OK\n");

    return lcat;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rsi, %rbx
movq %rdi, 0x18(%rsp)
cmpl $0x1, 0x2c(%rdi)
pushq $0x6
popq %r14
sbbl $0x0, %r14d
movq 0xbb22(%rip), %r13      # 0xdff8
movq (%r13), %rdi
leaq 0x6d70(%rip), %rsi      # 0x9251
movq %rbx, %rdx
xorl %eax, %eax
callq 0x21a0
leaq 0x768a(%rip), %rsi      # 0x9b7c
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x76b0
movq %rax, %r15
movq (%r13), %rcx
leaq 0x6d5c(%rip), %rdi      # 0x9269
pushq $0x12
popq %rsi
pushq $0x1
popq %rbx
movq %rbx, %rdx
callq 0x2290
movq %r15, %rdi
callq 0x7719
movq %rax, %r12
movq (%r13), %rdi
leaq 0x6f28(%rip), %rsi      # 0x9459
movq %rax, %rdx
xorl %eax, %eax
callq 0x21a0
movq (%r13), %rcx
leaq 0x6d36(%rip), %rdi      # 0x927c
pushq $0x14
popq %rsi
movq %rbx, %rdx
callq 0x2290
movq %r12, %rdi
callq 0x5294
movq %rax, (%rsp)
movq (%r13), %rcx
leaq 0x6d29(%rip), %rdi      # 0x9291
pushq $0x3
popq %rsi
movq %rbx, %rdx
callq 0x2290
movq (%r13), %rcx
leaq 0x6d17(%rip), %rdi      # 0x9295
pushq $0x13
popq %rsi
movq %rbx, %rdx
callq 0x2290
testq %r12, %r12
je 0x268d
movq (%rsp), %rax
movq 0x8(%rax), %rbp
xorl %ebx, %ebx
leaq 0x8(%rbp), %rcx
leaq 0x10(%rbp), %r8
leaq 0x18(%rbp), %r9
movq 0x18(%rsp), %rax
cmpl $0x0, 0x2c(%rax)
je 0x25ce
leaq 0x20(%rbp), %r10
leaq 0x48(%rbp), %r11
movq %r15, %rdi
leaq 0x6ce4(%rip), %rsi      # 0x92a9
movq %rbp, %rdx
xorl %eax, %eax
pushq %r11
jmp 0x25e5
leaq 0x48(%rbp), %r10
subq $0x8, %rsp
movq %r15, %rdi
leaq 0x6ce1(%rip), %rsi      # 0x92c1
movq %rbp, %rdx
xorl %eax, %eax
pushq %r10
callq 0x2070
popq %rcx
popq %rdx
movl %eax, %r13d
cmpl %r14d, %eax
jne 0x26c4
movsd 0x8(%rbp), %xmm2
movsd 0x6aa1(%rip), %xmm0     # 0x90a8
mulsd %xmm0, %xmm2
movsd %xmm2, 0x20(%rsp)
movsd 0x10(%rbp), %xmm1
mulsd %xmm0, %xmm1
movsd %xmm1, 0x8(%rsp)
movapd %xmm2, %xmm0
callq 0x2250
movsd %xmm0, 0x28(%rbp)
movsd 0x20(%rsp), %xmm0
callq 0x2140
movsd %xmm0, 0x30(%rbp)
movsd 0x8(%rsp), %xmm0
callq 0x2250
movsd %xmm0, 0x38(%rbp)
movsd 0x8(%rsp), %xmm0
callq 0x2140
movsd %xmm0, 0x40(%rbp)
movsd 0x8(%rbp), %xmm0
movsd 0x10(%rbp), %xmm1
leaq 0x60(%rbp), %rdi
leaq 0x68(%rbp), %rsi
leaq 0x70(%rbp), %rdx
leaq 0x78(%rbp), %rcx
callq 0x8c31
incq %rbx
subq $-0x80, %rbp
cmpq %rbx, %r12
jne 0x259c
movq %r15, %rdi
callq 0x20d0
movq 0xb95c(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x6beb(%rip), %rdi      # 0x9291
pushq $0x3
popq %rsi
pushq $0x1
popq %rdx
callq 0x2290
movq (%rsp), %rax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xb92d(%rip), %r14      # 0xdff8
movq (%r14), %rdi
leaq 0x6c00(%rip), %rsi      # 0x92d5
movq %rbx, %rdx
movq 0x10(%rsp), %rcx
xorl %eax, %eax
callq 0x21a0
movq (%r14), %rdi
movq (%rbp), %rcx
movsd 0x8(%rbp), %xmm0
movsd 0x10(%rbp), %xmm1
movsd 0x18(%rbp), %xmm2
movq 0x48(%rbp), %r8
leaq 0x6bf0(%rip), %rsi      # 0x92f5
movl %r13d, %edx
movb $0x3, %al
callq 0x21a0
pushq $0x1
popq %rdi
callq 0x2280