pro GRIFO_E_Data_Reader, $
      SIG_SUM_I_v, SIG_SUM_Q_v, $
      SIG_GUARD_I_v, SIG_GUARD_Q_v, $
      SIG_DAZ_I_v, SIG_DAZ_Q_v, $
      SIG_DEL_I_v, SIG_DEL_Q_v, $
      header_in_v, header_out_v, $
      n_batch, MTI_SUM_OUT_v                                               ; AAF solo quando c'è il segnale in uscita al mtifft

  ;--Opzioni di compilazione--
  COMPILE_OPT STRICTARR
  
  COMMON PCRDV_info_c , PCRDV_info , mat_data_intern_var_list , segnale , puntatore , vett_info_signal
  

  DSRM_LIB

  filename = dialog_pickfile()

  ;Dimensione file e calcolo parole a 32-bit
  file_size_byte = (file_info(filename)).size
  vector  = LONARR(file_size_byte/4L) ;lettura word da 32-bit

  openr, lun, filename, /get_lun
  point_lun, lun, 0
  readu, lun, vector

  ;################################################
  
;  DEVICE, Get_Screen_Size = screen_size
;  
;  ; Lettura della dimensione del file scelto (in Mbyte)
;  file_size = (FILE_INFO(filename)).size / 1024 / 1024
;
;;  file_size = LONG(file_size * 10000) / 10000.
;
;  ;...si richiede la dimensione da leggere solo se il file è più di 10 MByte...
;  start_reading_mb = 0.
;  stop_reading_mb = 1. * file_size
;  IF N_ELEMENTS(SET_DIM_MB) EQ 0 THEN $
;    BEGIN
;
;    IF (file_size GE 10) THEN $
;      BEGIN
;      min_start = start_reading_mb
;      max_start = stop_reading_mb
;      min_interval_to_read = 1 ; MByte
;      titolo_menu = 'SELEZIONE PORZIONE DA RIDURRE'
;      start_label = 'Inizio riduzione [MB]: '
;      stop_label  = '  Fine riduzione [MB]: '
;      interval_label = 'MByte da ridurre: '
;      x_offset = ROUND(screen_size[0] / 2) - 300
;      y_offset = ROUND(screen_size[1] / 2) - 50
;
;      sliding_selection , $
;        ;-Input------------------
;        min_start ,            $
;        max_start ,            $
;        min_interval_to_read , $
;        titolo_menu ,          $
;        start_label ,          $
;        stop_label ,           $
;        x_offset = x_offset ,  $
;        y_offset = y_offset ,  $
;        interval_label ,       $
;        ;------------------------
;        ;-Input/Output-----------
;        start_reading_mb ,     $
;        stop_reading_mb ,      $
;        ;------------------------
;        ;-Output-----------------
;        interval_conf
;      ;------------------------
;    ENDIF $
;    ELSE BEGIN
;      interval_conf = 1
;    ENDELSE
;  END ELSE $
;    BEGIN
;    stop_reading_mb = stop_reading_mb < SET_DIM_MB
;    interval_conf = 1
;  END

  ;##################################################
  
;  new_file_size_byte = (stop_reading_mb - start_reading_mb)*1024*1024
;  vector  = LONARR(new_file_size_byte/4L) ;lettura word da 32-bit
;  
;  openr, lun, filename, /get_lun
;  point_lun, lun, start_reading_mb
;  readu, lun, vector


;;;;;;;;;;;;;;;;;;;;;;;;; GR - 24-5-2022   lettura blocco dati FW  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  MARKER_BLOCK_FW = LONG('7A7A7A7A'x)

  idxBloks_fw = long64(where(vector EQ MARKER_BLOCK_FW, Num_blocks_fw))     ; gli step successivi entrano in un IF che controlla se (Num_blocks_fw > 0), ossia se sono stati trovati blocchi dati FW exp, pc, det
  
  if Num_blocks_fw GT 0 then begin
    
      ptrInput_fw  = where( (idxBloks_fw-shift(idxBloks_fw,1)) eq 1 ) - 1        ; serve a trovare il secondo pezzo "7A7A7A7A" consecutivo (in qs modo sono sicuro di aver trovato un nuovo blocco dati fw)
      ixBlocks_fw   = idxBloks_fw[ptrInput_fw]
      num_Blocks_fw = n_elements(ixBlocks_fw)     ; ---> numero di blocchi dati trovati
      
      
      NAME_BLOCK_V_FW = lonarr(num_Blocks_fw)
      NAME_BLOCK_V_FW = vector[ixBlocks_fw+2]      ; nome blocco si trova 2 word ( da 4byte ciascuna) dopo il marker
      
      index_exp = where(NAME_BLOCK_V_FW EQ 5265477, n_blocchi_exp)
      index_pc = where(NAME_BLOCK_V_FW EQ 17232, n_blocchi_pc)
      index_det = where(NAME_BLOCK_V_FW EQ 5522756, n_blocchi_det)
      
      index_exp_pc_det = where((NAME_BLOCK_V_FW EQ 5265477) OR (NAME_BLOCK_V_FW EQ 17232) OR (NAME_BLOCK_V_FW EQ 5522756) , n_blocchi_exp_pc_det)
      
      
    ;  idx_first_exp = ixBlocks_fw[index_exp[0]]
    ;  idx_first_pc = ixBlocks_fw[index_pc[0]]
    ;  idx_first_det = ixBlocks_fw[index_det[0]]
      
      idx_first_exp_pc_det = ixBlocks_fw[index_exp_pc_det[0]]
    
    
       det_exp_pc_fw_gen_basic, detector_raw_if_t, expander_raw_if_t, pc_raw_if_t
       
        
       
    ;    num_block_exp =  shift([index_exp,num_Blocks_fw],-1) - [index_exp,num_Blocks_fw]
    ;    num_block_exp = num_block_exp[0:n_blocchi_exp-1]
    ;    
    ;    num_block_pc =  shift([index_pc,num_Blocks_fw],-1) - [index_pc,num_Blocks_fw]
    ;    num_block_pc = num_block_pc[0:n_blocchi_pc-1]
    ;    
    ;    num_block_det =  shift([index_det,num_Blocks_fw],-1) - [index_det,num_Blocks_fw]
    ;    num_block_det = num_block_det[0:n_blocchi_det-1]
        
        ;;;;;;;;;;;;;Identifico il numero di blocchi dati FW (exp, pc, det) presenti per ogni batch  ;;;;;;;;
        num_block_exp_pc_det =  shift([index_exp_pc_det,num_Blocks_fw],-1) - [index_exp_pc_det,num_Blocks_fw]
        num_block_exp_pc_det = num_block_exp_pc_det[0:n_blocchi_exp_pc_det-1]
  
  endif
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;    fine lettura blocco dati fw  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  
  MARKER_BLOCK = LONG('5A5A5A5A'x)

  idxBloks = long64(where(vector EQ MARKER_BLOCK, Num_blocks))
  ptrInput  = where( (idxBloks-shift(idxBloks,1)) eq 1 ) - 1        ; serve a trovare il secondo pezzo "5A5A5A5A" consecutivo (in qs modo sono sicuro di aver trovato un nuovo blocco dati)
  ixBlocks   = idxBloks[ptrInput]
  num_Blocks = n_elements(ixBlocks)     ; ---> numero di blocchi dati trovati
  
  
  NAME_BLOCK_V = lonarr(num_Blocks)
  NAME_BLOCK_V = vector[ixBlocks+17]      ; nome blocco si trova 17 word ( da 4byte ciascuna) dopo il marker

  ;Identifico il numero di batch, che corrisponde al numero di header presenti
  index_hdr = where(NAME_BLOCK_V EQ 1213223748L, n_batch)
  

  ;Definizione Header
  idx_first_header = ixBlocks[index_hdr[0]]
  
  ;Controllo REV_ID
