pro fll_grid, dlong, dlat, label = label, charsize = charsize, $
              max_elongation = max_elongation, _extra = _extra
;+
; NAME:
;	FLL_GRID
;
;
; PURPOSE:
;	Ad a latitude/longitude grid to a fish-eye image
;
;
; CATEGORY:
;	Utils
;
;
; CALLING SEQUENCE:
;	fll_grid[, dlong, dlat]
;
;
; OPTIONAL INPUTS:
;	dlong	int	The step in longitude
;	dlat	int	The step in latitude
;
;
; KEYWORD PARAMETERS:
;	/label			If set, then label the grid.
;	charsize	Float	Set a size for the labels (ignored if
;				label is not set.
;	max_elongation	float	Set the largest elongation to show
;				(default=135)
;	Any key accepted by PLOTS will be passed through.
;
;
; RESTRICTIONS:
;	Steps will be rounded to the nearest whole degree
;
;
; MODIFICATION HISTORY:
;	Original: 13/2/04; SJT
;	Add labelling: 16/2/04; SJT
;-

if n_params() ne 2 then dlat = 30 else dlat = round(dlat)
if n_params() eq 0 then dlong = 30 else dlong = round(dlong)
if n_elements(max_elongation) eq 0 then max_elongation = 135

lng = findgen(361)-180
for lat = -90, 90, dlat do begin
    wcssph2xy, lng, replicate(lat, 361), x, y, 5, crval = [0, 0]
    el = sqrt(x^2+y^2)
    locs = where(el lt max_elongation, nvp)
    if nvp ne 0 then plots, x[locs], y[locs], _extra = _extra
endfor

lat = findgen(181)-90
for lng = -180, 180, dlong do begin
    wcssph2xy, replicate(lng, 181), lat, x, y, 5, crval = [0, 0]
    el = sqrt(x^2+y^2)
    locs = where(el lt max_elongation, nvp)
    if nvp ne 0 then begin
        lc1 = where(locs+1 ne locs[1:*], nseg)
        if nseg ne 0 then lc1 = [0, lc1+1, nvp] $
        else lc1 = [0, nvp]
        for j = 0, nseg do plots, x[locs[lc1[j]:lc1[j+1]-1]], $
          y[locs[lc1[j]:lc1[j+1]-1]], _extra = _extra
    endif
endfor

if keyword_set(label) then begin
    dy = .5
    for lng = -180, 180, dlong do begin
        wcssph2xy, lng, 0.0, x, y, 5, crval = [0, 0]
        el = sqrt(x^2+y^2)
        if el gt max_elongation then continue
        xyouts, x, y+dy, string(lng, format = "(I0)"), align = 0.5, $
                                charsize = charsize
    endfor

    dx = 0.5
    for lat = -90, 90, dlat do begin
        wcssph2xy, 0.0, lat, x, y, 5, crval = [0, 0]
        el = sqrt(x^2+y^2)
        if el gt max_elongation then continue
        xyouts, x+dx, y, string(lat, format = "(I0)"), charsize = $
                                charsize
    endfor
endif

end
