def areaImpacted(magnitudes=np.arange(0, 20., 1.), offset=0.5, star=False):
    """

    """
    s = 0.1

    Nvconst = stellarNumberCounts.integratedCountsVband()

    print '\n mag     b       l       stars      CCD        area'
    for b in [20, 25, 30, 50, 90]:
        for l in [0, 90, 180]:
            area = 0
            prev = 0
            for ml in magnitudes:
                m = s * math.ceil(float(ml + offset) / s)
                n = stellarNumberCounts.bahcallSoneira(m, l, b, Nvconst)
                n -= prev  #subtract the number of stars in the previous bin

                ccd = n * 49.6 / 3600
                covering = pixelsImpacted(m, star=star)
                area += (ccd * covering) / (4096 * 4132.) * 100.

                prev = n  #store the number of stars in the current bin

                if area > 100:
                    area = 100.

            txt = '%.1f     %2d     %3d     %.2f        %.1f        %.3f' % (
                m, b, l, n, ccd, area)
            print txt

            if b == 90:
                #no need to do more than once... l is irrelevant
                break

    print '\n\n\nIntegrated Area Loss:'
    blow = 20
    bhigh = 90
    llow = 0
    lhigh = 180
    bnum = 71
    lnum = 181

    prev = 0
    for i, ml in enumerate(magnitudes):
        m = s * math.ceil(float(ml + offset) / s)
        l, b, counts = stellarNumberCounts.skyNumbers(m, blow, bhigh, llow,
                                                      lhigh, bnum, lnum)

        counts -= prev
        prev = counts.copy()

        #average
        stars = np.mean(counts)

        ccd = stars * 49.6 / 3600
        covering = pixelsImpacted(m, star=star)
        area = (ccd * covering) / (4096 * 4132.) * 100.

        if area > 100:
            area = 100.

        print 'magnitude = %.1f, average = %.5f, max = %.5f' % (ml, stars,
                                                                np.max(counts))
        print '%i stars per square degree will mean %i stars per CCD and thus an area loss of %.4f per cent' % \
              (stars, ccd, area)

        if i < 1:
            z = counts * covering / (
                4096 * 4132. * (4096 * 0.1 * 4132 * 0.1 / 60. / 60.)) * 100.
        else:
            z += counts * covering / (
                4096 * 4132. * (4096 * 0.1 * 4132 * 0.1 / 60. / 60.)) * 100.

    msk = z > 100
    z[msk] = 100.

    _areaLossPlot(m, b, l, z, blow, bhigh, llow, lhigh, bnum, lnum,
                  'Masking of Saturated Pixels From All Stars', 'AreaLoss')
def areaImpacted(magnitudes=np.arange(0, 20., 1.), offset=0.5, star=False):
    """

    """
    s = 0.1

    Nvconst = stellarNumberCounts.integratedCountsVband()

    print '\n mag     b       l       stars      CCD        area'
    for b in [20, 25, 30, 50, 90]:
        for l in [0, 90, 180]:
            area = 0
            prev = 0
            for ml in magnitudes:
                m = s * math.ceil(float(ml + offset) / s)
                n = stellarNumberCounts.bahcallSoneira(m, l, b, Nvconst)
                n -= prev #subtract the number of stars in the previous bin

                ccd = n * 49.6 / 3600
                covering = pixelsImpacted(m, star=star)
                area += (ccd * covering) / (4096 * 4132.) * 100.

                prev = n  #store the number of stars in the current bin

                if area > 100:
                    area = 100.


            txt = '%.1f     %2d     %3d     %.2f        %.1f        %.3f' % (m, b, l, n, ccd, area)
            print txt

            if b == 90:
                #no need to do more than once... l is irrelevant
                break

    print '\n\n\nIntegrated Area Loss:'
    blow=20
    bhigh=90
    llow=0
    lhigh=180
    bnum=71
    lnum=181

    prev = 0
    for i, ml in enumerate(magnitudes):
        m = s * math.ceil(float(ml + offset) / s)
        l, b, counts = stellarNumberCounts.skyNumbers(m, blow, bhigh, llow, lhigh, bnum, lnum)

        counts -= prev
        prev = counts.copy()

        #average
        stars = np.mean(counts)

        ccd = stars * 49.6 / 3600
        covering = pixelsImpacted(m, star=star)
        area = (ccd * covering) / (4096 * 4132.) * 100.

        if area > 100:
            area = 100.

        print 'magnitude = %.1f, average = %.5f, max = %.5f' % (ml, stars, np.max(counts))
        print '%i stars per square degree will mean %i stars per CCD and thus an area loss of %.4f per cent' % \
              (stars, ccd, area)

        if i < 1:
            z = counts * covering / (4096 * 4132. * (4096 * 0.1 * 4132 * 0.1 / 60. / 60.)) * 100.
        else:
            z += counts * covering / (4096 * 4132. * (4096 * 0.1 * 4132 * 0.1 / 60. / 60.)) * 100.

    msk = z > 100
    z[msk] = 100.

    _areaLossPlot(m, b, l, z, blow, bhigh, llow, lhigh, bnum, lnum,
                  'Masking of Saturated Pixels From All Stars', 'AreaLoss')
