anchor.diff:        END
   
   C----------------------------------------------------------------------C
  - 
  -      SUBROUTINE gcmdln(ifile,ofile,opt,p)
  - 
  +
  +      SUBROUTINE gcmdln(ifile,ofile,arg1,arg2,opt,long,p)
         IMPLICIT NONE
  -      CHARACTER*30 ifile,ofile,tag
  -      INTEGER i,narg,opt,p
  -
  +      CHARACTER*40 ifile,ofile,tag,arg1,arg2
  +      INTEGER i,narg,opt,p,long
         ifile = 'none'
  -      ofile = 'dateutil.out'
  -C     1:date->nday; 2:nday->date; 3:cal->jd; 4:jd->cal
  +      ofile = 'none'
  +      arg1 = 'undef'
  +      arg2 = 'undef'
         opt = 0
  +      long = 0
         p = 0
  -
         narg = iargc()
  -      if (narg.eq.0) call usage()
  +      if (narg.eq.0) then
  +          call usage('!! Error: no command line arguments specified')
  +      endif
         i = 0
     201 i = i + 1
         if (i.gt.narg) goto 202
             call getarg(i,tag)
  -          if (tag(1:2).eq.'-h'.or.tag(1:2).eq.'-?') then
  -              call usage()
  -          elseif (tag(1:2).eq.'-o') then
  -              i = i + 1
  -              call getarg(i,ofile)
  -          elseif (tag(1:4).eq.'-day') then
  +          if (tag(1:6).eq.'-ndayf') then
                 opt = 1
  +              i = i + 1
  +              call getarg(i,ifile)
  +          elseif (tag(1:5).eq.'-nday') then
  +              opt = 1
  +              i = i + 1
  +              call getarg(i,arg1)
  +              i = i + 1
  +              call getarg(i,arg2)
  +              p = 1
  +          elseif (tag(1:6).eq.'-datef') then
  +              opt = 2
                 i = i + 1
                 call getarg(i,ifile)
             elseif (tag(1:5).eq.'-date') then
                 opt = 2
                 i = i + 1
  -              call getarg(i,ifile)
  -          elseif (tag(1:4).eq.'-jd') then
  -              opt = 3
  -              i = i + 1
  -              call getarg(i,ifile)
  -          elseif (tag(1:4).eq.'-cal') then
  -              opt = 4
  -              i = i + 1
  -              call getarg(i,ifile)
  +              call getarg(i,arg1)
  +              i = i + 1
  +              call getarg(i,arg2)
  +              p = 1
  +          elseif (tag(1:5).eq.'-long') then
  +              long = 1
  +          elseif (tag(1:2).eq.'-o') then
  +              i = i + 1
  +              call getarg(i,ofile)
             elseif (tag(1:2).eq.'-p') then
                 p = 1
  +          elseif (tag(1:2).eq.'-h') then
  +              call usage('')
             else
  -              write(*,*) '!! Error: No option '//tag
  -              call usage()
  +              call usage('!! Error: No option '//tag)
             endif
             goto 201
     202 continue
  
positive.code: C----
        IMPLICIT none
        INTEGER yr1,mo1,yr2,mo2
        REAL*8 dy1,dy2,jd1,jd2,ndy
        CHARACTER*30 ifile,ofile,date1,date2
        INTEGER opt,p
        LOGICAL ex
  
  C NOTE TO MATT: PUT IN OPTION (SOMETHING LIKE -hms) TO INCLUDE
  C HOUR, MINUTE, SECONDS IN COMPUTATIONS
  
  C----
  C Parse command line
  C----
        call gcmdln(ifile,ofile,opt,p)
        inquire(file=ifile,EXIST=ex)
        if (.not.ex) then
            write(*,*) '!! Error: No input file '//ifile
            call usage()
        endif
  
  C----
  C Open input and output files (unless -p flag is on)
  C----
        open (unit=11,file=ifile,status='old')
        if (p.eq.0) open (unit=12,file=ofile,status='unknown')
  
  C----
  C Operation differs depending on input option
  C   opt=1:
  C   opt=2:
  C   opt=3:
  C   opt=4:
  C----
  C     Option 1: Convert from date pairs to number of days
        if (opt.eq.1) then
    101     read (11,*,end=102) yr1,mo1,dy1,yr2,mo2,dy2
                call date2jd(jd1,yr1,mo1,dy1)
                call date2jd(jd2,yr2,mo2,dy2)
                ndy = jd2 - jd1
                if (p.eq.0) then
                    write (12,9998) ndy
                else
                    write (*,9998) ndy
                endif
                goto 101
    102     continue
  C     Option 2: Convert from date and number of days to date
        elseif (opt.eq.2) then
    103     read(11,*,end=104) yr1,mo1,dy1,ndy
                call date2jd(jd1,yr1,mo1,dy1)
                jd2 = jd1 + ndy
                call jd2date(yr1,mo1,dy1,jd2)
                if (p.eq.0) then
                    write (12,9999) yr1,mo1,dy1
                else
                    write (*,9999) yr1,mo1,dy1
                endif
                goto 103
    104     continue
        elseif (opt.eq.3) then
    105     read(11,*,end=106) yr1,mo1,dy1
                call date2jd(jd1,yr1,mo1,dy1)
                if (p.eq.0) then
                    write(12,9998) jd1
                else
                    write(*,9998) jd1
                endif
                goto 105
    106     continue
        elseif (opt.eq.4) then
    107     read(11,*,end=108) jd1
                call jd2date(yr1,mo1,dy1,jd1)
                if (p.eq.0) then
                    write(12,9999) yr1,mo1,dy1
                else
                    write(*,9999) yr1,mo1,dy1
                endif
                goto 107
    108     continue
        endif
  
   9998 format(F14.6)
   9999 format(I7,I4,F14.6)
  
        END
  
  C======================================================================C
  
        SUBROUTINE date2jd(jd,year,month,day)
  C----
  
negative.code: 
  C----------------------------------------------------------------------C
  
        SUBROUTINE usage(str)
        IMPLICIT NONE
        INTEGER lstr
        CHARACTER str*(*)
        if (str.ne.' ') then
            lstr = len(str)
            write(*,*) str(1:lstr)
            write(*,*)
        endif
        write(*,*)
       1 'Usage: dateutil -nday DATE1 DATE2 | -ndayf DATEFILE | -date ',
       2               'DATE NDAY | -datef DAYFILE'
        write(*,*)
       3 '                -o OUTFILE [-long] [-p] [-h]'
        write(*,*)
        write(*,*)
       1 '-nday DATE1 DATE2  Number of days between ',
       2                          'dates (YYYYMMDD[HHMMSS])'
        write(*,*)
       1 '-ndayf DATEFILE    Number of days between dates (YYYY MM DD ',
       2                           '[HH MM SS]) in DATEFILE'
        write(*,*)
       1 '-date DATE NDAY    Calendar date from DATE (YYYYMMDD[HHMMSS]) ',
       2                      'and # of days (NDAY)'
        write(*,*)
       1 '-dated DAYFILE     Calendar date from DATE (YYYY MM DD [HH ',
       2                         'MM SS]) and # of days (NDAY) in DAYFILE'
        write(*,*)
       1 '-long              Read/write all dates as YYYYMMDDHHMMSS or ',
       2                            'YYYY MM DD HH MM SS'
        write(*,*)
       1 '-o OUTFILE         Output of date computation'
        write (*,*)
       1 '-p                 Print dates or number of days to standard ',
       2                                       'output (overrides -o)'
        write (*,*)
       1 '-h                 Online help'
        write(*,*)
        write(*,*)
       1 '  NOTE: Dates on command line must be in YYYYMMDD[HHMMSS] ',
       2               'format, with no spaces.'
        write(*,*)
       1 '        Dates in files are delimited by spaces YYYY MM DD ',
       2                            '[HH MM SS]'
        write (*,*)
        STOP
        END
  