! input of data
! ngmax: the maximum number of granules detected in one samplping time window
! scale: the number of km per pixel

	subroutine input(ngmax,the_scale)
	use param_run
	use mgetpar
	use readfits
!	use map_dxy
	use m_time_set
	use thename

	implicit none

	real the_scale,s0,surfac
        real, pointer :: a(:,:)
        character equid*3,segm*3
	integer i,ii,ngmax,naxes(2)
	character*128, pointer :: frame(:)
	character*80 :: file_of_compl

 
	call read_stdin
	option=fetch('option','vitesse')
	convolution=fetch('convolution','Id')
	nconv=fetch('nconv',5)     ! size of the convolving window in pix
	ip_start=fetch('ip_start',1)  ! number of the first image
	np=fetch('np',2)
	n_ech = fetch('n_ech',2)
	n_life = fetch('n_life',60)
	ngmax0 = fetch('ngmax0',100)
	ngmax = fetch('ngmax',100)
	istep = fetch('istep',1)
	prec = fetch('precision',0.5)
	bin_sp = fetch('bin_sp',6)
	delta_pix = fetch('delta_pix',0.2)
	arcsec = fetch('arcsec',728e3)
	pixel = fetch('pixel',0.14)
	the_scale=arcsec*pixel
	timestep = fetch('timestep',20.)
	timestep=timestep*abs(istep)
	v_thres=fetch('v_thres',5.0)
	seuil_min=fetch('seuil_min',0.5)
	t_thres=fetch('t_thres',0.0)
	seuil=v_thres*timestep/pixel/arcsec
	print*,'threshold for ID in pixel', seuil

	input_file=fetch('input_file',' ')
	prefix=fetch('prefix','nodir')
	suffix=fetch('suffix','.fits')
	postprocessingpath=fetch('ppp','results/')
	check=fetch('check','no')
	verbose=fetch('verbose',0)
        print*,'verbo',verbose
	wavelet=fetch('wavelet','daub4') ! other options =daub8,splines,lemarie
! Segmentation parameters
	segm=fetch('segmentation','no')
	segmented=(segm == 'yes')
	typeseg=fetch('segmentation_type','strouss')
	ech_lap=fetch('ech_lap',1.5)
	seuil_lap=fetch('seuil_lap',10.)
	seuil_rel=fetch('seuil_rel',0.4)
	seuil_courb=fetch('seuil_courb',0.1)
	seuil_bas=fetch('seuil_bas',-10.)
	seuil_haut=fetch('seuil_haut',10.)
	n_moy=fetch('n_moy',5)


! The default time-sampling is assumed regular
	equid=fetch('equidistance','yes')
	equidistance=(equid == 'yes')
        if (.not. equidistance) the_time_file=fetch('the_times','nofiletime')
        print*,'Time file = ',the_time_file

! Number of time-series
	allocate(nbs(np))
	nbs_max=fetch('nb_series',1)
	n_ref=fetch('n_ref',1)
	nbs(:)=nbs_max
	if (nbs_max > 1) then 
	! Here we read the file containing the number of (good) images
	! at each time step
	  file_of_compl=fetch('file_of_completeness','nofile')
	  if (file_of_compl /= 'nofile') then
	   open(unit=1,file=file_of_compl)
	    read(1,*) nbs(1:np)
	   close(1)
	  endif
	! you must give the place of the distorsion maps
          path_distor=fetch('path_distor','distor')
!	  call setup_themap(nx,ny)
	endif

! parametres de la sous-image
	sub_im=1
	nx1=fetch('nx1',1)
	nx2=fetch('nx2',9999)
	ny1=fetch('ny1',1)
	ny2=fetch('ny2',9999)
	maxdim=fetch('maxdim',9999)
        nx=nx2-nx1+1; ny=ny2-ny1+1

	if (nx2== 9999 .or. ny2==9999) then
	 call the_name(1,frame)
         call readimage(frame(1),a,naxes)
         print*,'read frame in input : ',frame(1)
         nx2=naxes(1); ny2=naxes(2)
         if ( .not. associated(a)) print*,'pas d''image lue en input, nx2,ny2',nx2,ny2
	 if (associated(a)) deallocate(a)
       
         nx=nx2-nx1+1; ny=ny2-ny1+1
         if (nx <0 .or. ny <0 .or. nx>naxes(1) .or. ny>naxes(2)) &
               stop'in input.f90: the sub-image is larger than the image!'
	endif

! Surface of the image determines the maximum number of granules (ngmax)

	surfac=(nx2-nx1)*(ny2-ny1)*pixel**2 ! in arcsec^2
!	ngmax0=2300+700*n_ech*istep
!	ngmax0=230+70*n_ech*istep
	s0=4096
!	ngmax=2*ngmax0*surfac/s0         ! facteur 2 pour la surete
!	ngmax=900000  ! pour 1.8Go (altair)
!	ngmax=1000000 ! pour soleil2
!	ngmax=1000000 ! pour soleil2
!	ngmax=500000  ! pour 1.8Go (altair)
!	ngmax=700000  ! pour 1.8Go (altair)
!	ngmax=350000 ! pour soleil2 avec cst
!	ngmax=30000 ! pour soleil2 avec cst_new

	print*,'le ngmax =',ngmax, 'ngmax0 =',ngmax0

! test a priori: la vitesse max est de l'ordre de 1km/s, si la frequence
! d'echantillonnage est trop haute on ne peut atteindre une grande
! precision
	if (prec < delta_pix*pixel*arcsec/n_ech/timestep/1000) &
	    stop'precision required is too high !'

! the set of dates in seconds : if irregular they should be user-supplied

        if (equidistance) then 
	 allocate(t(ip_start+istep*np))
	 t=(/ (i*timestep, i=0,ip_start+istep*np-1) /)
	else 
	 call the_time_set(the_time_file)
	endif

	end
