PRO sortvtm ; 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 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. For this ; reason, an attempt is made to start one day earlier so as to complete ; a block of one day's data which was left incomplete earlier. iy0=2000; year of data read,'year: '+string(iy0),answer IF strlen(answer) gt 0 THEN iy0=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) print,'Data will be sorted into files by PST day, for dates of the year ',iy0,',' print,'starting on day '+strtrim(string(nd10),2)+', month ' + $ strtrim(string(nm10),2)+' and going through month '+ $ strtrim(string(nm20),2) print,'An attempt will also be made to complete the file for the day' print,'preceeding the start of this run.' ; Turn back the time by one calendar day. This will point to the day ; which would have been left incomplete in a previous calculation, because ; each EST day starts with boat data from the previous PST day. iyear=iy0 nm1=nm10 nm2=nm20 nd1=nd10-1 IF nd1 lt 1 THEN BEGIN nm1=nm10-1; Go to previous month. IF nm1 lt 1 THEN BEGIN iyear=iy0-1; Go to previous year. nm1=12; last month of previous year. ENDIF nd1=nday(iyear,nm1); last day of previous month. ENDIF ; Set the parameters for the starting output file. iyout=iyear imout=nm1 idout=nd1 ; Open the starting output file. 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. ; Before opening this file, check that the corresponding input file exists. infile=strmid(outfile,0,strlen(outfile)-12)+'200'+ $ strmid(outfile,strlen(outfile)-9,9) print,'starting infile = ',infile print,'starting outfile = ',outfile print,'The preceding files should correspond to the "lookback" date.' list = findfile(infile,count=ctinfil) list = findfile(outfile,count=ctoutfil) print,'ctinfil, ctoutfil = ',ctinfil,ctoutfil ; If the output file exists and the input file does not, open the output ; file in append mode. Otherwise open it as a new file. IF ctoutfil eq 1 and ctinfil eq 0 THEN $ openu,lunout,outfile,/get_lun,/append $ ELSE openw,lunout,outfile,/get_lun ;***** Start the main loops over months and days. FOR im = nm1,nm2 DO BEGIN path='/d4/Darmat/data/naut/vtm/'+ string(iyear,format='(i4.4)') + $ '/'+string(im,format="(i2.2)")+'/' nd2=nday(iyear,im); Go all the way to the end of the month. IF im eq nm2 and nd20 ge 1 THEN nd2 = nd20; Stop at indicated day. FOR id=nd1,nd2 DO BEGIN file=string(iyear,format='(i4.4)')+string(im,format='(i2.2)')+ $ string(id,format='(i2.2)')+'.log' infile = path+file list = findfile(infile,count=ctinfile) print,'infile in loop, count = ',infile,ctinfile ; (The only input file which might not exist is the first one.) 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, ; 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. infile=tempfile; Now read from the filtered file. openr,lunin,infile,/get_lun ; Start loop reading lines from tempfile and writing them to the file ; for the appropriate PST day. WHILE eof(lunin) eq 0 DO BEGIN string0=string1; save previous string. readf,lunin,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 iwrite=1; enable writing to output file. IF ilout lt nlout THEN print,iyear,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 'GPGGA' THEN BEGIN iytgt=iyear; 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 = iyear -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. iwrite=1; flag OK to write out data. 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 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 garget 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 printf,lunout,string0; Write out the previous string. ilout=ilout+1 ENDIF ELSE BEGIN iwrite = 0; inhibit writing. ENDELSE ENDIF ENDIF; end of IF strvar(3) eq 'CPGGA' THEN BEGIN ; Finally the line can be written. IF iwrite eq 1 THEN printf,lunout,string1 & ilout=ilout+1 ENDIF; end of IF strvar(0) eq '$PRDCV' and strvar(1) eq 'Marin' THEN BEGIN ENDWHILE close,lunin free_lun,lunin ENDIF ENDFOR nd1=1; From now on, start with first day of the month. ENDFOR close,lunout; close the last output file free_lun,lunout print,'next to last string: ',string0 print,'last string: ',string1 print,'last output file: ',outfile print,'last output file: ',outfile print,'Number of lines input and output = ',iline,ilout END