;  IF (vector[idx_first_header+37] EQ 2) THEN header_gen_v2, header ELSE header_gen, header
;  IF (vector[idx_first_header+37] EQ 2) THEN header_out_gen, header ELSE header_gen, header
  
  hdr_version = 0
  
  CASE (vector[idx_first_header+37]) OF
    1 : BEGIN
      header_gen, header
    END
    2 : BEGIN
      header_out_gen, header
    END
    3 : BEGIN
      header_out_gen_v3, header
      hdr_version = 3
      header_in = header
    END   
    4 : BEGIN
      header_out_gen_v4, header, header_in
    END
  ENDCASE
  
  header_out =  header
  dim_header = n_elements(struct2long(header))
;  idx_first_header = ixBlocks[index_hdr[0]]
  long2struct , vector[idx_first_header:idx_first_header+dim_header-1] , header
  
  NPRI_v = header.header_data.signal_descr.PACKETDESCR.npri
  NRBIN_v = header.header_data.signal_descr.PACKETDESCR.nrbin
  
  batch_id = 0
  
  sum = 0
  guard = 0
  daz = 0
  del = 0      

  ;Leggo i dati per ogni blocco e identifico la tipologia
  NAME_BLOCK_V = lonarr(num_Blocks)
;  FOR i=0, num_Blocks-1 DO BEGIN
;    INDEX_BLOCK = ixBlocks[i]
;    ;long2struct , vector[INDEX_BLOCK:INDEX_BLOCK+n_elements(struct2long(header.header_sw)-1)] , header.header_sw
;    ;Definisce il tipo di Blocco (header, segnale_somma, segnale_guardia, segnale_delta )
;    NAME_BLOCK_V[i] = header.header_sw.header_sw_chank.NOME_1
;  ENDFOR
  NAME_BLOCK_V = vector[ixBlocks+17]

  ;Identifico il numero di batch, che corrisponde al numero di header presenti
  index_hdr = where(((NAME_BLOCK_V EQ 1213223748L) AND (vector[ixBlocks+18] EQ 1313428036L)) OR (NAME_BLOCK_V EQ 1212956995L), n_batch)       ; individuo gli indici relativi agli header IN
  index_hdr_out = where((NAME_BLOCK_V EQ 1213223748L) AND (vector[ixBlocks+18] EQ 1431261764), n_hdr_out)       ; individuo gli indici relativi agli header OUT
    
  index_stt_laa = where(NAME_BLOCK_V EQ 1599362131 AND vector[ixBlocks+18] EQ 4276556L , n_stt)
  IF (n_stt NE 0) then NAME_BLOCK_V[index_stt_laa] = 30000000
  
  index_mtt_laa = where(NAME_BLOCK_V EQ 1599362125 AND vector[ixBlocks+18] EQ 4276556L , n_mtt)
  IF (n_mtt NE 0) then NAME_BLOCK_V[index_mtt_laa] = 40000000
  
  index_stt_int = where(NAME_BLOCK_V EQ 1599362131 AND vector[ixBlocks+18] EQ 5525065L , n_stt)
  IF (n_stt NE 0) then NAME_BLOCK_V[index_stt_int] = 50000000
  
  ;IF (n_hdr_out GT 0) AND (hdr_version EQ 3) THEN header_out_gen_v3, header_out  ELSE header_out_gen, header_out
  ;IF (hdr_version EQ 3) THEN header_out_gen_v3, header_out  ELSE header_out_gen, header_out
  
  STT_HEADER_GEN, LOCAL_A_A_TRACKING_DATA, STT_HEADER
  STT_RECORD_GEN, STT_DATA
  
    
  
  index_det_out = where(NAME_BLOCK_V EQ 5522756, n_det)       ; serve nel caso in cui registro solo uscita detector 
  
  NAME_BLOCK_V[index_hdr_out] = 1431261764      ; assegno un marker relativo all'HEADER OUT diverso altrimenti coinciderebbe con quello relativo all'header in (assegno lo stesso numero corrispondente alla parola OUT)
                                                ; questo marker servirà a gestire L'HEADER OUT (DSPHDROUT) in un case ad hoc (v. sotto)
                                                
;  NAME_BLOCK_V[index_hdr] = 1313428036L         ; nuovo marker relativo a HEADER IN    ---PROVA---

  IF n_batch GT 0 then begin
  	;Identifico il numero di blocchi di segnale presenti per ogni batch
  	num_block_sig_v =  shift([index_hdr,num_Blocks],-1) - [index_hdr,num_Blocks]
    num_block_sig_v = num_block_sig_v[0:n_batch-1]
 	
 	  header_in_v =  REPLICATE(header_in.header_data,n_batch)
 	  IF n_hdr_out GT 0 THEN header_out_v =  REPLICATE(header_out.header_data,n_batch)
 	  
 	  SIG_SUM_I_v = list()
 	  SIG_SUM_Q_v = list()
 	  
 	  SIG_GUARD_I_v = list()
 	  SIG_GUARD_Q_v = list()
 	  
 	  SIG_DAZ_I_v = list()
 	  SIG_DAZ_Q_v = list()
 	  
 	  SIG_DEL_I_v = list()
 	  SIG_DEL_Q_v = list()

 	  
 	  MTI_SUM_OUT_v = list()
 	  
 	  SIG_DET_I_v = list()
 	  SIG_DET_Q_v = list()
; 	  
; 	  SIG_GUARD_I_v = fltarr(NRBIN_v, NPRI_v, n_batch)
; 	  SIG_GUARD_Q_v = fltarr(NRBIN_v, NPRI_v, n_batch)
; 	  
; 	  SIG_DAZ_I_v = fltarr(NRBIN_v, NPRI_v, n_batch)
; 	  SIG_DAZ_Q_v = fltarr(NRBIN_v, NPRI_v, n_batch)
; 	  
; 	  SIG_DEL_I_v = fltarr(NRBIN_v, NPRI_v, n_batch)
; 	  SIG_DEL_Q_v = fltarr(NRBIN_v, NPRI_v, n_batch)

	  ;Lettura, header e segnali
	 
	  n_batch_read = 0
	  ant_prec = 0.
	  
	  det = 0
	  
	  start_batch = 0
	  stop_batch = n_batch-1
	  n_batch = 4200 < n_batch
	  
	  batch_input = { start_batch : 0, stop_batch : n_batch-1}
	  
	  cambia_dati_struttura, batch_input
	  
	  start_batch = batch_input.start_batch
	  stop_batch = batch_input.stop_batch
	  n_batch=stop_batch-start_batch+1
	  
	  conta_blocchi_fw = 0         ; viene usato per incrementare l'indice del blocco dati fw da leggere volta x volta
	  kk = 0                 ; mi serve a contare i batch "validi", ossia quelli per cui c'è segnale su almeno un canale
	  
	  indici_batch_senza_segnale = !null    ; se trovo un batch senza segnale su almeno un canale ne memorizzo l'indice (poi verrà scartato)
	  indici_batch_non_validi = !null       ; batch che non rispettano determinate condizioni (compatibilità tra HDR IN e HDR OUT) o per i quali manca HDR OUT  

    n_batch_selected = stop_batch - start_batch + 1
    header_out_v =  REPLICATE(header_out.header_data,n_batch_selected)   
	  
	  FOR i=start_batch, stop_batch-1 DO BEGIN
;    FOR i=0, 60 DO BEGIN
	   n_ch = 0
	   signal = !null    ; serve x concatenare il vettore segnale canale x canale
	   
	   hdr_out_trovato = 0
	   
	   
	   cont_exp = 0
	   cont_det = 0
	   cont_pc = 0
	   
;	    if (i eq 202) then stop
	   
	   INDEX_BLOCK_hdr = ixBlocks[index_hdr[i]]
	  
     
	    FOR j=0, num_block_sig_v[i]-1 DO BEGIN
	     
