pro ecrpos,xmin,xmax,idtms,ihdeb,ihfin,idate, $
 chfreq,chres,nbrec,chrec_cle, $
 nomfich2,npos,nbmaxpt,itete,trp
;+
; NAME:
;	ECRPOS
; PURPOSE:
;	ecrit au format HELIO un fichier de type F(TIME)
;	- un fichier de positions associees 1D  ou
;	- un fichier de positions associees 2D  ou
;	- un fichier de flux 
;
; CALLING SEQUENCE:
;	ECRPOS, xmin,xmax,idtms,ihdeb,ihfin,idate, $
;       chfreq,chres,nbrec,chrec_cle, $
;       nomfich2,npos,nbmaxpt,itete,trp
;
; INPUTS:
;    xmin,xmax	: min et max des amplitudes    		FLOAT
;    idtms	: delta t entre 2 images en ms          LONG
;    ihdeb,ihfin	: heure debut et fin en ms              LONG
;    idate	: date en caracteres			STRING
;    chfreq	: frequence issue du descripteur 164.0 MHZ	STRING
;    chres	: reseau        "    EST-OUEST ou NORD-SUD	STRING
;    nbrec	: nb de records par position                    INT
;    chrec_cle  : tableau de chaines decrivant le type de chacun des record
;                 d'une position                        STRARR(nbrec)
;    nomfich2	: nom du fichier de positions a ecrire		STRING
;    npos	: nb de positions				INT
;    nbmaxpt	: nb de points de la position la plus longue	LONG
;    itete(3,npos)	: entete de chaque enrgt		LONG(3)
;			   1- num de la position
;			   2- heure de debut de la position
;			   3- nb points de la position 
;    trp(nbrec,nbmaxpt,npos): tableau des positions	FLOAT(nbrec,*,*)
;
; pos1D:	0- indice des images ds le fichier initial
;		1- canal	  
;		2- amplitude -i ou v	 
;		3- flux i ou v		 
;		4- diametre
;
; pos2D:	0- position en X
;		1- position en Y
;		2- flux  ew
;		3- flux  ns
;		4- diametre ew
;		5- diametre ns
;		
; flux:         0- indice
;		1- canal central
;		2- flux_i
;		3- flux_v
;		4- largeur
;
;    (pour les positions comportant moins de nbmaxpt points, les 5(6) tableaux
;     sont completes par des zeros) 
;
; OUTPUTS:
; CALLS:
;	ECRDESCD
; MODIFICATION HISTORY: (bonmartin@obspm.fr)
;	adapte du logiciel XHELIO (20/11/98) (JB)
;	ecrit par A. Bouteille (juin 1996)
;	modif dec 1996 : generalisation a l'ecriture de fichiers positions 1D
;       2D non polar et polar, et flux
;-
mot_cle=strarr(200)
valcle=strarr(200)
comment=strarr(200)
;
;
;	DESCRIPTEUR HELIO
;
;	INTEGER*4 NBLOCS,ENTET,NAXES,NPIX(5),RE,RI(5),NORIG(5),NDELTA(5),
;     -		DATE(3),HDEB,HFIN,NREG,FACBLOC(5)
;	REAL*4    ORIG(5),DELTA(5),VMIN,VMAX,REG
;	INTEGER*4 TAB(64)
;
;	EQUIVALENCE 
;     -		(TAB(1),NBLOCS),     ! NBE DE BLOCS DU DESCRIPTEUR
;     -		(TAB(2),ENTET),      ! NOMBRE DE VALEURS DE L'ENTETE
;     -		(TAB(3),RE),         ! ENTETE D'IMAGE ENTIERE OU REELLE
;     -		(TAB(4),NAXES),      ! NBE D'AXES D'UNE IMAGE
;     -		(TAB(5),NPIX(1)),    ! NBE DE PIXELS SUR LES AXES
;     -		(TAB(10),RI(1)),     ! ORIG ET DELTA REEL (0) OU ENTIER (1)
;     -		(TAB(15),ORIG(1)),   ! VALEUR DE L'ORIGINE SUR LES AXES REELS
;     -		(TAB(20),DELTA(1)),  ! INCREMENT ENTRE CHAQUE PIXEL AXES REELS
;     -		(TAB(15),NORIG(1)),  ! VALEUR DE L'ORIGINE SUR LES AXES ENTIERS
;    -		(TAB(20),NDELTA(1)), ! INCREMENT ENTRE CHAQUE PIXEL AXES ENTIERS
;     -		(TAB(25),DATE(1)),   ! DATE DU FICHIER EN JJ/MM/AA
;     -		(TAB(28),HDEB),      ! HEURE DE DEBUT EN MS
;     -		(TAB(29),HFIN),      ! HEURE DE FIN EN MS
;     -		(TAB(30),VMIN),      ! VALEUR MINIMUM DU FICHIER
;     -		(TAB(31),VMAX),      ! VALEUR MAXIMUM DU FICHIER
;     -		(TAB(32),NREG),      ! SI LE DERNIER AXE EST IRREGULIER
;     -          (TAB(32),REG),        ! INCREMENT QUI PEUT LE RECONSTITUER
;     -		(TAB(33),FACBLOC(1)) !FACTEUR DE BLOCAGE PPOUR UN AXE DONNE
;
;		BLOC COMMUN A TOUT UN PROGRAMME
;
;  Si RI(n) est egal a 0, on se sert de ORIG(n) et de DELTA(n) et si RI(n) est 
;  egal a 1, on se sert de NORIG(n) et de NDELTA(n).
;
;  Si NDELTA(n) est egal a 0 pour le dernier axe, on sait que celui-ci est
;  irregulierement echantillonne et dans ce cas la description de chaque
;  pixel se fait en tete d'image, dans la zone reservee par ENTET=Nbe de 
;  donnees (INTEGER*4 ou REAL*4) qui se trouve dans chaque image avant les
;  pixels de l'image
;
orig=fltarr(5)     
delta=fltarr(5)    
norig=lonarr(5)    
ndelta=lonarr(5)   
;
dfixe={dfixe,               $
	nblocs:0L,          $
	entet:0L,           $
	re:0L,              $
	naxes:0L,           $
	npix:lonarr(5),     $
	ri:lonarr(5),       $
	ori:bytarr(4,5),    $
	del:bytarr(4,5),    $
	date:lonarr(3),     $
	hdeb:0L,            $
	hfin:0L,            $
	vmin:0.0,           $
	vmax:0.0,           $  
	breg:bytarr(4),     $
	facbloc:lonarr(5),  $
	tab:lonarr(64-37)}
