2 * Grace - GRaphing, Advanced Computation and Exploration of data
4 * Home page: http://plasma-gate.weizmann.ac.il/Grace/
6 * Copyright (c) 1991-1995 Paul J Turner, Portland, OR
7 * Copyright (c) 1996-2003 Grace Development Team
9 * Maintained by Evgeny Stambulchik
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 * read/write data/parameter files
41 #if defined(HAVE_SYS_PARAM_H)
42 # include <sys/param.h>
46 #include <Xm/DialogS.h>
47 #include <Xm/RowColumn.h>
51 #include "core_utils.h"
57 static int open_proc(FSBStructure
*fsb
, char *filename
, void *data
);
58 static int save_proc(FSBStructure
*fsb
, char *filename
, void *data
);
60 static int read_sets_proc(FSBStructure
*fsb
, char *filename
, void *data
);
61 static void set_load_proc(OptionStructure
*opt
, int value
, void *data
);
62 static void set_src_proc(Widget w
, XtPointer client_data
, XtPointer call_data
);
63 static int write_sets_proc(FSBStructure
*fsb
, char *filename
, void *data
);
65 void create_saveproject_popup(void)
67 static FSBStructure
*fsb
= NULL
;
72 fsb
= CreateFileSelectionBox(app_shell
, "Save project");
73 AddFileSelectionBoxCB(fsb
, save_proc
, NULL
);
74 ManageChild(fsb
->FSB
);
77 RaiseWindow(fsb
->dialog
);
83 * save project to a file
85 static int save_proc(FSBStructure
*fsb
, char *filename
, void *data
)
87 if (save_project(grace
->project
, filename
) == RETURN_SUCCESS
) {
95 void create_openproject_popup(void)
97 static FSBStructure
*fsb
= NULL
;
102 fsb
= CreateFileSelectionBox(app_shell
, "Open project");
103 AddFileSelectionBoxCB(fsb
, open_proc
, NULL
);
104 ManageChild(fsb
->FSB
);
106 RaiseWindow(fsb
->dialog
);
112 * open project from a file
114 static int open_proc(FSBStructure
*fsb
, char *filename
, void *data
)
116 if (load_project(grace
, filename
) == RETURN_SUCCESS
) {
118 xdrawgraph(grace
->project
, FALSE
);
127 StorageStructure
*graph_item
; /* graph choice item */
128 OptionStructure
*ftype_item
; /* set type choice item */
129 OptionStructure
*load_item
; /* load as single/nxy/block */
130 OptionStructure
*auto_item
; /* autoscale on read */
131 OptionStructure
*datehint
;
134 void create_file_popup(Widget but
, void *data
)
136 static FSBStructure
*rdata_dialog
= NULL
;
140 if (rdata_dialog
== NULL
) {
142 Widget lab
, rc
, rc2
, fr
, rb
, w
[2];
144 OptionItem option_items
[3];
145 OptionItem opitems
[4] = {
147 {FMT_european
, "European"},
149 {FMT_nohint
, "None" }
152 gui
= xmalloc(sizeof(rdataGUI
));
154 rdata_dialog
= CreateFileSelectionBox(app_shell
, "Read sets");
155 AddFileSelectionBoxCB(rdata_dialog
, read_sets_proc
, (void *) gui
);
157 fr
= CreateFrame(rdata_dialog
->rc
, NULL
);
158 rc
= CreateVContainer(fr
);
160 gui
->graph_item
= CreateGraphChoice(rc
,
161 "Read to graph:", LIST_TYPE_SINGLE
);
163 rc2
= CreateHContainer(rc
);
165 option_items
[0].value
= LOAD_SINGLE
;
166 option_items
[0].label
= "Single set";
167 option_items
[1].value
= LOAD_NXY
;
168 option_items
[1].label
= "NXY";
169 option_items
[2].value
= LOAD_BLOCK
;
170 option_items
[2].label
= "Block data";
171 gui
->load_item
= CreateOptionChoice(rc2
, "Load as:", 1, 3, option_items
);
172 AddOptionChoiceCB(gui
->load_item
, set_load_proc
, (void *) gui
);
173 gui
->ftype_item
= CreateSetTypeChoice(rc2
, "Set type:");
175 rc2
= CreateHContainer(rc
);
176 lab
= CreateLabel(rc2
, "Data source:");
177 rb
= XmCreateRadioBox(rc2
, "radio_box_2", NULL
, 0);
178 XtVaSetValues(rb
, XmNorientation
, XmHORIZONTAL
, NULL
);
179 w
[0] = CreateToggleButton(rb
, "Disk");
180 w
[1] = CreateToggleButton(rb
, "Pipe");
181 for (i
= 0; i
< 2; i
++) {
183 XmNvalueChangedCallback
, set_src_proc
, (XtPointer
) i
);
188 SetToggleButtonState(w
[0], TRUE
);
190 rc2
= CreateHContainer(rc
);
191 gui
->auto_item
= CreateASChoice(rc2
, "Autoscale on read:");
192 gui
->datehint
= CreateOptionChoice(rc2
, "Date hint:", 0, 4, opitems
);
193 SetOptionChoice(gui
->datehint
, get_date_hint());
195 ManageChild(rdata_dialog
->FSB
);
199 RaiseWindow(rdata_dialog
->dialog
);
204 static int read_sets_proc(FSBStructure
*fsb
, char *filename
, void *data
)
209 rdataGUI
*gui
= (rdataGUI
*) data
;
211 load
= GetOptionChoice(gui
->load_item
);
212 if (GetSingleStorageChoice(gui
->graph_item
, &gr
) != RETURN_SUCCESS
) {
213 errmsg("Please select a single graph");
215 if (load
== LOAD_SINGLE
) {
216 grace
->rt
->curtype
= GetOptionChoice(gui
->ftype_item
);
219 grace
->rt
->autoscale_onread
= GetOptionChoice(gui
->auto_item
);
220 set_date_hint(GetOptionChoice(gui
->datehint
));
222 getdata(grace
->project
, filename
, grace
->rt
->cursource
, load
);
224 if (load
== LOAD_BLOCK
) {
225 create_eblock_frame(gr
);
228 xdrawgraph(grace
->project
, FALSE
);
231 /* never close the popup */
235 static void set_src_proc(Widget w
, XtPointer client_data
, XtPointer call_data
)
237 int which
= (int) client_data
;
238 XmToggleButtonCallbackStruct
*state
= (XmToggleButtonCallbackStruct
*) call_data
;
241 grace
->rt
->cursource
= which
;
245 static void set_load_proc(OptionStructure
*opt
, int value
, void *data
)
247 rdataGUI
*gui
= (rdataGUI
*) data
;
249 if (value
== LOAD_SINGLE
) {
250 SetSensitive(gui
->ftype_item
->menu
, True
);
252 SetOptionChoice(gui
->ftype_item
, SET_XY
);
253 SetSensitive(gui
->ftype_item
->menu
, False
);
259 GraphSetStructure
*sel
;
263 void create_write_popup(Widget but
, void *data
)
265 static FSBStructure
*fsb
= NULL
;
273 gui
= xmalloc(sizeof(wdataGUI
));
275 fsb
= CreateFileSelectionBox(app_shell
, "Write sets");
276 AddFileSelectionBoxCB(fsb
, write_sets_proc
, (void *) gui
);
278 fr
= CreateFrame(fsb
->rc
, NULL
);
279 rc
= CreateVContainer(fr
);
280 gui
->sel
= CreateGraphSetSelector(rc
, NULL
, LIST_TYPE_MULTIPLE
);
281 gui
->format_item
= CreateTextItem2(rc
, 15, "Format: ");
282 xv_setstr(gui
->format_item
, project_get_sformat(grace
->project
));
284 ManageChild(fsb
->FSB
);
286 RaiseWindow(fsb
->dialog
);
292 * write a set or sets to a file
294 static int write_sets_proc(FSBStructure
*fsb
, char *filename
, void *data
)
296 wdataGUI
*gui
= (wdataGUI
*) data
;
298 Quark
*pset
, **selset
;
302 cp
= grace_openw(grace
, filename
);
307 cd
= GetStorageChoices(gui
->sel
->set_sel
, &selset
);
309 errmsg("No set selected");
311 strncpy(format
, xv_getstr(gui
->format_item
), 31);
312 for (i
= 0; i
< cd
; i
++) {
314 write_set(pset
, cp
, format
);
320 /* never close the popup */
335 static Widget netcdf_frame
= (Widget
) NULL
;
337 static Widget netcdf_listx_item
;
338 static Widget netcdf_listy_item
;
339 static Widget netcdf_file_item
;
341 void create_netcdffiles_popup(Widget w
, XtPointer client_data
, XtPointer call_data
);
343 static void do_netcdfquery_proc(Widget w
, XtPointer client_data
, XtPointer call_data
);
345 void update_netcdfs(void);
347 int getnetcdfvars(void);
349 static void do_netcdf_proc(Widget w
, XtPointer client_data
, XtPointer call_data
)
352 char xvar
[256], yvar
[256];
355 int j
, pos_cnt
, cnt
, retval
;
361 * setno == -1, then next set
363 strcpy(fname
, xv_getstr(netcdf_file_item
));
364 if (XmListGetSelectedPos(netcdf_listx_item
, &pos_list
, &pos_cnt
)) {
365 XtVaGetValues(netcdf_listx_item
,
366 XmNselectedItemCount
, &cnt
,
367 XmNselectedItems
, &s
,
369 cs
= XmStringCopy(*s
);
370 if ((cstr
= GetStringSimple(cs
))) {
376 errmsg("Need to select X, either variable name or INDEX");
380 if (XmListGetSelectedPos(netcdf_listy_item
, &pos_list
, &pos_cnt
)) {
382 XtVaGetValues(netcdf_listy_item
,
383 XmNselectedItemCount
, &cnt
,
384 XmNselectedItems
, &s
,
386 cs
= XmStringCopy(*s
);
387 if ((cstr
= GetStringSimple(cs
))) {
393 errmsg("Need to select Y");
397 if (strcmp(xvar
, "INDEX") == 0) {
398 retval
= readnetcdf(NULL
, fname
, NULL
, yvar
, -1, -1, 1);
400 retval
= readnetcdf(NULL
, fname
, xvar
, yvar
, -1, -1, 1);
403 xdrawgraph(grace
->project
, FALSE
);
408 void update_netcdfs(void)
411 char buf
[256], fname
[512];
413 int cdfid
; /* netCDF id */
414 int ndims
, nvars
, ngatts
, recdim
;
417 nc_type datatype
= 0;
422 ncopts
= 0; /* no crash on error */
424 if (netcdf_frame
!= NULL
) {
425 strcpy(fname
, xv_getstr(netcdf_file_item
));
427 XmListDeleteAllItems(netcdf_listx_item
);
428 XmListDeleteAllItems(netcdf_listy_item
);
429 xms
= XmStringCreateLocalized("INDEX");
430 XmListAddItemUnselected(netcdf_listx_item
, xms
, 0);
433 if (strlen(fname
) < 2) {
437 if ((cdfid
= ncopen(fname
, NC_NOWRITE
)) == -1) {
438 errmsg("Can't open file.");
442 ncinquire(cdfid
, &ndims
, &nvars
, &ngatts
, &recdim
);
443 for (i
= 0; i
< ndims
; i
++) {
444 ncdiminq(cdfid
, i
, NULL
, &dimlen
[i
]);
446 for (i
= 0; i
< nvars
; i
++) {
447 ncvarinq(cdfid
, i
, varname
, &datatype
, &ndims
, dim
, &natts
);
448 if ((var_id
= ncvarid(cdfid
, varname
)) == -1) {
450 sprintf(ebuf
, "update_netcdfs(): No such variable %s", varname
);
457 ncdiminq(cdfid
, dim
[0], (char *) NULL
, &len
);
458 sprintf(buf
, "%s", varname
);
459 xms
= XmStringCreateLocalized(buf
);
460 XmListAddItemUnselected(netcdf_listx_item
, xms
, 0);
461 XmListAddItemUnselected(netcdf_listy_item
, xms
, 0);
470 static void do_netcdfupdate_proc(Widget w
, XtPointer client_data
, XtPointer call_data
)
477 void create_netcdfs_popup(Widget but
, void *data
)
479 static Widget top
, dialog
;
488 label1
[0] = "Accept";
489 label1
[1] = "Files...";
490 label1
[2] = "Update";
493 top
= XmCreateDialogShell(app_shell
, "netCDF", NULL
, 0);
495 dialog
= CreateVContainer(top
);
497 XtSetArg(args
[0], XmNlistSizePolicy
, XmRESIZE_IF_POSSIBLE
);
498 XtSetArg(args
[1], XmNvisibleItemCount
, 5);
500 lab
= CreateLabel(dialog
, "Select set X:");
501 netcdf_listx_item
= XmCreateScrolledList(dialog
, "list", args
, 2);
502 ManageChild(netcdf_listx_item
);
504 lab
= CreateLabel(dialog
, "Select set Y:");
505 netcdf_listy_item
= XmCreateScrolledList(dialog
, "list", args
, 2);
506 ManageChild(netcdf_listy_item
);
508 netcdf_file_item
= CreateTextItem2(dialog
, 30, "netCDF file:");
510 CreateSeparator(dialog
);
512 CreateCommandButtons(dialog
, 5, but1
, label1
);
513 XtAddCallback(but1
[0], XmNactivateCallback
, (XtCallbackProc
) do_netcdf_proc
,
515 XtAddCallback(but1
[1], XmNactivateCallback
, (XtCallbackProc
) create_netcdffiles_popup
,
517 XtAddCallback(but1
[2], XmNactivateCallback
, (XtCallbackProc
) do_netcdfupdate_proc
,
519 XtAddCallback(but1
[3], XmNactivateCallback
, (XtCallbackProc
) do_netcdfquery_proc
,
521 XtAddCallback(but1
[4], XmNactivateCallback
, (XtCallbackProc
) destroy_dialog
,
532 static int do_netcdffile_proc(FSBStructure
*fsb
, char *filename
, void *data
)
534 xv_setstr(netcdf_file_item
, filename
);
540 void create_netcdffiles_popup(Widget w
, XtPointer client_data
, XtPointer call_data
)
542 static FSBStructure
*fsb
= NULL
;
547 fsb
= CreateFileSelectionBox(app_shell
, "Select netCDF file");
548 AddFileSelectionBoxCB(fsb
, do_netcdffile_proc
, NULL
);
549 ManageChild(fsb
->FSB
);
552 RaiseWindow(fsb
->dialog
);
557 char *getcdf_type(nc_type datatype
)
573 return "UNKNOWN (can't read this)";
578 static void do_netcdfquery_proc(Widget w
, XtPointer client_data
, XtPointer call_data
)
580 char xvar
[256], yvar
[256];
581 char buf
[256], fname
[512];
587 int cdfid
; /* netCDF id */
588 nc_type datatype
= 0;
593 nc_type xdatatype
= 0;
594 nc_type ydatatype
= 0;
595 int xndims
, xdim
[10], xnatts
;
596 int yndims
, ydim
[10], ynatts
;
603 ncopts
= 0; /* no crash on error */
607 strcpy(fname
, xv_getstr(netcdf_file_item
));
609 if ((cdfid
= ncopen(fname
, NC_NOWRITE
)) == -1) {
610 errmsg("Can't open file.");
614 if (XmListGetSelectedPos(netcdf_listx_item
, &pos_list
, &pos_cnt
)) {
615 XtVaGetValues(netcdf_listx_item
,
616 XmNselectedItemCount
, &cnt
,
617 XmNselectedItems
, &s
,
619 cs
= XmStringCopy(*s
);
620 if ((cstr
= GetStringSimple(cs
))) {
626 errmsg("Need to select X, either variable name or INDEX");
629 if (XmListGetSelectedPos(netcdf_listy_item
, &pos_list
, &pos_cnt
)) {
630 XtVaGetValues(netcdf_listy_item
,
631 XmNselectedItemCount
, &cnt
,
632 XmNselectedItems
, &s
,
634 cs
= XmStringCopy(*s
);
635 if ((cstr
= GetStringSimple(cs
))) {
641 errmsg("Need to select Y");
644 if (strcmp(xvar
, "INDEX") == 0) {
645 stufftext("X is the index of the Y variable\n");
647 if ((x_id
= ncvarid(cdfid
, xvar
)) == -1) {
649 sprintf(ebuf
, "do_query(): No such variable %s for X", xvar
);
653 ncvarinq(cdfid
, x_id
, NULL
, &xdatatype
, &xndims
, xdim
, &xnatts
);
654 ncdiminq(cdfid
, xdim
[0], NULL
, &nx
);
655 sprintf(buf
, "X is %s, data type %s \t length [%ld]\n", xvar
, getcdf_type(xdatatype
), nx
);
657 sprintf(buf
, "\t%d Attributes:\n", xnatts
);
659 for (i
= 0; i
< xnatts
; i
++) {
661 ncattname(cdfid
, x_id
, i
, attname
);
662 ncattinq(cdfid
, x_id
, attname
, &datatype
, &atlen
);
665 ncattget(cdfid
, x_id
, attname
, (void *) atcharval
);
666 atcharval
[atlen
] = 0;
667 sprintf(buf
, "\t\t%s: %s\n", attname
, atcharval
);
671 ncattget(cdfid
, x_id
, attname
, (void *) &f
);
672 sprintf(buf
, "\t\t%s: %f\n", attname
, f
);
676 ncattget(cdfid
, x_id
, attname
, (void *) &d
);
677 sprintf(buf
, "\t\t%s: %f\n", attname
, d
);
685 if ((y_id
= ncvarid(cdfid
, yvar
)) == -1) {
687 sprintf(ebuf
, "do_query(): No such variable %s for Y", yvar
);
691 ncvarinq(cdfid
, y_id
, NULL
, &ydatatype
, &yndims
, ydim
, &ynatts
);
692 ncdiminq(cdfid
, ydim
[0], NULL
, &ny
);
693 sprintf(buf
, "Y is %s, data type %s \t length [%ld]\n", yvar
, getcdf_type(ydatatype
), ny
);
695 sprintf(buf
, "\t%d Attributes:\n", ynatts
);
697 for (i
= 0; i
< ynatts
; i
++) {
699 ncattname(cdfid
, y_id
, i
, attname
);
700 ncattinq(cdfid
, y_id
, attname
, &datatype
, &atlen
);
703 ncattget(cdfid
, y_id
, attname
, (void *) atcharval
);
704 atcharval
[atlen
] = 0;
705 sprintf(buf
, "\t\t%s: %s\n", attname
, atcharval
);
709 ncattget(cdfid
, y_id
, attname
, (void *) &f
);
710 sprintf(buf
, "\t\t%s: %f\n", attname
, f
);
714 ncattget(cdfid
, y_id
, attname
, (void *) &d
);
715 sprintf(buf
, "\t\t%s: %f\n", attname
, d
);