subroutine get_cmaq_netcdf(grd,filename,mype,g_z,g_ps,g_u,g_v,g_tv,g_q,it,iret_read)
! subroutine to read in cmaq met and ozone now no2 also
! define dimension names
  use kinds, only : r_kind,i_kind
  use mpimod, only : npe,mpi_comm_world,mpi_rtype
  use gridmod
  use guess_grids, only : nfldsig
  use constants, only : r1000,rd_over_cp
  use general_sub2grid_mod, only : sub2grid_info
  use constants, only : one,fv,qmin,zero
!  use cmaqmod, only :i2d,j2d,cmaqpdash,cmaqpbar,i2dg,j2dg,szraq,cmaqoz,cmaqpsol
  use cmaqmod, only :i2d,j2d,cmaqpdash,cmaqpbar,i2dg,j2dg,szcmaq,cmaqpsol
  use cmaqmod, only : ncidcmaqd,nameoz
  use cmaqmod, only : nameno2,cmaqno2
!  use cmaqmod, only : aodobsgrd,wtaodobsgrd
  use ozinfo, only : ihave_oz
  use no2info, only : ihave_no2
  use coinfo, only : ihave_co
  implicit none
  include 'netcdf.inc'
! Declare passed variables
  type(sub2grid_info)                          ,intent(in) :: grd
  character(*), intent(in) :: filename
  integer(i_kind),intent(in) :: mype
! these are local grid on nodes
  real(r_kind), dimension(grd%lat2,grd%lon2) , intent(out) :: g_z,g_ps
  real(r_kind), dimension(grd%lat2,grd%lon2,grd%nsig), intent(out) :: g_u,g_v, g_q,g_tv
!  real(r_kind), dimension(grd%lat2,grd%lon2,grd%nsig), intent(out) :: g_no2
  integer(i_kind) iret_read,ncidcmaq
! declare local variables
  integer(i_kind) ndim,isum1,isum2,ibnd,jbnd,ip,ic,ii,kk,it
  parameter (ndim=4)
  character*4 dimname(ndim)
  data dimname/'lon','lat','lev','time'/
!  character *6 namesp,namesz,nameu,namev,nameq,nameptrop
  character *6 namesp,namesz,nameu,namev,nameq
  data namesp/'psfc'/,namesz/'zsfc'/,nameu/'uu'/,namev/'vv'/,nameq/'iq'/
!  data nameptrop/'ptrop'/
  integer(i_kind) dimid(ndim),ierr,dimlen(ndim),nccmaq,nrcmaq,nlcmaq,i,j,k,ierrcapa
  real(r_kind), allocatable :: pdash(:,:,:),u(:,:,:),v(:,:,:),q(:,:,:),oz(:,:,:)
  real(r_kind), allocatable :: temp(:,:,:)
!  real(r_kind), allocatable :: no2(:,:,:)
!  real(r_kind), allocatable :: co(:,:,:)
  real(r_kind), allocatable :: pdashin(:,:,:),psgather(:)
!  real(r_kind), allocatable :: delp(:,:,:),psol(:,:,:)
  real(r_kind), allocatable :: psol(:,:,:)
!  real(r_kind), allocatable :: sp(:,:),sz(:,:),ptrop(:,:)
  real(r_kind), allocatable :: sp(:,:),sz(:,:)
!  integer(i_kind) iu,iv,ioz,ipdash,idim,isz,isp,iq,idelp
  integer(i_kind) iu,iv,ioz,ipdash,idim,isz,isp,iq
  real(r_kind), allocatable :: tempps(:,:),tempsz(:,:),tempu(:,:,:),tempv(:,:,:),tempoz(:,:,:),t(:,:,:)
!  real(r_kind), allocatable :: tempno2(:,:,:)
!  real(r_kind), allocatable :: tempco(:,:,:)
  real(r_kind), allocatable :: tempq(:,:,:),tv(:,:,:)
  integer(i_kind), allocatable :: iremap(:,:,:),jremap(:,:,:)
  real(r_kind), allocatable :: send2d(:),send3d(:,:)
  real(r_kind), allocatable :: recv2d(:),recv3d(:,:,:),ps11(:,:)
  real(r_kind) :: div
  integer(i_kind) :: idateid
  integer *4 idate,idate2,ipnt
  character *10 cdate
