      program TheDoAll
      parameter(nvar=12,nmnt=12)
      character*10 TheVar(nvar)
      character*3  Month(nmnt)
      character*60 TheFile1, TheFile2
      character*300 command
      character(len=10)::parm
      integer*4 year_start,year_stop


      TheVar(1) = "ATM"
      TheVar(2)  = "SO2"
      TheVar(3)  = "SO4"
      TheVar(4)  = "NO"
      TheVar(5)  = "NO2"
      TheVar(6)  = "HNO3"
      TheVar(7)  = "O3"
      TheVar(8)  = "CO"
      TheVar(9) = "H2O2"
      TheVar(10) = "CH3O2"
      TheVar(11) = "XYL"
      TheVar(12) = "ETHE"
      
!      call system ("CDO='/share/apps/cdo/bin/cdo'")

      call getarg(1,parm)
      read(parm,*)year_start

      call getarg(2,parm)
      read(parm,*)year_stop


      do ivar = 1,1 !nvar
       do iYear = year_start,year_stop

        Month(1) = "31"
        Month(2) = "28"
        Month(3) = "31"
        Month(4) = "30"
        Month(5) = "31"
        Month(6) = "30"
        Month(7) = "31"
        Month(8) = "31"
        Month(9) = "30"
        Month(10) = "31"
        Month(11) = "30"
        Month(12) = "31"
        
        ryr = float(iYear)/float(4)
        iyr = iYear/4
        if(ryr .eq. iyr)  Month(2) = "29"

        do imnt = 1,12
         if (imnt .lt. 10) write(TheFile1,"(i4,'0',i1,'0100')") iYear,imnt
         if (imnt .ge. 10) write(TheFile1,"(i4,i2,'0100')") iYear,imnt
         TheFile1 = "wprs_run_" // trim(TheVar(ivar)) //"."// TheFile1
         command = "ln -sf /data1/ulrich/REGCM4/WPRS_RUN/" // trim(TheFile1)  // ".nc  ."
         write(*,*) command
         call system(command)

!-------- Convert to pressure level  
         command = "../Bin/sigma2p  " // trim(TheFile1) // ".nc"   
         write(*,*) trim(command)
         call system(command)

!-------- Regrid the the regional level
         command = "../Bin/regrid  " //  &
                   trim(TheFile1) // "_pressure.nc  "  // &
                   "0,28,0.32 -20,20,0.36 bil "
         write(*,*) trim(command)
         call system(command)      


!-------- Do daily averages 
         command = "cdo daymean "          // trim(TheFile1) //   &
           "_pressure_lonlat.nc  " // trim(TheFile1) // "_daily.nc"
         write(*,'(a100)') trim(command)     
         call system(command)
      

!-------- Remove extra day
         command = "cdo seltimestep,1/" // trim(Month(imnt))  // " " // &
                 trim(TheFile1) // "_daily.nc  " // trim(TheFile1) // "_dailyNew.nc"
         write(*,'(a120)') trim(command)     
         call system(command)


!-------- Clean up
         command = "rm  " // trim(TheFile1) // ".nc  " // &
                    trim(TheFile1) // "_pressure.nc  " // &
                    trim(TheFile1) // "_pressure_lonlat.nc " // &
                    trim(TheFile1) // "*_daily.nc "
          write(*,'(a220)') trim(command)
          call system(command)      
        enddo
       enddo

!-------- Concatinate..
         command = "ncrcat " //  &
                          "*" // trim(TheVar(ivar)) // "*_dailyNew.nc " // &
                   "wprs_run_" // trim(TheVar(ivar)) // "_Pressure_Daily.nc"
         write(*,'(a220)') trim(command)     
         call system(command)

      enddo
  

!-------- Clean up
         command = "rm *_dailyNew.nc"
          write(*,'(a120)') trim(command)
          call system(command)      

      end
      
      
