      program ssmis_rdfcdr

      use netcdf
      implicit none

      include 'params.inc'

!--- Declaration of internal subroutine variables.

      character (len=100) ::   input_file
      character (len=100) ::   output_file

      integer :: n, ios
      integer :: n_arguments, iargc
      logical :: fexists
      integer :: lastchar,ret=0,lin=1

      integer :: idate

!--- variable ids

      integer ::   ncid, igranule, nscan
  
      integer ::   nscan_dimid, rev_varid
      integer ::   sclat_varid, sclon_varid, scalt_varid
      integer ::   scantime_varid, datetime_varid
      integer ::   lat_env1_varid, lon_env1_varid, eia_env1_varid
      integer ::   sun_env1_varid, qual_env1_varid
      integer ::   lat_env2_varid, lon_env2_varid, eia_env2_varid
      integer ::   sun_env2_varid, qual_env2_varid
      integer ::   lat_img1_varid, lon_img1_varid, eia_img1_varid
      integer ::   sun_img1_varid, qual_img1_varid
      integer ::   lat_img2_varid, lon_img2_varid, eia_img2_varid
      integer ::   sun_img2_varid, qual_img2_varid
      integer ::   lat_las_varid, lon_las_varid, eia_las_varid
      integer ::   sun_las_varid, qual_las_varid
      integer ::   lat_uas_varid, lon_uas_varid, eia_uas_varid
      integer ::   sun_uas_varid, qual_uas_varid
      integer ::   tb19v_varid, tb19h_varid, tb22v_varid
      integer ::   tb37v_varid, tb37h_varid, tb91v_varid, tb91h_varid

      real(kind=knd8),    allocatable :: rev(:)
      real(kind=knd8),    allocatable :: scantime(:)
      character(len=23),  allocatable :: datetime(:)
      real,               allocatable :: sclat(:)
      real,               allocatable :: sclon(:)
      real,               allocatable :: scalt(:)

      real,               allocatable :: lat_env1(:,:)
      real,               allocatable :: lon_env1(:,:)
      real,               allocatable :: eia_env1(:,:)
      integer(kind=knd1), allocatable :: sun_env1(:,:)
      integer(kind=knd1), allocatable :: qual_env1(:,:)

      real,               allocatable :: lat_env2(:,:)
      real,               allocatable :: lon_env2(:,:)
      real,               allocatable :: eia_env2(:,:)
      integer(kind=knd1), allocatable :: sun_env2(:,:)
      integer(kind=knd1), allocatable :: qual_env2(:,:)

      real,               allocatable :: tb19v(:,:)
      real,               allocatable :: tb19h(:,:)
      real,               allocatable :: tb22v(:,:)
      real,               allocatable :: tb37v(:,:)
      real,               allocatable :: tb37h(:,:)

      real,               allocatable :: lat_img1(:,:)
      real,               allocatable :: lon_img1(:,:)
      real,               allocatable :: eia_img1(:,:)
      integer(kind=knd1), allocatable :: sun_img1(:,:)
      integer(kind=knd1), allocatable :: qual_img1(:,:)

      real,               allocatable :: lat_img2(:,:)
      real,               allocatable :: lon_img2(:,:)
      real,               allocatable :: eia_img2(:,:)
      integer(kind=knd1), allocatable :: sun_img2(:,:)
      integer(kind=knd1), allocatable :: qual_img2(:,:)

      real,               allocatable :: tb91v(:,:)
      real,               allocatable :: tb91h(:,:)

      character (len=100) ::   file
      character (len=100) ::   satellite
      character (len=100) ::   sensor
      character (len=100) ::   author
      character (len=100) ::   version
      character (len=100) ::   contact
      character (len=100) ::   inpfile
      character (len=100) ::   cgranule
      character (len=100) ::   filename
      character (len=100) ::   created
      character (len=100) ::   sdate
      character (len=100) ::   edate


!----------------------------------------------------------------------

!---check if file exists -------------------
    
      n_arguments = iargc()
      if(n_arguments .ne. 2) then
          write(*,*)' error in command line arguments '
          write(*,*)' <input file>  <output_file>'
          stop
      endif
      call getarg(1, input_file)     ! output filename 
      call getarg(2, output_file)    ! input filename 

      inquire(file=input_file,exist = fexists)
      if (fexists .eq. .false.) then
        write(6,*) 'Input file ',input_file,' not found!'
      endif
      
!--- start opening and reading netcdf fcdr file

      call check(nf90_open(input_file, nf90_NoWrite, ncid))

!--- Read nscan dimension value

      call check(nf90_inq_dimid(ncid, "nscan",  nscan_dimid))
      call check(nf90_inquire_dimension(ncid, nscan_dimid, len = nscan))
      write(6,*) 'nscan = ',nscan