!  integer(i_kind), allocatable :: i2d(:),j2d(:)
!  real(r_kind), dimension(grd%lat2,grd%lon2) , intent(out) :: g_z,g_ps
!  write(6,*)'mype',mype,'shape g_x',shape(g_z),'ps',shape(g_ps),'gridlat2',grd%lat2,grd%lon2
!  call flush(6)
  nccmaq=nlon
  nrcmaq=nlat
  nlcmaq=nsig
  if(mype.eq.0)then
    write(6,*)'find ajl getcmaqnetcdf filename',filename,'it',it
    call flush(6)
    if(.not.allocated(ncidcmaqd))then
      allocate(ncidcmaqd(nfldsig))
      ncidcmaqd=0
    endif
    if(it.eq.nfldsig)then
      ierr=nf_open(filename,NF_WRITE,ncidcmaqd(it))
      write(6,*)'find ajl ncidcmaq it 2 ',it,ncidcmaqd(it),'file',trim(filename)
    else
      ierr=nf_open(filename,NF_NOWRITE,ncidcmaqd(it))
      write(6,*)'find ajl ncidcmaq it 1 ',it,ncidcmaqd(it),'file',trim(filename)
    endif
    ncidcmaq=ncidcmaqd(it)
    do idim=1,ndim
      ierr=nf_inq_dimid(ncidcmaq,dimname(idim),dimid(idim))
      if(ierr /= NF_NOERR)then
        write(6,*)'can t get id for dim ',idim,dimname(idim)
        call stop2(9999)
        
      endif
      ierr=nf_inq_dimlen(ncidcmaq,dimid(idim),dimlen(idim))
      if(ierr /= NF_NOERR)then
        write(6,*)'can t get dimlen  ',idim,dimname(idim)
        call stop2(9999)
      endif
    end do
!   check if match gsi dimensions
    if(dimlen(1).ne.nlon.or.dimlen(2).ne.nlat.or.dimlen(3).ne.nsig)then
      write(6,*)'dimension mismatch dimlen',dimlen
      write(6,*)'nlon',nlon,'nlat',nlat,'nsig',nsig
      call stop2(999)
    endif
    ierr=nf_inq_varid(ncidcmaq,'IDATE',idateid)
    if(ierr /= NF_NOERR)then
      write(6,*)'can t get idateid  ',idateid,dimname(idim)
      call stop2(9999)
    endif
    idate2=2015123112
    ierr=nf_get_var_int(ncidcmaq,idateid,idate)
    ierr=nf_get_att_text(ncidcmaq,NF_GLOBAL,'CDATE',cdate)
!    write(6,*)'cdate',cdate
    
!    write(6,*)'idate',idate,idate2
!   get surface variables
    allocate (sp(nrcmaq,nccmaq),sz(nrcmaq,nccmaq))
!    allocate (ptrop(nrcmaq,nccmaq))
    call readcmaq2d(ncidcmaq,sp,namesp)
    call readcmaq2d(ncidcmaq,sz,namesz)
!    call readcmaq2d(ncidcmaq,ptrop,nameptrop)
!    write(6,*)'did sz'
!    call flush(6)
!    do j=1,10
!      write(6,*)'spin j',j,(sp(j,i),i=1,5)
!    end do
!   calculate temperature
!   need to read in theta and convert to temperature
!   first need to read in pressure on model layers
!    allocate(theta(nrcmaq,nccmaq,nlcmaq),t(nrcmaq,nccmaq,nlcmaq),tv(nrcmaq,nccmaq,nlcmaq),q(nrcmaq,nccmaq,nlcmaq))
    allocate(t(nrcmaq,nccmaq,nlcmaq),tv(nrcmaq,nccmaq,nlcmaq),q(nrcmaq,nccmaq,nlcmaq))
