;+ ; ; @file_comments ; Case 1: extract from a NetCDF file longitude and latitude arrays, their dimensions ; and make sure it is 1D or 2D arrays ; ; Case 2: given longitude and latitude arrays, get their dimensions and make ; sure they are 1D or 2D arrays ; ; @categories ; Interpolation ; ; @examples ; ; Case 1: ; IDL> get_gridparams, file name/id, lonname, latname, lon, lat, jpi, jpj, n_dimensions ; ; Case 2: ; IDL> get_gridparams, lon, lat, jpi, jpj, n_dimensions ; ; @param in1 {in}{required} ; Case 1: name or the id (returned by ncdf_open) of the netcdf file ; Case 2: 1D or 2D arrays defining longitudes, will be forced to have ; n_dimensions dimensions when returned ; ; @param in2 {in}{required} ; Case 1: name of the variable containing the longitude in the NetCDF file ; Case 2: 1D or 2D arrays defining latitudes, will be forced to have ; n_dimensions dimensions when returned ; ; @param in3 ; Case 1: name of the variable containing the latitude in the NetCDF file ; Case 2: returned number of points in longitudinal direction. ; ; @param in4 {out} ; Case 1: returned longitudes array, with n_dimensions dimensions ; Case 2: returned number of points in latitudinal direction ; ; @param in5 ; Case 1: returned latitudes array, with n_dimensions dimensions ; Case 2: input scalar (1 or 2) to specify if lon and lat should be returned ; as 1D or 2D arrays. Note that if n_dimensions = 1, the grid must be ; regular (longitude and latitudes can be described as 1D arrays). ; ; @param in6 {out} ; Case 1: returned number of points in longitudinal direction. ; ; @param in7 {out} ; Case 1: returned number of points in latitudinal direction ; ; @param in8 {in} ; Case 1: input scalar (1 or 2) to specify if lon and lat should be returned ; as 1D or 2D arrays. Note that if n_dimensions = 1, the grid must be ; regular (longitude and latitudes can be described as 1D arrays). ; ; @keyword DOUBLE {default=0}{type=scalar: 0 or 1} ; activate to force double precision for lon/lat arrays ; ; @examples ; ; 1) IDL> ncdf_get_gridparams, 'coordinates_ORCA_R05.nc', 'glamt', 'gphit' $ ; , olon, olat, jpio, jpjo, 2 ; ; 2) IDL> ncdf_get_gridparams, olon, olat, jpio, jpjo, 2 ; ; @history ; November 2005: Sebastien Masson (smasson\@lodyc.jussieu.fr) ; ; @version ; $Id$ ; ;- ; PRO get_gridparams, in1, in2, in3, in4, in5, in6, in7, in8, DOUBLE = double ; file, lonname, latname, lon, lat, jpi, jpj, n_dimensions ; lon, lat, jpi, jpj, n_dimensions ; compile_opt idl2, strictarrsubs ; CASE n_params() OF 8:BEGIN ; get longitude and latitude IF size(in1, /type) EQ 7 THEN BEGIN IF file_test(in1) EQ 0 THEN BEGIN ras = report('file ' + in1 + ' does not exist') stop ENDIF cdfido = ncdf_open(in1) ENDIF ELSE cdfido = in1 ncdf_varget, cdfido, in2, lon ncdf_varget, cdfido, in3, lat IF size(in1, /type) EQ 7 THEN ncdf_close, cdfido n_dimensions = in8 END 5:BEGIN lon = temporary(in1) lat = temporary(in2) n_dimensions = in5 END ELSE:BEGIN ras = report('Bad number of input parameters') stop end ENDCASE ; sizelon = size(lon) sizelat = size(lat) CASE 1 OF ;------- ; lon and lat are 1D arrays ;------- sizelon[0] EQ 1 AND sizelat[0] EQ 1:BEGIN ; get jpi and jpj jpi = sizelon[1] jpj = sizelat[1] ; make sure that lon and lat have the good number of dimensions CASE n_dimensions OF 1: 2:BEGIN ; make lon and lat 2D arrays lon = temporary(lon) # replicate(1, jpj) lat = replicate(1, jpi) # temporary(lat) END ELSE:stop ENDCASE END ;------- ; lon is 2D array and lat is 1D array ;------- sizelon[0] EQ 2 AND sizelat[0] EQ 1:BEGIN ; get jpi and jpj jpi = sizelon[1] jpj = sizelon[2] IF jpj NE n_elements(lat) THEN stop ; make sure that lon and lat have the good number of dimensions CASE n_dimensions OF 1:BEGIN IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN ras = report('Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes') stop ENDIF lon = lon[*, 0] END 2:lat = replicate(1, jpi) # temporary(lat) ELSE:stop ENDCASE END ;------- ; lon is 1D array and lat is 2D array ;------- sizelon[0] EQ 1 AND sizelat[0] EQ 2:BEGIN ; get jpi and jpj jpi = sizelat[1] jpj = sizelat[2] IF jpi NE n_elements(lon) THEN stop ; make sure that lon and lat have the good number of dimensions CASE n_dimensions OF 1:BEGIN IF array_equal(lat, replicate(1, jpi) # lat[0, *]) NE 1 THEN BEGIN ras = report('Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes') stop ENDIF lat = reform(lat[0, *]) END 2:lon = temporary(lon) # replicate(1, jpj) ELSE:stop ENDCASE END ;------- ; lon and lat are 2D arrays ;------- sizelon[0] EQ 2 AND sizelat[0] EQ 2:BEGIN ; get jpi and jpj IF array_equal(sizelon[1:2], sizelat[1:2]) NE 1 THEN stop jpi = sizelon[1] jpj = sizelon[2] ; make sure that lon and lat have the good number of dimensions CASE n_dimensions OF 1:BEGIN IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN ras = report('Longitudes are not the same for all latitudes, impossible to extract a 1D array of the longitudes') stop ENDIF lon = lon[*, 0] IF array_equal(lat, replicate(1, jpi) # reform(lat[0, *])) NE 1 THEN BEGIN ras = report('Latitudes are not the same for all longitudes, impossible to extract a 1D array of the latitudes') stop ENDIF lat = reform(lat[0, *]) END 2: ELSE:stop ENDCASE END ;------- ; lon and lat are not 1D and/or 2D arrays ;------- ELSE: BEGIN ras = report('Longitudes and latitudes are not 1D and/or 2D arrays') stop END ENDCASE ; ;------- ; double keyword ;------- if keyword_set(double) then BEGIN lon = double(temporary(lon)) lat = double(temporary(lat)) ENDIF ; ; give back the right outparameters. ; CASE n_params() OF 8:BEGIN in4 = temporary(lon) in5 = temporary(lat) in6 = temporary(jpi) in7 = temporary(jpj) END 5:BEGIN in1 = temporary(lon) in2 = temporary(lat) in3 = temporary(jpi) in4 = temporary(jpj) END ENDCASE ; return END