[157] | 1 | ;+ |
---|
| 2 | ; @file_comments |
---|
| 3 | ; |
---|
| 4 | ; |
---|
| 5 | ; @categories |
---|
| 6 | ; |
---|
| 7 | ; |
---|
| 8 | ; @param FILENAME |
---|
| 9 | ; The filename |
---|
| 10 | ; |
---|
| 11 | ; @param INFO |
---|
| 12 | ; General info |
---|
| 13 | ; |
---|
| 14 | ; @param DINFO |
---|
| 15 | ; Dimension info |
---|
| 16 | ; |
---|
| 17 | ; @param VINFO |
---|
| 18 | ; Variable info |
---|
| 19 | ; |
---|
| 20 | ; @param GATTS |
---|
| 21 | ; Global attributes |
---|
| 22 | ; |
---|
| 23 | ; @param VATTS |
---|
| 24 | ; Variable attributes |
---|
| 25 | ; |
---|
| 26 | ; @param DATA |
---|
| 27 | ; Data |
---|
| 28 | ; |
---|
| 29 | ; |
---|
| 30 | ; @returns |
---|
| 31 | ; |
---|
| 32 | ; |
---|
| 33 | ; @restrictions |
---|
| 34 | ; |
---|
| 35 | ; |
---|
| 36 | ; @examples |
---|
| 37 | ; |
---|
| 38 | ; |
---|
| 39 | ; @history |
---|
| 40 | ; |
---|
| 41 | ; |
---|
| 42 | ; @version |
---|
| 43 | ; $Id$ |
---|
| 44 | ; |
---|
| 45 | ;- |
---|
[67] | 46 | PRO ncdf_read,filename,info,dinfo,vinfo,gatts,vatts,data |
---|
| 47 | ; -------- ---- ----- ----- ----- ----- ---- |
---|
| 48 | ; | | | | | | |
---|
| 49 | ; general info --' | | | | `-- data |
---|
| 50 | ; dimension info --------' | | `-------- variable attributes |
---|
| 51 | ; variable info --------------' `-------------- global attributes |
---|
| 52 | ; |
---|
| 53 | ; ================== |
---|
| 54 | ; read a NetCDF file |
---|
| 55 | ; ================== |
---|
| 56 | ; |
---|
| 57 | ; NB The data is read into a rather nasty combination of structures, arrays, |
---|
| 58 | ; and pointers, which is, unfortunately, necessary in order to cope with |
---|
| 59 | ; the full generality of the data format. Here is the sort of syntax you |
---|
| 60 | ; might use to get at elements of the returned data -- cumbersome because |
---|
| 61 | ; IDL doesn't support C-type "a->b" shorthand for "(*a).b". |
---|
| 62 | ; |
---|
| 63 | ; "INFO.NDIMS" |
---|
| 64 | ; "INFO.NVARS" |
---|
| 65 | ; "INFO.NGATTS" |
---|
| 66 | ; "INFO.RECDIM" |
---|
| 67 | ; |
---|
| 68 | ; "DINFO[idim].NAME" |
---|
| 69 | ; "DINFO[idim].SIZE" |
---|
| 70 | ; |
---|
| 71 | ; "VINFO[ivar].NAME" |
---|
| 72 | ; "VINFO[ivar].NAME" |
---|
| 73 | ; "VINFO[ivar].DATATYPE" |
---|
| 74 | ; "VINFO[ivar].NDIMS" |
---|
| 75 | ; "VINFO[ivar].NATTS" |
---|
| 76 | ; "VINFO[ivar].DIM[ivdim]" |
---|
| 77 | ; |
---|
| 78 | ; "GATTS.NAME" |
---|
| 79 | ; "GATTS.DATATYPE" |
---|
| 80 | ; "GATTS.LENGTH" |
---|
| 81 | ; "*GATTS.VALUES" or maybe "STRING(*GATTS.VALUES)" |
---|
| 82 | ; |
---|
| 83 | ; "(*VATTS[ivar])[iatt].NAME" |
---|
| 84 | ; "(*VATTS[ivar])[iatt].DATATYPE" |
---|
| 85 | ; "(*VATTS[ivar])[iatt].LENGTH" |
---|
| 86 | ; "*(*VATTS[ivar])[iatt].VALUES" or maybe |
---|
| 87 | ; "STRING(*(*VATTS[ivar])[iatt].VALUES)" |
---|
| 88 | ; |
---|
| 89 | ; "*DATA[ivar]" or maybe "(*DATA[ivar])[idim1,idim2,idim3,...]" |
---|
| 90 | ; |
---|
| 91 | ;---------------------------------------------------------------------- |
---|
[114] | 92 | ; |
---|
| 93 | compile_opt idl2, strictarrsubs |
---|
| 94 | ; |
---|
[67] | 95 | |
---|
| 96 | |
---|
| 97 | ;; open file |
---|
| 98 | |
---|
| 99 | id=ncdf_open(filename) |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | ;; info |
---|
| 103 | |
---|
| 104 | info=ncdf_inquire(id) |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | ;; dimension info |
---|
| 108 | |
---|
| 109 | dinfo=replicate({name:"",size:0L},info.ndims) |
---|
| 110 | for idim=0,info.ndims-1 do begin $ |
---|
| 111 | ncdf_diminq,id,idim,name,size |
---|
| 112 | dinfo[idim].name=name |
---|
| 113 | dinfo[idim].size=size |
---|
| 114 | endfor |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | ;; variable info |
---|
| 118 | |
---|
| 119 | vinfo=replicate({ name:"", $ |
---|
| 120 | datatype:"", $ |
---|
| 121 | ndims:0l, natts:0l, $ |
---|
| 122 | dim:lonarr(info.ndims) $ |
---|
| 123 | },info.nvars) |
---|
| 124 | |
---|
| 125 | for ivar=0,info.nvars-1 do begin |
---|
| 126 | var=ncdf_varinq(id,ivar) |
---|
| 127 | vinfo[ivar].name=var.name |
---|
| 128 | vinfo[ivar].datatype=var.datatype |
---|
| 129 | vinfo[ivar].ndims=var.ndims |
---|
| 130 | vinfo[ivar].natts=var.natts |
---|
| 131 | vinfo[ivar].dim=var.dim |
---|
| 132 | endfor |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | ;; global attributes |
---|
| 136 | |
---|
[70] | 137 | if info.ngatts gt 0 then begin |
---|
| 138 | gatts=replicate({name:'', $ |
---|
| 139 | datatype:'', $ |
---|
| 140 | length:0L, $ |
---|
| 141 | values:ptr_new()}, $ |
---|
| 142 | info.ngatts) |
---|
| 143 | for iatt=0,info.ngatts-1 do begin |
---|
| 144 | name=ncdf_attname(id,iatt,/global) |
---|
| 145 | gatts[iatt].name=name |
---|
| 146 | att=ncdf_attinq(id,name,/global) |
---|
| 147 | gatts[iatt].length=att.length |
---|
| 148 | gatts[iatt].datatype=att.datatype |
---|
| 149 | ncdf_attget,id,name,vals,/global |
---|
| 150 | gatts[iatt].values=ptr_new(vals) |
---|
| 151 | endfor |
---|
| 152 | endif else begin |
---|
[493] | 153 | ;; arbitrary scalar value |
---|
[70] | 154 | ;; an empty list would be sensible but IDL doesn't support it |
---|
| 155 | gatts=-1 |
---|
| 156 | endelse |
---|
[67] | 157 | |
---|
| 158 | |
---|
| 159 | |
---|
| 160 | ;; variable attributes |
---|
| 161 | |
---|
| 162 | vatts=replicate(ptr_new(),info.nvars) |
---|
| 163 | for ivar=0,info.nvars-1 do begin |
---|
[70] | 164 | if vinfo[ivar].natts gt 0 then begin |
---|
| 165 | vatts[ivar]=ptr_new(replicate({name:'', $ |
---|
| 166 | datatype:'', $ |
---|
| 167 | length:0L, $ |
---|
| 168 | values:ptr_new()}, $ |
---|
[67] | 169 | vinfo[ivar].natts)) |
---|
[70] | 170 | for iatt=0,vinfo[ivar].natts-1 do begin |
---|
| 171 | name=ncdf_attname(id,ivar,iatt) |
---|
| 172 | (*vatts[ivar])[iatt].name=name |
---|
| 173 | att=ncdf_attinq(id,ivar,name) |
---|
| 174 | (*vatts[ivar])[iatt].length=att.length |
---|
| 175 | (*vatts[ivar])[iatt].datatype=att.datatype |
---|
| 176 | ncdf_attget,id,ivar,name,vals |
---|
| 177 | (*vatts[ivar])[iatt].values=ptr_new(vals) |
---|
| 178 | endfor |
---|
| 179 | endif else begin |
---|
| 180 | vatts[ivar]=ptr_new(-1) |
---|
| 181 | ;; Pointer to arbitrary scalar -- analogous to case of lack of |
---|
| 182 | ;; global attributes above. We could put a <NullPointer> here |
---|
| 183 | ;; instead, but try to be friendlier to code that might try |
---|
| 184 | ;; to dereference it. |
---|
| 185 | endelse |
---|
[67] | 186 | endfor |
---|
| 187 | |
---|
| 188 | |
---|
| 189 | ;; data |
---|
| 190 | |
---|
| 191 | data=replicate(ptr_new(),info.nvars) |
---|
| 192 | for ivar=0,info.nvars-1 do begin |
---|
| 193 | ncdf_varget,id,ivar,val |
---|
| 194 | data[ivar]=ptr_new(val) |
---|
| 195 | endfor |
---|
| 196 | |
---|
| 197 | |
---|
| 198 | end |
---|