[3403] | 1 | ;------------------------------------------------------------- |
---|
| 2 | ;+ |
---|
| 3 | ; NAME: |
---|
| 4 | ; STRUCT_AUTOCONVERT |
---|
| 5 | ; PURPOSE: |
---|
| 6 | ; Convert a structure wih all string items to numeric. |
---|
| 7 | ; CATEGORY: |
---|
| 8 | ; CALLING SEQUENCE: |
---|
| 9 | ; out = struct_autoconvert(in) |
---|
| 10 | ; INPUTS: |
---|
| 11 | ; in = Structure with all string items. in |
---|
| 12 | ; KEYWORD PARAMETERS: |
---|
| 13 | ; OUTPUTS: |
---|
| 14 | ; out = Converted structure with numeric out |
---|
| 15 | ; strings converted to numeric values. |
---|
| 16 | ; COMMON BLOCKS: |
---|
| 17 | ; NOTES: |
---|
| 18 | ; Notes: Non-numeric items are kept as strings. |
---|
| 19 | ; Integer items are returned as long ints. |
---|
| 20 | ; Floating items are returned as double. |
---|
| 21 | ; MODIFICATION HISTORY: |
---|
| 22 | ; R. Sterner, 2006 Sep 07 |
---|
| 23 | ; |
---|
| 24 | ; Copyright (C) 2006, Johns Hopkins University/Applied Physics Laboratory |
---|
| 25 | ; This software may be used, copied, or redistributed as long as it is not |
---|
| 26 | ; sold and this copyright notice is reproduced on each copy made. This |
---|
| 27 | ; routine is provided as is without any express or implied warranties |
---|
| 28 | ; whatsoever. Other limitations apply as described in the file disclaimer.txt. |
---|
| 29 | ;- |
---|
| 30 | ;------------------------------------------------------------- |
---|
| 31 | function struct_autoconvert, in, help=hlp |
---|
| 32 | |
---|
| 33 | if (n_params(0) lt 1) or keyword_set(hlp) then begin |
---|
| 34 | print,' Convert a structure wih all string items to numeric.' |
---|
| 35 | print,' out = struct_autoconvert(in)' |
---|
| 36 | print,' in = Structure with all string items. in' |
---|
| 37 | print,' out = Converted structure with numeric out' |
---|
| 38 | print,' strings converted to numeric values.' |
---|
| 39 | print,' Notes: Non-numeric items are kept as strings.' |
---|
| 40 | print,' Integer items are returned as long ints.' |
---|
| 41 | print,' Floating items are returned as double.' |
---|
| 42 | return,'' |
---|
| 43 | endif |
---|
| 44 | |
---|
| 45 | tags = tag_names(in) ; Tags in incoming strutcure. |
---|
| 46 | n = n_elements(tags) ; Number of tags. |
---|
| 47 | typ = bytarr(n) ; To hold data types. |
---|
| 48 | tbl = [7,3,5] ; Convert isnumber output to type. |
---|
| 49 | |
---|
| 50 | for i=0,n-1 do typ(i)=tbl(isnumber(in.(i))) ; Find data types. |
---|
| 51 | |
---|
| 52 | out = create_struct(tags(0), $ ; Create output struct. |
---|
| 53 | to_datatype(in.(0),typ(0))) |
---|
| 54 | for i=1,n-1 do out = create_struct(out, $ |
---|
| 55 | tags(i),to_datatype(in.(i),typ(i))) |
---|
| 56 | |
---|
| 57 | return, out |
---|
| 58 | |
---|
| 59 | end |
---|