;	    	print, i
	    	
	    	INDEX_BLOCK = ixBlocks[index_hdr[i]+j]
	    	;
	    	;print, header.header_data.header_info.MARKER_DATI_1, format='(z)'
	    	NAME_BLOCK = NAME_BLOCK_V[index_hdr[i]+j]
	    	BLOCK_SIZE = vector[INDEX_BLOCK+5]/4L
	    	BLOCK_DATA = vector[INDEX_BLOCK:INDEX_BLOCK+BLOCK_SIZE-1]
	    	
;	    	stop

	      	CASE (NAME_BLOCK) OF
	          ;Marker Header (DSPHDRIN)
	      		 1213223748 : BEGIN    
;            1313428036L : BEGIN     ;  prova x essere sicuro di discriminare il caso in cui è presente solo DSPHDRIN

	      				;long2struct , vector[INDEX_BLOCK:INDEX_BLOCK+n_elements(struct2long(header)-1)] , header
	      				point_lun, lun, INDEX_BLOCK*4L
	      				readu, lun, header_in
	      				N_PRI = header_in.header_data.signal_descr.PACKETDESCR.npri
	      				N_RBIN = header_in.header_data.signal_descr.PACKETDESCR.nrbin
	      				DIM_BATCH = N_PRI*N_RBIN
	      				HEADER_SIG = header_in
	      				header_in_v[i-start_batch] =  HEADER_SIG.header_data;header_out.header_data
	      				header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVAZ_RAD*!radeg
	      				header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVEL_RAD*!radeg
	      				header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYAZ_RAD*!radeg
	      				header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYEL_RAD*!radeg
	      				header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAAZ_RAD*!radeg
	      				header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAEL_RAD*!radeg
                header_sig.header_data.LOCAL_A_A_TRACKING_DATA = LOCAL_A_A_TRACKING_DATA
              
              point_lun, -lun, pos
              print, "trovato header in", pos
	      			END
	      			
	      			
	      		;;;;;;    gestione Marker Header OUT (DSPHDROUT)
	      		1431261764 : BEGIN
	      		  
	      		  	      		     		  
	      		  point_lun, lun, INDEX_BLOCK*4L
	      		  readu, lun, header_out
	      		  N_PRI_OUT = header_out.header_data.signal_descr.PACKETDESCR.npri
	      		  N_RBIN_OUT = header_out.header_data.signal_descr.PACKETDESCR.nrbin
	      		  DIM_BATCH_OUT = N_PRI*N_RBIN
;	      		  HEADER_SIG = header
              header_out = header_out
	      		  header_out_v[i-start_batch] =  header_out.header_data
;	      		  header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVAZ_RAD*!radeg
;	      		  header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVEL_RAD*!radeg
;	      		  header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYAZ_RAD*!radeg
;	      		  header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYEL_RAD*!radeg
;	      		  header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAAZ_RAD*!radeg
;	      		  header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAEL_RAD*!radeg
              
              hdr_out_trovato = 0

	      		  END
	      		  
	      		  1212956995L : BEGIN
	      		   
	      		   ;            1313428036L : BEGIN     ;  prova x essere sicuro di discriminare il caso in cui è presente solo DSPHDRIN

	      		    ;long2struct , vector[INDEX_BLOCK:INDEX_BLOCK+n_elements(struct2long(header)-1)] , header
	      		    point_lun, lun, INDEX_BLOCK*4L
	      		    readu, lun, header_out
	      		    N_PRI = header_out.header_data.signal_descr.PACKETDESCR.npri
	      		    N_RBIN = header_out.header_data.signal_descr.PACKETDESCR.nrbin
	      		    DIM_BATCH = N_PRI*N_RBIN
	      		    HEADER_SIG = header_out
	      		    header_out_v[i-start_batch] =  header_out.header_data
	      		    header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVAZ_RAD*!radeg
	      		    header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.NAVEL_RAD*!radeg
	      		    header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYAZ_RAD*!radeg
	      		    header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.BODYEL_RAD*!radeg
	      		    header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAAZ_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAAZ_RAD*!radeg
	      		    header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAEL_RAD = header_sig.header_data.GENERAL_SETTINGS.antenna.position.ANTENNAEL_RAD*!radeg
                

	      		    point_lun, -lun, pos
	      		    print, "trovato header in", pos
	      		   
	      		   
	      		  END
	      		  
	      		  
	      		  
	      		  
	      		  ;Marker Signal (HDROUT)
	      		  30000000 : BEGIN
	      		    ;print, "HDROUT"

	      		    TRACKING_DATA = { $
	      		      header_sw : header_out.header_sw, $
	      		      spare : lonarr(2), $
	      		      LOCAL_A_A_TRACKING_DATA : LOCAL_A_A_TRACKING_DATA }

	      		    point_lun, lun, INDEX_BLOCK*4L
	      		    readu, lun, TRACKING_DATA

;                TRACKING_DATA.LOCAL_A_A_TRACKING_DATA.DATA_FOR_THE_MOST_IMPORTANT_TARGET.AZIMUTH_FILTERING_RESULTS.AZIMUTH_STATE_ESTIMATION[0]= $
;                TRACKING_DATA.LOCAL_A_A_TRACKING_DATA.DATA_FOR_THE_MOST_IMPORTANT_TARGET.AZIMUTH_FILTERING_RESULTS.AZIMUTH_STATE_ESTIMATION[0]*!radeg
;                
;                TRACKING_DATA.LOCAL_A_A_TRACKING_DATA.DATA_FOR_THE_MOST_IMPORTANT_TARGET.ELEVATION_FILTERING_RESULTS.ELEVATION_STATE_ESTIMATION[0]= $
;                TRACKING_DATA.LOCAL_A_A_TRACKING_DATA.DATA_FOR_THE_MOST_IMPORTANT_TARGET.ELEVATION_FILTERING_RESULTS.ELEVATION_STATE_ESTIMATION[0]*!radeg
;                
	      		    HEADER_SIG.header_data.LOCAL_A_A_TRACKING_DATA = TRACKING_DATA.LOCAL_A_A_TRACKING_DATA
	      		    header_in_v[i-start_batch].LOCAL_A_A_TRACKING_DATA = TRACKING_DATA.LOCAL_A_A_TRACKING_DATA

	      		  END


	      		  ;Marker Signal (HDROUT)
	      		  ;1599362125 : BEGIN
	      		  40000000 : BEGIN
	      		    ;print, "HDROUT"
	      		    
	      		    MTT_DATA = replicate(LOCAL_A_A_TRACKING_DATA,16)

	      		    MTT_INT_DATA = { $
	      		      header_sw : header_out.header_sw, $
	      		      data : lonarr(2), $
	      		      MTT_DATA : MTT_DATA }

	      		    point_lun, lun, INDEX_BLOCK*4L
	      		    readu, lun, MTT_INT_DATA
                HEADER_SIG.header_data.gestione_mtt=1
	      		    HEADER_SIG.header_data.MTT_DATA = MTT_INT_DATA.MTT_DATA
	      		    header_in_v[i-start_batch].MTT_DATA = MTT_INT_DATA.MTT_DATA
	      		    print, 'MTT'

	      		  END
	      		  
	      		  50000000 : BEGIN
	      		    ;print, "HDROUT"

	      		    STT_INT_DATA = { $
	      		      header_sw : header_out.header_sw, $
	      		      spare : lonarr(2), $
	      		      STT_DATA : STT_DATA }

	      		    point_lun, lun, INDEX_BLOCK*4L
	      		    readu, lun, STT_INT_DATA

	      		    HEADER_SIG.header_data.STT_DATA = STT_INT_DATA.STT_DATA
	      		    header_in_v[i-start_batch].STT_DATA = STT_INT_DATA.STT_DATA
	      		    print, 'STT'

	      		  END
	      		  
	      		  
	      		  
	      		  ;;;;;;;Marker Signal (uscita Detector) - 06-08-2021 prova analisi registrazione all'uscita detector del segnale ricircolato sul somma 
	      		  5522756 : BEGIN
	      		   
	      		   
	      		    ;Cerco il marker inizio segnale per il blocco identificato
	      		    offset = 2
	      		    DIM_BATCH = DIM_BATCH * 8L
	      		    IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset

	      		    if (DIM_BATCH le 0) then break
	      		    
	      		    BATCH_DATA_SUM = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]

	      		    SIG_DET = FIX(BATCH_DATA_SUM,0,DIM_BATCH*2)
	      		    SIG_DET_I = SIG_DET[2L*lindgen(DIM_BATCH)]
	      		    SIG_DET_Q = SIG_DET[2L*lindgen(DIM_BATCH)+1]
	      		    
	      		    
	      		    SIG_DET_I = (reform(SIG_DET_I, N_RBIN*8L, N_PRI))
	      		    SIG_DET_Q = (reform(SIG_DET_Q, N_RBIN*8L, N_PRI))

