Task: Text processing_1







Python: import fileinput
import sys

nodata = 0;             
nodata_max=-1;          
nodata_maxline=[];      

tot_file = 0            
num_file = 0            

infiles = sys.argv[1:]

for line in fileinput.input():
  tot_line=0;             
  num_line=0;             

  
  field = line.split()
  date  = field[0]
  data  = [float(f) for f in field[1::2]]
  flags = [int(f)   for f in field[2::2]]

  for datum, flag in zip(data, flags):
    if flag<1:
      nodata += 1
    else:
      
      if nodata_max==nodata and nodata>0:
        nodata_maxline.append(date)
      if nodata_max<nodata and nodata>0:
        nodata_max=nodata
        nodata_maxline=[date]
      
      nodata=0; 
      
      tot_line += datum
      num_line += 1

  
  tot_file += tot_line
  num_file += num_line

  print "Line: %11s  Reject: %2i  Accept: %2i  Line_tot: %10.3f  Line_avg: %10.3f" % (
        date, 
        len(data) -num_line, 
        num_line, tot_line, 
        tot_line/num_line if (num_line>0) else 0)

print ""
print "File(s)  = %s" % (", ".join(infiles),)
print "Total    = %10.3f" % (tot_file,)
print "Readings = %6i" % (num_file,)
print "Average  = %10.3f" % (tot_file / num_file,)

print "\nMaximum run(s) of %i consecutive false readings ends at line starting with date(s): %s" % (
    nodata_max, ", ".join(nodata_maxline))




Fortran: Crunches a set of hourly data. Starts with a date, then 24 pairs of value,indicator for that day, on one line.
      INTEGER Y,M,D		
      INTEGER GOOD(24)		
      REAL*8 V(24),VTOT,T	
      INTEGER NV,N,NB		
      INTEGER I,NREC,HIC	
      INTEGER BI,BN,BBI,BBN	
      CHARACTER*10 BDATE,BBDATE	
      LOGICAL INGOOD		
      INTEGER IN,MSG		
      CHARACTER*666 ACARD	
      IN = 10		
      MSG = 6		
      OPEN (IN,FILE="Readings1.txt", FORM="FORMATTED",	
     1 STATUS ="OLD",ACTION="READ")			
      NB = 0		
      NV = 0		
      VTOT = 0		
      NREC = 0		
      HIC = 0		
      INGOOD = .TRUE.	
      BBN = 0		
Chew into the file.
   10 READ (IN,11,END=100,ERR=666) L,ACARD(1:MIN(L,LEN(ACARD)))	
      NREC = NREC + 1		
   11 FORMAT (Q,A)		
      READ (ACARD,12,END=600,ERR=601) Y,M,D	
   12 FORMAT (I4,2(1X,I2))				
      READ (ACARD(11:L),*,END=600,ERR=601) (V(I),GOOD(I),I = 1,24)	
Calculations. Could use COUNT(array) and SUM(array), but each requires its own pass through the array.
   20 T = 0		
      N = 0		
      DO I = 1,24	
        IF (GOOD(I).GT.0) THEN	
          N = N + 1		
          T = T + V(I)		
          IF (.NOT.INGOOD) THEN	
            INGOOD = .TRUE.	
            IF (BN.GT.BBN) THEN	
              BBN = BN		
              BBI = BI		
              BBDATE = BDATE	
            END IF		
          END IF		
         ELSE		
          IF (INGOOD) THEN	
            INGOOD = .FALSE.	
            BDATE = ACARD(1:10)	
            BI = I		
            BN = 0		
          END IF		
          BN = BN + 1	
        END IF		
      END DO		
Commentary for the day's data..
      IF (N.LE.0) THEN	
        WRITE (MSG,21) NREC,ACARD(1:10)	
   21   FORMAT ("Record",I8," (",A,") has no good data
       ELSE			
        WRITE(MSG,22) NREC,ACARD(1:10),N,T/N	
   22   FORMAT("Record",I8," (",A,")",I3," good, average",F9.3)	
        NB = NB + 24 - N	
        NV = NV + N		
        VTOT = VTOT + T		
      END IF			
      GO TO 10		

Complaints. Should really distinguish between trouble in the date part and in the data part.
  600 WRITE (MSG,*) '"END" declared - insufficient data?'	
      GO TO 602				
  601 WRITE (MSG,*) '"ERR" declared - improper number format?'	
  602 WRITE (MSG,603) NREC,L,ACARD(1:L)	
  603 FORMAT(" Record ",I0,", length ",I0," reads ",A)	
      HIC = HIC + 1			
      IF (HIC.LE.12) GO TO 10		
      STOP "Enough distaste."		
  666 WRITE (MSG,101) NREC,"format error
      GO TO 900				

Closedown.
  100 WRITE (MSG,101) NREC,"then end-of-file"	
  101 FORMAT (" Record ",I0,": ",A)		
      WRITE (MSG,102) NV,NB,VTOT/NV		
  102 FORMAT (I8," values, ",I0," bad. Average",F9.4)	
      IF (BBN.LE.0) THEN		
        WRITE (MSG,*) "No bad value presented, so no longest run."	
       ELSE				
        WRITE (MSG,103) BBN,BBI,BBDATE	
  103   FORMAT ("Longest bad run: ",I0,", starting hour ",I0," on ",A)	
      END IF			
  900 CLOSE(IN)		
      END