!    allocate(delp(nrcmaq,nccmaq,nlcmaq),psol(nrcmaq,nccmaq,nlcmaq+1),pdash(nrcmaq,nccmaq,nlcmaq))
    allocate(psol(nrcmaq,nccmaq,nlcmaq+1),pdash(nrcmaq,nccmaq,nlcmaq))
!   #define DIAGOZONE
#ifdef DIAGOZONE
    if(.not.allocated(cmaqoz))then
      allocate(cmaqoz(nccmaq,nrcmaq,nlcmaq,nfldsig))
    endif
!    if(.not.allocated(cmaqno2))then
!      allocate(cmaqno2(nccmaq,nrcmaq,nlcmaq,nfldsig))
!    endif
#endif
!    write(6,*)'call readcmaq3d'
!    call flush(6)
!   note readcmaq3d puts 1 at bottom like gsi wants
    call readcmaq3d(ncidcmaq,pdash,nlcmaq,'pdash')
    call readcmaq3d(ncidcmaq,psol,nlcmaq+1,'psol')
!    call readcmaq3d(ncidcmaq,delp,nlcmaq,'delp')
!    psol=0.0
!    do i=1,nccmaq
!      do j=1,nrcmaq
!        psol(j,i,1)=sp(j,i)
!      end do
!    end do
!   add to read cmaq temperature 6/7/2016
!   test if have temperature field
    ierr=nf_inq_varid(ncidcmaq,'T',ipnt)
!   have cmaq temperature use it instead of using theta to calculate temperature
    if(mype.eq.0)then
      write(6,*)'have cmaq temperature use it'
      call flush(6)
    endif
    allocate(temp(nrcmaq,nccmaq,nlcmaq))
    call readcmaq3d(ncidcmaq,temp,nlcmaq,'T')
    do k=1,nlcmaq
      do i=1,nccmaq
        do j=1,nrcmaq
          t(j,i,k)=temp(j,i,k)
!          psol(j,i,k+1)=psol(j,i,k)-delp(j,i,k)
        end do
      end do
    end do
    deallocate (temp)
    if(ihave_oz)then
      allocate (oz(nrcmaq,nccmaq,nlcmaq))
    endif
    allocate (u(nrcmaq,nccmaq,nlcmaq),v(nrcmaq,nccmaq,nlcmaq))
    call readcmaq3d(ncidcmaq,q,nlcmaq,nameq)
    call readcmaq3d(ncidcmaq,u,nlcmaq,nameu)
    call readcmaq3d(ncidcmaq,v,nlcmaq,namev)
!    if(ihave_no2)then
!      allocate (no2(nrcmaq,nccmaq,nlcmaq))
!      call readcmaq3d(ncidcmaq,no2,nlcmaq,'ino2')
!    endif
    write(6,*)'haveoz',ihave_oz
    call flush(6)
    if(ihave_oz)then
      call readcmaq3d(ncidcmaq,oz,nlcmaq,nameoz)
    endif
    if(it.ne.nfldsig)then
      ierr=nf_close(ncidcmaq)
    endif
!    print *,'maxoz cmaq',maxval(oz),minval(oz)
!    if(ihave_no2.and.ihave_oz)then
!      do k=1,nlcmaq
!        do i=1,nccmaq
!          do j=1,nrcmaq
!            q(j,i,k)=max(qmin,q(j,i,k))
!            tv(j,i,k)=t(j,i,k)*(one+fv*q(j,i,k))
!            !oz(j,i,k)=oz(j,i,k)*(47.98/28.97) ! convert vmr to mixing ratio !  close to 1/constoz time 1.e6
!            no2(j,i,k)=no2(j,i,k)*(46.0055/28.97) ! convert vmr to no2 mixing ratio !  
!#ifdef DIAGOZONE
!            cmaqno2(i,j,k,it)=no2(j,i,k)
!            cmaqoz(i,j,k,it)=oz(j,i,k)
!#endif
!          end do
!        end do
!      end do
!    elseif(ihave_no2)then
!      do k=1,nlcmaq
!        do i=1,nccmaq
!          do j=1,nrcmaq
!            q(j,i,k)=max(qmin,q(j,i,k))
!            tv(j,i,k)=t(j,i,k)*(one+fv*q(j,i,k))
!            oz(j,i,k)=oz(j,i,k)*(47.98/28.97) ! convert vmr to mixing ratio !  close to 1/constoz time 1.e6
!            no2(j,i,k)=no2(j,i,k)*(46.0055/28.97) ! convert vmr to no2 mixing ratio !  
!#ifdef DIAGOZONE
!            cmaqno2(i,j,k,it)=no2(j,i,k)
!#endif
!          end do
!        end do
!      end do
!    else
      do k=1,nlcmaq
        do i=1,nccmaq
          do j=1,nrcmaq
            q(j,i,k)=max(qmin,q(j,i,k))
            tv(j,i,k)=t(j,i,k)*(one+fv*q(j,i,k))
            if(ihave_oz)then
             oz(j,i,k)=oz(j,i,k)*(47.98/28.97) ! convert vmr to mixing ratio !  close to 1/constoz time 1.e6
            endif