;	      		    SIG_SUM_I = 1.*(reform(SIG_SUM_I, N_PRI,N_RBIN*8L)) 
;	      		    SIG_SUM_Q = 1.*(reform(SIG_SUM_Q, N_PRI,N_RBIN*8L))

	      		    ;             S_FFT = S_FFT


	      		     signal_s = complex( SIG_DET_I, SIG_DET_Q )
	      		     
                 m = 10
                 n = N_RBIN*8
	      		     signal_s = signal_s[m:n-1,*]
	      		     
	      		     for p=0, N_PRI-1 do begin 
	      		       signal_s[*, p] = signal_s[*, p]*hanning(n-m+1)
	      		     endfor
	      		     
	      		     
	      		     SIG_DET_I_v.add, SIG_DET_I
	      		     SIG_DET_Q_v.add, SIG_DET_Q
	      		     
;	      		    signal = [signal, SIG_DET]        ; DA VERIFICARE SE I DATI NEL VETTORE SIGNAL SONO ORDINATI IN UN CERTO MODO (ES. SOMMA, GUARDIA, DAZ, DEL)
	      		    
	      		    det = 1
	      		    
	      		  END
	      			
	          ;Marker Signal (SUM)
	      		5068115 : BEGIN
	            ;Cerco il marker inizio segnale per il blocco identificato
	            
;	            !debug_process_events=0
	            
	            
	      			offset = 2
	      			IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset

              if (DIM_BATCH le 0) then break
              
              if (n_elements(BLOCK_DATA) GE (IDX_SIG[0]+DIM_BATCH)) then begin
                
                BATCH_DATA_SUM = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]
                
              endif else begin
                
                break
                
              endelse
	      			  

	      			SIG_SUM = FIX(BATCH_DATA_SUM,0,DIM_BATCH*2)
	      			 SIG_SUM_I = SIG_SUM[2L*lindgen(DIM_BATCH)]
	      			 SIG_SUM_Q = SIG_SUM[2L*lindgen(DIM_BATCH)+1]

	      			 SIG_SUM_I = 1.*(reform(SIG_SUM_I, N_RBIN, N_PRI))   ; AEAF 12042021
	      			 SIG_SUM_Q = 1.*(reform(SIG_SUM_Q, N_RBIN, N_PRI))

;	      			S_FFT = S_FFT
	      			
	      			; signal_s = complex( SIG_SUM_I, SIG_SUM_Q )
	      			signal = [signal, SIG_SUM]
	      			sum = 1
	      			; error = Driver_header_PCRDV(HEADER_SIG.header_data   , $
                              	      		; transpose(signal)   , $
                              	      		; i)

;	      			 SIG_SUM_I_v[*,*,i] = SIG_SUM_I
;	      			 SIG_SUM_Q_v[*,*,i] = SIG_SUM_Q
               SIG_SUM_I_v.add, SIG_SUM_I
               SIG_SUM_Q_v.add, SIG_SUM_Q
	      			
              n_ch++
	      			END
	      			
	      			
	      			
	      		1397506371: BEGIN ;CALIBRAZIONE
	      			
	      		;Cerco il marker inizio segnale per il blocco identificato

	      		  ;             !debug_process_events=0


	      		  offset = 2
	      		  IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset

	      		  if (DIM_BATCH le 0) then break

	      		  if (n_elements(BLOCK_DATA) GE (IDX_SIG[0]+DIM_BATCH)) then begin

	      		    BATCH_DATA_SUM = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]

	      		  endif else begin

	      		    break

	      		  endelse


	      		  SIG_SUM = FIX(BATCH_DATA_SUM,0,DIM_BATCH*2)
	      		  SIG_SUM_I = SIG_SUM[2L*lindgen(DIM_BATCH)]
	      		  SIG_SUM_Q = SIG_SUM[2L*lindgen(DIM_BATCH)+1]

	      		  SIG_SUM_I = 1.*(reform(SIG_SUM_I, N_RBIN, N_PRI))   ; AEAF 12042021
	      		  SIG_SUM_Q = 1.*(reform(SIG_SUM_Q, N_RBIN, N_PRI))

	      		  ;             S_FFT = S_FFT

	      		  ; signal_s = complex( SIG_SUM_I, SIG_SUM_Q )
	      		  signal = [signal, SIG_SUM]
	      		  sum = 1
	      		  ; error = Driver_header_PCRDV(HEADER_SIG.header_data   , $
	      		  ; transpose(signal)   , $
	      		  ; i)

	      		  ;              SIG_SUM_I_v[*,*,i] = SIG_SUM_I
	      		  ;              SIG_SUM_Q_v[*,*,i] = SIG_SUM_Q
	      		  SIG_SUM_I_v.add, SIG_SUM_I
	      		  SIG_SUM_Q_v.add, SIG_SUM_Q

	      		  n_ch++
	      		END	
	      			
	      			
	      			
	      			
	          ;Marker Signal (GUARD)
	      		1380013383 : BEGIN
	      		 
	      		 
	            ;Cerco il marker inizio segnale per il blocco identificato
	            	offset = 2
		            IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset
		            
		            if (DIM_BATCH le 0) then break
		            
		            if (n_elements(BLOCK_DATA) GE (IDX_SIG[0]+DIM_BATCH)) then begin

		              BATCH_DATA_GUARD = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]

		            endif else begin

		              break

		            endelse

		            SIG_GUARD = FIX(BATCH_DATA_GUARD,0,DIM_BATCH*2)
		            SIG_GUARD_I = SIG_GUARD[2L*lindgen(DIM_BATCH)]
		            SIG_GUARD_Q = SIG_GUARD[2L*lindgen(DIM_BATCH)+1]

		            SIG_GUARD_I = (reform(SIG_GUARD_I, N_RBIN, N_PRI))        ; AEAF 12042021
		            SIG_GUARD_Q = (reform(SIG_GUARD_Q, N_RBIN, N_PRI))
;
;		            SIG_GUARD_I_v[*,*,i] = SIG_GUARD_I
;		            SIG_GUARD_Q_v[*,*,i] = SIG_GUARD_Q
		            
		            ; signal_g = complex( SIG_GUARD_I, SIG_GUARD_Q )
		            signal = [signal, SIG_GUARD]
                guard = 1
                
                SIG_GUARD_I_v.add, SIG_GUARD_I
                SIG_GUARD_Q_v.add, SIG_GUARD_Q

