/CZT_SIM/read_table.pro

https://github.com/emrahk/IDL_LAB · Prolog · 203 lines · 166 code · 37 blank · 0 comment · 31 complexity · b9607fe299e361a0623a335a061aa117 MD5 · raw file

  1. FUNCTION READ_TABLE, filename, $
  2. columns=columns, nrows=nrows, nmax=nmax, double=double, text=text, head=head
  3. ; ----------------------------------------------------------
  4. ;+
  5. ; NAME:
  6. ; READ_TABLE
  7. ;
  8. ; PURPOSE:
  9. ; Read an ASCII table into a data array.
  10. ;
  11. ; AUTHOR:
  12. ; Simon Vaughan (U.Leicester)
  13. ;
  14. ; CALLING SEQUENCE:
  15. ; data = READ_TABLE('file.dat')
  16. ;
  17. ; INPUTS:
  18. ; file - (string) file name
  19. ;
  20. ; OPTIONAL INPUTS:
  21. ; columns - (integer vector) which columns of table to retain
  22. ; nrows - (integer) number of lines to read
  23. ; nmax - (integer) minimum size of file, default is 100,000
  24. ; double - (logical) whether to use double or single prec.
  25. ; text - (logical) whether to load text from file
  26. ; head - (integer) number of lines to skip in header
  27. ;
  28. ; OUTPUTS:
  29. ; 2-dimensional data array (floating point values)
  30. ;
  31. ; DETAILS:
  32. ; Data are assumed to be floating point numbers, by
  33. ; default single precision, seperated by spaces in a
  34. ; rectangular grid (ncols*nrow)
  35. ;
  36. ; Example calls:
  37. ;
  38. ; IDL> data = read_table('table.txt')
  39. ; IDL> data = read_table('table.txt',columns=[2,3])
  40. ; IDL> data = read_table('table.txt',n=100)
  41. ;
  42. ; HISTORY:
  43. ; Based losely on DDREAD.PRO by Frank Knight (MIT)
  44. ; 11/01/2007 - v1.0 - first working version
  45. ; 27/04/2007 - v1.1 - added TEXT option
  46. ; 22/11/2007 - v1.2 - added HEAD option
  47. ; 22/09/2008 - v1.3 - HEAD now takes integer input
  48. ; 16/06/2010 - v1.4 - fixed handling of text input
  49. ; using STRSPLIT function.
  50. ;
  51. ;-
  52. ; ----------------------------------------------------------
  53. ; options for compilation (recommended by RSI)
  54. COMPILE_OPT idl2, HIDDEN
  55. ; watch out for errors
  56. on_error,2
  57. ; ----------------------------------------------------------
  58. ; Check the arguments
  59. ; is the file name defined?
  60. if (n_elements(filename) eq 0) then begin
  61. filename=''
  62. read,'-- Enter file name (ENTER to list current directory): ',filename
  63. if (filename eq '') then begin
  64. list = findfile()
  65. print, list
  66. read,'-- Enter file name: ',filename
  67. endif
  68. endif
  69. ; is there a user-defined maximum file size?
  70. if (n_elements(nmax) eq 0) then nmax=100000L
  71. ; is there a user-defined number of lines to read
  72. if (n_elements(nrows) eq 0) then nrows=nmax
  73. ; sanity check
  74. if (nrows gt nmax) then nmax = nrows
  75. ; are we reading in single (=4) or double precision (=5)?
  76. type=4
  77. if (keyword_set(double)) then type=5
  78. ; are we reading numbers or text?
  79. if (keyword_set(text)) then type=7
  80. ; ----------------------------------------------------------
  81. ; Checks of the file existance and shape
  82. ; check the file exists
  83. if ((findfile(filename))[0] eq '') then begin
  84. print,'** File not found.'
  85. return,0
  86. endif
  87. ; find the number of columns in the file by reading first line
  88. ; into a string (tmp)
  89. ncols = 0
  90. tmp = ''
  91. openr, lun, filename, /get_lun
  92. if keyword_set(head) then begin
  93. for i=0,head-1 do readf, lun, tmp ; skip header
  94. endif
  95. readf,lun,tmp
  96. free_lun, lun
  97. ; remove whitespace
  98. tmp = ' ' + strcompress(strtrim(tmp,2))
  99. ; count the spaces (there is one per column)
  100. for i=0,strlen(tmp)-1 do begin
  101. ncols = ncols + (strpos(tmp,' ',i) eq i)
  102. end
  103. ; ----------------------------------------------------------
  104. ; load the data into an array
  105. ; define the data array ready to receive data
  106. data = make_array(size=[2,ncols,nrows,type,ncols*nrows])
  107. ; define a single line (row) array for reading each line
  108. ; except for text which is loaded a whole line at a time
  109. if NOT KEYWORD_SET(text) then begin
  110. record = make_array(size=[1,ncols,type,ncols])
  111. endif else begin
  112. record = ''
  113. endelse
  114. ; Open the file ready to read
  115. openr, lun, filename, /get_lun
  116. ; skip header line if HEAD keyword is set
  117. if keyword_set(head) then begin
  118. for i=0,head-1 do readf, lun, tmp ; skip header
  119. endif
  120. ; Read each line one at a time, until either end-of-file
  121. ; or we reach nrows.
  122. n = 0L
  123. while (eof(lun) ne 1) do begin
  124. on_ioerror, IOERR
  125. error = 1
  126. readf, lun, record
  127. ; print,record
  128. ; help,record
  129. error = 0
  130. if KEYWORD_SET(text) then begin
  131. data[*,n] = STRSPLIT(record, ' ', /EXTRACT)
  132. endif else begin
  133. data[*,n] = record
  134. endelse
  135. n = n + 1L
  136. if (n eq nrows) then break
  137. IOERR:
  138. if (error eq 1) then begin
  139. print, '** Error reading line',n,' in READ_TABLE'
  140. free_lun,lun
  141. return,0
  142. endif
  143. endwhile
  144. ; Did we finish with the file or run out of rows in array?
  145. if (eof(lun) ne 1 && n ge nmax) then begin
  146. print,"** Increase nmax in READ_TABLE."
  147. endif
  148. ; Close the file
  149. free_lun, lun
  150. ; ----------------------------------------------------------
  151. ; Return the data array to the user
  152. ; trim the unused rows
  153. data = data[*,0:(n-1)]
  154. ; if no column selection, return entire array
  155. if (n_elements(columns) eq 0) then return, data
  156. ; otherwise remove unwanted columns before returning
  157. indx=where((columns ge ncols-1),count)
  158. if (count eq 0) then begin
  159. data = data[columns,*]
  160. endif else begin
  161. print,'** Requested columns outside allowed range'
  162. print,'** Returning all columns from READ_TABLE'
  163. endelse
  164. return,data
  165. END
  166. ; ----------------------------------------------------------