def _numberOfStarsAreaLoss(log,
                           magnitudelimits,
                           title,
                           offset=0.5,
                           covering=2550):
    """
    Calculates the area loss for given magnitude limit at a few galactic coordinates and
    integrated over an observing region.
    """
    Nvconst = stellarNumberCounts.integratedCountsVband()

    txt = '\n\n\nNumber of stars:'
    print txt
    log.info(txt)
    s = 0.1

    for ml in magnitudelimits:
        print '\nmag     b     l    stars    CCD    area'
        for b in [20, 25, 30, 50, 90]:
            for l in [0, 90, 180]:
                m = s * math.ceil(float(ml + offset) / s)
                n = stellarNumberCounts.bahcallSoneira(
                    m, l, b, Nvconst) * 1.15  #15 error in the counts
                ccd = n * 49.6 / 3600
                area = (ccd * covering) / (4096 * 4132.) * 100.

                if area > 100:
                    area = 100.
                #print 'At l=%i, b=%i, there are about %i stars in a square degree brighter than %.1f' % (l, b, n, m)
                #print '%i stars per square degree will mean %i stars per CCD and thus an area loss of %.2f per cent' % \
                #      (n, ccd, area)

                txt = '%.1f   %2d   %3d   %.1f    %.1f    %.2f' % (m, b, l, n,
                                                                   ccd, area)
                print txt
                log.info(txt)

                if b == 90:
                    #no need to do more than once... l is irrelevant
                    break

    txt = '\n\n\nIntegrated Area Loss:'
    print txt
    log.info(txt)
    blow = 20
    bhigh = 90
    llow = 0
    lhigh = 180
    bnum = 71
    lnum = 181

    for ml in magnitudelimits:
        m = s * math.ceil(float(ml + offset) / s)
        l, b, counts = stellarNumberCounts.skyNumbers(m, blow, bhigh, llow,
                                                      lhigh, bnum, lnum)
        counts *= 1.15  #15 error in the counts
        stars = np.mean(counts)
        ccd = stars * 49.6 / 3600
        area = (ccd * covering) / (4096 * 4132.) * 100.

        if area > 100:
            area = 100.

        txt = '%i stars per square degree will mean %i stars per CCD and thus an area loss of %.2f per cent' % \
              (stars, ccd, area)

        print txt
        log.info(txt)

        z = counts * covering / (4096 * 4132. *
                                 (4096 * 0.1 * 4132 * 0.1 / 60. / 60.)) * 100.
        msk = z > 100
        z[msk] = 100.

        _areaLossPlot(m, b, l, z, blow, bhigh, llow, lhigh, bnum, lnum, title,
                      'AreaLoss')
def _numberOfStarsAreaLoss(log, magnitudelimits, title, offset=0.5, covering=2550):
    """
    Calculates the area loss for given magnitude limit at a few galactic coordinates and
    integrated over an observing region.
    """
    Nvconst = stellarNumberCounts.integratedCountsVband()

    txt = '\n\n\nNumber of stars:'
    print txt
    log.info(txt)
    s = 0.1

    for ml in magnitudelimits:
        print '\nmag     b     l    stars    CCD    area'
        for b in [20, 25, 30, 50, 90]:
            for l in [0, 90, 180]:
                m = s * math.ceil(float(ml + offset) / s)
                n = stellarNumberCounts.bahcallSoneira(m, l, b, Nvconst) * 1.15  #15 error in the counts
                ccd = n * 49.6 / 3600
                area = (ccd * covering) / (4096 * 4132.) * 100.

                if area > 100:
                    area = 100.
                #print 'At l=%i, b=%i, there are about %i stars in a square degree brighter than %.1f' % (l, b, n, m)
                #print '%i stars per square degree will mean %i stars per CCD and thus an area loss of %.2f per cent' % \
                #      (n, ccd, area)

                txt = '%.1f   %2d   %3d   %.1f    %.1f    %.2f' % (m, b, l, n, ccd, area)
                print txt
                log.info(txt)

                if b == 90:
                    #no need to do more than once... l is irrelevant
                    break


    txt = '\n\n\nIntegrated Area Loss:'
    print txt
    log.info(txt)
    blow=20
    bhigh=90
    llow=0
    lhigh=180
    bnum=71
    lnum=181

    for ml in magnitudelimits:
        m = s * math.ceil(float(ml + offset) / s)
        l, b, counts = stellarNumberCounts.skyNumbers(m, blow, bhigh, llow, lhigh, bnum, lnum)
        counts *= 1.15 #15 error in the counts
        stars = np.mean(counts)
        ccd = stars * 49.6 / 3600
        area = (ccd * covering) / (4096 * 4132.) * 100.

        if area > 100:
            area = 100.

        txt = '%i stars per square degree will mean %i stars per CCD and thus an area loss of %.2f per cent' % \
              (stars, ccd, area)

        print txt
        log.info(txt)

        z = counts * covering / (4096 * 4132. * (4096 * 0.1 * 4132 * 0.1 / 60. / 60.)) * 100.
        msk = z > 100
        z[msk] = 100.

        _areaLossPlot(m, b, l, z, blow, bhigh, llow, lhigh, bnum, lnum, title, 'AreaLoss')