; Read_LWS_HPDP ; Procedure to read the ASCII format L01 and L02 Pipeline data, as written by WR_ASCII_PPP. ; T W Grundy ; SSTD, RAL, UK ; Original version 7/11/2003 ; Current Version 3.0 24/01/2007 ; ; Change History ; Version 1.0 - Original version. ; Version 1.1 - 10/11/03 - Modified to speed up procedure, to ensure performance is ; maintained for large files. ; Version 1.2 - 12/11/03 - Modified to correct for changes in wr_ascii_ppp. ; Version 1.3 - 28/11/03 - Release version. ; Version 2.0 - 20/03/05 - L02 HPDP version. ; Version 3.0 - 24/01/07 - SSC HPDP version. ; ; USAGE - read_lws_hpdp, file, data, header ; FILE - String full path to ascii file to be read. ; DATA - Structure output LSAN structure with reduced number of tags compared ; with OLP 10.1 LSAN format. ; HEADER - String array output variable containing primary and secondary headers. ; ; Performance - This procedure can be quite slow for large files. ; ; Limitations - This procedure is designed for use with Unix operating systems. Operation ; has not been tested on Windows, Linux or VMS, and is therefore not guaranteed. PRO READ_LWS_HPDP, FILE, DATA, HEADER ; try to open file IF (FILE_TEST(file) NE 1) THEN BEGIN PRINT, 'File not found' RETURN ENDIF PRINT, 'Reading headers...' ; read until end of primary header GET_LUN, lun OPENR, lun, file temp1 = '' phend = 0 count = 0 WHILE (phend EQ 0) DO BEGIN READF, lun, temp1, FORMAT='(A80)' IF (count EQ 0) THEN header = temp1 ELSE header = [header, temp1] IF (STRMID(temp1, 0, 3) EQ 'END') THEN phend = 1 count = count + 1 ENDWHILE ; include 1 blank line in header variable to separate primary from secondary gap = 1 WHILE (gap EQ 1) DO BEGIN READF, lun, temp1, FORMAT='(A80)' IF ((BYTE(STRMID(temp1, 0, 1)))[0] NE 32B) THEN gap = 0 ELSE temp2 = temp1 ENDWHILE header = [header, temp2, temp1] ; check aot nh = N_ELEMENTS(header) faot = 0 i = 0 WHILE (faot EQ 0) DO BEGIN iaot = STRPOS(header[i], 'EOHAAOTN') IF (iaot[0] NE -1) THEN BEGIN paot = i faot = 1 ENDIF i = i + 1 IF (i EQ nh) THEN BREAK ENDWHILE ; check origin forg = 0 i = 0 WHILE (forg EQ 0) DO BEGIN iorg = STRPOS(header[i], 'ORIGIN') IF (iorg[0] NE -1) THEN BEGIN porg = i forg = 1 ENDIF i = i + 1 IF (i EQ nh) THEN BREAK ENDWHILE ; select mode - L01 is default IF (faot NE 1) THEN aot = 'L01' ELSE aot = STRMID(header[paot], 11, 3) IF (forg NE 1) THEN org = 'ESA' ELSE org = STRMID(header[porg], 11, 3) IF ((STRUPCASE(aot) EQ 'L01') AND (org EQ 'ESA')) THEN mode = 'L01' ELSE mode = 'L02' ; read secondary header shend = 0 WHILE (shend EQ 0) DO BEGIN READF, lun, temp1, FORMAT='(A80)' header = [header, temp1] IF (STRMID(temp1, 0, 3) EQ 'END') THEN shend = 1 ENDWHILE ; header is now complete, so just need to cross blank space before data gap = 1 WHILE (gap EQ 1) DO BEGIN READF, lun, temp1, FORMAT='(A)' IF ((BYTE(STRMID(temp1, 0, 1)))[0] NE 32B) THEN gap = 0 ENDWHILE READF, lun, temp1, FORMAT='(A)' ; find current file position and compute number of records stat = FSTAT(lun) IF (mode EQ 'L01') THEN rec_len = 49L ELSE rec_len = 52L nrecs = (stat.size-stat.cur_ptr)/rec_len ; create the output data structure tdata = {lsanrpid: BYTARR(2), $ lsanline: 0, $ lsandet: 0, $ lsanscnt: 0, $ lsanwav: 0., $ lsanflx:0., $ lsanflxu:0.} data = REPLICATE(tdata, nrecs) ; file pointer is now at the start of the data temp2 = 0 temp3 = 0 temp41 = 0 temp42 = 0 temp5 = 0 temp6 = 0. temp7 = 0. temp8 = 0. row = 0L PRINT, 'Reading data...' PRINT, 'Records in file: ', nrecs PRINT, 'START: ', SYSTIME() IF (mode EQ 'L01') THEN BEGIN FOR i = 0L, (nrecs-1L) DO BEGIN READF, lun, FORMAT='(I4, I3, I3, I3, F9, E13, E13)', temp2, temp3, temp42, $ temp5, temp6, temp7, temp8 data[i].lsanrpid[0] = temp2 data[i].lsanrpid[1] = temp3 data[i].lsandet = temp42 data[i].lsanscnt = temp5 data[i].lsanwav = temp6 data[i].lsanflx = temp7 data[i].lsanflxu = temp8 IF (row EQ 10000L) THEN BEGIN PRINT, i, ' records read' row = 0L ENDIF row = row + 1L ENDFOR data.lsanline = 1 ENDIF ELSE BEGIN FOR i = 0L, (nrecs-1L) DO BEGIN READF, lun, FORMAT='(I4, I3, I3, I3, I3, F9, E13, E13)', temp2, temp3, $ temp41, temp42, temp5, temp6, temp7, temp8 data[i].lsanrpid[0] = temp2 data[i].lsanrpid[1] = temp3 data[i].lsanline = temp41 data[i].lsandet = temp42 data[i].lsanscnt = temp5 data[i].lsanwav = temp6 data[i].lsanflx = temp7 data[i].lsanflxu = temp8 IF (row EQ 10000L) THEN BEGIN PRINT, i, ' records read' row = 0L ENDIF row = row + 1L ENDFOR ENDELSE PRINT, nrecs, ' records read' PRINT, 'END: ', SYSTIME() PRINT, 'Finished' CLOSE, lun FREE_LUN, lun END