# Path: py/fiberassign/_version.py

# 
# Path: py/fiberassign/utils.py
# def option_list(opts):
# def assert_isoformat_utc(time_str):
# def get_date_cutoff(datetype, cutoff_case):
# def get_svn_version(svn_dir):
# def get_last_line(fn):
# def read_ecsv_keys(fn):
# 
# Path: py/fiberassign/targets.py
# class TargetTagalong(object):
#     def __init__(self, columns, outnames={}, aliases={}):
#     def get_default(self, column):
#     def get_output_name(self, column):
#     def add_data(self, targetids, tabledata, fake={}):
#     def set_data(self, targetids, tabledata):
#     def get_for_ids(self, targetids, names):
# def create_tagalong(plate_radec=True):
# def str_to_target_type(input):
# def default_main_sciencemask():
# def default_main_stdmask(rundate=None):
# def default_main_gaia_stdmask():
# def default_main_skymask():
# def default_main_suppskymask():
# def default_main_safemask():
# def default_main_excludemask():
# def default_sv3_sciencemask():
# def default_sv3_stdmask():
# def default_sv3_skymask():
# def default_sv3_suppskymask():
# def default_sv3_safemask():
# def default_sv3_excludemask():
# def default_sv2_sciencemask():
# def default_sv2_stdmask():
# def default_sv2_skymask():
# def default_sv2_suppskymask():
# def default_sv2_safemask():
# def default_sv2_excludemask():
# def default_sv1_sciencemask():
# def default_sv1_stdmask():
# def default_sv1_skymask():
# def default_sv1_suppskymask():
# def default_sv1_safemask():
# def default_sv1_excludemask():
# def default_cmx_sciencemask():
# def default_cmx_stdmask():
# def default_cmx_skymask():
# def default_cmx_suppskymask():
# def default_cmx_safemask():
# def default_cmx_excludemask():
# def desi_target_type(desi_target, mws_target, sciencemask, stdmask,
#                      skymask, suppskymask, safemask, excludemask, gaia_stdmask):
# def default_survey_target_masks(survey, rundate=None):
# def default_target_masks(data, rundate=None):
# def append_target_table(tgs, tagalong, tgdata, survey, typeforce, typecol,
#                         sciencemask,
#                         stdmask, skymask, suppskymask, safemask, excludemask, gaia_stdmask):
# def load_target_table(tgs, tagalong, tgdata, survey=None, typeforce=None, typecol=None,
#                       sciencemask=None, stdmask=None, skymask=None,
#                       suppskymask=None, safemask=None, excludemask=None, gaia_stdmask=None,
#                       rundate=None):
# def load_target_file(tgs, tagalong, tfile, survey=None, typeforce=None, typecol=None,
#                      sciencemask=None, stdmask=None, skymask=None,
#                      suppskymask=None, safemask=None, excludemask=None,
#                      rowbuffer=1000000, gaia_stdmask=None, rundate=None):
# def targets_in_tiles(hw, tgs, tiles, tagalong):
# def _radec2kd(ra, dec):
# def _radec2xyz(ra, dec):
# def _kd_query_radec(kd, ra, dec, radius_deg):
# 
# Path: py/fiberassign/hardware.py
# def expand_closed_curve(xx, yy, margin):
# def load_hardware(focalplane=None, rundate=None,
#                   add_margins={}):
#     def get_exclusions(exclname):
# def radec2xy(hw, tile_ra, tile_dec, tile_obstime, tile_obstheta, tile_obsha,
#              ra, dec, use_cs5, threads=0):
# def xy2radec(hw, tile_ra, tile_dec, tile_obstime, tile_obstheta, tile_obsha,
#              x, y, use_cs5, threads=0):
# def xy2cs5(x, y):
# def cs52xy(x, y):
#     N = len(xx)


