[59] | 1 | ;+ |
---|
| 2 | ; |
---|
[118] | 3 | ; @file_comments |
---|
[59] | 4 | ; 1) extract from a NetCDF file the longitude, latidude, and their dimensions |
---|
[118] | 5 | ; and make sure it is 1D or 2D arrays |
---|
[59] | 6 | ; |
---|
[125] | 7 | ; or |
---|
[121] | 8 | ; 2) given longitude and latitude arrays get their dimensions and make |
---|
[118] | 9 | ; sure they are 1D or 2D arrays |
---|
| 10 | ; |
---|
[101] | 11 | ; @categories interpolation |
---|
[59] | 12 | ; |
---|
[118] | 13 | ; @examples |
---|
| 14 | ; |
---|
[125] | 15 | ; 1) |
---|
[121] | 16 | ; IDL> get_gridparams, file, lonname, latname, lon, lat, jpi, jpj, n_dimensions |
---|
[59] | 17 | ; |
---|
[118] | 18 | ; or |
---|
[59] | 19 | ; |
---|
[125] | 20 | ; 2) |
---|
[121] | 21 | ; IDL> get_gridparams, lon, lat, jpi, jpj, n_dimensions |
---|
[59] | 22 | ; |
---|
[118] | 23 | ; 1) |
---|
| 24 | ; @param in1 {in}{required} the name of the netcdf file |
---|
| 25 | ; @param in2 {in}{required} the name of the variable that contains the longitude in the NetCDF file |
---|
| 26 | ; @param in3 {in}{required} the name of the variable that contains the latitude in the NetCDF file |
---|
| 27 | ; @param in4 {out} the number of points in the longitudinal direction |
---|
| 28 | ; @param in5 {out} the number of points in the latitudinal direction |
---|
[101] | 29 | ; @param in6 {out} the variable that will contain the longitudes |
---|
[118] | 30 | ; @param in7 {out} the variable that will contain the latitudes |
---|
| 31 | ; @param in8 {out} 1 or 2 to specify if lon and lat should be 1D (jpi or jpj) |
---|
[59] | 32 | ; |
---|
[118] | 33 | ; or |
---|
[59] | 34 | ; |
---|
[118] | 35 | ; 2) |
---|
| 36 | ; @param in1 {in}{required} 1d or 2D arrays defining longitudes and latitudes. |
---|
| 37 | ; @param in2 {in}{required} 1d or 2D arrays defining longitudes and latitudes. |
---|
| 38 | ; Note that these arrays are also outputs and can therefore be modified. |
---|
[59] | 39 | |
---|
[101] | 40 | ; @param in1 {out} the variable that will contain the longitudes |
---|
[118] | 41 | ; @param in2 {out} the variable that will contain the latitudes |
---|
| 42 | ; @param in3 {in} the number of points in the longitudinal direction |
---|
| 43 | ; @param in4 {in} the number of points in the latitudinal direction |
---|
| 44 | ; @param in5 {in} 1 or 2 to specify if lon and lat should be 1D (jpi or jpj) |
---|
[59] | 45 | ; arrays or 2D arrays (jpi,jpj). Note that of n_dimensions = 1, then the |
---|
| 46 | ; grid must be regular (each longitudes must be the same for all latitudes |
---|
[118] | 47 | ; and each latitudes should be the sae for all longitudes). |
---|
[59] | 48 | ; |
---|
[125] | 49 | ; @keyword DOUBLE use double precision to perform the computation |
---|
[118] | 50 | ; |
---|
| 51 | ; @examples |
---|
| 52 | ; |
---|
| 53 | ; 1) IDL> ncdf_get_gridparams, 'coordinates_ORCA_R05.nc', 'glamt', 'gphit' $ |
---|
[59] | 54 | ; , olon, olat, jpio, jpjo, 2 |
---|
| 55 | ; |
---|
[118] | 56 | ; 2) IDL> ncdf_get_gridparams, olon, olat, jpio, jpjo, 2 |
---|
[59] | 57 | ; |
---|
[101] | 58 | ; @history |
---|
[118] | 59 | ; November 2005: Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
| 60 | ; |
---|
| 61 | ; @version $Id$ |
---|
| 62 | ; |
---|
[59] | 63 | ;- |
---|
| 64 | ; |
---|
| 65 | ;---------------------------------------------------------- |
---|
| 66 | ;---------------------------------------------------------- |
---|
| 67 | ; |
---|
| 68 | PRO get_gridparams, in1, in2, in3, in4, in5, in6, in7, in8, DOUBLE = double |
---|
| 69 | ; file, lonname, latname, lon, lat, jpi, jpj, n_dimensions |
---|
| 70 | ; lon, lat, jpi, jpj, n_dimensions |
---|
| 71 | ; |
---|
[114] | 72 | ; |
---|
| 73 | compile_opt idl2, strictarrsubs |
---|
| 74 | ; |
---|
[59] | 75 | CASE n_params() OF |
---|
| 76 | 8:BEGIN |
---|
| 77 | ; get longitude and latitude |
---|
[118] | 78 | IF file_test(in1) EQ 0 THEN BEGIN |
---|
[59] | 79 | print, 'file ' + in1 + ' does not exist' |
---|
| 80 | stop |
---|
| 81 | ENDIF |
---|
| 82 | cdfido = ncdf_open(in1) |
---|
| 83 | ncdf_varget, cdfido, in2, lon |
---|
| 84 | ncdf_varget, cdfido, in3, lat |
---|
[118] | 85 | ncdf_close, cdfido |
---|
[59] | 86 | n_dimensions = in8 |
---|
| 87 | END |
---|
[118] | 88 | 5:BEGIN |
---|
[59] | 89 | lon = temporary(in1) |
---|
| 90 | lat = temporary(in2) |
---|
| 91 | n_dimensions = in5 |
---|
| 92 | END |
---|
[118] | 93 | ELSE:BEGIN |
---|
[59] | 94 | print, 'Bad nimber of input parameters' |
---|
| 95 | stop |
---|
| 96 | end |
---|
| 97 | ENDCASE |
---|
| 98 | ; |
---|
| 99 | sizelon = size(lon) |
---|
| 100 | sizelat = size(lat) |
---|
[118] | 101 | CASE 1 OF |
---|
[59] | 102 | ;------- |
---|
| 103 | ; lon and lat are 1D arrays |
---|
| 104 | ;------- |
---|
[118] | 105 | sizelon[0] EQ 1 AND sizelat[0] EQ 1:BEGIN |
---|
[59] | 106 | ; get jpi and jpj |
---|
| 107 | jpi = sizelon[1] |
---|
| 108 | jpj = sizelat[1] |
---|
| 109 | ; make sure that lon and lat have the good number of dimensions |
---|
[118] | 110 | CASE n_dimensions OF |
---|
[59] | 111 | 1: |
---|
| 112 | 2:BEGIN |
---|
| 113 | ; make lon and lat 2D arrays |
---|
| 114 | lon = temporary(lon) # replicate(1, jpj) |
---|
| 115 | lat = replicate(1, jpi) # temporary(lat) |
---|
| 116 | END |
---|
| 117 | ELSE:stop |
---|
| 118 | ENDCASE |
---|
| 119 | END |
---|
| 120 | ;------- |
---|
| 121 | ; lon is 2D array and lat is 1D array |
---|
| 122 | ;------- |
---|
[118] | 123 | sizelon[0] EQ 2 AND sizelat[0] EQ 1:BEGIN |
---|
[59] | 124 | ; get jpi and jpj |
---|
| 125 | jpi = sizelon[1] |
---|
| 126 | jpj = sizelon[2] |
---|
| 127 | IF jpj NE n_elements(lat) THEN stop |
---|
| 128 | ; make sure that lon and lat have the good number of dimensions |
---|
[118] | 129 | CASE n_dimensions OF |
---|
| 130 | 1:BEGIN |
---|
| 131 | IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN |
---|
[59] | 132 | print, 'Longitudes are not the same for all latitudes, imposible to extract a 1D array of the longitudes' |
---|
| 133 | stop |
---|
| 134 | ENDIF |
---|
| 135 | lon = lon[*, 0] |
---|
| 136 | END |
---|
| 137 | 2:lat = replicate(1, jpi) # temporary(lat) |
---|
| 138 | ELSE:stop |
---|
| 139 | ENDCASE |
---|
| 140 | END |
---|
| 141 | ;------- |
---|
| 142 | ; lon is 1D array and lat is 2D array |
---|
| 143 | ;------- |
---|
[118] | 144 | sizelon[0] EQ 1 AND sizelat[0] EQ 2:BEGIN |
---|
[59] | 145 | ; get jpi and jpj |
---|
| 146 | jpi = sizelat[1] |
---|
| 147 | jpj = sizelat[2] |
---|
| 148 | IF jpi NE n_elements(lon) THEN stop |
---|
| 149 | ; make sure that lon and lat have the good number of dimensions |
---|
[118] | 150 | CASE n_dimensions OF |
---|
| 151 | 1:BEGIN |
---|
| 152 | IF array_equal(lat, replicate(1, jpi) # lat[0, *]) NE 1 THEN BEGIN |
---|
[59] | 153 | print, 'Latitudes are not the same for all longitudes, imposible to extract a 1D array of the latitudes' |
---|
| 154 | stop |
---|
| 155 | ENDIF |
---|
| 156 | lat = reform(lat[0, *]) |
---|
| 157 | END |
---|
| 158 | 2:lon = temporary(lon) # replicate(1, jpj) |
---|
| 159 | ELSE:stop |
---|
| 160 | ENDCASE |
---|
| 161 | END |
---|
| 162 | ;------- |
---|
| 163 | ; lon and lat are 2D arrays |
---|
| 164 | ;------- |
---|
[118] | 165 | sizelon[0] EQ 2 AND sizelat[0] EQ 2:BEGIN |
---|
[59] | 166 | ; get jpi and jpj |
---|
| 167 | IF array_equal(sizelon[1:2], sizelat[1:2]) NE 1 THEN stop |
---|
| 168 | jpi = sizelon[1] |
---|
[118] | 169 | jpj = sizelon[2] |
---|
[59] | 170 | ; make sure that lon and lat have the good number of dimensions |
---|
[118] | 171 | CASE n_dimensions OF |
---|
| 172 | 1:BEGIN |
---|
| 173 | IF array_equal(lon, lon[*, 0] # replicate(1, jpj)) NE 1 THEN BEGIN |
---|
[59] | 174 | print, 'Longitudes are not the same for all latitudes, imposible to extract a 1D array of the longitudes' |
---|
| 175 | stop |
---|
| 176 | ENDIF |
---|
| 177 | lon = lon[*, 0] |
---|
[118] | 178 | IF array_equal(lat, replicate(1, jpi) # reform(lat[0, *])) NE 1 THEN BEGIN |
---|
[59] | 179 | print, 'Latitudes are not the same for all longitudes, imposible to extract a 1D array of the latitudes' |
---|
| 180 | stop |
---|
| 181 | ENDIF |
---|
| 182 | lat = reform(lat[0, *]) |
---|
| 183 | END |
---|
| 184 | 2: |
---|
| 185 | ELSE:stop |
---|
| 186 | ENDCASE |
---|
| 187 | END |
---|
| 188 | ;------- |
---|
| 189 | ; lon and lat are not 1D and/or 2D arrays |
---|
| 190 | ;------- |
---|
| 191 | ELSE:stop |
---|
| 192 | ENDCASE |
---|
| 193 | ; |
---|
| 194 | ;------- |
---|
| 195 | ; double keyword |
---|
| 196 | ;------- |
---|
[118] | 197 | if keyword_set(double) then BEGIN |
---|
[59] | 198 | lon = double(temporary(lon)) |
---|
| 199 | lat = double(temporary(lat)) |
---|
| 200 | ENDIF |
---|
| 201 | ; |
---|
| 202 | ; give back the right outparameters. |
---|
| 203 | ; |
---|
| 204 | |
---|
| 205 | CASE n_params() OF |
---|
| 206 | 8:BEGIN |
---|
| 207 | in4 = temporary(lon) |
---|
| 208 | in5 = temporary(lat) |
---|
| 209 | in6 = temporary(jpi) |
---|
| 210 | in7 = temporary(jpj) |
---|
| 211 | END |
---|
| 212 | 5:BEGIN |
---|
| 213 | in1 = temporary(lon) |
---|
| 214 | in2 = temporary(lat) |
---|
| 215 | in3 = temporary(jpi) |
---|
| 216 | in4 = temporary(jpj) |
---|
| 217 | END |
---|
| 218 | ENDCASE |
---|
| 219 | ; |
---|
| 220 | |
---|
| 221 | return |
---|
| 222 | END |
---|