00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 #include <gtk/gtk.h>
00099 #include "config.h"
00100 #include "gxsm/plugin.h"
00101 #include "gxsm/dataio.h"
00102 #include "gxsm/action_id.h"
00103 #include "gxsm/util.h"
00104 #include "batch.h"
00105 #include "fileio.c"
00106
00107 using namespace std;
00108
00109
00110 static void ascii_data_im_export_init (void);
00111 static void ascii_data_im_export_query (void);
00112 static void ascii_data_im_export_about (void);
00113 static void ascii_data_im_export_configure (void);
00114 static void ascii_data_im_export_cleanup (void);
00115
00116 static void ascii_data_im_export_filecheck_load_callback (gpointer data );
00117 static void ascii_data_im_export_filecheck_save_callback (gpointer data );
00118
00119 static void ascii_data_im_export_import_callback (GtkWidget *w, void *data);
00120 static void ascii_data_im_export_export_callback (GtkWidget *w, void *data);
00121
00122
00123 GxsmPlugin ascii_data_im_export_pi = {
00124 NULL,
00125 NULL,
00126 0,
00127 NULL,
00128
00129
00130 "Ascii_Data_Im_Export",
00131 NULL,
00132
00133 "Im/Export of the ASCII data file format.",
00134 "Percy Zahl",
00135 N_("_File/_Import/,_File/_Export/"),
00136 N_("ASCII,ASCII"),
00137 N_("ASCII import,ASCII export"),
00138 N_("ASCII data file import and export filter, using header for Reuben NSNOM."),
00139 NULL,
00140 NULL,
00141 ascii_data_im_export_init,
00142 ascii_data_im_export_query,
00143
00144
00145 ascii_data_im_export_about,
00146
00147
00148 ascii_data_im_export_configure,
00149
00150
00151 NULL,
00152
00153
00154 ascii_data_im_export_cleanup
00155 };
00156
00157
00158 static const char *about_text = N_("GXSM ASCII data file Import/Export Plugin\n\n");
00159
00160 static const char *file_mask = "*.asc";
00161
00162
00163
00164 GxsmPlugin *get_gxsm_plugin_info ( void ){
00165 ascii_data_im_export_pi.description = g_strdup_printf(N_("Gxsm im_export plugin %s"), VERSION);
00166 return &ascii_data_im_export_pi;
00167 }
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 static void ascii_data_im_export_query(void)
00178 {
00179 gchar **path = g_strsplit (ascii_data_im_export_pi.menupath, ",", 2);
00180 gchar **entry = g_strsplit (ascii_data_im_export_pi.menuentry, ",", 2);
00181 gchar **help = g_strsplit (ascii_data_im_export_pi.help, ",", 2);
00182
00183 static GnomeUIInfo menuinfo_i[] = {
00184 { GNOME_APP_UI_ITEM, NULL, NULL,
00185 NULL, NULL,
00186 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_OPEN,
00187 0, GDK_CONTROL_MASK, NULL },
00188 GNOMEUIINFO_END
00189 };
00190 menuinfo_i[0].label = entry[0];
00191 menuinfo_i[0].hint = help[0];
00192 menuinfo_i[0].moreinfo = (gpointer) ascii_data_im_export_import_callback;
00193 menuinfo_i[0].user_data = ascii_data_im_export_pi.name;
00194
00195 gnome_app_insert_menus (
00196 GNOME_APP(ascii_data_im_export_pi.app->getApp()),
00197 path[0],
00198 menuinfo_i
00199 );
00200
00201
00202 static GnomeUIInfo menuinfo_e[] = {
00203 { GNOME_APP_UI_ITEM, NULL, NULL,
00204 NULL, NULL,
00205 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_SAVE,
00206 0, GDK_CONTROL_MASK, NULL },
00207 GNOMEUIINFO_END
00208 };
00209 menuinfo_e[0].label = entry[1];
00210 menuinfo_e[0].hint = help[1];
00211 menuinfo_e[0].moreinfo = (gpointer) ascii_data_im_export_export_callback;
00212 menuinfo_e[0].user_data = ascii_data_im_export_pi.name;
00213
00214 gnome_app_insert_menus (
00215 GNOME_APP(ascii_data_im_export_pi.app->getApp()),
00216 path[1],
00217 menuinfo_e
00218 );
00219
00220 if(ascii_data_im_export_pi.status) g_free(ascii_data_im_export_pi.status);
00221 ascii_data_im_export_pi.status = g_strconcat (
00222 N_("Plugin query has attached "),
00223 ascii_data_im_export_pi.name,
00224 N_(": File IO Filters are ready to use."),
00225 NULL);
00226
00227
00228 g_strfreev (path);
00229 g_strfreev (entry);
00230 g_strfreev (help);
00231
00232
00233
00234
00235 ascii_data_im_export_pi.app->ConnectPluginToLoadFileEvent (ascii_data_im_export_filecheck_load_callback);
00236 ascii_data_im_export_pi.app->ConnectPluginToSaveFileEvent (ascii_data_im_export_filecheck_save_callback);
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246 static void ascii_data_im_export_init(void)
00247 {
00248 PI_DEBUG (DBG_L2, ascii_data_im_export_pi.name << "Plugin Init");
00249 }
00250
00251
00252 static void ascii_data_im_export_about(void)
00253 {
00254 const gchar *authors[] = { ascii_data_im_export_pi.authors, NULL};
00255 gtk_widget_show(gnome_about_new ( ascii_data_im_export_pi.name,
00256 VERSION,
00257 N_("(C) 2001 the Free Software Foundation"),
00258 about_text,
00259 authors,
00260 NULL, NULL, NULL
00261 ));
00262 }
00263
00264
00265 static void ascii_data_im_export_configure(void)
00266 {
00267 if(ascii_data_im_export_pi.app)
00268 ascii_data_im_export_pi.app->message("ascii_data_im_export Plugin Configuration");
00269 }
00270
00271
00272 static void ascii_data_im_export_cleanup(void)
00273 {
00274 gchar **path = g_strsplit (ascii_data_im_export_pi.menupath, ",", 2);
00275 gchar **entry = g_strsplit (ascii_data_im_export_pi.menuentry, ",", 2);
00276
00277 gchar *tmp = g_strconcat (path[0], entry[0], NULL);
00278 gnome_app_remove_menus (GNOME_APP (ascii_data_im_export_pi.app->getApp()), tmp, 1);
00279 g_free (tmp);
00280
00281 tmp = g_strconcat (path[1], entry[1], NULL);
00282 gnome_app_remove_menus (GNOME_APP (ascii_data_im_export_pi.app->getApp()), tmp, 1);
00283 g_free (tmp);
00284
00285 g_strfreev (path);
00286 g_strfreev (entry);
00287
00288 PI_DEBUG (DBG_L2, "Plugin Cleanup done.");
00289 }
00290
00291
00292 class ascii_ImExportFile : public Dataio{
00293 public:
00294 ascii_ImExportFile(Scan *s, const char *n) : Dataio(s,n){ };
00295 virtual FIO_STATUS Read();
00296 virtual FIO_STATUS Write();
00297 private:
00298 FIO_STATUS import(const char *fname);
00299 };
00300
00301 FIO_STATUS ascii_ImExportFile::Read(){
00302 FIO_STATUS ret;
00303 gchar *fname=NULL;
00304
00305 fname = (gchar*)name;
00306
00307
00308 if (fname == NULL || strlen(fname) < 4)
00309 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00310
00311
00312
00313 ifstream f;
00314 f.open(fname, ios::in);
00315 if(!f.good()){
00316 PI_DEBUG (DBG_L2, "Error at file open. File not good/readable.");
00317 return status=FIO_OPEN_ERR;
00318 }
00319 f.close();
00320
00321
00322 if ((ret=import (fname)) != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00323 return ret;
00324
00325 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00326 }
00327
00328 FIO_STATUS ascii_ImExportFile::import(const char *fname){
00329 double length = 1.;
00330 int columns = 1;
00331 int rows = 1;
00332 double dx = 1.;
00333 double dy = 1.;
00334 double x0 = 0.;
00335 double y0 = 0.;
00336 gchar line[0x4000];
00337
00338
00339 ifstream f;
00340 GString *FileList=NULL;
00341
00342 if (strncasecmp(fname+strlen(fname)-4,".asc",4)){
00343 f.close ();
00344 return status=FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00345 }
00346
00347 f.open(name, ios::in);
00348 if (!f.good())
00349 return status=FIO_OPEN_ERR;
00350
00351 FileList = g_string_new ("Imported by GXSM from ASCII/NSNOM-Reuben data file.\n");
00352 g_string_sprintfa (FileList, "Original Filename: %s\n", fname);
00353 g_string_append (FileList, "Orig. Header: \n");
00354
00355
00356 for (; f.good ();){
00357 f.getline (line, 0x4000);
00358
00359
00360 g_string_append (FileList, line);
00361 g_string_append (FileList, "\n");
00362
00363
00364
00365 if (strncmp (line, "scan length = ", 14) == 0)
00366 length = atof (&line[14]);
00367 if (strncmp (line, "points/line = ", 14) == 0)
00368 columns = atoi (&line[14]);
00369 if (strncmp (line, "# lines = ", 10) == 0)
00370 rows = atoi (&line[10]);
00371 if (strncmp (line, "point spacing = ", 16) == 0)
00372 dx = atof (&line[16]);
00373 if (strncmp (line, "line spacing = ", 15) == 0)
00374 dy = atof (&line[15]);
00375 if (strncmp (line, "X origin = ", 11) == 0)
00376 x0 = atof (&line[11]);
00377 if (strncmp (line, "Y origin = ", 11) == 0)
00378 y0 = atof (&line[11]);
00379
00380
00381 if (strncmp (line, "end", 3) == 0){
00382 f.getline (line, 0x4000);
00383 break;
00384 }
00385 }
00386
00387 time_t t;
00388 time(&t);
00389 gchar *tmp = g_strconcat ((ctime(&t)), " (Imported)", NULL); scan->data.ui.SetDateOfScan (tmp); g_free (tmp);
00390 scan->data.ui.SetName (fname);
00391 scan->data.ui.SetOriginalName (fname);
00392 scan->data.ui.SetType ("ASCII-NSNOM");
00393
00394
00395
00396 scan->data.s.ntimes = 1;
00397 scan->data.s.nvalues = 1;
00398
00399
00400
00401 if(getlogin()){
00402 scan->data.ui.SetUser (getlogin());
00403 }
00404 else{
00405 scan->data.ui.SetUser ("unkonwn user");
00406 }
00407
00408
00409 scan->data.s.nx = columns;
00410 scan->data.s.ny = rows;
00411 scan->data.s.dx = dx * 1e4;
00412 scan->data.s.dy = dy * 1e4;
00413 scan->data.s.dz = 1. * 1e4;
00414 scan->data.s.rx = length * 1e4;
00415 scan->data.s.ry = length * 1e4;
00416 scan->data.s.x0 = x0 * 1e4;
00417 scan->data.s.y0 = y0 * 1e4;
00418 scan->data.s.alpha = 0.;
00419
00420
00421 scan->data.display.cpshigh = 0.;
00422 scan->data.display.cpslow = 0.;
00423 scan->data.display.cnttime = 0.;
00424
00425
00426 scan->data.display.bright = 32.;
00427 scan->data.display.contrast = 1.0;
00428
00429
00430
00431
00432
00433
00434 UnitObj *u = gapp->xsm->MakeUnit ("um", "X");
00435 scan->data.SetXUnit(u);
00436 delete u;
00437
00438 u = gapp->xsm->MakeUnit ("um", "Y");
00439 scan->data.SetYUnit(u);
00440 delete u;
00441
00442 u = gapp->xsm->MakeUnit ("um", "Z");
00443 scan->data.SetZUnit(u);
00444 delete u;
00445
00446 scan->data.ui.SetComment (FileList->str);
00447 g_string_free(FileList, TRUE);
00448 FileList=NULL;
00449
00450
00451
00452
00453
00454 scan->mem2d->Resize (scan->data.s.nx, scan->data.s.ny, ZD_FLOAT);
00455
00456 for (int row=scan->mem2d->GetNy ()-1; row >= 0; --row){
00457 for (int col=0; col < scan->mem2d->GetNx (); ++col){
00458 double value = 0.;
00459 f >> value;
00460 scan->mem2d->PutDataPkt (value, col, row);
00461 if (! f.good ())
00462 break;
00463 }
00464 if (! f.good ())
00465 break;
00466 }
00467
00468 f.close ();
00469
00470 scan->data.orgmode = SCAN_ORG_CENTER;
00471 scan->mem2d->data->MkXLookup (-scan->data.s.rx/2., scan->data.s.rx/2.);
00472 scan->mem2d->data->MkYLookup (-scan->data.s.ry/2., scan->data.s.ry/2.);
00473
00474 return FIO_OK;
00475 }
00476
00477
00478 FIO_STATUS ascii_ImExportFile::Write(){
00479 GtkWidget *dialog = gtk_message_dialog_new (NULL,
00480 GTK_DIALOG_DESTROY_WITH_PARENT,
00481 GTK_MESSAGE_INFO,
00482 GTK_BUTTONS_OK,
00483 N_("Sorry, not yet implemented.")
00484 );
00485 gtk_dialog_run (GTK_DIALOG (dialog));
00486 gtk_widget_destroy (dialog);
00487
00488 #if 0
00489 gchar tmp[0x4004];
00490 const gchar *fname;
00491 ofstream f;
00492
00493 memset (tmp, 0, sizeof (tmp));
00494
00495 if(strlen(name)>0)
00496 fname = (const char*)name;
00497 else
00498 fname = gapp->file_dialog("File Export: ASCII"," ",file_mask,"","ASCII write");
00499 if (fname == NULL) return FIO_NO_NAME;
00500
00501
00502 if (strncmp(fname+strlen(fname)-4,".asc",4))
00503 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00504
00505
00506 f.open(name, ios::out);
00507 if (!f.good())
00508 return status=FIO_OPEN_ERR;
00509
00510
00511 #endif
00512 return FIO_OK;
00513 }
00514
00515
00516
00517
00518
00519
00520 static void ascii_data_im_export_filecheck_load_callback (gpointer data ){
00521 gchar **fn = (gchar**)data;
00522 if (*fn){
00523 PI_DEBUG (DBG_L2,
00524 "Check File: ascii_data_im_export_filecheck_load_callback called with >"
00525 << *fn << "<" );
00526
00527 Scan *dst = gapp->xsm->GetActiveScan();
00528 if(!dst){
00529 gapp->xsm->ActivateFreeChannel();
00530 dst = gapp->xsm->GetActiveScan();
00531 }
00532 ascii_ImExportFile fileobj (dst, *fn);
00533
00534 FIO_STATUS ret = fileobj.Read();
00535 if (ret != FIO_OK){
00536
00537 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00538 *fn=NULL;
00539
00540 gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE);
00541 PI_DEBUG (DBG_L2, "Read Error " << ((int)ret) << "!!!!!!!!" );
00542 }else{
00543
00544 *fn=NULL;
00545
00546
00547 gapp->xsm->ActiveScan->GetDataSet(gapp->xsm->data);
00548 gapp->spm_update_all();
00549 dst->draw();
00550 }
00551 }else{
00552 PI_DEBUG (DBG_L2, "ascii_data_im_export_filecheck_load: Skipping" );
00553 }
00554 }
00555
00556 static void ascii_data_im_export_filecheck_save_callback (gpointer data ){
00557 gchar **fn = (gchar**)data;
00558 if (*fn){
00559 Scan *src;
00560 PI_DEBUG (DBG_L2,
00561 "Check File: ascii_data_im_export_filecheck_save_callback called with >"
00562 << *fn << "<" );
00563
00564 ascii_ImExportFile fileobj (src = gapp->xsm->GetActiveScan(), *fn);
00565
00566 FIO_STATUS ret;
00567 ret = fileobj.Write();
00568
00569 if(ret != FIO_OK){
00570
00571 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00572 *fn=NULL;
00573 PI_DEBUG (DBG_L2, "Write Error " << ((int)ret) << "!!!!!!!!" );
00574 }else{
00575
00576 *fn=NULL;
00577 }
00578 }else{
00579 PI_DEBUG (DBG_L2, "ascii_data_im_export_filecheck_save: Skipping >" << *fn << "<" );
00580 }
00581 }
00582
00583
00584
00585 static void ascii_data_im_export_import_callback(GtkWidget *w, void *data){
00586 gchar **help = g_strsplit (ascii_data_im_export_pi.help, ",", 2);
00587 gchar *dlgid = g_strconcat (ascii_data_im_export_pi.name, "-import", NULL);
00588 gchar *fn = gapp->file_dialog(help[0], NULL, file_mask, NULL, dlgid);
00589 g_strfreev (help);
00590 g_free (dlgid);
00591 ascii_data_im_export_filecheck_load_callback (&fn );
00592 }
00593
00594 static void ascii_data_im_export_export_callback(GtkWidget *w, void *data){
00595 gchar **help = g_strsplit (ascii_data_im_export_pi.help, ",", 2);
00596 gchar *dlgid = g_strconcat (ascii_data_im_export_pi.name, "-export", NULL);
00597 gchar *fn = gapp->file_dialog(help[1], NULL, file_mask, NULL, dlgid);
00598 g_strfreev (help);
00599 g_free (dlgid);
00600 ascii_data_im_export_filecheck_save_callback (&fn );
00601 }