;+ ; ; @file_comments ; 1) extract from a NetCDF file the longitude, latidude, and their dimensions ; and make sure it is 1D or 2D arrays ; ; or 2) given longitude and latitude arrays get their dimensions and make sure ; they are 1D or 2D arrays ; ; @categories interpolation ; ; @examples ; ; 1) get_gridparams, file, lonname, latname, lon, lat, jpi, jpj, n_dimensions ; ; or ; ; 2) get_gridparams, lon, lat, jpi, jpj, n_dimensions ; ; 1) ; @param in1 {in}{required} the name of the netcdf file ; @param in2 {in}{required} the name of the variable that contains the longitude in the NetCDF file ; @param in3 {in}{required} the name of the variable that contains the latitude in the NetCDF file ; @param in4 {out} the number of points in the longitudinal direction ; @param in5 {out} the number of points in the latitudinal direction ; @param in6 {out} the variable that will contain the longitudes ; @param in7 {out} the variable that will contain the latitudes ; @param in8 {out} 1 or 2 to specify if lon and lat should be 1D (jpi or jpj) ; ; or ; ; 2) ; @param lon lat {in}{required} 1d or 2D arrays defining longitudes and latitudes. ; Note that these arrays are also outputs and can therefore be modified. ; @param in1 {out} the variable that will contain the longitudes ; @param in2 {out} the variable that will contain the latitudes ; @param in3 {out} the number of points in the longitudinal direction ; @param in4 {out} the number of points in the latitudinal direction ; @param in5 {out} 1 or 2 to specify if lon and lat should be 1D (jpi or jpj) ; arrays or 2D arrays (jpi,jpj). Note that of n_dimensions = 1, then the ; grid must be regular (each longitudes must be the same for all latitudes ; and each latitudes should be the sae for all longitudes). ; ; @examples ; ; 1) ncdf_get_gridparams, 'coordinates_ORCA_R05.nc', 'glamt', 'gphit' $ ; , olon, olat, jpio, jpjo, 2 ; ; 2) ncdf_get_gridparams, olon, olat, jpio, jpjo, 2 ; ; @history ; November 2005: Sebastien Masson (smasson\@lodyc.jussieu.fr) ; ;- ; ;---------------------------------------------------------- ;---------------------------------------------------------- ; 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 ; CASE n_params() OF 8:BEGIN ; get longitude and latitude IF file_test(in1) EQ 0 THEN BEGIN print, 'file ' + in1 + ' does not exist' stop ENDIF cdfido = ncdf_open(in1) ncdf_varget, cdfido, in2, lon ncdf_varget, cdfido, in3, lat ncdf_close, cdfido n_dimensions = in8 END 5:BEGIN lon = temporary(in1) lat = temporary(in2) n_dimensions = in5 END ELSE:BEGIN print, 'Bad nimber 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 print, 'Longitudes are not the same for all latitudes, imposible 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 print, 'Latitudes are not the same for all longitudes, imposible 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 print, 'Longitudes are not the same for all latitudes, imposible 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 print, 'Latitudes are not the same for all longitudes, imposible 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:stop 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