diff FXAnalyse.c @ 76:5e0c314528bf

Refactor data writing to disk. Implement dayly chunking to separate files
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 30 Nov 2012 17:42:02 +0100
parents 6aae1ebf397f
children 3aa1f1c18961
line wrap: on
line diff
--- a/FXAnalyse.c	Thu Nov 29 13:16:32 2012 +0100
+++ b/FXAnalyse.c	Fri Nov 30 17:42:02 2012 +0100
@@ -17,6 +17,7 @@
 
 #define FXLINELENGTH 123
 #define LOGFILEPATH "C:\\Femto\\Software\\FXQE80"
+#define DATAFOLDER "Z:\\Measures-2012"
 
 #define DEDRIFT_DDS_FREQUENCY 70000000
 
@@ -200,6 +201,21 @@
 }
 
 
+void writeData(char *folder, char *name, char *id, char *date, char *time, double utc, double value)
+{
+	char line[1024];
+	char filename[FILENAME_MAX];
+	
+	// construct filename in the form folder\\id-name.txt
+	snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name);
+	
+	int fd = OpenFile(name, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
+	Fmt(line, "%s\t%s\t%f[p3]\t%f[p3]", date, time, utc, value);
+	WriteLine(fd, line, -1);
+	CloseFile(fd);
+}
+
+
 int main (int argc, char *argv[])
 {
 	double frequency;
@@ -482,7 +498,7 @@
 int CVICALLBACK CB_OnTimer (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {
-	int LogFile, FileOpt, FileHg, FileExtraMath;
+	int LogFile;
 	long LogFileSize;
 	char LineBuffer[FXLINELENGTH+10] = "\r\n_1 ";
 	
@@ -498,7 +514,6 @@
 	char Sec[] = "00.000";
 	struct tm LocalTime ;
 	time_t utcTime;
-	char * ReportString = "dd.mm.yy\t00:00:00\t3481610838.000\t0000000000000.00000000";
 
 	int BoxChecked = FALSE; 
 	
@@ -1277,36 +1292,29 @@
 					}
 				}
 				
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &BoxChecked);			   // Save OL (Math2)
-				if (BoxChecked) {
-					FileOpt = OpenFile("z:\\MeasuresFifi1\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
-					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
-					WriteLine(FileOpt, ReportString, -1);
-					CloseFile(FileOpt);
-					FileOpt = OpenFile("C:\\Femto\\Results\\OptCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);   // a local backup for debugging
-					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math2);
-					WriteLine(FileOpt, ReportString, -1);
-					CloseFile(FileOpt);
+				int save;
+				// run id derived from current date in the form YYMMDD
+				char id[7];
+				snprintf(id, sizeof(id), "%2s%2s%2s", ShortYear, Month, Day);
+				
+				// write LO frequency (Math2) to disk
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &save);
+				if (save) {
+					writeData(DATAFOLDER, "OptCavity", id, Date, Time, utc, Math2);
+					writeData("C:\\Femto\\Results", "OptCavity", id, Date, Time, utc, Math2);
 				}
 				
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, &BoxChecked);			   // Save Hg
-				if (BoxChecked) {
-					FileHg = OpenFile("z:\\MeasuresFifi1\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
-					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
-					WriteLine(FileHg, ReportString, -1);
-					CloseFile(FileHg);
-					FileHg = OpenFile("C:\\Femto\\Results\\HgCavity.txt", VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
-					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p3]", Date, Time, utc, Math3);
-					WriteLine(FileHg, ReportString, -1);
-					CloseFile(FileHg);
+				// write Hg frequency (Math3) to disk
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, &save);
+				if (save) {
+					writeData(DATAFOLDER, "HgCavity", id, Date, Time, utc, Math3);
+					writeData("C:\\Femto\\Results", "HgCavity", id, Date, Time, utc, Math3);
 				}
 				
-				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5SAVE, &BoxChecked);				// Save ExtraMath (Math5)
-				if (BoxChecked) {
-					FileExtraMath = OpenFile(ExtraMathFileName, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
-					Fmt(ReportString, "%s\t%s\t%f[p3]\t%f[p7]", Date, Time, utc, Math5);
-					WriteLine(FileExtraMath, ReportString, -1);
-					CloseFile(FileExtraMath);
+				// write ExtraMath (Math5) to disk
+				GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5SAVE, &save);
+				if (save) {
+					writeData(DATAFOLDER, "ExtraMath", id, Date, Time, utc, Math5);
 				}
 				
 				// Special case to handle change of day at next second
@@ -1681,22 +1689,6 @@
 }
 
 
-int CVICALLBACK OnChooseSaveFile (int panel, int control, int event,
-		void *callbackData, int eventData1, int eventData2)
-{
-	switch (event)
-		{
-		case EVENT_COMMIT:
-
-			FileSelectPopup("D:\\Manip Femto", "*.txt", "*.txt", "Selection of the ExtraMath saving file", VAL_SAVE_BUTTON, 0, 0, 0, FALSE, ExtraMathFileName) ;
-			SetCtrlAttribute(MainPanel, PANEL_CHECKBOX_MATH5SAVE, ATTR_DIMMED, FALSE) ;
-			
-			break;
-		}
-	return 0;
-}
-
-
 int CVICALLBACK CB_OnNCalculus (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {