#include #include #include #include #include #include #include #include #include #include #include // for map operations #include #include #include #include #include #include #include #include #include #define getcwd _getcwd //#undef UNICODE using namespace std; #define __VER__ "PN2060 CSV to KE5FX GPIB Toolkit noise plot file converter PN2060toPNP V0.1" string CurrentPath; string getCurrentPath(); void dumphex(const vector&); int GetFileList(const char *, std::vector &); int time_offset(); int timeoffset(); bool verbose; void usage(string program_name) { cout << "Usage is " << program_name << " [options] [file-list]\n"; cout << "Options:\n"; cout << " -i " << endl; cout << " -v verbose" << endl; } struct Comma final : std::numpunct { char do_decimal_point() const override { return ','; } }; int main(int argc, char *argv[]) { int opt; string filename; string outfilename; char *outdir = ".\\"; unsigned long fin = 10000000; //reference frequency input string debugstring; vector filelist; verbose = false; cout << __VER__ << endl; // Save the program name for future use string program_name = argv[0]; // put ':' in the starting of the // string so that program can //distinguish between '?' and ':' //printf("option cnt: %d\n", argc); if (argc<2) usage(program_name); while ((opt = getopt(argc, argv, ":i:o:VvHh")) != -1) { switch (opt) { case 'i': fin = atol(optarg); //printf("option: %d\n", argc); break; case 'V': case 'v': //printf("option: %c\n", opt); verbose = true; break; case ':': printf("option needs a value\n"); break; case 'H': case 'h': case '?': usage(program_name); return 0; default: cerr << "Usage is " << program_name << " [options] [file-list]\n"; cerr << "Options:\n"; cerr << " -i " << endl; cerr << " -v verbose" << endl; return 1; } } if (verbose) cout << "Current path is " << getCurrentPath() << '\n'; //if(verbose) printf("Input frequency: %d Hz\n", fin); if (verbose) cout << "Carrier frequency: " << fin << " Hz\n"; //if(verbose) cout << "Output frequency: " << fout << " Hz\n"; for (; optind < argc; optind++) { filelist.push_back(argv[optind]); } for (unsigned int i = 0; i < filelist.size(); i++) { if (verbose) cout << "File to convert: " << filelist[i].c_str() << endl; //cout << filelist[i] << endl; filename = filelist[i].substr(filelist[i].find_last_of("/\\") + 1); vector< vector > content; vector row; string line, word; time_t timeNow; struct tm* timeinfo; time(&timeNow); timeinfo = gmtime(&timeNow); ifstream ifd(filelist[i].c_str(), ios::in); if (ifd.is_open()) { //printf("Read file = %s\n", filename); while (getline(ifd, line)) { row.clear(); stringstream str(line); while (getline(str, word, ',')) row.push_back(word); content.push_back(row); } printf("Read file = %s (%d rows)\n", filelist[i].c_str(), content.size()); ofstream outfile; ofstream outfile2; outfilename = outdir + filename + ".pnp"; outfile.open(outfilename.c_str()); if (!outfile.good()) return false; printf("Write PN data to PNP-file = %s\n", outfilename.c_str()); outfilename = outdir + filename + ".img.pnp"; outfile2.open(outfilename.c_str()); if (!outfile2.good()) return false; printf("Write PN floor data to PNP-file = %s\n", outfilename.c_str()); outfile << ";" << endl << ";Composite-noise plot file " << outdir + filename + ".pnp" << endl << ";" << endl; outfile2 << ";" << endl << ";Composite-noise plot file " << outfilename << endl << ";" << endl; outfile << ";Generated by " << __VER__ << endl, outfile2 << ";Generated by " << __VER__ << endl; outfile << ";by Martin Pernter IW3AUT" << endl << ";" << endl << endl; outfile2 << ";by Martin Pernter IW3AUT" << endl << ";" << endl << endl; outfile << "CAP " << filename << endl, outfile2 << "CAP " << filename << " (imaginary part)" << endl; //outfile << "TIM " << asctime(timeinfo) << endl; outfile << "TIM " << timeinfo->tm_mon + 1 << "/" << setfill('0') << setw(2) << timeinfo->tm_mday << "/" << timeinfo->tm_year + 1900 << " " << timeinfo->tm_hour << ":" << setfill('0') << setw(2) << timeinfo->tm_min << ":" << setfill('0') << setw(2) << timeinfo->tm_sec << endl; outfile2 << "TIM " << timeinfo->tm_mon + 1 << "/" << setfill('0') << setw(2) << timeinfo->tm_mday << "/" << timeinfo->tm_year + 1900 << " " << timeinfo->tm_hour << ":" << setfill('0') << setw(2) << timeinfo->tm_min << ":" << setfill('0') << setw(2) << timeinfo->tm_sec << endl; outfile << "IMO PN2060A Phase Noise Analyzer" << endl, outfile2 << "IMO PN2060A Phase Noise Analyzer" << endl; outfile << "IRV N/A" << endl << endl, outfile2 << "IRV N/A" << endl << endl; //outfile << ";disable smoothing"<< endl << "SFL 0" << endl; // skip first decade if (atof(content[0][0].c_str())<0.01) outfile << "DRG -2,5" << endl, outfile2 << "DRG -2,5" << endl; else if (atof(content[0][0].c_str())<0.1) outfile << "DRG -1,5" << endl, outfile2 << "DRG -1,5" << endl; else if (atof(content[0][0].c_str()) >= 0.1 && atof(content[0][0].c_str())<1.0) outfile << "DRG 0,5" << endl, outfile2 << "DRG 0,5" << endl; else if (atof(content[0][0].c_str()) >= 1.0 && atof(content[0][0].c_str())<10.0) outfile << "DRG 1,5" << endl, outfile2 << "DRG 1,5" << endl; int decade_start = -1; // skip first decade for (unsigned int line = 1; line= 0.01 && atof(content[line - 1][value_index].c_str())<0.01) { decade_start = line; } else if ((atof(content[line][value_index].c_str()) >= 0.1 && atof(content[line - 1][value_index].c_str())<0.1) || (atof(content[line][value_index].c_str()) >= 1.0 && atof(content[line - 1][value_index].c_str())<1.0) || (atof(content[line][value_index].c_str()) >= 10.0 && atof(content[line - 1][value_index].c_str())<10.0) || (atof(content[line][value_index].c_str()) >= 100.0 && atof(content[line - 1][value_index].c_str())<100.0) || (atof(content[line][value_index].c_str()) >= 1000.0 && atof(content[line - 1][value_index].c_str())<1000.0) || (atof(content[line][value_index].c_str()) >= 10000.0 && atof(content[line - 1][value_index].c_str())<10000.0) || (atof(content[line][value_index].c_str()) >= 100000.0 && atof(content[line - 1][value_index].c_str())<100000.0)) { if (decade_start != -1) { outfile << "DCC " << line - decade_start << endl; // points/decade outfile2 << "DCC " << line - decade_start << endl; // points/decade decade_start = -2; break; } decade_start = line; } } if (decade_start == -2) break; } //outfile << "DCC 200" << endl; // 200 point/decade outfile << "OVC 0" << endl, outfile2 << "OVC 0" << endl;// << content.size()+1 << endl << endl; outfile << "EIF -1 Hz" << endl, outfile2 << "EIF -1 Hz" << endl; outfile << "ELO -1 Hz" << endl, outfile2 << "ELO -1 Hz" << endl; outfile << "MUL 1.000000" << endl, outfile2 << "MUL 1.000000" << endl; outfile << "VBF 1.00" << endl << endl, outfile2 << "VBF 1.00" << endl << endl; outfile << "CAR " << fin << " Hz, 3dBm" << endl, outfile2 << "CAR " << fin << " Hz, 3dBm" << endl; outfile << "ATT 0 dB" << endl, outfile2 << "ATT 0 dB" << endl; if (atof(content[0][0].c_str())<0.01) outfile << "DEC -2" << endl, outfile2 << "DEC -2" << endl; else if (atof(content[0][0].c_str())<0.1) outfile << "DEC -1" << endl, outfile2 << "DEC -1" << endl; else if (atof(content[0][0].c_str())<1.0) outfile << "DEC 0" << endl, outfile2 << "DEC 0" << endl; else if (atof(content[0][0].c_str())<10.0) outfile << "DEC 1" << endl, outfile2 << "DEC 1" << endl; //outfile << "DEC -1" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = -1; // -1 = skip first decade for (unsigned int line = 0; line0 && atof(content[line][value_index].c_str()) >= 0.01 && atof(content[line - 1][value_index].c_str())<0.01) { outfile << "DEC -2" << endl, outfile2 << "DEC -2" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = line; } else if (line>0 && atof(content[line][value_index].c_str()) >= 0.1 && atof(content[line - 1][value_index].c_str())<0.1) { outfile << "DEC -1" << endl, outfile2 << "DEC -1" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = line; } else if (line>0 && atof(content[line][value_index].c_str()) >= 1.0 && atof(content[line - 1][value_index].c_str())<1.0) { outfile << "DEC 0" << endl, outfile2 << "DEC 0" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = line; } else if (line>0 && atof(content[line][value_index].c_str()) >= 10.0 && atof(content[line - 1][value_index].c_str())<10.0) { outfile << "DEC 1" << endl, outfile2 << "DEC 1" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = line; } else if (line>0 && atof(content[line][value_index].c_str()) >= 100.0 && atof(content[line - 1][value_index].c_str())<100.0) { outfile << "DEC 2" << endl, outfile2 << "DEC 2" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = line; } else if (line>0 && atof(content[line][value_index].c_str()) >= 1000.0 && atof(content[line - 1][value_index].c_str())<1000.0) { outfile << "DEC 3" << endl, outfile2 << "DEC 3" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = line; } else if (line>0 && atof(content[line][value_index].c_str()) >= 10000.0 && atof(content[line - 1][value_index].c_str())<10000.0) { outfile << "DEC 4" << endl, outfile2 << "DEC 4" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = line; } else if (line>0 && atof(content[line][value_index].c_str()) >= 100000.0 && atof(content[line - 1][value_index].c_str())<100000.0) { outfile << "DEC 5" << endl, outfile2 << "DEC 5" << endl; outfile << "NCF 0 dB" << endl, outfile2 << "NCF 0 dB" << endl; decade_start = line; } if (decade_start != -1 && value_index == 0) { outfile << " TRA " << line - decade_start << ": " << fixed << setprecision(3) << (double)fin + atof(content[line][value_index].c_str()) << " Hz, " << atof(content[line][1].c_str()) << " dBc" << endl; outfile2 << " TRA " << line - decade_start << ": " << fixed << setprecision(3) << (double)fin + atof(content[line][value_index].c_str()) << " Hz, " << atof(content[line][2].c_str()) << " dBc" << endl; //cout<tm_mon + 1 << "/" << setfill('0') << setw(2) << timeinfo->tm_mday << "/" << timeinfo->tm_year + 1900 << " " << timeinfo->tm_hour << ":" << setfill('0') << setw(2) << timeinfo->tm_min << ":" << setfill('0') << setw(2) << timeinfo->tm_sec << endl; outfile2 << "EOF " << timeinfo->tm_mon + 1 << "/" << setfill('0') << setw(2) << timeinfo->tm_mday << "/" << timeinfo->tm_year + 1900 << " " << timeinfo->tm_hour << ":" << setfill('0') << setw(2) << timeinfo->tm_min << ":" << setfill('0') << setw(2) << timeinfo->tm_sec << endl; outfile.close(); outfile2.close(); //dumphex(buffer); } else cout << "Could not open " << filename << "\n"; } cout << "Done!" << endl; return 0; } /* int GetFileList(const char *Lsearchkey, std::vector &list) { WIN32_FIND_DATA fd; HANDLE h = FindFirstFile(Lsearchkey, &fd); if (h == INVALID_HANDLE_VALUE) { return 0; // no files found } while (1) { list.push_back(fd.cFileName); if (FindNextFile(h, &fd) == FALSE) break; } return list.size(); } */ string getCurrentPath() { size_t size = 0; char *path = NULL; path = getcwd(path, size); CurrentPath = path; return path; } void dumphex(const vector& v) { //const int N = 16; //const char hex[] = "0123456789ABCDEF"; for (unsigned int c = 0; c < v.size(); ++c) { // Show the hex codes for (unsigned int i = 0; i < 1; i++) { if (i % 8 == 0) cout << ' '; //if( i < c ) cout << dec << c << ": " << setfill('0') << setw(2) << right << hex << (unsigned int)(unsigned char)v[c] << endl; // else // cout << " "; } // Show printable characters // cout << " "; //for( int i = 0; i < c; i++) // { // if( buf[i] < 32 ) cout << '.'; // else cout << buf[i]; // } } } int time_offset() { time_t gmt, rawtime = time(NULL); struct tm *ptm; #if !defined(WIN32) struct tm gbuf; ptm = gmtime_r(&rawtime, &gbuf); #else ptm = gmtime(&rawtime); #endif // Request that mktime() looksup dst in timezone database ptm->tm_isdst = -1; gmt = mktime(ptm); return (int)difftime(rawtime, gmt); } int timeoffset() { std::time_t now = std::time(NULL); std::time_t local = std::mktime(std::localtime(&now)); std::time_t gmt = std::mktime(std::gmtime(&now)); return static_cast (local - gmt); }