;		            error = Driver_header_PCRDV(HEADER_SIG.header_data   , $
;		              transpose(signal)   , $
;		              i)
              n_ch++
	      			END
	      			
	      			;Marker Signal (DELTA AZIMUTH)
	      			5914948 : BEGIN

	      			  ;Cerco il marker inizio segnale per il blocco identificato
	      			  offset = 2
	      			  IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset
	      			  
	      			  if (DIM_BATCH le 0) then break

	      			  if (n_elements(BLOCK_DATA) GE (IDX_SIG[0]+DIM_BATCH)) then begin

	      			    BATCH_DATA_AUX = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]

	      			  endif else begin

	      			    break

	      			  endelse
	      			  	      			  
	      			  SIG_DAZ = FIX(BATCH_DATA_AUX,0,DIM_BATCH*2)
	      			  SIG_DAZ_I = SIG_DAZ[2L*lindgen(DIM_BATCH)]
	      			  SIG_DAZ_Q = SIG_DAZ[2L*lindgen(DIM_BATCH)+1]

	      			  SIG_DAZ_I = (reform(SIG_DAZ_I, N_RBIN, N_PRI))        ; AEAF 12042021
	      			  SIG_DAZ_Q = (reform(SIG_DAZ_Q, N_RBIN, N_PRI))        ; AEAF 12042021
;	      			  
;	      			  SIG_DAZ_I_v[*,*,i] = SIG_DAZ_I
;	      			  SIG_DAZ_Q_v[*,*,i] = SIG_DAZ_Q
;	      			  
	      			  SIG_DAZ_I_v.add, SIG_DAZ_I
                SIG_DAZ_Q_v.add, SIG_DAZ_Q
;	      			  
;	      			  
;	      			  signal_daz = complex( SIG_DAZ_I, SIG_DAZ_Q )
	      			  signal = [signal, SIG_DAZ]
	      			  daz = 1
	      			  
	      			  ; print, 'daz'
                n_ch++
	      			END
	      			
	      			;Marker Signal (DELTA ELEVATION)
	      			4998468  : BEGIN    

	      			  ;Cerco il marker inizio segnale per il blocco identificato
	      			  offset = 2
	      			  IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset
	      			  
	      			  if (DIM_BATCH le 0) then break

	      			  if (n_elements(BLOCK_DATA) GE (IDX_SIG[0]+DIM_BATCH)) then begin

	      			    BATCH_DATA_AUX = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]

	      			  endif else begin

	      			    break

	      			  endelse
	      			        			  

	      			  SIG_DEL = FIX(BATCH_DATA_AUX,0,DIM_BATCH*2)
	      			  SIG_DEL_I = SIG_DEL[2L*lindgen(DIM_BATCH)]
	      			  SIG_DEL_Q = SIG_DEL[2L*lindgen(DIM_BATCH)+1]

	      			  SIG_DEL_I = (reform(SIG_DEL_I, N_RBIN, N_PRI))        ; AEAF 12042021
	      			  SIG_DEL_Q = (reform(SIG_DEL_Q, N_RBIN, N_PRI))        ; AEAF 12042021
;	      			  
;	      			  SIG_DEL_I_v[*,*,i] = SIG_DEL_I
;	      			  SIG_DEL_Q_v[*,*,i] = SIG_DEL_Q
;	      			  
                SIG_DEL_I_v.add, SIG_DEL_I
                SIG_DEL_Q_v.add, SIG_DEL_Q

;      			    signal_del = complex( SIG_DEL_I, SIG_DEL_Q )
      			    signal = [signal, SIG_DEL]
      			    del = 1

;	      			  error = Driver_header_PCRDV(HEADER_SIG.header_data   , $
;	      			    transpose(signal)   , $
;	      			    i)
;	      			    
;      			  print, 'del'
              n_ch++
	      			END
	      			
	      			
            ;MTI-FFT
            1181316173 :  BEGIN

              ;Cerco il marker inizio segnale per il blocco identificato
              offset = 2
              IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset
              ;N_PRI=1024                         
              DIM_BATCH = N_PRI * N_RBIN
              
              if (DIM_BATCH le 0) then break
              
              BATCH_DATA_AUX = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]
            
              SIG_MTI = float(BATCH_DATA_AUX,0,N_PRI * N_RBIN)
              SIG_MTI_SUM = (reform(SIG_MTI, N_PRI, N_RBIN))
              
              STARTRBIN = 15
              STOPRBIN = 23
              
              
              sig_test =  20.*alog10(SIG_MTI_SUM[*,15:23])
              
              MAX_SPURIE = 80 ;dBc
              START1 = 3
              STOP1 = 1792-3
              START2 = 1792+3
              STOP2 = 2047-2
              dem_freq_hz=2000
              
              prf = 1/((header_sig.header_data.function_settings.mtifft_settings.prt+0.5)*1e-6)
              
              test_spurie1 = where((sig_test[1792,4] - sig_test[START1:STOP1,*]) LT MAX_SPURIE, num_spurie_1)
              test_spurie2 = where((sig_test[1792,4] - sig_test[START2:STOP2,*]) LT MAX_SPURIE, num_spurie_2)
 
              
              IF num_spurie_1 GT 0 THEN BEGIN
                
                NDBINTEST1 = STOP1-START1+1
                NRBINTEST1 = 9

                SPURIE1 = [(test_spurie1 mod NDBINTEST1)+START1, test_spurie1/NDBINTEST1+15]
                
                print, 'SPURIE:', num_spurie_1
                print, 'SPURIE doppler bin:', SPURIE1[0:num_spurie_1-1]*prf/2048+dem_freq_hz
                print, 'SPURIE range bin:', SPURIE1[num_spurie_1:2*num_spurie_1-1]
                print, 'dBC:', sig_test[1792,4]-(sig_test[START1:STOP1,*])[test_spurie1]
                
                STOP
              ENDIF ELSE BEGIN
                IF num_spurie_2 GT 0 THEN BEGIN
                  
                  NDBINTEST2 = STOP2-START2+1
                  NRBINTEST2 = 9

                  SPURIE2 = [(test_spurie2 mod NDBINTEST2)+START2, test_spurie2/NDBINTEST2+15]
                  
                  print, 'SPURIE:', num_spurie_2
                  print, 'SPURIE doppler bin:', SPURIE2[0:num_spurie_2-1]*prf/2048+dem_freq_hz
                  print, 'SPURIE range bin:', SPURIE2[num_spurie_2:2*num_spurie_2-1]
                  
                  STOP
                ENDIF ELSE print, 'TEST OK', sig_test[1792,4]
              END
              
              MTI_SUM_OUT_v.add, SIG_MTI_SUM
              
            END

            ;SLBCRES
            1380076627 : BEGIN
            
              offset = 2
              IDX_DATA = where(BLOCK_DATA EQ LONG('41544144'x)) + offset
            
              DIM_BATCH = 4096
              BATCH_DATA_AUX = BLOCK_DATA[IDX_DATA[0]:IDX_DATA[0]+DIM_BATCH-1]
            
              S_DET_AMP = float(BATCH_DATA_AUX[0:2047],0,2048)
              S_DET_AMP = S_DET_AMP[0:N_RBIN-1]
              S_DET_FIL = BATCH_DATA_AUX[2048:4095]
              S_DET_FIL = S_DET_FIL[0:N_RBIN-1]
            
              print, "DET_AMP DET_FIL"
            
            END
            
            1918985795 : BEGIN

              offset = 2
              IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset

              N_PRI_DET_MATRIX = header.header_data.signal_descr.PACKETDESCR.npri
              N_RBIN_DET_MATRIX = header.header_data.signal_descr.PACKETDESCR.NRBIN

              DIM_BATCH = N_PRI_DET_MATRIX * N_RBIN_DET_MATRIX
              BATCH_DATA_AUX = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]

              DETECTION_MATRIX_TEMP = float(BATCH_DATA_AUX,0,DIM_BATCH)

              DETECTION_MATRIX = reform(DETECTION_MATRIX_TEMP, N_RBIN_DET_MATRIX, N_PRI_DET_MATRIX)

              print, "Detection Matrix"

            END
            
            ;Marker Signal (Detector)
            1098147140 :  BEGIN
              
              
              
              ;Cerco il marker inizio segnale per il blocco identificato
              DIM_BATCH = DIM_BATCH * 8L
              offset = 2
              IDX_SIG = where(BLOCK_DATA EQ LONG('4e474953'x)) + offset
              
              if (DIM_BATCH le 0) then break

              BATCH_DATA_SUM = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH-1]

              SIG_DET = FIX(BATCH_DATA_SUM,0,DIM_BATCH*2)
              SIG_DET_I = SIG_DET[2L*lindgen(DIM_BATCH)+1]
              SIG_DET_Q = SIG_DET[2L*lindgen(DIM_BATCH)]

              SIG_DET_I = (reform(SIG_DET_I, N_RBIN*8L, N_PRI))
              SIG_DET_Q = (reform(SIG_DET_Q, N_RBIN*8L, N_PRI))

              ;print, "Detector"

            END
            
            ;Marker Signal (NCI_SUM)
            1399415630 : BEGIN

              ;Cerco il marker inizio segnale per il blocco identificato
              DIM_BATCH_NCI = 1L * N_RBIN
              BATCH_DATA_NCI = BLOCK_DATA[IDX_SIG[0]:IDX_SIG[0]+DIM_BATCH_NCI-1]

              SIG_NCI = float(BATCH_DATA_NCI,0,DIM_BATCH_NCI)

            END


          ELSE:
                      
	      	ENDCASE
	      
	      ;;;;;;;;;;;;;;;;;;;;;;;; 27-5-2022  GR   -  ciclo per blocchi dati exp, det, pc  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;	
	      	
