text: import sys,os 
  import csv
  import numpy as np
  
  print " "
  print "                              ----WELCOME !! I GENERATE PMF FILES FROM CANTERA FILES FOR PELE----"
  print " "
  print " WARNING : This script expects from you a CANTERA .csv file containing z(m), u(m/s), V(1/s), T(K), rho(kg/m3) + species (X or Y) IN THAT ORDER !! " 
  print " USAGE :   python pmf_builder.py file.csv file_out.f offset"
  print " USAGE :   offset is to rescale the profile so you can put it wherever in your domain"
  print " NOTE :    file_out.f will contain X(cm), T(K), u(cm/s), rho(g/cm3), species (Y or X)"  
  print " " 
  
  file_in  =  sys.argv[1]
  file_out =  sys.argv[2]
  offset   = float(sys.argv[3])
  
  
  with open(file_in, 'r') as infile:
      lines = infile.readlines()
  
  xdata    = []
  ydata    = []
  for i, line in enumerate(lines):
      if (i==0):
          header = line.strip()
          Vars = header.split(",")
          Nvar = len(header.split(","))
      else:
          raw_line = line.strip().split(",")
          xdata.append(raw_line[0] )
          ydata.append(raw_line[1:])
  
  
  
  csv_file = str(file_out)
  with open(csv_file, 'w') as outfile:
      outfile.write('!-------------------------------------------------\n')
      outfile.write('!\n')
      outfile.write('!\n')
      outfile.write('!                              -USAGE- \n')
      outfile.write('!\n')
      outfile.write('!     OVERVIEW: call pmf(xlo,xhi,y,Niny) \n')
      outfile.write('!     computes y(xlo,xhi) by averaging data over \n')
      outfile.write('!     the range [xlo,xhi]. Here x is a scalar, y   \n')
      outfile.write('!     a vector, with Niny components used. Piece- \n')
      outfile.write('!     constant Extrapolation is applied to points \n')
      outfile.write('!     laying outside the range of the orig data \n')
      outfile.write('!\n')
      outfile.write('!     domain: x (m) in ['+str(xdata[0])+ '\n')
      outfile.write('!                  ,'+str(xdata[-1])+ ']\n')
      outfile.write('!     number of states in vector y: ' + str(Nvar-2) + '\n')
      outfile.write('!     number of x data points: ' + str(len(xdata)) + '\n')
      outfile.write('!\n')
      outfile.write('!     CALLING PROGRAM NEEDS: none \n')
      outfile.write('!     PROCEDURE NEEDS:  none \n')
      outfile.write('!     COMPILER NEEDS: f77, long names (14 char) \n')
      outfile.write('!     SYSTEM NEEDS: none \n')
      outfile.write('!     NOTES: This routine was generated by pmf_builder.py \n')
      outfile.write('!\n')
      outfile.write('!-------------------------------------------------\n')
      outfile.write('!\n')
      outfile.write('!                              -INFO- \n')
      outfile.write('!     CANTERA HEADER: \n')
      outfile.write('!     ' + str(header) +', \n')
      outfile.write('!\n')
      outfile.write('!-------------------------------------------------\n')
      outfile.write('!\n')
      outfile.write('!                          -SPECIFICATION- \n')
      outfile.write('!     SYNTAX:\n')
      outfile.write('!     subroutine pmf(xlo,xhi:dp,y:dp arr, Niny: int)\n')
      outfile.write('!     returns: none                               \n')
      outfile.write('!     REQUIRES: none                              \n')
      outfile.write('!     MODIFIES: y, N                              \n')
      outfile.write('!     SIGNALS: none                               \n')
      outfile.write('!     EFFECTS: computes the average value of y    \n')
      outfile.write('!     over the range [xlo,xhi] and returns number \n')
      outfile.write('!     of components used, N.                      \n')
      outfile.write('!     \n')
      outfile.write('!-------------------------------------------------\n')
      outfile.write('!\n')
      outfile.write('!                         -IMPLEMENTATION- \n')
      outfile.write('!     AUTHOR: auto-generated by makeAveInterp  \n')
      outfile.write('!     MODIFICATION HISTORY: none                  \n')
      outfile.write('!     NOTES: interpolation x,y data is defined in \n')
      outfile.write('!     data statements for efficiency.             \n')
      outfile.write('!     REQUIRES: none                              \n')
      outfile.write('!     LOCAL VARIABLES:                            \n')
      outfile.write('!     xlo,xhi.........(dp) average y on this range\n')
      outfile.write('!     y_vector........(1D dp arr) computed y(x)   \n')
      outfile.write('!     Niny............(int) no. of used components\n')
      outfile.write('!     x_data..........(dp) x data points          \n')
      outfile.write('!     y_data..........(dp) y(x) data points       \n')
      outfile.write('!     N...............(int) number of x points    \n')
      outfile.write('!     M...............(int) dimension of y_data   \n')
      outfile.write('!     i,j,k...........(int) loop index            \n')
      outfile.write('!     lo_{lo,hi}side..(int) idx of data near xlo  \n')
      outfile.write('!     hi_{lo,hi}side..(int) idx of data near xhi  \n')
      outfile.write('!     x1..............(dp) interpolation point    \n')
      outfile.write('!     x2..............(dp) interpolation point    \n')
      outfile.write('!     y1..............(dp) interpolation value    \n')
      outfile.write('!     y2..............(dp) interpolation value    \n')
      outfile.write('!     dydx............(dp) slope of linear interp \n')
      outfile.write('!     ylo.............(dp) value at xlo           \n')
      outfile.write('!     yhi.............(dp) value at xhi           \n')
      outfile.write('!     GLOBAL VARIABLES:none                       \n')
      outfile.write('!     PROCEDURES: none                            \n')
      outfile.write('!     FILES: none                                 \n')
      outfile.write('!     I/O: none                                   \n')
      outfile.write('!-------------------------------------------------\n')
      outfile.write('! \n')
      outfile.write('!                             -CODE-\n')
      outfile.write('!     FIRST LINE:\n')
      outfile.write('      subroutine pmf(xlo,xhi,y_vector,Niny) \n')
      outfile.write('! \n')
      outfile.write('!     declare local variables \n')
      outfile.write('!    \n')
      outfile.write('      integer N,M,i,j,k,lo_loside,lo_hiside             \n')
      outfile.write('      integer hi_loside,hi_hiside,Niny                  \n')
      outfile.write('      double precision xlo,xhi,y_vector(*),sum          \n')
      outfile.write('      double precision ylo,yhi,x1,y1,x2,y2,dydx          \n')
      outfile.write('      double precision x_data(' + str(len(xdata)) + ')           \n')
      outfile.write('      double precision y_data(' + str(len(xdata)) + ',' + str(Nvar-2) + ')      \n')
      outfile.write('      data N,M /' + str(len(xdata)) + ',' + str(Nvar-2) + '/\n')
      outfile.write('      Niny = ' + str(Nvar-2) + '\n')
      outfile.write('! \n')
      for i in range(len(xdata)) :
          outfile.write('      data x_data('+str(i+1)+') /'+str((float(xdata[i])*100.0 + offset))+'/\n')
          outfile.write('      data y_data('+str(i+1)+','+str(1)+') /'+ ydata[i][2] +'/\n')
          outfile.write('      data y_data('+str(i+1)+','+str(2)+') /'+ str(float(ydata[i][0])*100.0) +'/\n')
          outfile.write('      data y_data('+str(i+1)+','+str(3)+') /'+ str(float(ydata[i][3])*1.0e-03) +'/\n')
          for j in range(Nvar-5) :
              outfile.write('      data y_data('+str(i+1)+','+str(4+j)+') /'+ ydata[i][4+j] +'/\n')
  
      outfile.write('!    \n')                     
      outfile.write('!     interpolation routine    \n')                    
      outfile.write('!    \n')                   
      outfile.write('      lo_loside = 0\n') 
      outfile.write('      lo_hiside = 0 \n') 
      outfile.write('      hi_loside = 0 \n') 
      outfile.write('      hi_hiside = 0  \n') 
      outfile.write('      if (xlo .le. x_data(1)) then\n') 
      outfile.write('         lo_loside = 1\n') 
      outfile.write('         lo_hiside = 1\n') 
      outfile.write('      end if\n') 
      outfile.write('      if (xhi .le. x_data(1)) then\n') 
      outfile.write('         hi_loside = 1\n') 
      outfile.write('         hi_hiside = 1\n') 
      outfile.write('      end if\n') 
      outfile.write('      if (xlo .ge. x_data(N)) then\n') 
      outfile.write('         lo_loside = N\n') 
      outfile.write('         lo_hiside = N\n') 
      outfile.write('      end if\n') 
      outfile.write('      if (xhi .ge. x_data(N)) then\n') 
      outfile.write('         hi_loside = N\n') 
      outfile.write('         hi_hiside = N\n') 
      outfile.write('      end if\n') 
      outfile.write('      if (lo_loside.eq.0) then\n') 
      outfile.write('         do i = 1, N-1         \n')                   
      outfile.write('            if ( (xlo .ge. x_data(i)) \n') 
      outfile.write('     &          .and. \n')
      outfile.write('     &       (xlo .le. x_data(i+1)) ) then \n')
      outfile.write('               lo_loside  = i\n')
      outfile.write('               lo_hiside  = i+1\n')
      outfile.write('            end if\n')
      outfile.write('         end do\n')
      outfile.write('      end if\n')
      outfile.write('      if (hi_loside.eq.0) then\n')
      outfile.write('         do i = 1, N-1\n')                           
      outfile.write('            if ( (xhi .ge. x_data(i)) \n')
      outfile.write('     &           .and. \n')
      outfile.write('     &           (xhi .le. x_data(i+1)) ) then \n')
      outfile.write('               hi_loside = i \n')
      outfile.write('               hi_hiside = i + 1\n')
      outfile.write('            end if\n')
      outfile.write('         end do\n')
      outfile.write('      end if\n')
      outfile.write('         \n')
      outfile.write('      do j = 1, M \n')
      outfile.write('         \n')
      outfile.write('         x1 = x_data(lo_loside) \n')
      outfile.write('         y1 = y_data(lo_loside,j)\n')
      outfile.write('         \n')
      outfile.write('         x2 = x_data(lo_hiside) \n')
      outfile.write('         y2 = y_data(lo_hiside,j) \n')
      outfile.write('          \n')
      outfile.write('         if (lo_loside.eq.lo_hiside) then \n')
      outfile.write('            dydx = 0.d0 \n')
      outfile.write('         else \n')
      outfile.write('            dydx = (y2-y1)/(x2-x1) \n')
      outfile.write('         end if \n')
      outfile.write('         \n')
      outfile.write('         ylo = y1 + dydx*(xlo - x1) \n')
      outfile.write('         \n')
      outfile.write('         if (lo_loside .eq. hi_loside) then \n')
      outfile.write('            \n')
      outfile.write('            yhi = y1 + dydx*(xhi - x1) \n')
      outfile.write('           \n') 
      outfile.write('            y_vector(j) = 0.5d0*(ylo + yhi) \n')
      outfile.write('            \n')
      outfile.write('         else \n')
      outfile.write('            \n')
      outfile.write('            sum = (x2 - xlo) * 0.5d0 * (ylo + y2) \n')
      outfile.write('            \n')
      outfile.write('            x1 = x_data(hi_loside) \n')
      outfile.write('            y1 = y_data(hi_loside,j) \n')
      outfile.write('         \n')
      outfile.write('            x2 = x_data(hi_hiside) \n')
      outfile.write('            y2 = y_data(hi_hiside,j) \n')
      outfile.write('            \n')
      outfile.write('            if (hi_loside.eq.hi_hiside) then \n')
      outfile.write('               dydx = 0.d0 \n')
      outfile.write('            else \n')
      outfile.write('               dydx = (y2-y1)/(x2-x1) \n')
      outfile.write('            end if \n')
      outfile.write('            \n')
      outfile.write('            yhi = y1 + dydx*(xhi - x1) \n')
      outfile.write('            \n')
      outfile.write('            sum = sum + (xhi - x1)*0.5d0*(yhi+y1) \n')
      outfile.write('            \n')
      outfile.write('            do k = lo_hiside,hi_loside-1 \n')
      outfile.write('               \n')
      outfile.write('               sum = sum + (x_data(k+1)-x_data(k)) \n')
      outfile.write('     &              * 0.5d0 \n')
      outfile.write('     &              * (y_data(k,j) + y_data(k+1,j)) \n')
      outfile.write('               \n')
      outfile.write('            end do \n')
      outfile.write('             \n') 
      outfile.write('            y_vector(j) = sum / (xhi - xlo) \n')
      outfile.write('            \n')
      outfile.write('         end if \n')
      outfile.write('      end do \n')
      outfile.write('      end        \n')                                 
      outfile.write('!     LAST LINE      \n')                             
      outfile.write('!------------------------------------------------- \n')
  