import os
import re
import numpy as np
import multiprocessing as mp
import fitsio
import desimodel.focalplane
from astropy import units
from astropy.coordinates import SkyCoord
from multiprocessing.sharedctypes import RawArray
from functools import partial
from collections import OrderedDict
from types import SimpleNamespace
from desiutil.depend import add_dependencies, setdep
from desiutil.dust import SFDMap
from desitarget.targetmask import desi_mask
from ._version import __version__
from .utils import Logger, Timer, default_mp_proc, GlobalTimers
from .targets import (TARGET_TYPE_SCIENCE, TARGET_TYPE_SKY, TARGET_TYPE_SUPPSKY,
                      TARGET_TYPE_STANDARD, TARGET_TYPE_SAFE, desi_target_type,
                      default_target_masks, default_survey_target_masks)
from .hardware import (FIBER_STATE_UNASSIGNED, FIBER_STATE_STUCK,
                       FIBER_STATE_BROKEN, FIBER_STATE_RESTRICT,
                       radec2xy, xy2radec, xy2cs5)
from ._internal import Assignment
            )
            print_counts('After assigning %ssky per-slitblock: ' % tag)

            # Then assign using the petal requirement, because it may(should) require
            # more fibers overall.
            asgn.assign_unused(
                ttype, sky_per_petal, -1, "POS",
                start_tile, stop_tile
            )
            print_counts('After assigning %ssky per-petal: ' % tag)
        else:
            asgn.assign_unused(
                ttype, sky_per_petal, sky_per_slitblock, "POS",
                start_tile, stop_tile
            )
            print_counts('After assigning %ssky: ' % tag)

    # Assign sky to unused fibers, up to some limit
    gt.start("Assign unused fibers to sky")
    do_assign_unused_sky(TARGET_TYPE_SKY)
    gt.stop("Assign unused fibers to sky")

    # Assign suppsky to unused fibers, up to some limit
    gt.start("Assign unused fibers to supp_sky")
    do_assign_unused_sky(TARGET_TYPE_SUPPSKY, supp=True)
    gt.stop("Assign unused fibers to supp_sky")

    # Force assignment if needed
    gt.start("Force assignment of sufficient standards")
    asgn.assign_force(

# Path: py/fiberassign/_version.py

# 
# Path: py/fiberassign/utils.py
# def option_list(opts):
# def assert_isoformat_utc(time_str):
# def get_date_cutoff(datetype, cutoff_case):
# def get_svn_version(svn_dir):
# def get_last_line(fn):
# def read_ecsv_keys(fn):
# 
# Path: py/fiberassign/targets.py
# class TargetTagalong(object):
#     def __init__(self, columns, outnames={}, aliases={}):
#     def get_default(self, column):
#     def get_output_name(self, column):
#     def add_data(self, targetids, tabledata, fake={}):
#     def set_data(self, targetids, tabledata):
#     def get_for_ids(self, targetids, names):
# def create_tagalong(plate_radec=True):
# def str_to_target_type(input):
# def default_main_sciencemask():
# def default_main_stdmask(rundate=None):
# def default_main_gaia_stdmask():
# def default_main_skymask():
# def default_main_suppskymask():
# def default_main_safemask():
# def default_main_excludemask():
# def default_sv3_sciencemask():
# def default_sv3_stdmask():
# def default_sv3_skymask():
# def default_sv3_suppskymask():
# def default_sv3_safemask():
# def default_sv3_excludemask():
# def default_sv2_sciencemask():
# def default_sv2_stdmask():
# def default_sv2_skymask():
# def default_sv2_suppskymask():
# def default_sv2_safemask():
# def default_sv2_excludemask():
# def default_sv1_sciencemask():
# def default_sv1_stdmask():
# def default_sv1_skymask():
# def default_sv1_suppskymask():
# def default_sv1_safemask():
# def default_sv1_excludemask():
# def default_cmx_sciencemask():
# def default_cmx_stdmask():
# def default_cmx_skymask():
# def default_cmx_suppskymask():
# def default_cmx_safemask():
# def default_cmx_excludemask():
# def desi_target_type(desi_target, mws_target, sciencemask, stdmask,
#                      skymask, suppskymask, safemask, excludemask, gaia_stdmask):
# def default_survey_target_masks(survey, rundate=None):
# def default_target_masks(data, rundate=None):
# def append_target_table(tgs, tagalong, tgdata, survey, typeforce, typecol,
#                         sciencemask,
#                         stdmask, skymask, suppskymask, safemask, excludemask, gaia_stdmask):
# def load_target_table(tgs, tagalong, tgdata, survey=None, typeforce=None, typecol=None,
#                       sciencemask=None, stdmask=None, skymask=None,
#                       suppskymask=None, safemask=None, excludemask=None, gaia_stdmask=None,
#                       rundate=None):
# def load_target_file(tgs, tagalong, tfile, survey=None, typeforce=None, typecol=None,
#                      sciencemask=None, stdmask=None, skymask=None,
#                      suppskymask=None, safemask=None, excludemask=None,
#                      rowbuffer=1000000, gaia_stdmask=None, rundate=None):
# def targets_in_tiles(hw, tgs, tiles, tagalong):
# def _radec2kd(ra, dec):
# def _radec2xyz(ra, dec):
# def _kd_query_radec(kd, ra, dec, radius_deg):
# 
# Path: py/fiberassign/hardware.py
# def expand_closed_curve(xx, yy, margin):
# def load_hardware(focalplane=None, rundate=None,
#                   add_margins={}):
#     def get_exclusions(exclname):
# def radec2xy(hw, tile_ra, tile_dec, tile_obstime, tile_obstheta, tile_obsha,
#              ra, dec, use_cs5, threads=0):
# def xy2radec(hw, tile_ra, tile_dec, tile_obstime, tile_obstheta, tile_obsha,
#              x, y, use_cs5, threads=0):
# def xy2cs5(x, y):
# def cs52xy(x, y):
#     N = len(xx)


# Path: py/fiberassign/assign.py
import os
import re
import numpy as np
import multiprocessing as mp
import fitsio
import desimodel.focalplane
from astropy import units
from astropy.coordinates import SkyCoord
from multiprocessing.sharedctypes import RawArray
from functools import partial
from collections import OrderedDict
from types import SimpleNamespace
from desiutil.depend import add_dependencies, setdep
from desiutil.dust import SFDMap
from desitarget.targetmask import desi_mask
from ._version import __version__
from .utils import Logger, Timer, default_mp_proc, GlobalTimers
from .targets import (TARGET_TYPE_SCIENCE, TARGET_TYPE_SKY, TARGET_TYPE_SUPPSKY,
                      TARGET_TYPE_STANDARD, TARGET_TYPE_SAFE, desi_target_type,
                      default_target_masks, default_survey_target_masks)
from .hardware import (FIBER_STATE_UNASSIGNED, FIBER_STATE_STUCK,
                       FIBER_STATE_BROKEN, FIBER_STATE_RESTRICT,
                       radec2xy, xy2radec, xy2cs5)
from ._internal import Assignment

            )
            print_counts('After assigning %ssky per-slitblock: ' % tag)

            # Then assign using the petal requirement, because it may(should) require
            # more fibers overall.
            asgn.assign_unused(
                ttype, sky_per_petal, -1, "POS",
                start_tile, stop_tile
            )
            print_counts('After assigning %ssky per-petal: ' % tag)
        else:
            asgn.assign_unused(
                ttype, sky_per_petal, sky_per_slitblock, "POS",
                start_tile, stop_tile
            )
            print_counts('After assigning %ssky: ' % tag)

    # Assign sky to unused fibers, up to some limit
    gt.start("Assign unused fibers to sky")
    do_assign_unused_sky(TARGET_TYPE_SKY)
    gt.stop("Assign unused fibers to sky")

    # Assign suppsky to unused fibers, up to some limit
    gt.start("Assign unused fibers to supp_sky")
    do_assign_unused_sky(TARGET_TYPE_SUPPSKY, supp=True)
    gt.stop("Assign unused fibers to supp_sky")

    # Force assignment if needed
    gt.start("Force assignment of sufficient standards")
    asgn.assign_force(

        TARGET_TYPE_STANDARD, std_per_petal, -1, start_tile, stop_tile