;	    if Num_blocks_fw GT 0 then begin
;	    	    
;	      	INDEX_BLOCK_fw = ixBlocks_fw[index_exp_pc_det[conta_blocchi_fw]]
;	      	
;	      	IF (INDEX_BLOCK_fw LT INDEX_BLOCK_hdr) THEN BEGIN
;	      	
;    	      	NAME_BLOCK_FW = NAME_BLOCK_V_FW[index_exp_pc_det[conta_blocchi_fw]]
;    
;    	      	CASE (NAME_BLOCK_FW) OF
;    	      	  ;Marker EXPANDER
;    	      	  5265477 : BEGIN
;    	      	    
;    	      	    indice_primo_reg_utile = INDEX_BLOCK_fw+16      ; indice primo registro definito in ICD (Exp ctrl register)
;    	      	    
;    	      	    point_lun, lun, indice_primo_reg_utile*4L
;                  readu, lun, expander_raw_if_t
;
;;                  dim_exp_struct = n_tags(expander_raw_if_t, /data_length)
;;;                 
;;                  long2struct , vector[(indice_primo_reg_utile*4L):(indice_primo_reg_utile*4L)+dim_exp_struct-1] , expander_raw_if_t
;;    
;    	      	    cont_exp++
;    
;    	      	    print, "num blocchi exp: ", cont_exp
;    	      	    
;    	      	    
;;    	      	    expander_raw_if_t.wf1.frq_start = long(expander_raw_if_t.wf1.frq_start*ulong64(1792)/2^12/2^10/2^10) ; calcolo della frequenza di start applicando lsb (MHz)
;;    	      	    expander_raw_if_t.wf1.incr = long(expander_raw_if_t.wf1.incr*ulong64(1792)*224/2^12/2^10/2^10) ; calcolo incremento della frequenza applicando lsb (MHz)
;;    	      	    expander_raw_if_t.wf1.len = long(expander_raw_if_t.wf1.len/ulong64(224)) ; calcolo waveform length applicando lsb (microsecondi)
;;    	      	    expander_raw_if_t.wf1.amp_peak = long(expander_raw_if_t.wf1.amp_peak/2^10/2^6) ; calcolo amplitude peak applicando lsb
;                  
;                  
;;                  expander_raw_if_t.wf1.frq_start = float(double(expander_raw_if_t.wf1.frq_start)*1792/2^12/2^10/2^10) ; calcolo della frequenza di start applicando lsb (MHz)
;;                  expander_raw_if_t.wf1.incr = float(double(expander_raw_if_t.wf1.incr)*1792*224/2^12/2^10/2^10) ; calcolo incremento della frequenza applicando lsb (MHz)
;;                  expander_raw_if_t.wf1.len = float(double(expander_raw_if_t.wf1.len)/224) ; calcolo waveform length applicando lsb (microsecondi)
;                  
;                  expander_wf1_freq_start = float(double(expander_raw_if_t.wf1.frq_start)*1792/2^12/2^10/2^10) ; calcolo della frequenza di start applicando lsb (MHz)
;                  expander_wf1_freq_incr = float(double(expander_raw_if_t.wf1.incr)*1792/2^12/2^10/2^10) ; calcolo incremento della frequenza applicando lsb (MHz)
;                  expander_wf1_len = float(double(expander_raw_if_t.wf1.len)/224) ; calcolo waveform length applicando lsb (microsecondi)
;                  expander_raw_if_t.wf1.amp_peak = long(ulong(expander_raw_if_t.wf1.amp_peak)/2^10/2^6) ; calcolo amplitude peak applicando lsb
;                  
;;                  expander_raw_if_t.wf2.frq_start = float(double(expander_raw_if_t.wf2.frq_start)*1792/2^12/2^10/2^10) ; calcolo della frequenza di start applicando lsb (MHz)
;;                  expander_raw_if_t.wf2.incr = float(double(expander_raw_if_t.wf2.incr)*1792*224/2^12/2^10/2^10) ; calcolo incremento della frequenza applicando lsb (MHz)
;;                  expander_raw_if_t.wf2.len = float(double(expander_raw_if_t.wf2.len)/224) ; calcolo waveform length applicando lsb (microsecondi)
;
;                  expander_wf2_freq_start = float(double(expander_raw_if_t.wf2.frq_start)*1792/2^12/2^10/2^10) ; calcolo della frequenza di start applicando lsb (MHz)
;                  expander_wf2_freq_incr = float(double(expander_raw_if_t.wf2.incr)*1792/2^12/2^10/2^10) ; calcolo incremento della frequenza applicando lsb (MHz)
;                  expander_wf2_len = float(double(expander_raw_if_t.wf2.len)/224) ; calcolo waveform length applicando lsb (microsecondi)
;                  expander_raw_if_t.wf2.amp_peak = long(ulong(expander_raw_if_t.wf2.amp_peak)/2^10/2^6) ; calcolo amplitude peak applicando lsb
;                  
;;                  exp_mof_bin = expander_raw_if_t.mof_fifo[0]
;;                  exp_mof_bin = reverse(binary(exp_mof_bin))
;                 
;                  exp_mof_bin = STRARR(32)
;                  FOR k=0,31 DO BEGIN
;                    powerOfTwo = 2L^k
;                    IF (LONG(expander_raw_if_t.mof_fifo[0]) AND powerOfTwo) EQ powerOfTwo THEN $
;                      exp_mof_bin[k] = '1' ELSE exp_mof_bin[k] = '0'
;                  ENDFOR
;                  
;                  exp_mof = 0L
;                  exp_phase_reset = 0L
;                  
;                  for k=0,2 do begin
;                    
;                    exp_mof = exp_mof + (2L^k)*exp_mof_bin[k]
;                    
;                  endfor
;                  
;                  for k=8,10 do begin
;
;                    exp_phase_reset = exp_phase_reset + (2L^k)*exp_mof_bin[k]
;
;                  endfor
;                  
;                  
;                  header_out.header_data.sp_settings.exp_settings.exp_mof = exp_mof     
;                  header_out.header_data.sp_settings.exp_settings.phase_rst_code = exp_phase_reset
;                  header_out.header_data.sp_settings.exp_settings.exp_f_base = expander_wf1_freq_start[0]
;                  header_out.header_data.sp_settings.exp_settings.exp_f_delta = expander_wf1_freq_incr[0]
;                  header_out.header_data.sp_settings.exp_settings.pulse_len = expander_wf1_len[0]
;                  header_out.header_data.sp_settings.exp_settings.amp_peak = expander_raw_if_t.wf1.amp_peak[0] 
;                  header_out.header_data.sp_settings.exp_settings.exp_f_base_2 = expander_wf2_freq_start[0]
;                  header_out.header_data.sp_settings.exp_settings.exp_f_delta_2 = expander_wf2_freq_incr[0]
;                  header_out.header_data.sp_settings.exp_settings.pulse_len_2 = expander_wf2_len[0]
;                  header_out.header_data.sp_settings.exp_settings.amp_peak_2 = expander_raw_if_t.wf2.amp_peak[0]
;                  
;                  exp_settings = header_out.header_data.sp_settings.exp_settings      ; mi tengo salvata la struttura per aggiornare l'header out alla fine del ciclo (per evitare che venga sovrascritto se trova un nuovo blocco hdr out)
;                              
;
;    
;    
;    	      	  END
;    
;    
;    	      	  ;Marker DETECTOR
;    	      	  5522756 : BEGIN
;    	      	    
;    	      	    indice_primo_reg_utile = INDEX_BLOCK_fw+16      ; indice primo registro definito in ICD (Det mof)
;
;    	      	    point_lun, lun, indice_primo_reg_utile*4L
;    	      	    readu, lun, detector_raw_if_t
;    
;    	      	    cont_det++
;    
;    	      	    print, "num blocchi det: ", cont_det
;    	      	    
;;    	      	    detector_raw_if_t.settings.sett.f_base = float(double(detector_raw_if_t.settings.sett.f_base)*16/2^11/2^10/2^10)   ; applico lsb per calcolo f_base
;;    	      	    detector_raw_if_t.settings.sett.f_delta = float(double(detector_raw_if_t.settings.sett.f_delta)*16/2^11/2^10/2^10)   ; applico lsb per calcolo f_delta
;                  
;                  detector_f_base = float(double(detector_raw_if_t.settings.sett.f_base)*16/2^12/2^10/2^10)   ; applico lsb per calcolo f_base - creo nuova var non potendo modificare il campo della struttura da long a float
;                  detector_f_delta = float(double(detector_raw_if_t.settings.sett.f_delta)*16/2^12/2^10/2^10)   ; applico lsb per calcolo f_delta - creo nuova var non potendo modificare il campo della struttura da long a float  
;    	      	    
;    	      	    detector_raw_if_t.settings.sett.lsn_start_56mhz = long(ulong(detector_raw_if_t.settings.sett.lsn_start_56mhz)/56/10^6)   ; applico lsb per calcolo lsn_start
;    	      	    detector_raw_if_t.settings.sett.lsn_len_56mhz = long(ulong(detector_raw_if_t.settings.sett.lsn_len_56mhz)/56/10^6)   ; applico lsb per calcolo lsn_len
;    	      	    
;    	      	    
;    	      	    header_out.header_data.sp_settings.det_settings.det_mof = detector_raw_if_t.settings.sett.mof
;    	      	    header_out.header_data.sp_settings.det_settings.lpf7_filter_id = detector_raw_if_t.settings.sett.pre_filter_id
;    	      	    header_out.header_data.sp_settings.det_settings.lpf8_filter_id = detector_raw_if_t.settings.sett.post_filter_id
;    	      	    header_out.header_data.sp_settings.det_settings.det_f_base = detector_f_base
;    	      	    header_out.header_data.sp_settings.det_settings.det_f_delta = detector_f_delta
;    	      	    header_out.header_data.sp_settings.det_settings.det_lsn_start = detector_raw_if_t.settings.sett.lsn_start_56mhz
;    	      	    header_out.header_data.sp_settings.det_settings.det_lsn_len = detector_raw_if_t.settings.sett.lsn_len_56mhz
;    	      	    
;    	      	    det_settings = header_out.header_data.sp_settings.det_settings      ; mi tengo salvata la struttura per aggiornare l'header out alla fine del ciclo (per evitare che venga sovrascritto se trova un nuovo blocco hdr out)
;;    
;    
;    	      	  END
;    
;    
;    	      	  ;Marker PULSE COMPRESSION
;    	      	  17232 : BEGIN
;    	      	    
;    	      	    indice_primo_reg_utile = INDEX_BLOCK_fw+16      ; indice primo registro definito in ICD (mof)
;
;    	      	    point_lun, lun, indice_primo_reg_utile*4L
;    	      	    readu, lun, pc_raw_if_t
;    
;    	      	    cont_pc++
;    
;    	      	    print, "num blocchi pc: ", cont_pc
;    	      	    
;    	      	    pc_raw_if_t.settings.lsn_start_56mhz = long(ulong(pc_raw_if_t.settings.lsn_start_56mhz)/56/10^6)   ; applico lsb per calcolo lsn_start
;    	      	    pc_raw_if_t.settings.lsn_len_56mhz = long(ulong(pc_raw_if_t.settings.lsn_len_56mhz)/56/10^6)   ; applico lsb per calcolo lsn_len
;    	      	    
;    	      	    
;    	      	    header_out.header_data.sp_settings.pc_settings.pc_mof = pc_raw_if_t.settings.mof
;    	      	    header_out.header_data.sp_settings.pc_settings.filt_id = pc_raw_if_t.settings.lpf_filter_id
;    	      	    header_out.header_data.sp_settings.pc_settings.pc_lsn_start = pc_raw_if_t.settings.lsn_start_56mhz
;    	      	    header_out.header_data.sp_settings.pc_settings.pc_lsn_len = pc_raw_if_t.settings.lsn_len_56mhz
;    	      	    header_out.header_data.sp_settings.pc_settings.pc_out_offset = pc_raw_if_t.settings.out_ofs
;    	      	    
;    	      	    pc_settings = header_out.header_data.sp_settings.pc_settings    ; mi tengo salvata la struttura per aggiornare l'header out alla fine del ciclo (per evitare che venga sovrascritto se trova un nuovo blocco hdr out)
;    	      	    
;    
;    
;    	      	  END
;    
;    	      	  ELSE:
;    
;    	      	ENDCASE  
;    	      	
;    	      	conta_blocchi_fw++
;    	      	
;;    	      	header_out.header_data.sp_settings.exp_settings.exp_mof = exp_mof
;;    	      	header_out.header_data.sp_settings.exp_settings.phase_rst_code = exp_phase_reset
;;    	      	header_out.header_data.sp_settings.exp_settings.exp_f_base = expander_raw_if_t.wf1.frq_start[0]
;;    	      	header_out.header_data.sp_settings.exp_settings.exp_f_delta = expander_raw_if_t.wf1.incr[0]
;;    	      	header_out.header_data.sp_settings.exp_settings.pulse_len = expander_raw_if_t.wf1.len[0]
;;    	      	header_out.header_data.sp_settings.exp_settings.amp_peak = expander_raw_if_t.wf1.amp_peak[0]
;;    	      	header_out.header_data.sp_settings.exp_settings.exp_f_base_2 = expander_raw_if_t.wf2.frq_start[0]
;;    	      	header_out.header_data.sp_settings.exp_settings.exp_f_delta_2 = expander_raw_if_t.wf2.incr[0]
;;    	      	header_out.header_data.sp_settings.exp_settings.pulse_len_2 = expander_raw_if_t.wf2.len[0]
;;    	      	header_out.header_data.sp_settings.exp_settings.amp_peak_2 = expander_raw_if_t.wf2.amp_peak[0]
;;
;;
;;    	      	header_out.header_data.sp_settings.det_settings.det_mof = detector_raw_if_t.settings.sett.mof
;;    	      	header_out.header_data.sp_settings.det_settings.lpf7_filter_id = detector_raw_if_t.settings.sett.pre_filter_id
;;    	      	header_out.header_data.sp_settings.det_settings.lpf8_filter_id = detector_raw_if_t.settings.sett.post_filter_id
;;    	      	header_out.header_data.sp_settings.det_settings.det_f_base = detector_raw_if_t.settings.sett.f_base
;;    	      	header_out.header_data.sp_settings.det_settings.det_f_delta = detector_raw_if_t.settings.sett.f_delta
;;    	      	header_out.header_data.sp_settings.det_settings.pc_lsn_start = detector_raw_if_t.settings.sett.lsn_start_56mhz
;;    	      	header_out.header_data.sp_settings.det_settings.pc_lsn_len = detector_raw_if_t.settings.sett.lsn_len_56mhz
;;
;;    	      	header_out.header_data.sp_settings.pc_settings.pc_mof = pc_raw_if_t.settings.mof
;;    	      	header_out.header_data.sp_settings.pc_settings.filt_id = pc_raw_if_t.settings.lpf_filter_id
;;    	      	header_out.header_data.sp_settings.pc_settings.pc_lsn_start = pc_raw_if_t.settings.lsn_start_56mhz
;;    	      	header_out.header_data.sp_settings.pc_settings.pc_lsn_len = pc_raw_if_t.settings.lsn_len_56mhz
;;    	      	header_out.header_data.sp_settings.pc_settings.pc_out_offset = pc_raw_if_t.settings.out_ofs
;    	      	
;    	    ENDIF ELSE BEGIN
;    	     
;    	     
;    	    ENDELSE
;	      	
;	      	
;    	endif    
;    	    
;	      	;;;;;;;;;;;;;;;;;;;;;;;;  fine ciclo per blocchi dati exp, det, pc  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	      	
;	      	      
    ENDFOR   ; ciclo legato ai blocchi contenuti nel singolo batch
