module param_mod

  use rotcig_types
  use sep

  implicit none

  contains

subroutine param_rotcig(rotcig_par)

  type (rotcig_param_type) rotcig_par

  integer ::  omp_get_num_threads
  integer, external :: auxpar,getch,fetch,auxin,auxinout,sreed,sseek,auxout

  character(len=128) :: tag_name
  integer n_threads_env,n_threads_init
  integer n,n_ang,esize
  real d,o,ang_min,ang_max

  n_threads_init=-1

  call from_param('verb',rotcig_par%verb,.false.)
  call from_param('debug1',rotcig_par%debug1,.false.)
  call from_param('debug2',rotcig_par%debug2,.false.)

  call from_param('create_off_ddcig',rotcig_par%create_off_ddcig,.false.)
  call from_param('create_cig_tilt',rotcig_par%create_cig_tilt,.false.)
  call from_param('create_ang_ddcig',rotcig_par%create_ang_ddcig,.false.)
  call from_param('create_ang_cig',rotcig_par%create_ang_cig,.false.)

  call from_param('n_threads',rotcig_par%n_threads,n_threads_init)
  call from_param('max_memory',rotcig_par%max_memory,100)

! checking parameters
! !$omp parallel
  n_threads_env=1 ! omp_get_num_threads()
! !$omp end parallel
  if(rotcig_par%n_threads == n_threads_init) then
	     rotcig_par%n_threads=n_threads_env
  else
	     if(n_threads_env > rotcig_par%n_threads) then
	        call omp_set_num_threads(rotcig_par%n_threads)
	        write(0,*)'WARNING: overwriting environment variable OMP_NUM_THREADS'
	        write(0,*)'rotcig_par-n_threads=',rotcig_par%n_threads
	     end if
	     if(n_threads_env < rotcig_par%n_threads) then
	        write(0,*)'rotcig_par-n_threads=',rotcig_par%n_threads
	        write(0,*)'OMP_NUM_THREADS=',n_threads_env
	        call seperr('ERROR: Asked for more threads than the environment variable allows')
	     end if
  end if
  if(rotcig_par%debug1)write(0,*)'rotcig_par-n_threads=',rotcig_par%n_threads
  call to_history('n_threads',rotcig_par%n_threads)

  if(rotcig_par%create_off_ddcig .and. rotcig_par%create_ang_ddcig) then
	    call seperr('Cannot create both off_ddcig and ang_ddcig')
  end if
  if(rotcig_par%create_off_ddcig .and. rotcig_par%create_ang_cig) then
	    call seperr('Cannot create both off_ddcig and ang_cig')
  end if
  if(rotcig_par%create_off_ddcig .and. rotcig_par%create_cig_tilt) then
	    call seperr('Cannot create both off_ddcig and cig_tilt')
  end if
  if(rotcig_par%create_ang_ddcig .and. rotcig_par%create_ang_cig) then
	    call seperr('Cannot create both ang_ddcig and ang_cig')
  end if

