IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38998


Ignore:
Timestamp:
Oct 29, 2015, 10:49:32 AM (11 years ago)
Author:
eugene
Message:

update fits_to_mysql to include sequence columns and tablename prefix and suffix options

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/tools/src/fits_to_mysql.c

    r38997 r38998  
    3434  TYPE_INT64,
    3535  TYPE_FLOAT,
    36   TYPE_DOUBLE
     36  TYPE_DOUBLE,
     37  TYPE_SEQUENCE,
    3738} TypeInt;
    3839
     
    5354char        *TABLENAME;
    5455char        *TABLEPREFIX;
     56char        *TABLESUFFIX;
     57char        *SEQUENCE_COLUMN;
     58int          SEQUENCE_COLUMN_START;
    5559
    5660int args (int *argc, char **argv);
     
    6064ColumnDef *parse_table (Header *header, int *ncolumn);
    6165int create_table (ColumnDef *columns, int Ncolumns, char *extname, IOBuffer *insert, MYSQL *mysql);
    62 void write_data (ColumnDef *columns, int Ncolumns, FTable *table, IOBuffer *insert, MYSQL *mysql);
     66void write_data (ColumnDef *columns, int Ncolumns, int start, FTable *table, IOBuffer *insert, MYSQL *mysql);
    6367
    6468void *gfits_get_bintable_column_data_by_Ncol (FTable *table, int Nfield, char *type, off_t *Nrow, int *Ncol, char nativeOrder);
     
    167171      fprintf (stderr, "read "OFF_T_FMT" rows\n", header.Naxis[1]);
    168172
    169       write_data (columns, Ncolumns, &table, &insert, mysqlReal);
     173      write_data (columns, Ncolumns, start, &table, &insert, mysqlReal);
    170174
    171175      start += Nrows;
     
    195199  FREE (TABLENAME);
    196200  FREE (TABLEPREFIX);
     201  FREE (TABLESUFFIX);
     202  FREE (SEQUENCE_COLUMN);
    197203
    198204  ohana_memdump (TRUE);
     
    228234  int Nfields;
    229235  if (!gfits_scan (header, "TFIELDS", "%d", 1, &Nfields)) return NULL;
     236
     237  // generate the sequence column first so that any actual table columns which match the
     238  // requested name get a uniquified colname
     239  if (SEQUENCE_COLUMN) {
     240    column[Ncolumn].colname = strcreate (SEQUENCE_COLUMN);
     241    column[Ncolumn].rawname = strcreate (SEQUENCE_COLUMN);
     242    column[Ncolumn].format = NULL;
     243    replace_dot_with_underscore (column[Ncolumn].colname);
     244    column[Ncolumn].Nval = 0;
     245    column[Ncolumn].Nbytes = 0;
     246    column[Ncolumn].type = TYPE_SEQUENCE;
     247    Ncolumn++;
     248  }
    230249
    231250  // first, extract the table data:
     
    311330
    312331  char *tablename = NULL;
    313   if (TABLEPREFIX) {
    314     if (TABLENAME) {
    315       strextend (&tablename, "%s%s", TABLEPREFIX, TABLENAME);
    316     } else {
    317       strextend (&tablename, "%s%s", TABLEPREFIX, extname);
    318     }
     332  int length = TABLENAME ? strlen(TABLENAME) : strlen(extname);
     333  if (TABLEPREFIX) length += strlen(TABLEPREFIX);
     334  if (TABLESUFFIX) length += strlen(TABLESUFFIX);
     335  ALLOCATE_ZERO (tablename, char, length + 1);
     336  if (TABLEPREFIX) strcpy (tablename, TABLEPREFIX);
     337  if (TABLENAME) {
     338    strcat (tablename, TABLENAME);
    319339  } else {
    320     tablename = TABLENAME ? strcreate (TABLENAME) : strcreate (extname);
    321   }
     340    strcat (tablename, extname);
     341  }
     342  if (TABLESUFFIX) strcat (tablename, TABLESUFFIX);
    322343  replace_dot_with_underscore (tablename);
    323344
     
    374395        PrintIOBuffer (&buffer, "%s VARCHAR(%d)", columns[i].colname, columns[i].Nval);
    375396        break;
     397      case TYPE_SEQUENCE:
     398        // SEQUENCE is a (single) special column with a sequence of values from SEQUENCE_COLUMN_START
     399        PrintIOBuffer (&buffer, "%s INT", columns[i].colname);
     400        break;
    376401      default:
    377402        myAbort ("oops");
     
    415440}
    416441
    417 void write_data (ColumnDef *columns, int Ncolumns, FTable *table, IOBuffer *insert, MYSQL *mysql) {
     442void write_data (ColumnDef *columns, int Ncolumns, int start, FTable *table, IOBuffer *insert, MYSQL *mysql) {
    418443
    419444  int i, j, Nf;
     
    429454
    430455  // first, extract the table data: (probably should save this structure and re-use in create_table)
     456  int Nfield = 1;
    431457  for (i = 0; i < Ncolumns; i++) {
    432458    off_t Nrow;
    433459    int Ncol;
    434460    char type[16];
    435     data[i] = gfits_get_bintable_column_data_by_Ncol (table, i + 1, type, &Nrow, &Ncol, FALSE);
    436     myAssert (Nrow == Ny, "oops");
     461
     462    // we need to skip past virtual columns that do not correspond to actual table columns
     463    if (columns[i].type == TYPE_SEQUENCE) { data[i] = NULL; continue; }
     464    data[i] = gfits_get_bintable_column_data_by_Ncol (table, Nfield, type, &Nrow, &Ncol, FALSE);
     465    Nfield ++;
     466    myAssert (Ncol == columns[i].Nval, "oops (1)");
     467    myAssert (Nrow == Ny, "oops (2)");
    437468  }
    438469
     
    444475    for (Nf = 0; Nf < Ncolumns; Nf++) {
    445476      switch (columns[Nf].type) {
     477        case TYPE_SEQUENCE:
     478          // XXX need to include offset from table block and possible starting offset
     479          PrintIOBuffer (&output, "%d", i + start + SEQUENCE_COLUMN_START);
     480          break;
    446481        case TYPE_CHAR: {
    447482          char line[1024];
     
    719754    remove_argument (N, argc, argv);
    720755  }
     756  TABLESUFFIX = NULL;
     757  if ((N = get_argument (*argc, argv, "-tablesuffix"))) {
     758    remove_argument (N, argc, argv);
     759    TABLESUFFIX = strcreate (argv[N]);
     760    remove_argument (N, argc, argv);
     761  }
     762
     763  SEQUENCE_COLUMN = NULL;
     764  if ((N = get_argument (*argc, argv, "-sequence-column"))) {
     765    remove_argument (N, argc, argv);
     766    SEQUENCE_COLUMN = strcreate (argv[N]);
     767    remove_argument (N, argc, argv);
     768  }
     769  SEQUENCE_COLUMN_START = 0;
     770  if ((N = get_argument (*argc, argv, "-sequence-column-start"))) {
     771    remove_argument (N, argc, argv);
     772    SEQUENCE_COLUMN_START = atoi (argv[N]);
     773    remove_argument (N, argc, argv);
     774  }
    721775
    722776  return TRUE;
Note: See TracChangeset for help on using the changeset viewer.