;	    
;	    
;	    ; RISCRIVO LE STRUTTURE DEI SETTINGS EXP, DET, PC NELL'HEADER OUT
;	    
;	    IF (isa(pc_settings)) THEN   header_out.header_data.sp_settings.pc_settings = pc_settings
;	    
;	    IF (isa(det_settings)) THEN   header_out.header_data.sp_settings.det_settings = det_settings
;	    
;	    IF (isa(exp_settings)) THEN   header_out.header_data.sp_settings.exp_settings = exp_settings
;	    
;	    ;;;;;;;;;;;;;;;;;;;;;;;; 27-5-2022  GR   -  ciclo per blocchi dati exp, det, pc  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;	    
;;	    FOR j=0, num_block_exp_pc_det[i]-1 DO BEGIN      ;;;;; condizione da modificare 
;;
;;	       print, "siamo al batch: ", i
;;
;;	      INDEX_BLOCK_FW = ixBlocks_fw[index_exp[i]+j]
;;	      
;	      
;;	      
;;	    ENDFOR
;	    
;	    ;;;;;;;;;;;;;;;;;;;;;;;; fine ciclo per blocchi dati exp, det, pc ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	    
	      
	    
      
      print, "FINE LETTURA BATCH NUMERO: ", i
      
      
;                if (i eq 48) then stop

      COND1 = HEADER_SIG.header_data.signal_descr.PACKETDESCR.npri EQ header_out.header_data.signal_descr.PACKETDESCR.npri
      COND2 = HEADER_SIG.header_data.signal_descr.PACKETDESCR.nrbin EQ header_out.header_data.signal_descr.PACKETDESCR.nrbin
      COND3 = HEADER_SIG.header_data.mode.master_mode EQ header_out.header_data.mode.master_mode
      
      
      IF (signal eq !null) THEN BEGIN           ; passo alla funzione Driver_header_PCRDV solo i batch validi (in cui c'è segnale su almeno un canale)
          
          indici_batch_senza_segnale = [indici_batch_senza_segnale, i]    ; tengo traccia se il batch in esame ha vettore segnale nullo
          N_PRI_IN = HEADER_SIG.header_data.signal_descr.PACKETDESCR.npri
          N_RBIN_IN = HEADER_SIG.header_data.signal_descr.PACKETDESCR.nrbin
          DIM_BATCH_IN = N_PRI_IN*N_RBIN_IN
          batch_data_fake = lonarr(DIM_BATCH_IN)+0.0000001
          SIG_SUM_FAKE = FIX(batch_data_fake,0,DIM_BATCH_IN*2)
          signal = [signal, SIG_SUM_FAKE]       ; nel caso in cui non ci sia segnale su nessun canale, passo al pcrdv un segnale somma fittizio (tutto a zero)
          n_ch = 1
          
      ENDIF     
          
         
        if (n_hdr_out NE 0) AND (hdr_out_trovato EQ 1) AND COND1 AND COND2 AND COND3 then begin
;         if (n_hdr_out NE 0) AND COND1 AND COND2 AND COND3 then begin
          error = Driver_header_PCRDV(header_out.header_data , n_ch, $
            signal   , $
            kk)            ; anzichè i (indice batch complessivo) gli do kk, che incrementa solo se il batch è valido
          ;i-n_batch_read)
          
          kk++
          
          
