    pro proc_realtime,file,year,month,day,FIELDS=fields,SKIP_DROP=skip_drop,$
                      SCAN_TYPE=scan_type,COPY_IMAGES=copy_images,          $
                      SWEEP_INDEX=sweep_index,DPQC=DPQC,                    $
                      PLOT_ALL_SWEEPS=plot_all_sweeps,REAL_TIME=real_time,  $
                      ZTIME=ztime,SIMPLE_MODE=simple_mode,POSTSCRIPT=postscript
;
; *** Program to run mainline code for processing NPOL data
; *** to generate DROP, Rain Rates, HID and associated imagery.
;
    COMMON misc,TRUE,FALSE
    COMMON dirs,img_dir,uf_dir,hid_img_dir,hid_dir,dsd_dir
    COMMON graphics,USE_Z_BUFFER,TC,XS,YS,cols,rows
    COMMON graphic_parms,scale,bottom,ncolors
    COMMON my_colors,black,purple,blue,cyan,green,yellow,orange,red,white
    COMMON symbols,plot,asterisk,dot,diamond,triangle,square,exx
    COMMON linestyles,solid,dotted,dashed,dashdot,dashdotdot,longdash

    spawn,'date',beg_proc_time

    TRUE=1 & FALSE=0
    !QUIET=TRUE
    USE_Z_BUFFER = FALSE
    xs=499 & ys=499
;
; *** Deal with keywords
;
    if(NOT KEYWORD_SET(SKIP_DROP))   then skip_drop=FALSE
    if(NOT KEYWORD_SET(POSTSCRIPT))  then postscript=FALSE
    if(NOT KEYWORD_SET(COPY_IMAGES)) then copy_images=FALSE
    if(NOT KEYWORD_SET(SCAN_TYPE))   then scan_type='ppi'
    if(NOT KEYWORD_SET(SWEEP_INDEX)) then sweep_index = 0
    if(NOT KEYWORD_SET(DPQC))        then dpqc=FALSE
    if(NOT KEYWORD_SET(PLOT_ALL_SWEEPS))  then plot_all_sweeps=FALSE
    if(NOT KEYWORD_SET(REAL_TIME))   then real_time=FALSE
    if(NOT KEYWORD_SET(SIMPLE_MODE)) then simple_mode=FALSE

    if(n_elements(FIELDS) eq 0)      then fields = ['DZ','DR','PH','RH','VR','SW','LDH']
;
; *** Set date to starings
;
    year  = string(year,format='(I4.4)')
    syear = strmid(year,2,2)
    month = string(month,format='(I2.2)')
    day   = string(day,format='(I2.2)')
;
; *** Locate the data
;
    fileb = file_basename(file,'.gz')
    org_file_base = fileb
;
; *** Get the scan type so we can place in special directories
;
    flag = get_scan_type(file,the_scan_type)
    if(flag ne 'OK') then begin
        print,flag
        goto,next_file
    endif

    if(the_scan_type eq 'PPI SECTOR') then scan_type='PPS'
;
; *** Now, sort the output by scan_type
;
    out_dir = '/data/Radar/NPOL/Newark/Processed/' + strlowcase(scan_type) + '/' 
    print,'Out: ' + out_dir
    spawn,'mkdir -p ' + out_dir
;
; *** Sounndings are automatically updated by cron, but latest good
; *** sounding file, sans the header lines, that DROPS can't handle
; *** are removed and update to sounding_file.
;
    sounding_file = 'WAL_Sounding.txt'
;
; *** Create a UF directory to hold data by scan mode
;
    uf_dir= out_dir + 'IRIS_TO_UF/'
    spawn,'mkdir -p ' + uf_dir
    uf_file = uf_dir + org_file_base + '.uf'
    flag = iris_to_uf(file,uf_file,scan_type,FIELDS=fields)
    print,'INPUT/UF --> ' + uf_file

    if(SIMPLE_MODE) then begin
        dsd_uf_file = uf_file
        goto,plot_uf
    endif
