File indexing completed on 2024-12-22 04:17:11

0001 /***************************************************************************
0002                                 d2d.cpp
0003                              -------------------
0004     begin                : Tue Jan 16 2007
0005     copyright            : (C) 2007 by The University of Toronto
0006     email                : netterfield@astro.utoronto.ca
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include <stdlib.h> // atoi
0019 #include <qsettings.h>
0020 
0021 // hack to make main() a friend of kstdatasource
0022 #define protected public
0023 #include "datavector.h"
0024 #include "datacollection.h"
0025 #undef protected
0026 
0027 #include "dataplugin.h"
0028 
0029 void Usage() {
0030   fprintf(stderr, "usage: d2d in_filename out_filename [-t <out_type>]\n");
0031   fprintf(stderr, "           [-f <first frame>] [-n <numframes>] [-s skip [-a]] \n");
0032   fprintf(stderr, "           [-x] col1 [[-x] col2 ... [-x] coln]\n");
0033 }
0034 
0035 
0036 static void exitHelper() {
0037   Kst::vectorList.clear();
0038   Kst::scalarList.clear();
0039   Kst::dataObjectList.clear();
0040 }
0041 
0042 int main(int argc, char *argv[]) {
0043   atexit(exitHelper);
0044 
0045   Kst::DataSource::init();
0046 
0047   char field_list[40][120], in_filename[180], out_filename[180], out_type[40];
0048   int n_field=0;
0049   int start_frame=0, n_frames=-1;
0050   bool do_ave = false, do_skip = false;
0051   int n_skip = 0;
0052 
0053   if (argc < 4 || argv[1][0] == '-' || argv[2][0] == '-') {
0054     Usage();
0055     return -1;
0056   }
0057 
0058   strncpy(in_filename, argv[1], 180);
0059   strncpy(out_filename, argv[2], 180);
0060   for (int i = 3; i < argc; i++) {
0061     if (argv[i][0] == '-') {
0062       if (argv[i][1] == 'f') {
0063         i++;
0064         start_frame = atoi(argv[i]);
0065       } else if (argv[i][1] == 'n') {
0066         i++;
0067         n_frames = atoi(argv[i]);
0068       } else if (argv[i][1] == 's') {
0069         i++;
0070         n_skip = atoi(argv[i]);
0071         if (n_skip>0) do_skip = true;
0072       } else if (argv[i][1] == 'a') {
0073         do_ave = true;
0074       } else if (argv[i][1] == 't') {
0075         i++;
0076         strncpy(out_type, argv[i], 40);
0077       } else {
0078         Usage();
0079       }
0080     } else {
0081       strncpy(field_list[n_field], argv[i], 120);
0082       n_field++;
0083     }
0084   }
0085 
0086   if (!do_skip) do_ave = false;
0087 
0088   Kst::DataSourcePtr file = Kst::DataSource::loadSource(in_filename);
0089   if (!file || !file->isValid() || file->isEmpty()) {
0090     fprintf(stderr, "d2asc error: file %s has no data\n", in_filename);
0091     return -2;
0092   }
0093 
0094   if (n_frames < 0) {
0095     n_frames = file->frameCount();
0096   }
0097 
0098   /** make vectors and fill the list **/
0099   QList<Kst::DataVector*> vlist;
0100 
0101   for (int i = 0; i < n_field; i++) {
0102     if (!file->isValidField(field_list[i])) {
0103       fprintf(stderr, "d2asc error: field %s in file %s is not valid\n",
0104               field_list[i], in_filename);
0105       return -3;
0106     }
0107     Kst::DataVectorPtr v = new Kst::DataVector(file, field_list[i], Kst::ObjectTag(field_list[i], file->tag()), start_frame, n_frames, n_skip, n_skip>0, do_ave);
0108     vlist.append(v);
0109   }
0110 
0111   /* find NS */
0112   int NS=0;
0113   for (int i = 0; i < n_field; i++) {
0114     while (vlist.at(i)->update(-1) != Kst::Object::NO_CHANGE)
0115       ; // read vector
0116 
0117     if (vlist.at(i)->length() > NS)
0118       NS = vlist.at(i)->length();
0119   }
0120 
0121   Kst::DataSourcePtr out_file;
0122 //FIXME
0123 #if 0
0124   KService::List sl = KServiceTypeTrader::self()->query("Kst Data Source");
0125   for (KService::List::ConstIterator it = sl.begin(); it != sl.end(); ++it) {
0126     if ((*it)->library() == out_type) {
0127       KstSharedPtr<KST::DataSourcePlugin> p = new KST::DataSourcePlugin(*it);
0128       out_file = p->create(kConfigObject, out_filename, QString::null);
0129       break;
0130     }
0131   }
0132 #endif
0133 
0134   if (out_file) {
0135     for (int i = 0; i < n_field; i++) {
0136       double *data = vlist.at(i)->value();
0137       int n = out_file->writeField(data, field_list[i], start_frame, n_frames);
0138       qDebug() << "wrote" << n << "samples for field" << field_list[i];
0139     }
0140   }
0141 }
0142 
0143 /* vim: sw=2 ts=2 et
0144  */