;         endif else $
;           
;         if (n_hdr_out EQ 0) OR (COND1 NE 1) OR (COND2 NE 1) then begin
        endif else begin
          error = Driver_header_PCRDV(HEADER_SIG.header_data , n_ch, $
            signal   , $
            kk)            ; anzichè i (indice batch complessivo) gli do kk, che incrementa solo se il batch è valido
          ;i-n_batch_read)
          kk++
        endelse
          
        
        
        
;        endif else begin
;          
;          indici_batch_non_validi = [indici_batch_non_validi, i]    ; scarto il batch se non viene trovato HDR OUT oppure 
;                                                                    ; se c'è discrepanza tra HDR IN e HDR OUT in merito a 
;        endelse                                                     ; numero di PRI e/o numero RBIN e/o master mode
         
	      
	  ENDFOR   ; ciclo legato ai batch

	ENDIF ELSE BEGIN

		print, "Nessun Header Trovato"
	ENDELSE
	
	check_ch = [sum, guard, daz, del]      ; parola di 4 bit che indicano ciascuno la presenza o la mancanza di uno dei 4 canali
	
	
	
	;;;;;-------------------------------CALCOLO PUREZZA SPETTRALE A PARTIRE DAI DATI IN USCITA DAL DETECTOR----------------------------------
	
;	if(det EQ 1) then begin      ; stima della purezza spettrale a partire dai dati in uscita dal detector
;	 
;	   gmcplot, y1=20*alog10(total(abs(fft(signal_s,dim=1)),2)+0.0000001)
;	   close, lun & free_lun, lun
;;	   return
;	 
;	endif
;	
	
	;;;;;-------------------------------FINE CALCOLO PUREZZA SPETTRALE A PARTIRE DAI DATI IN USCITA DAL DETECTOR----------------------------------

  point_lun, -lun, pos
  print, "FINE DELLA REGISTRAZIONE", pos
	close, lun & free_lun, lun
	
	vector = 0
	
	print, "***********************************************************************************************************"
	print, ""
 	print, "ATTENZIONE!!! I SEGUENTI BATCH SONO STATI SCARTATI IN QUANTO NON CONTENGONO SEGNALE:", indici_batch_senza_segnale
	print, "ATTENZIONE!!! I SEGUENTI BATCH SONO STATI SCARTATI IN QUANTO NON VALIDI:", indici_batch_non_validi
	print, ""
	print, "***********************************************************************************************************"


	if (n_hdr_out NE 0) AND (hdr_out_trovato EQ 1) AND COND1 AND COND2 AND COND3 then begin
	  Close_File_MTI, header_out.header_data, kk, check_ch    ; anzichè i gli do kk, che incrementa solo se il batch è valido
	endif else begin
	  Close_File_MTI, HEADER_SIG.header_data, kk, check_ch    ; anzichè i gli do kk, che incrementa solo se il batch è valido
	Endelse

	;Close_File_MTI, header_out.header_data , i, check_ch

END