#ifdef DIAGOZONE
            cmaqoz(i,j,k,it)=oz(j,i,k)
#endif
          end do
        end do
      end do
!    endif
!    write(6,*)'q',maxval(q),'u',maxval(u),'oz',maxval(oz),'tv',maxval(tv),'t',maxval(t),'v',maxval(v)
!    write(6,*)'tv',maxval(tv),minval(tv)
!    call flush(6)
  else
    allocate (psol(1,1,1),pdash(1,1,1))
    allocate (sp(1,1),sz(1,1),q(1,1,1),tv(1,1,1))
    if(ihave_oz)then
      allocate (oz(1,1,1))
    endif
    allocate (u(1,1,1),v(1,1,1))
!    allocate (no2(1,1,1))
!    allocate (ptrop(1,1))
  endif
! scatter values to other processors
! set up big array on masterproc and scatterv them
  isum1=0
  isum2=0 
  if(mype.eq.0)then
    allocate (send2d(itotsub),send3d(nlcmaq,itotsub))
    if(.not.allocated(i2d))then
      allocate (i2d(itotsub),j2d(itotsub))
    endif
 
    allocate (i2dg(iglobal),j2dg(iglobal))
  else
    allocate (send2d(1),send3d(1,1))
    if(.not.allocated(i2d))then
      allocate (i2d(1),j2d(1))
    endif
    allocate (i2dg(1),j2dg(1))
  endif
  if(.not.allocated(cmaqpdash))then
    allocate (cmaqpdash(grd%lat2,grd%lon2,grd%nsig,nfldsig),cmaqpsol(grd%lat2,grd%lon2,grd%nsig+1,nfldsig))
    allocate (cmaqpbar(grd%nsig+1,nfldsig))
  endif
  if(.not.allocated(szcmaq))allocate (szcmaq(nlon,nlat))
!  if(.not.allocated(aodobsgrd))then
!    allocate (aodobsgrd(grd%lat2,grd%lon2),wtaodobsgrd(grd%lat2,grd%lon2))
!    aodobsgrd=-999.
!    wtaodobsgrd=0.0
!  endif
!  if(.not.allocated(cmaqptrop))allocate (cmaqptrop(grd%lat2,grd%lon2))
!   write(500+mype,*)'shape cmaqptrop',shape(cmaqptrop)
!  write(500+mype,*)mype
!  do i=1,npe
!    write(500+mype,*)i,ijn_s(i),displs_s(i)
!  end do
!  call flush(500+mype)

  if(mype.eq.0)then
#ifdef DIAGCMAQ
    write(6,*)'itotsub',itotsub,'npe',npe
    call flush(6)
    write(6,*)'ilat1',shape(ilat1),shape(jlon1)
    write(6,*)'ird_s',shape(ird_s)
    call flush(6)
    write(6,*)'ijn_s',shape(ijn_s)
    call flush(6)
    write(6,*)'ijn',shape(ijn)
    call flush(6)
    do i=1,npe
      write(6,*)'i,ilat1',ilat1(i),'jlon1',jlon1(i),'irc_s',irc_s(i)
      write(6,*)'proid',ilat1(i)*jlon1(i)
      call flush(67)
      write(6,*)i,'ird_s',ird_s(i),'isd_g',isd_g(i)
      write(6,*)i,'ijn_s',ijn_s(i),'ijn',ijn(i)
      isum1=isum1+ilat1(i)*jlon1(i)
      isum2=isum2+ijn_s(i)
      if(ilat1(i)*jlon1(i).ne.ijn(i))then
       write(6,*)'ne i',i
      endif
      write(6,*)'displs_s',displs_s(i),'g',displs_g(i)
      write(6,*)'isc_g',isc_g(i),'ltosi',ltosi(i),'ltosj',ltosj(i)
    end do
    write(6,*)'isum1',isum1,isum2,'itotsub',itotsub
