c A program for calculate the good exposure time for mag-limit=22.5
c Example: maglimit-zx dmag sky seeing exposur-time
c The result is new exposure time for magnitue limit = 22.5
      character*2880 headline
      character*100 imgname(2000),objname
      character*10 cdmag,caduskypix,cseeing,cexp

      real magsdss,magsdss0
      integer number_fits
  
      call getarg(1,cdmag)
      call getarg(2,caduskypix)
      call getarg(3,cseeing)
      call getarg(4,cexp)
      k=0
      k=lnblnk(cexp)
      if(k.lt.1) then
         write(*,*) ' A program for calculate the good exposure'//
     +        ' time for mag-limit=22.5'
         write(*,*) ' Example: maglimit-zx dmag sky seeing'//
     +        ' exposur-time'
         write(*,*) ' The result is new exposure time for '//
     +        'magnitue limit = 22.5'
         stop
      endif
      read(cdmag,*) dmag1
      read(caduskypix,*) aduskypix1
      read(cseeing,*) seeing
      read(cexp,*) texp
      ddmag0=99.99
      do nr=30,190
         ratio=nr/100.
         dmag=dmag1+2.5*log10(ratio)
         aduskypix=ratio*aduskypix1
c          write(*,'(a8,3x,3f9.3)') objname,dmag,aduskypix,seeing

c gain=1.5 e/adu
c      dmag=4.27
c      aduskypix=202.0
c      seeing=1.97
c seeing is arcsecond
          s2n=5.0
          readnoise=10.0
c  electron
          gain=1.5
c  gain: e/adu
        adusky=aduskypix*3.14*(2.0*seeing/2.0/0.454)**2.0
        aduread=readnoise**2.0/gain*3.14*(2.0*seeing/2.0/0.454)**2.0
c     adustar is dn of a star
c     mag=-2.5*log10(adustar)+25.0
c     2.5*log10(adustar)=25.0-mag
c     log10(adustar)=(25.0-mag)/2.5=0.4*(25.0-mag)
          adustar=10.0**(0.4*(25.0-mag))
c     magsdss=mag+dmag
          adustar=10.0**(0.4*(25.0-magsdss+dmag))
      
c      estar=gain*adustar
c      esky=gain*adusky
c      s2n=estar/sqrt(estar+esky+readnoise**2)
c         =sqrt(gain)*adustar/sqrt(adustar+adusky+(readnoise/gain)**2)
ctian   =sqrt(gain)*adustar/sqrt(adustar+adusky+(readnoise/sqrt(gain))**2)
cz       aduread=(readnoise/gain)**2
        
c         =sqrt(gain)*adustar/sqrt(adustar+adusky-aduread)
c      s2n**2=gain*adustar**2/(adustar+adusky+aduread)
c      s2n**2*adustar+s2n**2*(adusky+aduread)=gain*adustar**2
c      gain*adustar**2-s2n**2*adustar-s2n**2*(adusky+aduread)=0

          a=gain
          b=-s2n**2
          c=-s2n**2*(adusky+aduread)
      
          adustar=(sqrt(b*b-4*a*c)-b)/2/a
          vmag=-2.5*log10(adustar)+25.0
          magsdss=vmag+dmag


c          open(40,file=imgname(i)(kk-12:kk)//'log')

c          write(40,'(a13,a3,4x,a10,3x,3f9.3,2x,f8.3)') 
c     +         imgname(i)(kk-12:kk),
c     +         'fit',objname,dmag, aduskypix,seeing,magsdss
c         close(40)
         ddmag=abs(magsdss-22.5)
         if(ddmag.lt.ddmag0) then
            ddmag0=ddmag
            dmag0=dmag
            aduskypix0=aduskypix
            magsdss0=magsdss
            ratio0=ratio
         endif
c          write(*,'(3f9.3,2x,2f8.3)') 
c     +     dmag, aduskypix,seeing,magsdss,ratio
      enddo
c          write(*,'(3f9.3,2x,2f8.3)') 
c     +     dmag0, aduskypix0,seeing,magsdss0,ratio0
      write(*,'(a,f5.0)') 
     + 'For maglimit=22.5, the exposure time should be ',
     +     texp*ratio0
      stop
      end
