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