!  write(0,*)'auxin=',auxin(trim(rotcig_par%tag_cig_hx))
!
!  write(0,*)'rotcig_par%tag_cig_hz=',trim(rotcig_par%tag_cig_hz)
!  write(0,*)'rotcig_par%tag_cig_hx=',trim(rotcig_par%tag_cig_hx)
!
!  write(0,*)'rotcig_par%cig_hz=',rotcig_par%cig_hz
!  write(0,*)'rotcig_par%cig_hx=',rotcig_par%cig_hx

  if ((rotcig_par%create_off_ddcig) .or. (rotcig_par%create_ang_ddcig) .or. (rotcig_par%create_ang_cig)) then
  !{
	   call from_param('inp_dip_center',rotcig_par%inp_dip_center,0.)
	   call from_param('tag_cig_hdip',rotcig_par%tag_cig_hdip,'Cig_hdip')
	   call from_param(trim(rotcig_par%tag_cig_hdip),tag_name,'Cig_hdip not found')
	   rotcig_par%cig_hdip=auxin(trim(rotcig_par%tag_cig_hdip)) > 0
	   if(.not. rotcig_par%cig_hdip) call seperr("Cig_hdip missing")
	   call from_param('tag_cig_hdip_transf',rotcig_par%tag_cig_hdip_transf,'Cig_hdip_transf')
	   call from_param(trim(rotcig_par%tag_cig_hdip_transf),tag_name,'Cig_hdip_transf not found')
	   write(0,*)'rotcig_par%tag_cig_hdip_transf=',trim(rotcig_par%tag_cig_hdip_transf)
	   if(auxinout(trim(rotcig_par%tag_cig_hdip_transf)) > 0) then
		     if(rotcig_par%verb)write(0,*)'Cig_hdip_transf already exists'
	   end if
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'n1',rotcig_par%n_z)
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'n2',rotcig_par%n_mx)
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'n3',rotcig_par%n_hdip)
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'d1',rotcig_par%d_z)
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'d2',rotcig_par%d_mx)
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'d3',rotcig_par%d_hdip)
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'o1',rotcig_par%z_0)
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'o2',rotcig_par%mx_0)
	   call from_aux(trim(rotcig_par%tag_cig_hdip),'o3',rotcig_par%hdip_0)

	   call to_history("From Cig_hdip: n1",rotcig_par%n_z)
	   call to_history("From Cig_hdip: n2",rotcig_par%n_mx)
	   call to_history("From Cig_hdip: n3",rotcig_par%n_hdip)
	   call to_history("From Cig_hdip: d1",rotcig_par%d_z)
	   call to_history("From Cig_hdip: d2",rotcig_par%d_mx)
	   call to_history("From Cig_hdip: d3",rotcig_par%d_hdip)
	   call to_history("From Cig_hdip: o1",rotcig_par%z_0)
	   call to_history("From Cig_hdip: o2",rotcig_par%mx_0)
	   call to_history("From Cig_hdip: o3",rotcig_par%hdip_0)
	
	   call from_param('n_z_pad',rotcig_par%n_z_pad,rotcig_par%n_z)
	   call from_param('n_mx_pad',rotcig_par%n_mx_pad,rotcig_par%n_mx)
  
	   esize=8
	   call to_history("esize",esize,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("n1",rotcig_par%n_z_pad/2+1,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("n2",rotcig_par%n_hdip,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("n3",rotcig_par%n_mx_pad,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("d2",rotcig_par%d_hdip,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("d3",rotcig_par%d_mx,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("o2",rotcig_par%hdip_0,trim(rotcig_par%tag_cig_hdip_transf))
	   call to_history("o3",rotcig_par%mx_0,trim(rotcig_par%tag_cig_hdip_transf))

	   if (rotcig_par%create_off_ddcig) then
    !{
		      call from_param('tag_off_ddcig',rotcig_par%tag_off_ddcig,'Off_ddcig')
		      call from_param(trim(rotcig_par%tag_off_ddcig),tag_name,'Off_ddcig not found')
		      write(0,*)'rotcig_par%tag_off_ddcig=',trim(rotcig_par%tag_off_ddcig)
	  
		      call from_param('tag_off_ddcig_transf',rotcig_par%tag_off_ddcig_transf,'Off_ddcig_transf')
		      call from_param(trim(rotcig_par%tag_off_ddcig_transf),tag_name,'Off_ddcig_transf not found')
		      write(0,*)'rotcig_par%tag_off_ddcig_transf=',trim(rotcig_par%tag_off_ddcig_transf)
  
		      if(auxinout(trim(rotcig_par%tag_off_ddcig_transf)) > 0) then
			       if(rotcig_par%verb)write(0,*)'Off_ddcig_transf already exists'
		      end if
  
		      rotcig_par%n_hpar=rotcig_par%n_hdip
		      rotcig_par%d_hpar=rotcig_par%d_hdip
		      rotcig_par%hpar_0=rotcig_par%hdip_0
	
		      call from_param('rotate_off_ddcig',rotcig_par%rotate_off_ddcig,.false.)
		      call from_param('n_hpar',rotcig_par%n_hpar,rotcig_par%n_hpar)
		      call from_param('d_hpar',rotcig_par%d_hpar,rotcig_par%d_hpar)
		      call from_param('hpar_0',rotcig_par%hpar_0,rotcig_par%hpar_0)
	    
		      esize=4
		      call to_history("esize",esize,trim(rotcig_par%tag_off_ddcig))
		      call to_history("n1",rotcig_par%n_z,trim(rotcig_par%tag_off_ddcig))
		      call to_history("n2",rotcig_par%n_mx,trim(rotcig_par%tag_off_ddcig))
		      call to_history("n3",rotcig_par%n_hpar,trim(rotcig_par%tag_off_ddcig))
		      call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_off_ddcig))
		      call to_history("d2",rotcig_par%d_mx,trim(rotcig_par%tag_off_ddcig))
		      call to_history("d3",rotcig_par%d_hpar,trim(rotcig_par%tag_off_ddcig))
		      call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_off_ddcig))
		      call to_history("o2",rotcig_par%mx_0,trim(rotcig_par%tag_off_ddcig))
		      call to_history("o3",rotcig_par%hpar_0,trim(rotcig_par%tag_off_ddcig))
		    
		      esize=8
		      call to_history("esize",esize,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("n1",rotcig_par%n_z_pad/2+1,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("n2",rotcig_par%n_hpar,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("n3",rotcig_par%n_mx_pad,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("d2",rotcig_par%d_hpar,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("d3",rotcig_par%d_mx,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("o2",rotcig_par%hpar_0,trim(rotcig_par%tag_off_ddcig_transf))
		      call to_history("o3",rotcig_par%mx_0,trim(rotcig_par%tag_off_ddcig_transf))
    !}
	    else
    !{
	   	      call from_param('n_hdip_pad',rotcig_par%n_hdip_pad,rotcig_par%n_hdip)
  	   	      call from_param('tag_ang_ddcig',rotcig_par%tag_ang_ddcig,'Ang_ddcig')
	   	      call from_param(trim(rotcig_par%tag_ang_ddcig),tag_name,'Ang_ddcig not found')
	       	      write(0,*)'rotcig_par%tag_ang_ddcig=',trim(rotcig_par%tag_ang_ddcig)
	 
		      call from_param('tag_ang_ddcig_transf',rotcig_par%tag_ang_ddcig_transf,'Ang_ddcig_transf')
		      call from_param(trim(rotcig_par%tag_ang_ddcig_transf),tag_name,'Ang_ddcig_transf not found')
		      write(0,*)'rotcig_par%tag_ang_ddcig_transf=',trim(rotcig_par%tag_ang_ddcig_transf)
	  
		      if(auxinout(trim(rotcig_par%tag_ang_ddcig_transf)) > 0) then
			        if(rotcig_par%verb)write(0,*)'Ang_ddcig_transf already exists'
		      end if
   
		      n_ang=81
		      ang_min=-80.
		      ang_max=80.
		  
		      write(0,*)'ang_min=',ang_min
		      write(0,*)'ang_max=',ang_max
	
		      call from_param('n_ang',rotcig_par%n_ang,n_ang)
		      call from_param('ang_min',ang_min,ang_min)
		      call from_param('ang_max',ang_max,ang_max)
		      write(0,*)'ang_min=',ang_min
		      write(0,*)'ang_max=',ang_max
		      rotcig_par%ang_0=ang_min
		      rotcig_par%d_ang=(ang_max-ang_min)/(n_ang-1)
		      write(0,*)'d_ang=',rotcig_par%d_ang
	
		      esize=4
		      call to_history("esize",esize,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("n1",rotcig_par%n_z,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("n2",rotcig_par%n_mx,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("n3",rotcig_par%n_ang,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("d2",rotcig_par%d_mx,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("d3",rotcig_par%d_ang,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("o2",rotcig_par%mx_0,trim(rotcig_par%tag_ang_ddcig))
		      call to_history("o3",rotcig_par%ang_0,trim(rotcig_par%tag_ang_ddcig))
	    
		      esize=8
		      call to_history("esize",esize,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("n1",rotcig_par%n_z_pad/2+1,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("n2",rotcig_par%n_ang,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("n3",rotcig_par%n_mx_pad,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("d2",rotcig_par%d_ang,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("d3",rotcig_par%d_mx,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("o2",rotcig_par%ang_0,trim(rotcig_par%tag_ang_ddcig_transf))
		      call to_history("o3",rotcig_par%mx_0,trim(rotcig_par%tag_ang_ddcig_transf))
    !}
    	end if
  !}
  else
  !{
    	if (rotcig_par%create_cig_tilt) then
    !{
    		    if(rotcig_par%n_threads /= 1) then
 		      call seperr("At the present n_threads must be == 1")
 		    end if

 		    call from_param('positive_rot',rotcig_par%positive_rot,.true.)
 		    call from_param('push_interp',rotcig_par%push_interp,.true.)

 		    call from_param('tag_cig_hx',rotcig_par%tag_cig_hx,'Cig_hx')
 		    call from_param(trim(rotcig_par%tag_cig_hx),tag_name,'Cig_hx not found')
 		    rotcig_par%cig_hx=auxin(trim(rotcig_par%tag_cig_hx)) > 0
 		    if(.not. rotcig_par%cig_hx) call seperr("Cig_hx missing")
	
		    call from_aux(trim(rotcig_par%tag_cig_hx),'n1',rotcig_par%n_z)
		    call from_aux(trim(rotcig_par%tag_cig_hx),'n2',rotcig_par%n_mx)
		    call from_aux(trim(rotcig_par%tag_cig_hx),'n3',rotcig_par%n_hdip)
  
		    call from_aux(trim(rotcig_par%tag_cig_hx),'d1',rotcig_par%d_z)
		    call from_aux(trim(rotcig_par%tag_cig_hx),'d2',rotcig_par%d_mx)
		    call from_aux(trim(rotcig_par%tag_cig_hx),'d3',rotcig_par%d_hdip)
    
		    call from_aux(trim(rotcig_par%tag_cig_hx),'o1',rotcig_par%z_0)
		    call from_aux(trim(rotcig_par%tag_cig_hx),'o2',rotcig_par%mx_0)
		    call from_aux(trim(rotcig_par%tag_cig_hx),'o3',rotcig_par%hdip_0)
    
		    call to_history("From Cig_hx: n1",rotcig_par%n_z)
		    call to_history("From Cig_hx: n2",rotcig_par%n_mx)
		    call to_history("From Cig_hx: n3",rotcig_par%n_hdip)
		    call to_history("From Cig_hx: d1",rotcig_par%d_z)
		    call to_history("From Cig_hx: d2",rotcig_par%d_mx)
		    call to_history("From Cig_hx: d3",rotcig_par%d_hdip)
		    call to_history("From Cig_hx: o1",rotcig_par%z_0)
		    call to_history("From Cig_hx: o2",rotcig_par%mx_0)
		    call to_history("From Cig_hx: o3",rotcig_par%hdip_0)
  
		    call from_param('n_z_pad',rotcig_par%n_z_pad,rotcig_par%n_z)
		    call from_param('n_mx_pad',rotcig_par%n_mx_pad,rotcig_par%n_mx)
		    call from_param('n_hdip_pad',rotcig_par%n_hdip_pad,rotcig_par%n_hdip)

		     call from_param('tag_cig_tilt',rotcig_par%tag_cig_tilt,'Cig_tilt')
		     call from_param(trim(rotcig_par%tag_cig_tilt),tag_name,'Cig_tilt not found')
		     write(0,*)'rotcig_par%tag_cig_tilt=',trim(rotcig_par%tag_cig_tilt)
  
		     rotcig_par%n_hpar=rotcig_par%n_hdip
		     rotcig_par%d_hpar=rotcig_par%d_hdip
		     rotcig_par%hpar_0=rotcig_par%hdip_0
    
		     call from_param('n_hpar',rotcig_par%n_hpar,rotcig_par%n_hpar)
		     call from_param('d_hpar',rotcig_par%d_hpar,rotcig_par%d_hpar)
		     call from_param('hpar_0',rotcig_par%hpar_0,rotcig_par%hpar_0)
    
		     esize=4
		     call to_history("esize",esize,trim(rotcig_par%tag_cig_tilt))
		     call to_history("n1",rotcig_par%n_z,trim(rotcig_par%tag_cig_tilt))
		     call to_history("n2",rotcig_par%n_mx,trim(rotcig_par%tag_cig_tilt))
		     call to_history("n3",rotcig_par%n_hpar,trim(rotcig_par%tag_cig_tilt))
		     call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_cig_tilt))
		     call to_history("d2",rotcig_par%d_mx,trim(rotcig_par%tag_cig_tilt))
		     call to_history("d3",rotcig_par%d_hpar,trim(rotcig_par%tag_cig_tilt))
		     call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_cig_tilt))
		     call to_history("o2",rotcig_par%mx_0,trim(rotcig_par%tag_cig_tilt))
		     call to_history("o3",rotcig_par%hpar_0,trim(rotcig_par%tag_cig_tilt))
    
    !}
	   else
    !{
		     call from_param('merge_hx_hz',rotcig_par%merge_hx_hz,.true.)
		     call from_param('linear_interp',rotcig_par%linear_interp,.true.)
		     call from_param('out_dip_center',rotcig_par%out_dip_center,0.)
		     call from_param('out_dip_range',rotcig_par%out_dip_range,180.)
 		 
		     call from_param('tag_cig_hz',rotcig_par%tag_cig_hz,'Cig_hz')
		     call from_param(trim(rotcig_par%tag_cig_hz),tag_name,'Cig_hz not found')
		     rotcig_par%cig_hz=auxin(trim(rotcig_par%tag_cig_hz)) > 0
			   
		     call from_param('tag_cig_hx',rotcig_par%tag_cig_hx,'Cig_hx')
		     call from_param(trim(rotcig_par%tag_cig_hx),tag_name,'Cig_hx not found')
		     rotcig_par%cig_hx=auxin(trim(rotcig_par%tag_cig_hx)) > 0 
  
  
		     call from_param('rot_center_z',rotcig_par%rot_center_z,rotcig_par%z_0)
		     call from_param('rot_center_mx',rotcig_par%rot_center_mx,rotcig_par%mx_0)
  
  
		     if_rot_cig_hx_1 : if (rotcig_par%cig_hx) then
    
			       call from_aux(trim(rotcig_par%tag_cig_hx),'n1',rotcig_par%n_z)
			       call from_aux(trim(rotcig_par%tag_cig_hx),'n2',rotcig_par%n_mx)
			       call from_aux(trim(rotcig_par%tag_cig_hx),'n3',rotcig_par%n_hx)
    
			       call from_aux(trim(rotcig_par%tag_cig_hx),'d1',rotcig_par%d_z)
			       call from_aux(trim(rotcig_par%tag_cig_hx),'d2',rotcig_par%d_mx)
			       call from_aux(trim(rotcig_par%tag_cig_hx),'d3',rotcig_par%d_hx)
    
			       call from_aux(trim(rotcig_par%tag_cig_hx),'o1',rotcig_par%z_0)
			       call from_aux(trim(rotcig_par%tag_cig_hx),'o2',rotcig_par%mx_0)
			       call from_aux(trim(rotcig_par%tag_cig_hx),'o3',rotcig_par%hx_0)
    
			       call to_history("From Cig_hx: n1",rotcig_par%n_z)
			       call to_history("From Cig_hx: n2",rotcig_par%n_mx)
			       call to_history("From Cig_hx: n3",rotcig_par%n_hx)
			       call to_history("From Cig_hx: d1",rotcig_par%d_z)
			       call to_history("From Cig_hx: d2",rotcig_par%d_mx)
			       call to_history("From Cig_hx: d3",rotcig_par%d_hx)
			       call to_history("From Cig_hx: o1",rotcig_par%z_0)
			       call to_history("From Cig_hx: o2",rotcig_par%mx_0)
			       call to_history("From Cig_hx: o3",rotcig_par%hx_0)
    
			       call from_param('n_z_pad',rotcig_par%n_z_pad,rotcig_par%n_z)
			       call from_param('n_mx_pad',rotcig_par%n_mx_pad,rotcig_par%n_mx)
    
			       call from_param('tag_cig_hx_rot_weight',rotcig_par%tag_cig_hx_rot_weight,'Cig_hx_rot_weight')
			       call from_param(trim(rotcig_par%tag_cig_hx_rot_weight),tag_name,'Cig_hx_rot_weight not found')
    
			       if(auxinout(trim(rotcig_par%tag_cig_hx_rot_weight)) > 0) then
				          if(rotcig_par%verb)write(0,*)'Cig_hx_rot_weight already exists'
			       end if
    
			       esize=4
			       call to_history("esize",esize,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("n1",rotcig_par%n_z,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("n2",rotcig_par%n_mx,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("n3",rotcig_par%n_hx,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("d2",rotcig_par%d_mx,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("d3",rotcig_par%d_hx,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("o2",rotcig_par%mx_0,trim(rotcig_par%tag_cig_hx_rot_weight))
			       call to_history("o3",rotcig_par%hx_0,trim(rotcig_par%tag_cig_hx_rot_weight))
	    
		      end if if_rot_cig_hx_1
  
		      if_rot_cig_hz_1 : if (rotcig_par%cig_hz) then
    
			        if (rotcig_par%cig_hx) then
				          call from_aux(trim(rotcig_par%tag_cig_hz),'n1',n)
				          if(n /= rotcig_par%n_z) call seperr('n1 in Cig_hz differs from Cig_hx')
				          call from_aux(trim(rotcig_par%tag_cig_hz),'n2',n)
				          if(n /= rotcig_par%n_mx) call seperr('n2 in Cig_hz differs from Cig_hx')
				          call from_aux(trim(rotcig_par%tag_cig_hz),'n3',rotcig_par%n_hz)
	      
				          call from_aux(trim(rotcig_par%tag_cig_hz),'d1',d)
				          if(d /= rotcig_par%d_z) call seperr('d1 in Cig_hz differs from Cig_hx')
				          call from_aux(trim(rotcig_par%tag_cig_hz),'d2',d)
				          if(d /= rotcig_par%d_mx) call seperr('d2 in Cig_hz differs from Cig_hx')
				          call from_aux(trim(rotcig_par%tag_cig_hz),'d3',rotcig_par%d_hz)
		      
				          call from_aux(trim(rotcig_par%tag_cig_hz),'o1',o)
				          if(o /= rotcig_par%z_0) call seperr('o1 in Cig_hz differs from Cig_hx')
				          call from_aux(trim(rotcig_par%tag_cig_hz),'o2',o)
				          if(o /= rotcig_par%mx_0) call seperr('o2 in Cig_hz differs from Cig_hx')
				          call from_aux(trim(rotcig_par%tag_cig_hz),'o3',rotcig_par%hz_0)
			        else
    
				          call from_aux(trim(rotcig_par%tag_cig_hz),'n1',rotcig_par%n_z)
				          call from_aux(trim(rotcig_par%tag_cig_hz),'n2',rotcig_par%n_mx)
				          call from_aux(trim(rotcig_par%tag_cig_hz),'n3',rotcig_par%n_hz)
      
				          call from_aux(trim(rotcig_par%tag_cig_hz),'d1',rotcig_par%d_z)
				          call from_aux(trim(rotcig_par%tag_cig_hz),'d2',rotcig_par%d_mx)
				          call from_aux(trim(rotcig_par%tag_cig_hz),'d3',rotcig_par%d_hz)
      
				          call from_aux(trim(rotcig_par%tag_cig_hz),'o1',rotcig_par%z_0)
				          call from_aux(trim(rotcig_par%tag_cig_hz),'o2',rotcig_par%mx_0)
				          call from_aux(trim(rotcig_par%tag_cig_hz),'o3',rotcig_par%hz_0)
   
				          call from_param('n_z_pad',rotcig_par%n_z_pad,rotcig_par%n_z)
				          call from_param('n_mx_pad',rotcig_par%n_mx_pad,rotcig_par%n_mx)
    
			        end if
  
			        call to_history("From Cig_hz: n1",rotcig_par%n_z)
			        call to_history("From Cig_hz: n2",rotcig_par%n_mx)
			        call to_history("From Cig_hz: n3",rotcig_par%n_hz)
			        call to_history("From Cig_hz: d1",rotcig_par%d_z)
			        call to_history("From Cig_hz: d2",rotcig_par%d_mx)
			        call to_history("From Cig_hz: d3",rotcig_par%d_hz)
			        call to_history("From Cig_hz: o1",rotcig_par%z_0)
			        call to_history("From Cig_hz: o2",rotcig_par%mx_0)
			        call to_history("From Cig_hz: o3",rotcig_par%hz_0)
  
			        call from_param('tag_cig_hz_rot_weight',rotcig_par%tag_cig_hz_rot_weight,'Cig_hz_rot_weight')
			        call from_param(trim(rotcig_par%tag_cig_hz_rot_weight),tag_name,'Cig_hz_rot_weight not found')
    
			        if(auxinout(trim(rotcig_par%tag_cig_hz_rot_weight)) > 0) then
				          if(rotcig_par%verb)write(0,*)'Cig_hz-rot_weight already exists'
			        end if
    
			        esize=4
			        call to_history("esize",esize,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("n1",rotcig_par%n_z,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("n2",rotcig_par%n_mx,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("n3",rotcig_par%n_hz,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("d2",rotcig_par%d_mx,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("d3",rotcig_par%d_hz,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("o2",rotcig_par%mx_0,trim(rotcig_par%tag_cig_hz_rot_weight))
			        call to_history("o3",rotcig_par%hz_0,trim(rotcig_par%tag_cig_hz_rot_weight))
   
		      end if if_rot_cig_hz_1
  
  
		      if(rotcig_par%merge_hx_hz) then
			       if ((.not. rotcig_par%cig_hx) .or. (.not. rotcig_par%cig_hz)) then
				        call seperr ('When merge_hx_hz both cig_hx and cig_hz must be present')
			       end if
  
			      call from_param('tag_cig_hrot_weight',rotcig_par%tag_cig_hrot_weight,'Cig_hrot_weight')
			      call from_param(trim(rotcig_par%tag_cig_hrot_weight),tag_name,'Cig_hrot_weight not found')
      !  rotcig_par%cig_hrot_weight=auxinout(trim(rotcig_par%tag_cig_hrot_weight))
			      write(0,*)'rotcig_par%tag_cig_hrot_weight=',trim(rotcig_par%tag_cig_hrot_weight)
  
			      rotcig_par%n_hrot=max(rotcig_par%n_hx,rotcig_par%n_hz)
			      rotcig_par%d_hrot=min(rotcig_par%d_hx,rotcig_par%d_hz)
			      rotcig_par%hrot_0=min(rotcig_par%hx_0,rotcig_par%hz_0)
  
			      call from_param('n_hrot',rotcig_par%n_hrot,rotcig_par%n_hrot)
			      call from_param('d_hrot',rotcig_par%d_hrot,rotcig_par%d_hrot)
			      call from_param('hrot_0',rotcig_par%hrot_0,rotcig_par%hrot_0)
  
			      call to_history("n1",rotcig_par%n_z,trim(rotcig_par%tag_cig_hrot_weight))
			      call to_history("n2",rotcig_par%n_mx,trim(rotcig_par%tag_cig_hrot_weight))
			      call to_history("n3",rotcig_par%n_hrot,trim(rotcig_par%tag_cig_hrot_weight))
			      call to_history("d1",rotcig_par%d_z,trim(rotcig_par%tag_cig_hrot_weight))
			      call to_history("d2",rotcig_par%d_mx,trim(rotcig_par%tag_cig_hrot_weight))
			      call to_history("d3",rotcig_par%d_hrot,trim(rotcig_par%tag_cig_hrot_weight))
			      call to_history("o1",rotcig_par%z_0,trim(rotcig_par%tag_cig_hrot_weight))
			      call to_history("o2",rotcig_par%mx_0,trim(rotcig_par%tag_cig_hrot_weight))
			      call to_history("o3",rotcig_par%hrot_0,trim(rotcig_par%tag_cig_hrot_weight))
  
		      end if
	    end if
  end if
  
  
  end subroutine param_rotcig

end module 
