/*
 * inquire list of basetimes
 *
 * inq_nrdbtime(type1, type2, type3, pflag=false)
 *  arguments:
 *   type1, type2, type3: String
 *   pflag: true or false, (if true, informations are printed out)
 *  return:
 *   btlist: Array
 */
VALUE
rb_inq_nrdbtime(int argc, VALUE *argv, VALUE self)
{
  VALUE type1, type2, type3;
  VALUE pflag;

  VALUE btlist;

  N_SI4 *cbtlist, cbtlistsize;
  N_SI4 cpflag;

  N_SI4 code;
  int i;

  rb_scan_args(argc, argv, "31", &type1, &type2, &type3, &pflag);
  GetTypes;
  cpflag = (pflag==Qtrue ? 1 : 0) ;

  cbtlistsize = 1;
  cbtlist = xmalloc(4);
  code = nusdas_inq_nrdbtime(ctype1, ctype2, ctype3,
                             cbtlist, &cbtlistsize, cpflag);
  if ( code == 1 )
    return rb_ary_new3(1, INT2NUM( (int)(cbtlist[0]) ) );
  else if ( code == -1 )
    rb_raise(rb_eRuntimeError, "file IO error");
  else if ( code == -2 )
    rb_raise(rb_eRuntimeError, "control part does not exit");
  else if ( code == -3 )
    rb_raise(rb_eRuntimeError, "record length is invalid");
  else if ( code == -4 )
    rb_raise(rb_eRuntimeError, "failed to open file or directory");
  else if ( code < 0 )
    rb_raise(rb_eRuntimeError, "failed");
  free(cbtlist);

  cbtlistsize = code;
  cbtlist = xmalloc(4*cbtlistsize);
  code = nusdas_inq_nrdbtime(ctype1, ctype2, ctype3,
                             cbtlist, &cbtlistsize, cpflag);
  if (code != cbtlistsize)
    rb_raise(rb_eRuntimeError, "bug");

  btlist = rb_ary_new();
  for (i=0;i<cbtlistsize;i++)
    rb_ary_push(btlist, INT2NUM((int)(cbtlist[i])));

  return btlist;
}