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