; 
;   CENTER FOR OCEAN-ATMOSPHERIC PREDICTION STUDIES, 
;   THE FLORIDA STATE UNIVERSITY
;     
;   DIRECT ALL QUESTIONS TO:  nscat-anim@coaps.fsu.edu

;***************************************************
;   MODIFIED: August 25th, 2000
;   Version:  1.0
;   Author:   Josh Grant, COAPS
;   Description: Now instead of saving the arrays to a file, the arrays are
;                passed back to the user.
;****************************************************
;
;****************************************************
;   MODIFIED: February 15th, 2001
;   Version : 1.1
;   Author  : Josh Grant, COAPS
;   Description: Added two arguments to the function.  Now the function is
;                called with 5 arguments.  The last two were added to store
;                the latitude and longitude values of the points.  Updated
;                the example program below to reflect the new changes.
;****************************************************
;   Parameters:
;
;   input       Name of the NetCDF file you wish to read
;   u           the variable you would like the u wind component saved to
;   v           the variable you would like the v wind component saved to
;   lat         the latitude values stored in an array
;   lon         the longitude values stored in an array
;
;   Example: Prints all the u and v component values to the screen with their
;            respective latitude and longitude values.
;-----------
;     @readncdf.pro
;
;       file = "gcv_NSCAT_1997077.nc"
;
;       readncdf, file, u, v, lat, lon
;
;       dims = SIZE(lat)
;       latdim = dims[1]
;       dims = SIZE(lon)
;       londim = dims[1]
;
;       FOR i = 0, latdim - 1 DO BEGIN
;         FOR j = 0, londim - 1 DO BEGIN
;           print, FORMAT="(F6.2,A2,F7.2,A4,F8.2,A2,F8.2,A1)", lat[i], $
;                  ', ', lon[j], ' = (', u[j,i], ', ', v[j,i], ')'
;        ENDFOR
;      ENDFOR
;
;    END
;-----------

PRO readncdf2, input, u, v, lat, lon

;  Opens NetCDF for reading and other two files for writing.

   ncid = NCDF_OPEN(input, /NOWRITE)

;  Gets the u and v values from the opened file, plus their scale
;  factors.

   lat_id = NCDF_VARID(ncid, 'lat')
   lon_id = NCDF_VARID(ncid, 'lon')
;   mon_id = NCDF_VARID(ncid, 'mon')

   NCDF_VARGET, ncid, lat_id, lat
   NCDF_VARGET, ncid, lon_id, lon

   u_id = NCDF_VARID(ncid, 'u')
   v_id = NCDF_VARID(ncid, 'v')

   NCDF_VARGET, ncid, u_id, u
   NCDF_VARGET, ncid, v_id, v

   NCDF_ATTGET, ncid, u_id, 'scale_factor', usf
   NCDF_ATTGET, ncid, v_id, 'scale_factor', vsf

   NCDF_ATTGET, ncid, u_id, 'missing_value', u_miss
   NCDF_ATTGET, ncid, v_id, 'missing_value', v_miss

;  Multiplies by the scale factor and prints to ouput files.

   u_bad = where( u EQ u_miss, n_u_bad )
   v_bad = where( v EQ v_miss, n_v_bad ) 

   u = u * usf
   v = v * vsf

   if ( n_u_bad GT 0 ) then u( u_bad ) = -9999.
   if ( n_v_bad GT 0 ) then v( v_bad ) = -9999.

;   u = u(*,*,3)
;   v = v(*,*,3)

;  Closes *.nc file and output files.

   NCDF_CLOSE, ncid

END