;+
; NAME:
;	MAKE_DIPOLES
; PURPOSE:
;	Given a magnetogram (in either image or heliographic coords)
; 	create a collection of dipoles that mimics the surface
;	field
; USAGE:
;	make_dipoles,p,ploc,in=in,ihdr=ihdr,lstep=lstep,lat=lat,lon=lon
; KEYWORD PARAMETERS:
;	in	The image to use
;	ihdr	The image header
;	lstep	The lat/lon stepsize to use
;	lat	A range of latitudes
;	lon	A range of longitudes
; OUTPUT PARAMTERS:
;	p	The dipoles
;	ploc	The locations of the dipoles
; AUTHOR:
;	Craig DeForest
; HISTORY:
;	Written 17-Dec-1997
;-
pro make_dipoles,p,ploc,in=in,ihdr=ihdr,lstep=lstep,lat=lat,lon=lon
if not isvalid(lstep) then lstep=0.2
if not isvalid(lon) then lon=[60,80]
if not isvalid(lat) then lat=[-45,45]

if ihdr.CTYPE1 ne "degrees-longitude" then begin
	zheliographinize,in=in,ihdr=ihdr,out=map,ohdr=mhdr,lstep=lstep,lat=lat,lon=lon
end else begin
	m1hdr = ihdr
	ok = zcheck_hdr(ihdr,/fix)
	m1hdr.CRPIX1 = 1
	m1hdr.CRPIX2 = 1
	m1hdr.CRVAL1 = lon(0)
	m1hdr.CRVAL2 = lat(0)
	m1hdr.CDELT1 = lstep
	m1hdr.CDELT2 = lstep
	m1hdr.NAXIS1 = fix((lon(1)-lon(0))/lstep)
	m1hdr.NAXIS2 = fix((lat(1)-lat(0))/lstep)
	zmatch,in=in,ihdr=ihdr,out=map,ohdr=mhdr,tohdr=m1hdr
end

t0 = mhdr.DATE_OBS+' '+mhdr.TIME_OBS
coords = replicate(v4(t0,0,0,1.0,'timestamp','degrees','degrees','solar-radii','heliographic'),nlm(map))
coords.(1) = (lindgen(nlm(map)) mod mhdr.NAXIS1)*lstep+lon(0)
coords.(2) = (lindgen(nlm(map))  /  mhdr.NAXIS2)*lstep+lat(0)

dipoles = coords
dipoles.(3) = map(*)

plocv4 = v4xform(coords,'HC')
pv4 = v4xform(dipoles,'HC')

p    = transpose([[pv4.(1)],[pv4.(2)],[pv4.(3)]])
ploc = transpose([[plocv4.(1)],[plocv4.(2)],[plocv4.(3)]])

end