/* * Initialize graphics context, color palette, local preferences. * Called both at program startup and by "reset session". */ void init_session() { /* Disable pipes and system commands during initialization */ successful_initialization = FALSE; /* Undefine any previously-used variables */ del_udv_by_name("",TRUE); /* Restore default colors before loading local preferences */ set_colorsequence(1); /* Reset program variables not handled by 'reset' */ overflow_handling = INT64_OVERFLOW_TO_FLOAT; /* Reset voxel data structures if supported */ init_voxelsupport(); /* Make sure all variables start in the same state 'reset' * would set them to. */ reset_command(); /* FIXME: this does c_token++ */ load_rcfile(0); /* System-wide gnuplotrc if configured */ load_rcfile(1); /* ./.gnuplot if configured */ /* After this point we allow pipes and system commands */ successful_initialization = TRUE; load_rcfile(2); /* ~/.gnuplot */ }
/* * Initialize graphics context, color palette, local preferences. * Called both at program startup and by "reset session". */ void init_session() { /* Disable pipes and system commands during initialization */ successful_initialization = FALSE; /* Undefine any previously-used variables */ del_udv_by_name("",TRUE); /* Restore default colors before loadin local preferences */ set_colorsequence(1); /* Make sure all variables start in the same state 'reset' * would set them to. */ reset_command(); /* FIXME: this does c_token++ */ load_rcfile(0); /* System-wide gnuplotrc if configured */ load_rcfile(1); /* ./.gnuplot if configured */ /* After this point we allow pipes and system commands */ successful_initialization = TRUE; load_rcfile(2); /* ~/.gnuplot */ }
void statsrequest(void) { int i; int columns; int columnsread; double v[2]; static char *file_name = NULL; /* Vars to hold data and results */ long n; /* number of records retained */ long max_n; static double *data_x = NULL; static double *data_y = NULL; /* values read from file */ long invalid; /* number of missing/invalid records */ long blanks; /* number of blank lines */ long doubleblanks; /* number of repeated blank lines */ long out_of_range; /* number pts rejected, because out of range */ struct file_stats res_file; struct sgl_column_stats res_x, res_y; struct two_column_stats res_xy; float *matrix; /* matrix data. This must be float. */ int nc, nr; /* matrix dimensions. */ int index; /* Vars for variable handling */ static char *prefix = NULL; /* prefix for user-defined vars names */ /* Vars that control output */ TBOOLEAN do_output = TRUE; /* Generate formatted output */ c_token++; /* Parse ranges */ AXIS_INIT2D(FIRST_X_AXIS, 0); AXIS_INIT2D(FIRST_Y_AXIS, 0); parse_range(FIRST_X_AXIS); parse_range(FIRST_Y_AXIS); /* Initialize */ columnsread = 2; invalid = 0; /* number of missing/invalid records */ blanks = 0; /* number of blank lines */ doubleblanks = 0; /* number of repeated blank lines */ out_of_range = 0; /* number pts rejected, because out of range */ n = 0; /* number of records retained */ nr = 0; /* Matrix dimensions */ nc = 0; max_n = INITIAL_DATA_SIZE; free(data_x); free(data_y); data_x = vec(max_n); /* start with max. value */ data_y = vec(max_n); if ( !data_x || !data_y ) int_error( NO_CARET, "Internal error: out of memory in stats" ); n = invalid = blanks = doubleblanks = out_of_range = nr = 0; /* Get filename */ free ( file_name ); file_name = try_to_get_string(); if ( !file_name ) int_error(c_token, "missing filename"); /* =========================================================== v923z: insertion for treating matrices EAM: only handles ascii matrix with uniform grid, and fails to apply any input data transforms =========================================================== */ if ( almost_equals(c_token, "mat$rix") ) { df_open(file_name, 3, NULL); index = df_num_bin_records - 1; /* We take these values as set by df_determine_matrix_info See line 1996 in datafile.c */ nc = df_bin_record[index].scan_dim[0]; nr = df_bin_record[index].scan_dim[1]; n = nc * nr; matrix = (float *)df_bin_record[index].memory_data; /* Fill up a vector, so that we can use the existing code. */ if ( !redim_vec(&data_x, n ) ) { int_error( NO_CARET, "Out of memory in stats: too many datapoints (%d)?", n ); } for( i=0; i < n; i++ ) { data_y[i] = (double)matrix[i]; } /* We can close the file here, there is nothing else to do */ df_close(); /* We will invoke single column statistics for the matrix */ columns = 1; } else { /* Not a matrix */ columns = df_open(file_name, 2, NULL); /* up to 2 using specs allowed */ if (columns < 0) int_error(NO_CARET, "Can't read data file"); if (columns > 2 ) int_error(c_token, "Need 0 to 2 using specs for stats command"); /* If the user has set an explicit locale for numeric input, apply it here so that it affects data fields read from the input file. */ /* v923z: where exactly should this be? here or before the matrix case? * I think, we should move everything here to before trying to open the file. * There is no point in trying to read anything, if the axis is logarithmic, e.g. */ set_numeric_locale(); /* For all these below: we could save the state, switch off, then restore */ if ( axis_array[FIRST_X_AXIS].log || axis_array[FIRST_Y_AXIS].log ) int_error( NO_CARET, "Stats command not available with logscale active"); if ( axis_array[FIRST_X_AXIS].datatype == DT_TIMEDATE || axis_array[FIRST_Y_AXIS].datatype == DT_TIMEDATE ) int_error( NO_CARET, "Stats command not available in timedata mode"); if ( polar ) int_error( NO_CARET, "Stats command not available in polar mode" ); if ( parametric ) int_error( NO_CARET, "Stats command not available in parametric mode" ); /* The way readline and friends work is as follows: - df_open will return the number of columns requested in the using spec so that "columns" will be 0, 1, or 2 (no using, using 1, using 1:2) - readline always returns the same number of columns (for us: 1 or 2) - using 1:2 = return two columns, skipping lines w/ bad data - using 1 = return sgl column (supply zeros (0) for the second col) - no using = return two columns (first two), fail on bad data We need to know how many columns to process. If columns==1 or ==2 (that is, if there was a using spec), all is clear and we use the value of columns. But: if columns is 0, then we need to figure out the number of cols read from the return value of readline. If readline ever returns 1, we take that; only if it always returns 2 do we assume two cols. */ while( (i = df_readline(v, 2)) != DF_EOF ) { columnsread = ( i > columnsread ? i : columnsread ); if ( n >= max_n ) { max_n = (max_n * 3) / 2; /* increase max_n by factor of 1.5 */ /* Some of the reallocations went bad: */ if ( 0 || !redim_vec(&data_x, max_n) || !redim_vec(&data_y, max_n) ) { df_close(); int_error( NO_CARET, "Out of memory in stats: too many datapoints (%d)?", max_n ); } } /* if (need to extend storage space) */ switch (i) { case DF_MISSING: case DF_UNDEFINED: /* Invalids are only recognized if the syntax is like this: stats "file" using ($1):($2) If the syntax is simply: stats "file" using 1:2 then df_readline simply skips invalid records (does not return anything!) Status: 2009-11-02 */ invalid += 1; continue; case DF_FIRST_BLANK: blanks += 1; continue; case DF_SECOND_BLANK: blanks += 1; doubleblanks += 1; continue; case 0: int_error( NO_CARET, "bad data on line %d of file %s", df_line_number, df_filename ? df_filename : "" ); break; case 1: /* Read single column successfully */ if ( validate_data(v[0], FIRST_Y_AXIS) ) { data_y[n] = v[0]; n++; } else { out_of_range++; } break; case 2: /* Read two columns successfully */ if ( validate_data(v[0], FIRST_X_AXIS) && validate_data(v[1], FIRST_Y_AXIS) ) { data_x[n] = v[0]; data_y[n] = v[1]; n++; } else { out_of_range++; } break; } } /* end-while : done reading file */ df_close(); /* now resize fields to actual length: */ redim_vec(&data_x, n); redim_vec(&data_y, n); /* figure out how many columns where really read... */ if ( columns == 0 ) columns = columnsread; } /* end of case when the data file is not a matrix */ /* Now finished reading user input; return to C locale for internal use*/ reset_numeric_locale(); /* PKJ - TODO: similar for logscale, polar/parametric, timedata */ /* No data! Try to explain why. */ if ( n == 0 ) { if ( out_of_range > 0 ) int_error( NO_CARET, "All points out of range" ); else int_error( NO_CARET, "No valid data points found in file" ); } /* Parse the remainder of the command line: 0 to 2 tokens possible */ while( !(END_OF_COMMAND) ) { if ( almost_equals( c_token, "out$put" ) ) { do_output = TRUE; c_token++; } else if ( almost_equals( c_token, "noout$put" ) ) { do_output = FALSE; c_token++; } else if ( almost_equals(c_token, "pre$fix") || equals(c_token, "name")) { c_token++; free ( prefix ); prefix = try_to_get_string(); if (!legal_identifier(prefix) || !strcmp ("GPVAL_", prefix)) int_error( --c_token, "illegal prefix" ); } else { int_error( c_token, "Expecting [no]output or prefix"); } } /* Set defaults if not explicitly set by user */ if (!prefix) prefix = gp_strdup("STATS_"); i = strlen(prefix); if (prefix[i-1] != '_') { prefix = gp_realloc(prefix, i+2, "prefix"); strcat(prefix,"_"); } /* Do the actual analysis */ res_file = analyze_file( n, out_of_range, invalid, blanks, doubleblanks ); if ( columns == 1 ) { res_y = analyze_sgl_column( data_y, n, nr ); } if ( columns == 2 ) { /* If there are two columns, then the data file is not a matrix */ res_x = analyze_sgl_column( data_x, n, 0 ); res_y = analyze_sgl_column( data_y, n, 0 ); res_xy = analyze_two_columns( data_x, data_y, res_x, res_y, n ); } /* Store results in user-accessible variables */ /* Clear out any previous use of these variables */ del_udv_by_name( prefix, TRUE ); file_variables( res_file, prefix ); if ( columns == 1 ) { sgl_column_variables( res_y, prefix, "" ); } if ( columns == 2 ) { sgl_column_variables( res_x, prefix, "_x" ); sgl_column_variables( res_y, prefix, "_y" ); two_column_variables( res_xy, prefix ); } /* Output */ if ( do_output ) { file_output( res_file ); if ( columns == 1 ) sgl_column_output( res_y, res_file.records ); else two_column_output( res_x, res_y, res_xy, res_file.records ); } /* Cleanup */ free(data_x); free(data_y); data_x = NULL; data_y = NULL; free( file_name ); file_name = NULL; free( prefix ); prefix = NULL; }