!--- Allocate data arrays
 
      allocate(rev(nscan))
      allocate(scantime(nscan))
      allocate(datetime(nscan))
      allocate(sclat(nscan))
      allocate(sclon(nscan))
      allocate(scalt(nscan))

      allocate(lat_env1(nenv, nscan))
      allocate(lon_env1(nenv, nscan))
      allocate(eia_env1(nenv, nscan))
      allocate(sun_env1(nenv, nscan))
      allocate(qual_env1(nenv, nscan))

      allocate(lat_env2(nenv, nscan))
      allocate(lon_env2(nenv, nscan))
      allocate(eia_env2(nenv, nscan))
      allocate(sun_env2(nenv, nscan))
      allocate(qual_env2(nenv, nscan))
      
      allocate(tb19v(nenv,nscan))
      allocate(tb19h(nenv,nscan))
      allocate(tb22v(nenv,nscan))
      allocate(tb37v(nenv,nscan))
      allocate(tb37h(nenv,nscan))

      allocate(lat_img1(nimg, nscan))
      allocate(lon_img1(nimg, nscan))
      allocate(eia_img1(nimg, nscan))
      allocate(sun_img1(nimg, nscan))
      allocate(qual_img1(nimg, nscan))

      allocate(lat_img2(nimg, nscan))
      allocate(lon_img2(nimg, nscan))
      allocate(eia_img2(nimg, nscan))
      allocate(sun_img2(nimg, nscan))
      allocate(qual_img2(nimg, nscan))

      allocate(tb91v(nimg, nscan))
      allocate(tb91h(nimg, nscan))

!--- Read global attribute metadata fields

      call check(nf90_get_att(ncid,nf90_global,"creator_name",author))
      call check(nf90_get_att(ncid,nf90_global,"creator_email",contact))
      call check(nf90_get_att(ncid,nf90_global,"product_version",version))
      call check(nf90_get_att(ncid,nf90_global,"platform",satellite))
      call check(nf90_get_att(ncid,nf90_global,"sensor",sensor))
      call check(nf90_get_att(ncid,nf90_global,"orbit_number",cgranule))
      call check(nf90_get_att(ncid,nf90_global,"source",inpfile))
      call check(nf90_get_att(ncid,nf90_global,"id",filename))
      call check(nf90_get_att(ncid,nf90_global,"time_coverage_start",sdate))
      call check(nf90_get_att(ncid,nf90_global,"time_coverage_end",edate))
      call check(nf90_get_att(ncid,nf90_global,"date_created",created))
        
      write(6,*) '  author         : ',trim(author)
      write(6,*) '  contact        : ',trim(contact)
      write(6,*) '  version        : ',trim(version)
      write(6,*) '  satellite      : ',trim(satellite)
      write(6,*) '  sensor         : ',trim(sensor)
      
      read(cgranule,'(i5.5)') igranule
      write(6,9)'   granule        : ',igranule
  9   format(a,i5.5)
  
      write(6,*) '  inpfile        : ',trim(inpfile)
      write(6,*) '  filename       : ',trim(filename)
      write(6,*) '  startdate      : ',trim(sdate)
      write(6,*) '  enddate        : ',trim(edate)
      write(6,*) '  created        : ',trim(created)
      write(6,*) '  nscan          : ',nscan

!--- Get variable IDs 

      call check(nf90_inq_varid(ncid, "orbit",           rev_varid))
      call check(nf90_inq_varid(ncid, "spacecraft_lat",  sclat_varid))
      call check(nf90_inq_varid(ncid, "spacecraft_lon",  sclon_varid))
      call check(nf90_inq_varid(ncid, "spacecraft_alt",  scalt_varid))
      call check(nf90_inq_varid(ncid, "scan_time",       scantime_varid))
      call check(nf90_inq_varid(ncid, "scan_datetime",   datetime_varid))

      call check(nf90_inq_varid(ncid, "lat_env1",        lat_env1_varid))
      call check(nf90_inq_varid(ncid, "lon_env1",        lon_env1_varid))
      call check(nf90_inq_varid(ncid, "eia_env1",        eia_env1_varid))
      call check(nf90_inq_varid(ncid, "sun_glint_env1",  sun_env1_varid))
      call check(nf90_inq_varid(ncid, "quality_env1",    qual_env1_varid))

      call check(nf90_inq_varid(ncid, "lat_env2",        lat_env2_varid))
      call check(nf90_inq_varid(ncid, "lon_env2",        lon_env2_varid))
      call check(nf90_inq_varid(ncid, "eia_env2",        eia_env2_varid))
      call check(nf90_inq_varid(ncid, "sun_glint_env2",  sun_env2_varid))
      call check(nf90_inq_varid(ncid, "quality_env2",    qual_env2_varid))

      call check(nf90_inq_varid(ncid, "lat_img1",        lat_img1_varid))
      call check(nf90_inq_varid(ncid, "lon_img1",        lon_img1_varid))
      call check(nf90_inq_varid(ncid, "eia_img1",        eia_img1_varid))
      call check(nf90_inq_varid(ncid, "sun_glint_img1",  sun_img1_varid))
      call check(nf90_inq_varid(ncid, "quality_img1",    qual_img1_varid))

      call check(nf90_inq_varid(ncid, "lat_img2",        lat_img2_varid))
      call check(nf90_inq_varid(ncid, "lon_img2",        lon_img2_varid))
      call check(nf90_inq_varid(ncid, "eia_img2",        eia_img2_varid))
      call check(nf90_inq_varid(ncid, "sun_glint_img2",  sun_img2_varid))
      call check(nf90_inq_varid(ncid, "quality_img2",    qual_img2_varid))

      call check(nf90_inq_varid(ncid, "fcdr_tb19v_env1", tb19v_varid))
      call check(nf90_inq_varid(ncid, "fcdr_tb19h_env1", tb19h_varid))
      call check(nf90_inq_varid(ncid, "fcdr_tb22v_env1", tb22v_varid))
      call check(nf90_inq_varid(ncid, "fcdr_tb37v_env2", tb37v_varid))
      call check(nf90_inq_varid(ncid, "fcdr_tb37h_env2", tb37h_varid))

      call check(nf90_inq_varid(ncid, "fcdr_tb91v_img2", tb91v_varid))
      call check(nf90_inq_varid(ncid, "fcdr_tb91h_img2", tb91h_varid))

