  program ssmi_rdfcdr_v1

  use netcdf
  implicit none

  include 'params.inc'

  !--- Declaration of internal subroutine variables.

  integer            :: n_arguments, iargc
  character(len=256) :: input_file
  character(len=256) :: output_file

  integer :: nscanl, nscanh
  integer :: ic, ipix, chan, nx, nx2
  integer :: i, j, n, ios
  real    :: sec
  logical :: fexists
  integer :: lastchar,ret=0,lin=1
  character(len=100) :: dname

  integer :: idate

  !--- variable ids

  integer ::   ncid, igranule
  integer ::   nscanl_dim, nscanh_dim
  integer ::   rev_lr_var, time_lr_var, date_lr_var
  integer ::   sclat_lr_var, sclon_lr_var, scalt_lr_var
  integer ::   lat_lr_var, lon_lr_var, eia_lr_var
  integer ::   sun_lr_var, qual_lr_var
  integer ::   rev_hr_var, time_hr_var, date_hr_var
  integer ::   sclat_hr_var, sclon_hr_var, scalt_hr_var
  integer ::   lat_hr_var, lon_hr_var, eia_hr_var
  integer ::   sun_hr_var, qual_hr_var
  integer ::   tb19v_var, tb19h_var, tb22v_var
  integer ::   tb37v_var, tb37h_var, tb85v_var, tb85h_var

  real*8,             allocatable :: scantime_lr(:)
  real*8,             allocatable :: rev_lr(:)
  character(len=23),  allocatable :: scandate_lr(:)
  real,               allocatable :: sclat_lr(:)
  real,               allocatable :: sclon_lr(:)
  real,               allocatable :: scalt_lr(:)

  real,               allocatable :: lat_lr(:,:)
  real,               allocatable :: lon_lr(:,:)
  real,               allocatable :: eia_lr(:,:)
  integer,            allocatable :: sun_lr(:,:)
  integer,            allocatable :: qual_lr(:,:)
      
  real,               allocatable :: tb19v(:,:)
  real,               allocatable :: tb19h(:,:)
  real,               allocatable :: tb22v(:,:)
  real,               allocatable :: tb37v(:,:)
  real,               allocatable :: tb37h(:,:)

  real*8,             allocatable :: scantime_hr(:)
  real*8,             allocatable :: rev_hr(:)
  character(len=23),  allocatable :: scandate_hr(:)
  real,               allocatable :: sclat_hr(:)
  real,               allocatable :: sclon_hr(:)
  real,               allocatable :: scalt_hr(:)

  real,               allocatable :: lat_hr(:,:)
  real,               allocatable :: lon_hr(:,:)
  real,               allocatable :: eia_hr(:,:)
  integer,            allocatable :: sun_hr(:,:)
  integer,            allocatable :: qual_hr(:,:)

  real,               allocatable :: tb85v(:,:)
  real,               allocatable :: tb85h(:,:)

  character (len=100) ::   file
  character (len=100) ::   title
  character (len=100) ::   author
  character (len=100) ::   contact
  character (len=100) ::   url
  character (len=100) ::   institution
  character (len=100) ::   version
  character (len=100) ::   revision_date
  character (len=100) ::   satellite
  character (len=100) ::   sensor
  character (len=100) ::   cgranule
  character (len=100) ::   inpfile
  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(6,*) 'Usage: ssmi_rdfcdr_v1 <input_file>  <output_file>'
    stop
  endif
  call getarg(1, input_file)         !input filename
  call getarg(2, output_file)        !output filename
  
  inquire(file=input_file, exist = fexists)
  if (fexists .eq. .false.) then
    write(6,*) 'Input file ',input_file,' not found!'
  endif
     
  open(unit=10, file=output_file, form='formatted', status='unknown', iostat = ios)
 
  !--- start opening and reading netcdf fcdr file

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

  !--- Read NscanLowRes and NscanHighRes

  call check(nf90_inq_dimid(ncid, "nscan_lores", nscanl_dim))
  call check(nf90_inq_dimid(ncid, "nscan_hires", nscanh_dim))
  call check(nf90_inquire_dimension(ncid, nscanl_dim, dname, nscanl))
  call check(nf90_inquire_dimension(ncid, nscanh_dim, dname, nscanh))

  write(6,'(a,i4)') ' Nscan lores   = ',nscanl
  write(6,'(a,i4,/)') ' Nscan hires   = ',nscanh

  !--- Allocate data arrays
 
  allocate(scantime_lr(nscanl))
  allocate(scandate_lr(nscanl))
  allocate(rev_lr(nscanl))
  allocate(sclat_lr(nscanl))
  allocate(sclon_lr(nscanl))
  allocate(scalt_lr(nscanl))

  allocate(lat_lr(NL, nscanl))
  allocate(lon_lr(NL, nscanl))
  allocate(eia_lr(NL, nscanl))
  allocate(sun_lr(NL, nscanl))
  allocate(qual_lr(NL, nscanl))
      
  allocate(tb19v(NL,nscanl))
  allocate(tb19h(NL,nscanl))
  allocate(tb22v(NL,nscanl))
  allocate(tb37v(NL,nscanl))
  allocate(tb37h(NL,nscanl))

  allocate(scantime_hr(nscanh))
  allocate(scandate_hr(nscanh))
  allocate(rev_hr(nscanh))
  allocate(sclat_hr(nscanh))
  allocate(sclon_hr(nscanh))
  allocate(scalt_hr(nscanh))

  allocate(lat_hr(NH, nscanh))
  allocate(lon_hr(NH, nscanh))
  allocate(eia_hr(NH, nscanh))
  allocate(sun_hr(NH, nscanh))
  allocate(qual_hr(NH, nscanh))

  allocate(tb85v(NH, nscanh))
  allocate(tb85h(NH, nscanh))

  !--- Read global attribute metadata fields

  call check(nf90_get_att(ncid,nf90_global,"title",title))
  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,"creator_url",url))
  call check(nf90_get_att(ncid,nf90_global,"institution",institution))
  call check(nf90_get_att(ncid,nf90_global,"product_version",version))
  call check(nf90_get_att(ncid,nf90_global,"revision_date",revision_date))
  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,'(a,a)') ' Title         = ',trim(title)
  write(6,'(a,a)') ' Author        = ',trim(author)
  write(6,'(a,a)') ' Contact       = ',trim(contact)
  write(6,'(a,a)') ' URL           = ',trim(url)
  write(6,'(a,a)') ' Institution   = ',trim(institution)
  write(6,'(a,a)') ' Version       = ',trim(version)
  write(6,'(a,a)') ' Revision Date = ',trim(revision_date)
  write(6,'(a,a)') ' Satellite     = ',trim(satellite)
  write(6,'(a,a)') ' Sensor        = ',trim(sensor)
  write(6,'(a,a)') ' Orbit Number  = ',trim(cgranule)
  write(6,'(a,a)') ' Source File   = ',trim(inpfile)
  write(6,'(a,a)') ' Filename      = ',trim(filename)
  write(6,'(a,a)') ' Granule Start = ',trim(sdate)
  write(6,'(a,a)') ' Granule End   = ',trim(edate)
  write(6,'(a,a)') ' Creation Date = ',trim(created)
  read(cgranule,'(i5.5)') igranule
  write(6,'(a,i5.5)') ' Granule       = ',igranule
  
  !--- Get variable IDs 

  call check(nf90_inq_varid(ncid, "orbit_lores",          rev_lr_var))
  call check(nf90_inq_varid(ncid, "spacecraft_lat_lores", sclat_lr_var))
  call check(nf90_inq_varid(ncid, "spacecraft_lon_lores", sclon_lr_var))
  call check(nf90_inq_varid(ncid, "spacecraft_alt_lores", scalt_lr_var))
  call check(nf90_inq_varid(ncid, "scan_time_lores",      time_lr_var))
  call check(nf90_inq_varid(ncid, "scan_datetime_lores",  date_lr_var))

  call check(nf90_inq_varid(ncid, "lat_lores",            lat_lr_var))
  call check(nf90_inq_varid(ncid, "lon_lores",            lon_lr_var))
  call check(nf90_inq_varid(ncid, "eia_lores",            eia_lr_var))
  call check(nf90_inq_varid(ncid, "sun_glint_lores",      sun_lr_var))
  call check(nf90_inq_varid(ncid, "quality_lores",        qual_lr_var))
  call check(nf90_inq_varid(ncid, "fcdr_tb19v",           tb19v_var))
  call check(nf90_inq_varid(ncid, "fcdr_tb19h",           tb19h_var))
  call check(nf90_inq_varid(ncid, "fcdr_tb22v",           tb22v_var))
  call check(nf90_inq_varid(ncid, "fcdr_tb37v",           tb37v_var))
  call check(nf90_inq_varid(ncid, "fcdr_tb37h",           tb37h_var))

  call check(nf90_inq_varid(ncid, "orbit_hires",          rev_hr_var))
  call check(nf90_inq_varid(ncid, "spacecraft_lat_hires", sclat_hr_var))
  call check(nf90_inq_varid(ncid, "spacecraft_lon_hires", sclon_hr_var))
  call check(nf90_inq_varid(ncid, "spacecraft_alt_hires", scalt_hr_var))
  call check(nf90_inq_varid(ncid, "scan_time_hires",      time_hr_var))
  call check(nf90_inq_varid(ncid, "scan_datetime_hires",  date_hr_var))

  call check(nf90_inq_varid(ncid, "lat_hires",            lat_hr_var))
  call check(nf90_inq_varid(ncid, "lon_hires",            lon_hr_var))
  call check(nf90_inq_varid(ncid, "eia_hires",            eia_hr_var))
  call check(nf90_inq_varid(ncid, "sun_glint_hires",      sun_hr_var))
  call check(nf90_inq_varid(ncid, "quality_hires",        qual_hr_var))
  call check(nf90_inq_varid(ncid, "fcdr_tb85v",           tb85v_var))
  call check(nf90_inq_varid(ncid, "fcdr_tb85h",           tb85h_var))

  !--- Read variables

  call check(nf90_get_var(ncid, rev_lr_var,   rev_lr))     
  call check(nf90_get_var(ncid, sclat_lr_var, sclat_lr))
  call check(nf90_get_var(ncid, sclon_lr_var, sclon_lr))
  call check(nf90_get_var(ncid, scalt_lr_var, scalt_lr))
  call check(nf90_get_var(ncid, time_lr_var,  scantime_lr))
  call check(nf90_get_var(ncid, date_lr_var,  scandate_lr))

  call check(nf90_get_var(ncid, lat_lr_var,   lat_lr))
  call check(nf90_get_var(ncid, lon_lr_var,   lon_lr))
  call check(nf90_get_var(ncid, eia_lr_var,   eia_lr))
  call check(nf90_get_var(ncid, sun_lr_var,   sun_lr))
  call check(nf90_get_var(ncid, qual_lr_var,  qual_lr))

  call check(nf90_get_var(ncid, tb19v_var,    tb19v))
  call check(nf90_get_var(ncid, tb19h_var,    tb19h))
  call check(nf90_get_var(ncid, tb22v_var,    tb22v))
  call check(nf90_get_var(ncid, tb37v_var,    tb37v))
  call check(nf90_get_var(ncid, tb37h_var,    tb37h))
      
  call check(nf90_get_var(ncid, rev_hr_var,   rev_hr))     
  call check(nf90_get_var(ncid, sclat_hr_var, sclat_hr))
  call check(nf90_get_var(ncid, sclon_hr_var, sclon_hr))
  call check(nf90_get_var(ncid, scalt_hr_var, scalt_hr))
  call check(nf90_get_var(ncid, time_hr_var,  scantime_hr))
  call check(nf90_get_var(ncid, date_hr_var,  scandate_hr))

  call check(nf90_get_var(ncid, lat_hr_var,   lat_hr))
  call check(nf90_get_var(ncid, lon_hr_var,   lon_hr))
  call check(nf90_get_var(ncid, eia_hr_var,   eia_hr))
  call check(nf90_get_var(ncid, sun_hr_var,   sun_hr))
  call check(nf90_get_var(ncid, qual_hr_var,  qual_hr))
  call check(nf90_get_var(ncid, tb85v_var,    tb85v))
  call check(nf90_get_var(ncid, tb85h_var,    tb85h))
      
  !--- Close the output data file

  call check(nf90_close(ncid))

  !--- Write sample output data

  do n=1,nscanl
    write(10,'(i4,")",x,a,x,3(f7.2,x))') n,scandate_lr(n),sclat_lr(n),sclon_lr(n),scalt_lr(n)
  enddo

  !--- Subroutine check

  contains

  subroutine check(status)
  integer, intent ( in) :: status

  if (status /= nf90_noerr) then 
    print *, trim(nf90_strerror(status))
    stop "Stopped"
  end if
  end subroutine check  

  end program ssmi_rdfcdr_v1
