Index: trunk/tools/generic_tools.py
===================================================================
--- trunk/tools/generic_tools.py	(revision 1317)
+++ trunk/tools/generic_tools.py	(revision 1318)
@@ -68,4 +68,5 @@
 # index_vec: Function to provide the coordinates of a given value inside a vector
 # julday_360d: Function to provide the julian day of a date in a 360 days/yr (or 12 30-days months) calendar
+# latex_fig_array: Function to add an array of figures to an existing tex file
 # latex_text: Function to transform a text to LaTeX following style rules
 # list_combos: Function to construct a new list with all possible N-combinations of the list-values
@@ -10296,4 +10297,133 @@
     return newmat, Lopn[opn]
 
+def latex_fig_array(figs, obf, figcaption, figlabel, dist='sqr', width='auto',       \
+  height='None', dorest='nothing'):
+    """ Function to add an array of figures to an existing tex file
+      figs= list of figures names (with path)
+      obf= already exisiting object open tex file 
+      figcaption= caption for the figure
+      figlabel= label for the figure
+      dist= kind of distribution
+        'sqr': squared, figures are equivenly distributed in rows and columns (sqrt(Nfigs), default)
+        [Ncol]'x'Nrow: a given number of columns and rows
+      width= with of the figures
+        'None': no value passed
+        'auto': getting according to fractions of 0.9\textwidth (default)
+        [value]: exactly as it is desired (e.g.: '0.3\\textwidht', '5cm' ...)
+      height= height of the figures
+        'None': no value passed (default)
+        'auto': getting according to fractions of 0.9\textheight
+        [value]: exactly as it is desired (e.g.: '0.3\\textheight', '5cm' ...)
+      dorest= what to do with the last row if it does not fill the number of columns
+        'nothing': left empty columns (default)
+        'center': center the figures
+    >>> latex_fig_array(['figs/figA.png','figs/figB.png','figs/figC.png','figs/figD.png','figs/figE.png'], \
+      objf, 'figure test', 'fig:test', dist='2x3', width='None', height='auto', dorest='center')
+    \begin{table}
+    \begin{center}
+    \begin{tabular}{cc}
+    \includegraphics[height=0.3\textheight]{figs/figA.png}
+    &
+    \includegraphics[height=0.3\textheight]{figs/figB.png}
+    \\
+    \includegraphics[height=0.3\textheight]{figs/figC.png}
+    &
+    \includegraphics[height=0.3\textheight]{figs/figD.png}
+    \\
+    \multicolumn{2}{c}{\begin{tabular}{c}
+    \includegraphics[height=0.3\textheight]{figs/figE.png}
+    \end{tabular}}
+    \end{tabular}
+    \end{center}
+    \caption{figure test}
+    \label{fig:test}
+    \end{table}
+    """
+    fname = 'latex_fig_array'
+
+    Nfig = len(figs)
+
+    if dist == 'sqr':
+        Ncol = int(np.sqrt(Nfig))
+        Nrow = Nfig/Ncol
+        Nrest = np.mod(Nfig,Ncol)
+        if Nrest != 0: Nrow = Nrow + 1
+    elif dist.find('x') != -1:
+        Ncol = int(dist.split('x')[0])
+        Nrow = int(dist.split('x')[1])
+        Nrest = np.mod(Nfig,Ncol)
+        if Ncol*Nrow < Nfig:
+            print errormsg
+            print '  ' + fname +': wrong number of rows and columns!!'
+            print "  passed value of '" + dist + "' does not fit", Nfig, 'figures'
+            quit(-1)
+    print '  ' + fname + ': table of figures of: ', Ncol, 'columns', Nrow, 'rows'
+
+    # Width and height section
+    widthheight = ''
+    if width != 'None':
+        if width == 'auto':
+            sec = 0.9/Ncol
+            widthheight = 'width=' + str(sec) + '\\textwidth'
+        else:
+            widthheight = 'width=' + width
+
+    if height != 'None':
+        if len(widthheight) > 0: widthheight = widthheight + ', '
+        if height == 'auto':
+            sec = 0.9/Nrow
+            widthheight = widthheight + 'height=' + str(sec) + '\\textheight'
+        else:
+            widthheight = widthheight + 'height=' + height
+    # num cols
+    cS = ''
+    for ic in range(Ncol): cS = cS + 'c'
+    
+    obf.write('\n')
+    obf.write('\\begin{table}\n')
+    obf.write('\\begin{center}\n')
+    obf.write('\\begin{tabular}{' + cS + '}\n')
+
+    # loop of figures distributed by Ncol, Nrow
+    iifig = 0
+    for irow in range(Nrow):
+        if irow < Nrow -1 or (irow == Nrow - 1 and dorest == 'nothing'):
+            for icol in range(Ncol):
+                # Getting figure name if it does exist
+                if iifig <= Nfig - 1:
+                    fign = figs[iifig]
+                    obf.write('\\includegraphics[' + widthheight + ']{' +fign+ '}\n')
+                    if icol < Ncol - 1:
+                        obf.write('&\n')
+                    elif irow != Nrow - 1:
+                        obf.write('\\\\\n')
+                else:
+                    if icol < Ncol -2: obf.write('&\n')
+                iifig = iifig + 1
+        elif irow == Nrow -1 and dorest == 'center':
+            crestS = ''
+            for irc in range(Nrest): crestS = crestS + 'c'
+            obf.write('\\multicolumn{'+str(Ncol)+'}{c}{\\begin{tabular}{'+crestS +   \
+              '}\n')
+            for icol in range(Nrest):
+                fign = figs[iifig]
+                print icol, fign, iifig
+                obf.write('\\includegraphics[' + widthheight + ']{' + fign + '}\n')
+                if icol < Nrest - 1:
+                    obf.write('&\n')
+                else:
+                    obf.write('\\end{tabular}}\n')
+                iifig = iifig + 1
+
+
+    obf.write('\\end{tabular}\n')
+    obf.write('\\end{center}\n')
+    obf.write('\\caption{' + figcaption + '}\n')
+    obf.write('\\label{' + figlabel + '}\n')
+    obf.write('\\end{table}\n')
+    obf.write('\n')
+
+    return
+
 #quit()
 
