Exemplo n.º 1
0
static void
cnew(G_GNUC_UNUSED GwyContainer *data, GwyRunType run)
{
    GwyDataField *dfield;
    CNewArgs args;
    gboolean ok;
    gint oldid, i, j, k, n;
    GwyCalibration *calibration;
    GwyCalData *caldata = NULL, *old;
    gchar *filename;
    gchar *contents;
    gsize len = 0;
    GError *err = NULL;
    gsize pos = 0;
    GString *str;
    gdouble *x, *y, *z, *xunc, *yunc, *zunc, *xerr, *yerr, *zerr;
    FILE *fh;


    g_return_if_fail(run & CNEW_RUN_MODES);
    gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield,
                                     GWY_APP_DATA_FIELD_ID, &oldid,
                                     0);
    g_return_if_fail(dfield);

    cnew_load_args(gwy_app_settings_get(), &args);
    if (run == GWY_RUN_INTERACTIVE) {
        ok = cnew_dialog(&args, dfield);
        cnew_save_args(gwy_app_settings_get(), &args);
        if (!ok)
            return;
    }

    /*create the caldata*/
    caldata = gwy_caldata_new(8);
    x = gwy_caldata_get_x(caldata);
    y = gwy_caldata_get_y(caldata);
    z = gwy_caldata_get_z(caldata);
    xerr = gwy_caldata_get_xerr(caldata);
    yerr = gwy_caldata_get_yerr(caldata);
    zerr = gwy_caldata_get_zerr(caldata);
    xunc = gwy_caldata_get_xunc(caldata);
    yunc = gwy_caldata_get_yunc(caldata);
    zunc = gwy_caldata_get_zunc(caldata);
    gwy_caldata_set_si_unit_x(caldata, gwy_data_field_get_si_unit_xy(dfield));
    gwy_caldata_set_si_unit_y(caldata, gwy_data_field_get_si_unit_xy(dfield));
    gwy_caldata_set_si_unit_z(caldata, gwy_data_field_get_si_unit_z(dfield));

    n = 0;
    for (i=0; i<2; i++)
    {
        for (j=0; j<2; j++)
        {
            for (k=0; k<2; k++)
            {

                if (i) x[n] = args.xrange_from;
                else x[n] = args.xrange_to;
                if (j) y[n] = args.yrange_from;
                else y[n] = args.yrange_to;
                if (k) z[n] = args.zrange_from;
                else z[n] = args.zrange_to;

                if (i) xerr[n] = (args.xrange_to-args.xrange_from)*(args.xmult-1);
                else xerr[n] = 0;
                if (j) yerr[n] = (args.yrange_to-args.yrange_from)*(args.ymult-1);
                else yerr[n] = 0;
                if (k) zerr[n] = (args.zrange_to-args.zrange_from)*(args.zmult-1);
                else zerr[n] = 0;

                xunc[n] = args.xunc;
                yunc[n] = args.yunc;
                zunc[n] = args.zunc;
                n++;
            }
        }
    }

    if (args.duplicate == DUPLICATE_APPEND && (calibration = gwy_inventory_get_item(gwy_calibrations(), args.name)))
        {
        filename = g_build_filename(gwy_get_user_dir(), "caldata", calibration->filename, NULL);
        if (!g_file_get_contents(filename,
                                 &contents, &len, &err) || !len)
        {
             g_warning("Error loading file: %s", err->message);
             g_clear_error(&err);
             return;
        }
        else {
            if (len)
              old = GWY_CALDATA(gwy_serializable_deserialize(contents, len, &pos));
            g_free(contents);
        }

        gwy_caldata_append(old, caldata);
        g_object_unref(caldata);
        caldata = old;

    }

    gwy_caldata_set_range(caldata, args.xrange_from, args.xrange_to,
                          args.yrange_from, args.yrange_to,
                          args.zrange_from, args.zrange_to);

    //printf("Saving %g %g %g %g\n", args.xrange_from, args.xrange_to, args.yrange_from, args.yrange_to);

    /*now create and save the resource*/
    if ((calibration = GWY_CALIBRATION(gwy_inventory_get_item(gwy_calibrations(), args.name)))==NULL)
    {
        calibration = gwy_calibration_new(args.name, g_strconcat(args.name, ".dat", NULL));
        gwy_inventory_insert_item(gwy_calibrations(), calibration);
        g_object_unref(calibration);
    }
    calibration->caldata = caldata;

    filename = gwy_resource_build_filename(GWY_RESOURCE(calibration));
    if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
        g_mkdir(g_build_filename(gwy_get_user_dir(), "calibrations", NULL), 0700);
    }
    fh = gwy_fopen(filename, "wb");
    if (!fh) {
        g_warning(_("Cannot save preset: %s"), filename);
        g_free(filename);
        return;
    }
    g_free(filename);

    str = gwy_resource_dump(GWY_RESOURCE(calibration));
    fwrite(str->str, 1, str->len, fh);
    fclose(fh);
    g_string_free(str, TRUE);

    gwy_resource_data_saved(GWY_RESOURCE(calibration));
    gwy_resource_data_changed(GWY_RESOURCE(calibration));

    /*now save the calibration data*/
    //gwy_caldata_debug(caldata, "Saving: ");

    gwy_caldata_save_data(caldata, calibration->filename);

}
Exemplo n.º 2
0
static void
load_caldata(CLoadControls *controls)
{
    GtkWidget *dialog;
    gchar *filename;
    GwyCalData *caldata = controls->args->caldata;
    FILE *fr;
    gint i, ndata;
    gchar text[50];
    gdouble xfrom, xto, yfrom, yto, zfrom, zto;
    gdouble x, y, z, xerr, yerr, zerr, xunc, yunc, zunc;
    gchar six[50], siy[50], siz[50];

    dialog = gtk_file_chooser_dialog_new ("Load calibration data",
                      GTK_WINDOW(controls->dialog),
                      GTK_FILE_CHOOSER_ACTION_OPEN,
                      GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                      GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                      NULL);

    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
    {
        filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
        fr = fopen(filename, "r");
        if (!fr) { printf("Error: no file open\n");
        } else {
            
            fscanf(fr, "%d", &ndata);
            fscanf(fr, "%lf", &xfrom);
            fscanf(fr, "%lf", &xto);
            fscanf(fr, "%lf", &yfrom);
            fscanf(fr, "%lf", &yto);
            fscanf(fr, "%lf", &zfrom);
            fscanf(fr, "%lf", &zto);

            //printf("loading %d caldata\n", ndata);
            caldata = gwy_caldata_new(ndata);    //FIXME free it somewhere if allocated previously
            caldata->ndata = ndata;
            caldata->x_from = xfrom;
            caldata->x_to = xto;
            caldata->y_from = yfrom;
            caldata->y_to = yto;
            caldata->z_from = zfrom;
            caldata->z_to = zto;
            fscanf(fr, "%s", six);
            fscanf(fr, "%s", siy);
            fscanf(fr, "%s", siz);
            caldata->si_unit_x = gwy_si_unit_new(six);
            caldata->si_unit_y = gwy_si_unit_new(siy);
            caldata->si_unit_z = gwy_si_unit_new(siz);

            for (i=0; i<caldata->ndata; i++)
            {
                fscanf(fr, "%lf", &x);
                fscanf(fr, "%lf", &y);
                fscanf(fr, "%lf", &z);
                fscanf(fr, "%lf", &xerr);
                fscanf(fr, "%lf", &yerr);
                fscanf(fr, "%lf", &zerr);
                fscanf(fr, "%lf", &xunc);
                fscanf(fr, "%lf", &yunc);
                fscanf(fr, "%lf", &zunc);
                caldata->x[i] = x;
                caldata->y[i] = y;
                caldata->z[i] = z;
                caldata->xerr[i] = xerr;
                caldata->yerr[i] = yerr;
                caldata->zerr[i] = zerr;
                caldata->xunc[i] = xunc;
                caldata->yunc[i] = yunc;
                caldata->zunc[i] = zunc;
               /* printf("adding %g %g %g  %g %g %g   %g %g %g\n",
                       caldata->x[i], caldata->y[i], caldata->z[i],
                       caldata->xerr[i], caldata->yerr[i], caldata->zerr[i],
                       caldata->xunc[i], caldata->yunc[i], caldata->zunc[i]);*/
              }
            
            fclose(fr);
            //printf("done.\n");
            g_snprintf(text, sizeof(text), "Loaded %d data points", caldata->ndata);
            gtk_label_set_text(GTK_LABEL(controls->text), text);
        }
        g_free (filename);
        controls->args->caldata = caldata;
    }
    gtk_widget_destroy (dialog);

}