#endif
    ic=0
    do ip=1,npe
#ifdef DIAGCMAQ
      write(6,*)'j limlat',istart(ip)-1,istart(ip)+ilat1(ip)
      call flush(6)
#endif
      do i=jstart(ip)-1,jstart(ip)+jlon1(ip)
        ibnd=i
        if(i<1)ibnd=i+nccmaq
        if(i>nccmaq)ibnd=i-nccmaq
        do j=istart(ip)-1,istart(ip)+ilat1(ip)
          jbnd=max(1,min(nrcmaq,j))
!         write(6,*)'j',j,'jbnd',jbnd,'limlon',jstart(ip)-1,jstart(ip)+jlon1(ip)
!         call flush(6)
          ic=ic+1
          if(ic>itotsub)then
            write(6,*)'ic out of bounds'
            call flush(6)
          endif
          i2d(ic)=ibnd ! lon
          j2d(ic)=jbnd ! lat
        end do
      end do
#ifdef DIAGCMAQ
      write(6,*)'ip',ip,'ic',ic,'nccmaq',nccmaq,nrcmaq
#endif
    end do
#ifdef DIAGCMAQ
      write(6,*)'ip',ip,'ic',ic,'nccmaq',nccmaq,nrcmaq
    do ic=1,100
      write(6,*)'ic',ic,'i2d',i2d(ic),'j2d',j2d(ic)
    end do
    call flush(6)
#endif
    ic=0
    do ip=1,npe
#ifdef DIAGCMAQ
      write(6,*)'ip',ip,'ic',ic,'nccmaq',nccmaq,nrcmaq
      write(6,*)'j limlat',istart(ip)-1,istart(ip)+ilat1(ip)
      call flush(6)
#endif
      do i=jstart(ip),jstart(ip)+jlon1(ip)-1
        ibnd=i
        if(i<1)ibnd=i+nccmaq
        if(i>nccmaq)ibnd=i-nccmaq
        do j=istart(ip),istart(ip)+ilat1(ip)-1
          jbnd=max(1,min(nrcmaq,j))
!         write(6,*)'j',j,'jbnd',jbnd,'limlon',jstart(ip)-1,jstart(ip)+jlon1(ip)
!         call flush(6)
          ic=ic+1
          if(ic>itotsub)then
            write(6,*)'ic out of bounds'
            call flush(6)
          endif
          i2dg(ic)=ibnd ! lon
          j2dg(ic)=jbnd ! lat
        end do
      end do
#ifdef DIAGCMAQ
      write(6,*)'ip',ip,'icg',ic,'nccmaq',nccmaq,nrcmaq
#endif
    end do
#ifdef DIAGCMAQ
    write(6,*)'ic',ic,'iglobal',iglobal
    call flush(6)
#endif
        
          
!   ijn_s number to send displs_s displace for scatter for ilat2*jlon2
!   ijnm number to gather displs_g displace for gather for ilat1*jlon1
    send2d=-10.e20
    do ii=1,itotsub
      i=i2d(ii)
      j=j2d(ii)
      if(i<1.or.i>nccmaq.or.j<1.or.j>nrcmaq)then
         write(6,*)'ij out of bounds',i,j,nccmaq,nrcmaq
         call flush(6)
     endif
     end do
     sp=sp*.1 ! cb
   
     pdash=pdash*.1 ! cb
     psol=psol*.1 ! cb
     cmaqpbar(:,it)=0.0
