Skip to content

Commit

Permalink
Merge pull request #504 from hibtc/read_table
Browse files Browse the repository at this point in the history
Unify read_table  and read_my_table
  • Loading branch information
ldeniau committed Nov 20, 2017
2 parents 36df890 + 3fa6ffc commit 776964d
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 190 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -84,6 +84,7 @@ tests/**/*.txt
tests/**/resultb[0-9]
tests/**/result[0-9]
tests/**/*.tab[0-9]
tests/**/dummyone

tests/test-cororbit-2/cx12.tab_1
tests/test-cororbit-2/cx12.tab_2
Expand Down
4 changes: 2 additions & 2 deletions doc/latexuguide/cororbit.tex
Expand Up @@ -220,7 +220,7 @@ \section{CORRECT}
\texttt{TARGET} table
are assumed to be computed by \mad with a previous \texttt{TWISS}
command. When set to true, that option allows to use twiss tables
imported from an external file (with the \texttt{READMYTABLE} command), for
imported from an external file (with the \texttt{READTABLE} command), for
example to use measured BPM data. In that case, the imported twiss
table is allowed to contain coordinate data only at the location of
the monitors.
Expand All @@ -240,7 +240,7 @@ \section{CORRECT}
twiss, table=twiss_ref;
! The bpm.tsv is a reduced Twiss file containing only lines for the BPMs
readmytable, file="bpm.tsv", table="twiss_bpm";
readtable, file="bpm.tsv", table="twiss_bpm";
! correct orbit using external measurements
correct, flag=ring, mode=micado, ncorr=5, cond=1 ,plane=x, extern,
Expand Down
4 changes: 2 additions & 2 deletions doc/latexuguide/error.tex
Expand Up @@ -675,12 +675,12 @@ \section{SETERR: Reading errors from a table or file}
}

The table \textit{errtab} can be generated internally or from an
external file (\textit{errfile}) with the generic command \texttt{READMYTABLE}.
external file (\textit{errfile}) with the generic command \texttt{READTABLE}.


The command sequence:
\madxmp{
READMYTABLE, file=errfile, table=errtab; \\
READTABLE, file=errfile, table=errtab; \\
SETERR, TABLE=errtab;
}

Expand Down
2 changes: 1 addition & 1 deletion doc/latexuguide/ptc-general.tex
Expand Up @@ -338,7 +338,7 @@ \section{PTC\_READ\_ERRORS}

The \texttt{PTC\_READ\_ERRORS} command reads any number of
\textbf{"errors\_read"} table through the
\hyperref[sec:readmytable]{\texttt{READMYTABLE}} mechanism.
\hyperref[sec:readtable]{\texttt{READTABLE}} mechanism.