!--- Read variables

      call check(nf90_get_var(ncid, rev_varid,       rev))     
      call check(nf90_get_var(ncid, sclat_varid,     sclat))
      call check(nf90_get_var(ncid, sclon_varid,     sclon))
      call check(nf90_get_var(ncid, scalt_varid,     scalt))
      call check(nf90_get_var(ncid, scantime_varid,  scantime))
      call check(nf90_get_var(ncid, datetime_varid,  datetime))

      call check(nf90_get_var(ncid, lat_env1_varid,  lat_env1))
      call check(nf90_get_var(ncid, lon_env1_varid,  lon_env1))
      call check(nf90_get_var(ncid, eia_env1_varid,  eia_env1))
      call check(nf90_get_var(ncid, sun_env1_varid,  sun_env1))
      call check(nf90_get_var(ncid, qual_env1_varid, qual_env1))

      call check(nf90_get_var(ncid, lat_env2_varid,  lat_env2))
      call check(nf90_get_var(ncid, lon_env2_varid,  lon_env2))
      call check(nf90_get_var(ncid, eia_env2_varid,  eia_env2))
      call check(nf90_get_var(ncid, sun_env2_varid,  sun_env2))
      call check(nf90_get_var(ncid, qual_env2_varid, qual_env2))

      call check(nf90_get_var(ncid, lat_img1_varid,  lat_img1))
      call check(nf90_get_var(ncid, lon_img1_varid,  lon_img1))
      call check(nf90_get_var(ncid, eia_img1_varid,  eia_img1))
      call check(nf90_get_var(ncid, sun_img1_varid,  sun_img1))
      call check(nf90_get_var(ncid, qual_img1_varid, qual_img1))

      call check(nf90_get_var(ncid, lat_img2_varid,  lat_img2))
      call check(nf90_get_var(ncid, lon_img2_varid,  lon_img2))
      call check(nf90_get_var(ncid, eia_img2_varid,  eia_img2))
      call check(nf90_get_var(ncid, sun_img2_varid,  sun_img2))
      call check(nf90_get_var(ncid, qual_img2_varid, qual_img2))

      call check(nf90_get_var(ncid, tb19v_varid,     tb19v))
      call check(nf90_get_var(ncid, tb19h_varid,     tb19h))
      call check(nf90_get_var(ncid, tb22v_varid,     tb22v))
      call check(nf90_get_var(ncid, tb37v_varid,     tb37v))
      call check(nf90_get_var(ncid, tb37h_varid,     tb37h))
      call check(nf90_get_var(ncid, tb91v_varid,     tb91v))
      call check(nf90_get_var(ncid, tb91h_varid,     tb91h))
      
!--- Close the input data file

      call check(nf90_close(ncid))

!--- Write data to output file

      open(unit=10, file=trim(output_file), form='formatted', status='unknown', iostat=ios)
      do n=1,nscan
        write(10,'("Scan: ",i4,", Date: ",a,", SCpos: ",3(f7.2,x))') n,datetime(n),sclat(n),sclon(n),scalt(n)
      enddo

!--- Close the output data file

      close(10)

      contains      

      subroutine check(status)

      integer, intent (in) :: status
      if (status /= nf90_noerr) then
        print *, 'error in check ',trim(nf90_strerror(status))
        stop "Stopped"
      end if
      return
      end subroutine check

      end  program ssmis_rdfcdr
