PRO sortvtm,ny10,ny20,nm10,nm20,nd10,nd20 ; This program reads daily log files of VTM data from the US Coast ; Guard. Data sent from the ferry Marin of the Golden Gate Transit ; System are selected and separated into files by date. ; The date is determined using PST, while the data were separated into ; days by the Coast Guard using EST. The actual data variable is kept ; in GMT. ; 20001101.log --> mar01101.log (for example) ; ********|*******|******** ; * GMT GMT GMT GMT GMT * ; ********|*******|******** ; ; ********|*******|******** ; * EST EST EST EST EST * ; ********|*******|******** ; ; ********|*******|******** ; * PST PST PST PST PST * ; ********|*******|******** ; answer=''; answer buffer string0=''; input buffer, previous line string1=''; input buffer, current line stringa=''; first of three message lines stringb=''; second of three message lines stringc=''; third of three message lines iline=long(0); counter of input lines nline=long(10); number of input lines to print out. ilout=long(0); counter of output lines nlout=long(6); number of output lines to print out. ; Initialize some control flags. ifirst=1; flag to indicate the first day to be processed. iopen=0; flag to indicate if there is a data file open yet. ; The parameters below give the starting date and range of time for the ; block of USCG data to be analyzed. The first EST day file includes some ; ferry runs from the last day of a previous block of data. This data ; is lost. IF n_params() lt 6 THEN BEGIN ny10=2000; first year read,'first year: '+string(ny10)+': ',answer IF strlen(answer) gt 0 THEN ny10=fix(answer) ny20=2000; second year read,'second year: '+string(ny20)+': ',answer IF strlen(answer) gt 0 THEN ny20=fix(answer) nm10=11; first month read,'first month: '+string(nm10)+': ',answer IF strlen(answer) gt 0 THEN nm10=fix(answer) nm20=11; last montht read,'second month: '+string(nm20)+': ',answer IF strlen(answer) gt 0 THEN nm20=fix(answer) nd10=1; first day of first month read,'first day: '+string(nd10)+': ',answer IF strlen(answer) gt 0 THEN nd10=fix(answer) nd20=0; last day of last month; if zero, all days will be done. read,'last day (enter zero to finish out last month): '+ $ string(nd20)+': ',answer IF strlen(answer) gt 0 THEN nd20=fix(answer) ENDIF print,'Data will be sorted into files by PST day, for dates ' print,'starting with year ',ny10,', month ',nm10,', day ',nd10 print,'and ending with year ',ny20,', month ',nm20,', day ',nd20 ;***** Start the main loops over months and days. ioutopen=0; no output file open yet. nm1=nm10; special value, first year only. nd1=nd10; special value, first month only. nm2=12; all except last year. FOR iy=ny10,ny20 DO BEGIN IF iy eq ny20 THEN nm2=nm20; special value, last year only FOR im = nm1,nm2 DO BEGIN path='/d4/Darmat/data/naut/vtm/'+ string(iy,format='(i4.4)') + $ '/'+string(im,format="(i2.2)")+'/' nd2=nday(iy,im); Go all the way to the end of the month. IF iy eq ny20 and im eq nm20 and nd20 ge 1 THEN $ nd2 = nd20; In this case, stop at indicated day. FOR id=nd1,nd2 DO BEGIN file=string(iy,format='(i4.4)')+string(im,format='(i2.2)')+ $ string(id,format='(i2.2)')+'.log' infile = path+file dummy = findfile(infile,count=ctinfile) print,'infile, count = ',infile,ctinfile IF ctinfile ne 0 THEN BEGIN ; An existing input file has been found. (If this input file is not ; found, we just go around the loops and try the next one.) ; Now filter it to the tempfile with grep, ; keeping only lines including the string "Marin". tempfile='temp.tmp' message='rm '+tempfile spawn,message; Remove any existing file named tempfile. message="grep 'Marin' "+infile+' > '+tempfile spawn,message; Filter to tempfile. openr,luntemp,tempfile,/get_lun; Open file of "Marin" lines. ; Start loop reading lines from tempfile and writing them to the file ; for the appropriate PST day. WHILE eof(luntemp) eq 0 DO BEGIN string0=string1; save previous string. readf,luntemp,string1; read in a new string. iline=iline+1 ; IF iline lt nline then print,iline,string1 oneline,string1,nstr,strvar; Parse the comma-delimited strings. IF strvar(0) eq '$PRDCV' and strvar(1) eq 'Marin' THEN BEGIN; test for any of the three types of lines. IF ilout lt nlout THEN print,iy,im,id,string1 ; Test for the second line of the three-line cluster. This line has the ; time (GMT) as string 4. If the hour is four or greater, the output file ; should be for the same date (PST) as the input file. If the hour is equal ; to 5, 6, or 7, the output file should be for the date previous to the ; input file; otherwise, the date of the output file should be the same ; as that of the input file. ; iytgt, imtgt, and idtgt give the date of the target output file where this ; line should go. IF strvar(3) eq 'SFRDV' THEN stringa=string1; IF strvar(3) eq 'GPGGA' THEN BEGIN; second line, with date stringb=string1; ; This line should be stored with the same date as the input file, ; except if the GMT hour is 5, 6, or 7; in these cases, the EST date is ; one more than the PST date, and we have to back up the "target" date. iytgt=iy; same as input file imtgt=im idtgt=id ihr=long(strvar(4))/10000; Pick off the hour. IF ilout lt nlout THEN print,'ihr = ',ihr IF ihr ge 5 and ihr le 7 THEN BEGIN ; These go with the date previous to that of the input file. idtgt=id-1 IF idtgt le 0 THEN BEGIN ; Have to go back to the last day of the previous month. imtgt=im-1 IF imtgt le 0 THEN BEGIN iytgt = iy -1 imtgt = 12 ENDIF idtgt=nday(iytgt,imtgt) ENDIF ENDIF; end of "IF ihr ge 5 and ihr le 7 THEN BEGIN" ; Now we have the target date for this triplet of lines. If it is equal ; to the date of the current output file, we continue to write out every ; line. If it is ; later, we close the current output file and open a new one for the ; target date. If it is previous to the date of the current output ; file, we set a flag which inhibits writing until the target date ; catches up with the output file. ; If no optput file has yet been opened, open one now. The date of this ; file will agree with the current input file. If there are lines for ; the previous PST day, they will be lost (for this first day file only). IF ioutopen eq 0 THEN BEGIN ioutopen=1; Do this only once. outfile=strmid(infile,0,strlen(infile)-12)+'mar'+ $ strmid(infile,strlen(infile)-9,9) print,'first output file: '+outfile openw,lunout,outfile,/get_lun iyout=iy; date of output file imout=im; date of output file idout=id; date of output file ENDIF IF ilout lt nlout THEN print,'iytgt, iyout, imtgt, imout, idtgt, idout = ', $ iytgt,iyout,imtgt,imout,idtgt,idout IF not (iytgt eq iyout and imtgt eq imout and $ idtgt eq idout) THEN BEGIN ; Target date not right for currently open output file. IF iytgt gt iyout or $ (iytgt eq iyout and (imtgt gt imout or $ (imtgt eq imout and idtgt gt idout))) THEN BEGIN ; This is where we close the current output file and open a new one. print,'infile: ',infile print,'close file ',outfile print,'lunout = ',lunout close,lunout free_lun,lunout ; Set the output-file date to agree with the target date. iyout=iytgt imout=imtgt idout=idtgt path='/d4/Darmat/data/naut/vtm/'+ $ string(iyout,format='(i4.4)') + $ '/'+string(imout,format="(i2.2)")+'/' file='mar'+string(iyout mod 10,format='(i1.1)') + $ string(imout,format='(i2.2)')+ $ string(idout,format='(i2.2)')+'.log' outfile = path+file; This is the full file name. print,string1 print,'new outfile = ',outfile openw,lunout,outfile,/get_lun; open the file for writing ENDIF ENDIF ENDIF; end of IF strvar(3) eq 'CPGGA' THEN BEGIN IF strvar(3) eq 'GPVTG' THEN BEGIN stringc=string1; Third line of message. ; Now write out all three lines of the message, if the output file is ;right.. ; IF ioutopen eq 1) THEN BEGIN IF iytgt eq iyout and imtgt eq imout and idtgt eq $ idout THEN BEGIN printf,lunout,stringa printf,lunout,stringb printf,lunout,stringc ilout=ilout+3 ENDIF ENDIF; end of "IF strvar(3) eq 'GPVTG' THEN BEGIN" ENDIF; end of IF strvar(0) eq '$PRDCV' and strvar(1) eq 'Marin' THEN BEGIN ENDWHILE; end of "WHILE eof(luntemp) eq 0 DO BEGIN" close,luntemp free_lun,luntemp ENDIF; end of "IF cntinfile ne 0 THEN BEGIN" ENDFOR; end of loop over id. nd1=1; Value for all except first month of first year. ENDFOR; end of loop over im nm1=1; value for all except first year. ENDFOR; end of loop over iy close,lunout; close the last output file free_lun,lunout print,'next to last string: ',string0 print,'last string: ',string1 print,'last input file: ',infile print,'last output file: ',outfile print,'Number of lines input and output = ',iline,ilout END