;
; *** Name a new DROP UF file
; 
    drop_uf_file=''
    if(skip_drop) then begin
        print
        print,'******************* Skipping DROP program ***************'
        goto,proc_hid
    endif
    print,'Performing DROP....'
    
    config_file = 'npol.conf'   ; For Chandra's code
    drop_uf_dir = out_dir + 'DROP_UF/'
    spawn,'mkdir -p ' + drop_uf_dir
    
    fileb = file_basename(file)
    drop_uf_file = drop_uf_dir + org_file_base + '_DROP.uf'
    
    c = './DROPS ' + uf_file + ' -o ' + drop_uf_file + ' -c ' + config_file + $
      ' -s ' + sounding_file + ' -m VHS'
        print,c
    spawn,c
    print,'DROP/UF  --> ' + drop_uf_file
;
; *** Now call CSU's HID
;
proc_hid:        
    print
    print,'Calculating HID...'
    hid_dir = out_dir + 'HID_UF/UF/'
    spawn,'mkdir -p ' + hid_dir
    
    the_uf_file = drop_uf_file
    if(skip_drop) then the_uf_file=uf_file
    
    flag = retrieve_hid(file=the_uf_file,org_file_base,HID_UF_FILE=hid_uf_file, $
                        SOUNDING_FILE=sounding_file,POSTSCRIPT=postscript)
    if(flag ne 'OK') then begin
        print,flag
        goto,next_file
    end
    print
    print,'HID/UF   --> ' + hid_uf_file
;
; *** Now add the D0 and Nw fields to a new UF file.
;
    print
    print,'Adding DSD Fields...'
    dsd_dir = out_dir +  'DSD_UF/UF/' + year + '/' + month + day + '/'
    spawn,'mkdir -p ' + dsd_dir
    
    flag = add_dsd_fields(hid_uf_file,org_file_base,DSD_UF_FILE=dsd_uf_file,$
                          DPQC=dpqc)
    if(flag ne 'OK') then begin
        print,flag
        goto,next_file
    end
    print
    print,'DSD/UF   --> ' + dsd_uf_file
;
; *** Remove uf_file (IRIS_TO_UF), drop_uf_file (DROPS), and
; *** hid_uf_file (HID).  Final file, which is kept is dsd_uf_file.
;
    print,'Removing UF files...'
    c = 'rm ' + uf_file + ' ' + hid_uf_file
    print,c
    spawn,c

    if(NOT SKIP_DROP) then begin
        c = 'rm ' + drop_uf_file
        print,c
        spawn,c
    endif

plot_uf:

;
; *** Plot the results
;
    img_dir =  out_dir + 'Plots/' + year + '/' + month + day + '/'
    spawn,'mkdir -p ' + img_dir
    
    max_range = 150
    if(strlowcase(scan_type) eq 'rhi') then max_range=75

    if(strupcase(scan_type) eq 'PPI' or strupcase(scan_type) eq 'PPS') then begin
        sweep_index=1           ; 2nd tilt
    endif
    flag = plot_realtime(dsd_uf_file,org_file_base,img_files,max_range=max_range, $
                         SCAN_TYPE=scan_type,SKIP_DROP=skip_drop,          $
                         PLOT_ALL_SWEEPS=plot_all_sweeps,                  $
                         SWEEP_INDEX=sweep_index,POSTSCRIPT=postscript)

    if(flag ne 'OK') then begin
        print,flag
        goto,next_file
    end
;
; *** If so desired, copy images to TRMM-FC or other server specified
; *** in copy_images_to_server.pro
;
    if(COPY_IMAGES) then begin
        print,'Copying images to server...'
        copy_images_to_server,img_files
    endif else begin
        print        
        print,'NOT COPYING IMAGES!!!'
        print        
        print        
    endelse
;
; *** Calculate the processing times
;
    spawn,'date',end_proc_time
    calc_proc_time,beg_proc_time,end_proc_time
    print,end_proc_time
    print,'Done with ' + file

next_file:
    return
    end