; 

; descripteur fixe 
	dfixe.re=1L	; entete d'image entiere
	dfixe.naxes=2L		
	dfixe.entet=3L
	LONG2=dfixe.ENTET+NBMAXPT

; OUVERTURE DU FICHIER DE SORTIE
	openw,iu,NOMFICH2,/get_lun

;	Remplissage du descripteur
;
	dfixe.date=idate
	dfixe.NPIX(0)=NBMAXPT
	dfixe.npix(1)=nbrec*npos
	dfixe.VMIN=XMIN
	dfixe.VMAX=XMAX
	DELTA(1)=1.
	orig(1)=1.
	dfixe.breg(*)=byte(long(0.),0,4)
	dfixe.RE=1.
	dfixe.HDEB=ihdeb
	dfixe.hFIN=ihfin
	dfixe.RI(0)=1.
	dfixe.RI(1)=0.
	Ndelta(0)=idtms
	NORIG(0)=IHDEB
	dfixe.facbloc(0)=1L
	dfixe.FACBLOC(1)=nbrec
; codage de reel ou entier suivant ri
    for k=0,dfixe.naxes-1 do begin
        if dfixe.ri(k) eq 0 then begin
	    dfixe.ori(*,k)=byte(orig(k),0,4)
	    dfixe.del(*,k)=byte(delta(k),0,4)
	end else begin
	    dfixe.ori(*,k)=byte(norig(k),0,4)
	    dfixe.del(*,k)=byte(ndelta(k),0,4)
	endelse
    endfor
;   PARTIE CARACTERE
;stop
        mot_cle(0:nbrec-1)='BLOC'+string(format='(i1)',indgen(nbrec))
        valcle(0:nbrec-1)=chrec_cle

 	MOT_CLE(nbrec)='INSTRUMENT'
	VALCLE(nbrec)='RH NANCAY'
	MOT_CLE(nbrec+1)='FREQ'
	VALCLE(nbrec+1)=chfreq
	MOT_CLE(nbrec+2)='LABEL'
	VALCLE(nbrec+2)=chres
	MOT_CLE(nbrec+3)='ENTETDESC1'
	VALCLE(nbrec+3)=' POSITION'
	MOT_CLE(nbrec+4)='ENTETTYP1'
	VALCLE(nbrec+4)='INTEGER*4'
	mot_cle(nbrec+5)='ENTETDESC2'
	valcle(nbrec+5)='HEURE'
	MOT_CLE(nbrec+6)='ENTETTYP2'
	VALCLE(nbrec+6)='INTEGER*4'
	mot_cle(nbrec+7)='ENTETDESC3'
	valcle(nbrec+7)='NOMBRE POINTS'
	MOT_CLE(nbrec+8)='ENTETTYP3'
	VALCLE(nbrec+8)='INTEGER*4'
	MOT_CLE(nbrec+9)='ENTETUNIT2'
	VALCLE(nbrec+9)='MS'
	MOT_CLE(nbrec+10)='FICHIER'
; recherche du / pour UNIX
	n1=0
	nc=0
	while (nc ne -1) do begin
	   nc=strpos(nomfich2,'/',n1)

	   if (nc ne -1) then n1=nc+1
	endwhile
	length=strlen(nomfich2)
	valcle(nbrec+10)=strmid(nomfich2,n1,length)

	mot_cle(nbrec+11)='AXE1'
	valcle(nbrec+11)='TEMPS'

	INDICE=nbrec+11
	for ik=1,npos do begin
	   MOT_CLE(indice+ik)='NUMERO DE POSITION'
	   IF(ik LT 10)THEN begin
	      chari1=string(format='(i1)',ik)
	      valcle(INDICE+ik)='POSITION'+CHARI1
	   end ELSE begin
	      chari2=string(format='(i2)',ik)
	      valcle(INDICE+ik)='POSITION'+CHARI2
	   ENDelse
	endfor
	INDICE=INDICE+npos+1
	mot_cle(INDICE)='END'
	ecrdescd,iu,long2,dfixe,mot_cle,valcle,comment

;	
; ECRITURE DANS LE FICHIER FORMAT HELIO
	for ip=0,npos-1 do for ji=0,nbrec-1 do begin
	   writeu,iu,itete(*,ip),TRP(ji,*,ip)
	endfor
	close,iu
        free_lun,iu

	END