!     write(6,*)mype,'do pbar'
!     call flush(6)
     do k=1,nsig+1
       do i=1,nlon
         do j=1,nlat
           cmaqpbar(k,it)=cmaqpbar(k,it)+psol(j,i,k)
!           if(i.eq.1.and.j.eq.1)print *,'cmaqpbar',k,cmaqpbar(k,it),'psol',psol(j,i,k)
         end do
       end do
     end do
 !    write(6,*)mype,'did cmaqpbar'
 !    call flush(6)
     div=one/float(nlon*nlat)
     cmaqpbar(:,it)=cmaqpbar(:,it)*div
!     do k=1,nsig+1
!       write(6,*)'cmaqpbar',k,cmaqpbar(k)
!     end do
     
  endif
  call mpi_bcast(cmaqpbar(1,it),nsig+1,mpi_rtype,0,mpi_comm_world,ierr)
!  if(mype.eq.1)then
!    do k=1,nsig+1
!       write(6,*)'cmaqpbar',k,cmaqpbar(k)
!       call flush(6)
!    end do
!  endif
  call scat2d(sp,g_ps)
  call scat2d(sz,g_z)
!  call scat2d(ptrop,cmaqptrop)
  call scat3d(tv,g_tv)
  call scat3d(q,g_q)
  call scat3d(u,g_u)
  call scat3d(v,g_v)
!  if(ihave_no2)then
!    call scat3d(no2,g_no2)
!  endif
  call scat3d(pdash,cmaqpdash(1,1,1,it))
  call scat3dp(psol,cmaqpsol(1,1,1,it))
  if(mype.eq.0)then
   szcmaq=sz
  endif
 ! write(6,*)'do bast',mype
 ! call flush(6)
  call mpi_bcast(szcmaq,nlon*nlat,mpi_rtype,0,mpi_comm_world,ierr)
#ifdef DIAGCMAQ
  write(6,*)mype,'maxps',maxval(g_ps),minval(g_ps)
  write(6,*)mype,'maxsz',maxval(g_z),minval(g_z)
  write(6,*)mype,'tv',maxval(g_tv),minval(g_tv)
  call flush(6)
#endif
! now gather to test transfer
  allocate (ps11(lat1,lon1))
  call strip(g_ps,ps11,1)
  if(mype.eq.0)then
    allocate(psgather(nlat*nlon))
#ifdef DIAGCMAQ
    do j=1,lat2
      write(6,*)'g_ps',j,(g_ps(j,i),i=1,5)
    end do
    do j=1,lat1
      write(6,*)'ps11',j,(ps11(j,i),i=1,5)
    end do
#endif
  else
    allocate(psgather(1))
  endif
! write(6,*)'gather',mype
! call flush(6)
  call mpi_gatherv(ps11,ijn(mype+1),mpi_rtype,psgather,ijn,displs_g,mpi_rtype,0,mpi_comm_world,ierr)
  if(mype.eq.0)then
    do ii=1,iglobal
!      i=ltosi(ii)
!      j=ltosj(ii)
      i=i2dg(ii)
      j=j2dg(ii)
      if(abs(sp(j,i)-psgather(ii))>1)then
        write(6,*)'i',i,j,'sp',sp(j,i),'psgather',psgather(ii),'ii',ii,'ltosi',ltosi(ii),ltosj(ii)
      endif
    end do
!    deallocate (delp,t)
    deallocate (t)

  endif
  deallocate (psgather,sp,oz,u,v,ps11)
  deallocate (sz,tv,q,psol,pdash)
!  deallocate(send2d,send3d,i2d,j2d,i2dg,j2dg)
  deallocate(send2d,send3d,i2dg,j2dg)
  write(6,*)mype,'ihave_co',ihave_co
  call flush(6)
  if(ihave_co)then
    call get_cmaq_co(ncidcmaq)
  endif
  write(6,*)mype,'ihave_no2',ihave_no2
  call flush(6)
  if(ihave_no2)then
    call get_cmaq_no2(ncidcmaq)
  endif
  if(mype.eq.0)then
    write(6,*)'end get_cmaq_netcdf'
    call flush(6)
  endif
  return
end subroutine get_cmaq_netcdf
