/*
 * inquire list of validtime
 *
 * inq_nrdvtime(type1, type2, type3, basetime=-1, pflag=false)
 *  arguments:
 *   type1, type2, type3: String
 *   basetime: Integer (minuits from 00:00 1 Jan 1801) if basetime=-1, returns validtimes for all basetime
 *   pflag: true or false, (if true, informations are printed out)
 *  return:
 *   vtlist: Array
 */
VALUE
rb_inq_nrdvtime(int argc, VALUE *argv, VALUE self)
{
  VALUE type1, type2, type3;
  VALUE basetime;
  VALUE pflag;

  VALUE vtlist;

  N_SI4 *cvtlist, cvtlistsize;
  N_SI4 cbasetime;
  N_SI4 cpflag;

  N_SI4 code;
  int i;


  rb_scan_args(argc, argv, "32", &type1, &type2, &type3, &basetime, &pflag);
  GetTypes;
  if (basetime==Qnil)
    cbasetime = -1;
  else
    cbasetime = get_ctime(basetime);
  cpflag = (pflag==Qtrue ? 1 : 0) ;

  cvtlistsize = 1;
  cvtlist = xmalloc(4);
  code = nusdas_inq_nrdvtime(ctype1, ctype2, ctype3,
                             cvtlist, &cvtlistsize,
                             &cbasetime, cpflag);
  if ( code == 1 )
    return INT2NUM( (int)(cvtlist[0]) );
  else if ( code < 0 )
    rb_raise(rb_eRuntimeError, "failed");
  free(cvtlist);

  cvtlistsize = code;
  cvtlist = xmalloc(4*cvtlistsize);
  code = nusdas_inq_nrdvtime(ctype1, ctype2, ctype3,
                             cvtlist, &cvtlistsize,
                             &cbasetime, cpflag);
  if (code != cvtlistsize)
    rb_raise(rb_eRuntimeError, "bug");

  vtlist = rb_ary_new();
  for (i=0;i<cvtlistsize;i++)
    rb_ary_push(vtlist, INT2NUM((int)(cvtlist[i])));

  return vtlist;
}