\madbox{
PTC\_READ\_ERRORS, OVERWRITE=logical;
Expand Down
20 changes: 5 additions & 15 deletions doc/latexuguide/tables.tex
Expand Up @@ -29,10 +29,11 @@ \section{DELETE}
\section{READTABLE}
\label{sec:readtable}
\madbox{
READTABLE, FILE="filename";
READTABLE, FILE="filename", TABLE=tabname;
}
reads the \texttt{TFS} file \texttt{filename} containing a \mad table
and loads the table into memory with the name specified in the
loads the table into memory with the name \texttt{tabname}. If the table
name is not specified, use the name specified in the
information section of the TFS file. The table can then be manipulated
as any other table, \textsl{i.e.} its values can be accessed, its data
can be plotted or changed, and it can be written out again.
Expand All @@ -42,17 +43,7 @@ \section{READMYTABLE}
\madbox{
READMYTABLE, FILE="filename", TABLE=tabname;
}
reads a \texttt{TFS} file \texttt{filename} containing a \mad table and
loads the table into memory with the name \texttt{tabname}. The table
can then be manipulated as any other table, \textsl{i.e.} its values can
be accessed, its data can be plotted or changed, and it can be written
out again.

An internal name for the table can be freely assigned, while for the
command \hyperref[sec:readtable]{\texttt{READTABLE}} the table name is
taken from the information section of the table itself.
This feature allows to store multiple tables of the same type in memory
without overwriting existing ones.
deprecated alias for \texttt{READTABLE}.

\section{WRITE}
\label{sec:write}
Expand Down Expand Up @@ -295,8 +286,7 @@ \section{SHRINK}
\ttitem{TABLE} is the name of the table from which rows should be removed.
The table must have been previously \hyperref[sec:create]{created} and
\hyperref[sec:fill]{filled} or read from file with
\hyperref[sec:readtable]{\texttt{READTABLE}}
or \hyperref[sec:readmytable]{\texttt{READMYTABLE}}.
\hyperref[sec:readtable]{\texttt{READTABLE}}.

\ttitem{ROW} is the number of the last row to be kept in the table.
All rows beyond the given row number are removed. \\
Expand Down
2 changes: 1 addition & 1 deletion src/mad_orbit.c
Expand Up @@ -3187,7 +3187,7 @@ void pro_correct(struct in_cmd* cmd) {
else if (strcmp(cmd->tok_list->p[0], "usemonitor") == 0) correct_usemonitor(cmd);
else if (strcmp(cmd->tok_list->p[0], "getorbit") == 0) correct_getorbit(cmd); // FIXME obsolete command; should be flagged and not call anything...
else if (strcmp(cmd->tok_list->p[0], "putorbit") == 0) correct_putorbit(cmd); // FIXME obsolete command; should be flagged and not call anything...
else if (strcmp(cmd->tok_list->p[0], "readmytable") == 0) read_my_table(cmd);
else if (strcmp(cmd->tok_list->p[0], "readmytable") == 0) read_table(cmd);
else if (strcmp(cmd->tok_list->p[0], "readcorr") == 0) correct_readcorr(cmd); //FIXME not documented
else if (strcmp(cmd->tok_list->p[0], "setcorr") == 0) correct_setcorr(cmd); // FIXME not documented
// else if (strcmp(cmd->tok_list->p[0], "prtcorr") == 0) correct_prtcorr(cmd); // FIXME not documented
Expand Down
169 changes: 1 addition & 168 deletions src/mad_table.c
Expand Up @@ -1295,7 +1295,7 @@ read_table(struct in_cmd* cmd)
type = permbuff(stolower(name));
}
}
else if (strcmp(tmp, "NAME") == 0)
else if (strcmp(tmp, "NAME") == 0 && !namtab)
{
if ((name = strtok(NULL, " \"\n")) != NULL) /* skip format */
{
Expand Down Expand Up @@ -1491,173 +1491,6 @@ table_range(char* table, char* range, int* rows)
// fprintf(stderr, "table_range: row[0]='%d', row[1]='%d', table->curr=%d\n", rows[0], rows[1], t->curr);
}

struct table*
read_my_table(struct in_cmd* cmd)
/* reads and stores TFS table */
{
struct table* t = NULL;
struct char_p_array* tcpa = NULL;
struct name_list* tnl = NULL;
int i, k, error = 0;
short sk;
char *cc, *filename, *type = NULL, *tmp, *name;
double tmpd;
char* namtab;

if ((namtab = command_par_string("table",cmd->clone)) != NULL) {
printf("Want to make named table: %s\n",namtab);
} else {
if (get_option("debug")) {
printf("No table name requested\n");
printf("Use default name (i.e. name from file) \n");
}
namtab = NULL;
}

if((filename = command_par_string_user("file", cmd->clone)))
{
if ((tab_file = fopen(filename, "r")) == NULL)
{
fatal_error("cannot open file:", filename); return NULL; /* frs: to avoid unwanted results */
}
}
else
{
warning("no filename,","ignored"); return NULL;
}
while (fgets(aux_buff->c, aux_buff->max, tab_file))
{
cc = strtok(aux_buff->c, " \"\n");
if (*cc == '@')
{
if ((tmp = strtok(NULL, " \"\n")) != NULL
&& strcmp(tmp, "TYPE") == 0)
{
if ((name = strtok(NULL, " \"\n")) != NULL) /* skip format */
{
if ((name = strtok(NULL, " \"\n")) != NULL)
type = permbuff(stolower(name));
}
}
}
else if (*cc == '*' && tnl == NULL)
{
tnl = new_name_list("table_names", 20);
while ((tmp = strtok(NULL, " \"\n")) != NULL)
add_to_name_list(permbuff(stolower(tmp)), 0, tnl);
}
else if (*cc == '$' && tcpa == NULL)
{
if (tnl == NULL)
{
warning("formats before names","skipped"); return NULL;
}
tcpa = new_char_p_array(20);
while ((tmp = strtok(NULL, " \"\n")) != NULL)
{
if (tcpa->curr == tcpa->max) grow_char_p_array(tcpa);
if (strcmp(tmp, "%s") == 0) tnl->inform[tcpa->curr] = 3;
else if (strcmp(tmp, "%hd") == 0) tnl->inform[tcpa->curr] = 1;
else if (strcmp(tmp, "%d") == 0) tnl->inform[tcpa->curr] = 1;
else tnl->inform[tcpa->curr] = 2;
tcpa->p[tcpa->curr++] = permbuff(tmp);
}
}
else
{
if(t == NULL)
{
if (type == NULL)
{
warning("TFS table without type,","skipped"); error = 1;
}
else if (tcpa == NULL)
{
warning("TFS table without formats,","skipped"); error = 1;
}
else if (tnl == NULL)
{
warning("TFS table without column names,","skipped"); error = 1;
}
else if (tnl->curr == 0)
{
warning("TFS table: empty column name list,","skipped");
error = 1;
}
else if (tnl->curr != tcpa->curr)
{
warning("TFS table: number of names and formats differ,",
"skipped");
error = 1;
}
if (error)
{
delete_name_list(tnl); return NULL;
}
if(namtab != NULL) {
t = new_table(namtab, type, 500, tnl);
} else {
t = new_table(type, type, 500, tnl);
}
}
for (i = 0; i < tnl->curr; i++)
{
if (t->curr == t->max) grow_table(t);
tmp = tcpa->p[i];

/*
printf("read_my_table %d <%s> <%s> \n", i, tcpa->p[i], tnl->names[i] );
*/

if (strcmp(tmp,"%s") == 0)
{
/* printf("reading format %s \n", tmp); */
t->s_cols[i][t->curr] = stolower(tmpbuff(cc));
/* printf("read %d %d = %s \n", i,t->curr, t->s_cols[i][t->curr]); */

/*printf("read_my_table coln [%d %d]=%s\n",i,t->curr,t->s_cols[i][t->curr]);*/
}
else if (strcmp(tmp,"%d") == 0 )
{
/* printf("reading format %s \n", tmp); */
sscanf(cc, tmp, &k);
/* printf("read %d %d = %d \n", i,t->curr, k); */
t->d_cols[i][t->curr] = k;
}
else if (strcmp(tmp,"%hd") == 0 )
{
/* printf("reading format %s \n", tmp); */
sscanf(cc, tmp, &sk);
/* printf("read %d %d = %d \n", i,t->curr, sk); */
t->d_cols[i][t->curr] = sk;
}
else
{
/* printf("reading format %s \n", tmp); */
sscanf(cc, tmp, &tmpd);
/* printf("read %d %d = %f \n", i,t->curr, tmpd); */
t->d_cols[i][t->curr] = tmpd;
}


if (i+1 < tnl->curr)
{
if ((cc =strtok(NULL, " \"\n")) == NULL)
{
warning("read_my_table: incomplete table line starting with:", aux_buff->c);
return NULL;
}
}
}
t->curr++;
}
}
fclose(tab_file);
t->origin = 1;
add_to_table_list(t, table_register);
return NULL;
}

void
set_selected_columns(struct table* t, struct command_list* select)
{
Expand Down
1 change: 0 additions & 1 deletion src/mad_table.h
Expand Up @@ -63,7 +63,6 @@ struct table_list* new_table_list(int size);
struct table_list_list* new_table_list_list(int size);
struct table* delete_table(struct table*);
struct table* read_table(struct in_cmd*);
struct table* read_my_table(struct in_cmd*);
struct table* find_table(const char* name);

void check_table(char* string);
Expand Down

0 comments on commit 776964d

Please sign in to comment.