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 #include <gtk/gtk.h>
00058 #include "config.h"
00059 #include "gxsm/plugin.h"
00060 #include "gxsm/dataio.h"
00061 #include "gxsm/action_id.h"
00062 #include "gxsm/util.h"
00063 #include "gxsm/xsmtypes.h"
00064
00065
00066
00067 #include "batch.h"
00068 #include "g_dat_types.h"
00069 #include "fileio.c"
00070
00071
00072
00073 using namespace std;
00074
00075
00076 static void g_dat_im_export_init (void);
00077 static void g_dat_im_export_query (void);
00078 static void g_dat_im_export_about (void);
00079 static void g_dat_im_export_configure (void);
00080 static void g_dat_im_export_cleanup (void);
00081
00082 static void g_dat_im_export_filecheck_load_callback (gpointer data );
00083 static void g_dat_im_export_filecheck_save_callback (gpointer data );
00084
00085 static void g_dat_im_export_import_callback (GtkWidget *w, void *data);
00086 static void g_dat_im_export_export_callback (GtkWidget *w, void *data);
00087
00088
00089 GxsmPlugin g_dat_im_export_pi = {
00090 NULL,
00091 NULL,
00092 0,
00093 NULL,
00094
00095
00096
00097 "G-dat-ImExport",
00098 NULL,
00099
00100 "Im/Export of the old (G-) dat file format.",
00101 "Percy Zahl",
00102 N_("_File/_Import/,_File/_Export/"),
00103 N_("G-dat,G-dat"),
00104 N_("Old 'dat' file import,Old 'dat' file export"),
00105 N_("Old 'dat' file import and export filter."),
00106
00107 NULL,
00108 NULL,
00109 g_dat_im_export_init,
00110 g_dat_im_export_query,
00111
00112
00113 g_dat_im_export_about,
00114
00115
00116 g_dat_im_export_configure,
00117
00118
00119 NULL,
00120
00121
00122 g_dat_im_export_cleanup
00123 };
00124
00125
00126 static const char *about_text = N_("GXSM 'G-dat' plugin for im-/export of old the 'dat' data file type.\n"
00127 "This format was used in Hannover in early times and can also be used "
00128 "with the 'gnutools'. This apps were using this format: PMSTM and older "
00129 "very first versions of XXSM and older GXSM versions were able to use it "
00130 "instead of NetCDF if configured this way.\n\n"
00131 "Now this filter is here to visit/manipulate old data files."
00132 );
00133
00134 static const char *file_mask = "*.dat";
00135
00136
00137
00138 GxsmPlugin *get_gxsm_plugin_info ( void ){
00139 g_dat_im_export_pi.description = g_strdup_printf(N_("Gxsm im_export plugin %s"), VERSION);
00140 return &g_dat_im_export_pi;
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 static void g_dat_im_export_query(void)
00152 {
00153 gchar **path = g_strsplit (g_dat_im_export_pi.menupath, ",", 2);
00154 gchar **entry = g_strsplit (g_dat_im_export_pi.menuentry, ",", 2);
00155 gchar **help = g_strsplit (g_dat_im_export_pi.help, ",", 2);
00156
00157 static GnomeUIInfo menuinfo_i[] = {
00158 { GNOME_APP_UI_ITEM, NULL, NULL,
00159 NULL, NULL,
00160 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_OPEN,
00161 0, GDK_CONTROL_MASK, NULL },
00162 GNOMEUIINFO_END
00163 };
00164 menuinfo_i[0].label = entry[0];
00165 menuinfo_i[0].hint = help[0];
00166 menuinfo_i[0].moreinfo = (gpointer) g_dat_im_export_import_callback;
00167 menuinfo_i[0].user_data = g_dat_im_export_pi.name;
00168
00169 gnome_app_insert_menus (
00170 GNOME_APP(g_dat_im_export_pi.app->getApp()),
00171 path[0],
00172 menuinfo_i
00173 );
00174
00175
00176 static GnomeUIInfo menuinfo_e[] = {
00177 { GNOME_APP_UI_ITEM, NULL, NULL,
00178 NULL, NULL,
00179 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_SAVE,
00180 0, GDK_CONTROL_MASK, NULL },
00181 GNOMEUIINFO_END
00182 };
00183 menuinfo_e[0].label = entry[1];
00184 menuinfo_e[0].hint = help[1];
00185 menuinfo_e[0].moreinfo = (gpointer) g_dat_im_export_export_callback;
00186 menuinfo_e[0].user_data = g_dat_im_export_pi.name;
00187
00188 gnome_app_insert_menus (
00189 GNOME_APP(g_dat_im_export_pi.app->getApp()),
00190 path[1],
00191 menuinfo_e
00192 );
00193
00194 if(g_dat_im_export_pi.status) g_free(g_dat_im_export_pi.status);
00195 g_dat_im_export_pi.status = g_strconcat (
00196 N_("Plugin query has attached "),
00197 g_dat_im_export_pi.name,
00198 N_(": File IO Filters are ready to use."),
00199 NULL);
00200
00201
00202 g_strfreev (path);
00203 g_strfreev (entry);
00204 g_strfreev (help);
00205
00206
00207
00208
00209 g_dat_im_export_pi.app->ConnectPluginToLoadFileEvent (g_dat_im_export_filecheck_load_callback);
00210 g_dat_im_export_pi.app->ConnectPluginToSaveFileEvent (g_dat_im_export_filecheck_save_callback);
00211 }
00212
00213
00214
00215
00216
00217
00218
00219
00220 static void g_dat_im_export_init(void)
00221 {
00222 PI_DEBUG (DBG_L2, g_dat_im_export_pi.name << " Plugin Init");
00223 }
00224
00225
00226 static void g_dat_im_export_about(void)
00227 {
00228 const gchar *authors[] = { g_dat_im_export_pi.authors, NULL};
00229 gtk_widget_show(gnome_about_new ( g_dat_im_export_pi.name,
00230 VERSION,
00231 N_("(C) 2001 the Free Software Foundation"),
00232 about_text,
00233 authors,
00234 NULL, NULL, NULL
00235 ));
00236 }
00237
00238
00239 static void g_dat_im_export_configure(void)
00240 {
00241 if(g_dat_im_export_pi.app)
00242 g_dat_im_export_pi.app->message("im_export Plugin Configuration");
00243 }
00244
00245
00246 static void g_dat_im_export_cleanup(void)
00247 {
00248 gchar **path = g_strsplit (g_dat_im_export_pi.menupath, ",", 2);
00249 gchar **entry = g_strsplit (g_dat_im_export_pi.menuentry, ",", 2);
00250
00251 gchar *tmp = g_strconcat (path[0], entry[0], NULL);
00252 gnome_app_remove_menus (GNOME_APP (g_dat_im_export_pi.app->getApp()), tmp, 1);
00253 g_free (tmp);
00254
00255 tmp = g_strconcat (path[1], entry[1], NULL);
00256 gnome_app_remove_menus (GNOME_APP (g_dat_im_export_pi.app->getApp()), tmp, 1);
00257 g_free (tmp);
00258
00259 g_strfreev (path);
00260 g_strfreev (entry);
00261
00262 PI_DEBUG (DBG_L2, "Plugin Cleanup done.");
00263 }
00264
00265
00266 class Gdat_ImExportFile : public Dataio{
00267 public:
00268 Gdat_ImExportFile(Scan *s, const char *n) : Dataio(s,n){ memset(&Kopf, 0, sizeof(Kopf)); };
00269 virtual FIO_STATUS Read();
00270 virtual FIO_STATUS Write();
00271 private:
00272 FIO_STATUS import_data(const char *fname);
00273 HEADER Kopf;
00274 };
00275
00276 FIO_STATUS Gdat_ImExportFile::Read(){
00277 FIO_STATUS ret;
00278 gchar *fname=NULL;
00279
00280 PI_DEBUG (DBG_L2, "reading");
00281
00282 fname = (gchar*)name;
00283
00284
00285 if (fname == NULL || strlen(fname) < 4)
00286 return status=FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00287
00288
00289
00290 ifstream f;
00291 f.open(fname, ios::in);
00292 if(!f.good()){
00293 PI_DEBUG (DBG_L2, "Error at file open. File not good/readable.");
00294 return status=FIO_OPEN_ERR;
00295 }
00296 f.close();
00297
00298
00299 if ((ret=import_data (fname)) != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00300 return ret;
00301
00302 return status=FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00303 }
00304
00305
00306 double Contrast_to_VRangeZ (double contrast, double dz){
00307 return 64.*dz/contrast;
00308 }
00309 double VRangeZ_to_Contrast (double vrz, double dz){
00310 return 64.*dz/vrz;
00311 }
00312
00313 FIO_STATUS Gdat_ImExportFile::import_data(const char *fname){
00314 gboolean byteswap = FALSE;
00315 GtkWidget *dialog;
00316 int pcnt = 0;
00317
00318
00319
00320 ifstream f;
00321 GString *FileList=NULL;
00322
00323 PI_DEBUG (DBG_L2, "importing from >" << fname << "<");
00324
00325 f.open(fname, ios::in);
00326 if (!f.good())
00327 return status=FIO_OPEN_ERR;
00328
00329
00330 SCAN_DATA scan_template;
00331
00332 scan->data.GetScan_Param (scan_template);
00333
00334 scan->data.GetUser_Info (scan_template);
00335
00336 scan->data.GetDisplay_Param (scan_template);
00337
00338
00339 f.seekg(0, ios::beg);
00340 f.read((char*)&Kopf, sizeof(HEADER));
00341
00342 PI_DEBUG (DBG_L2, "Checking 'Kopf.Kennung'");
00343 switch(Kopf.Kennung){
00344 case 0xABCE: PI_DEBUG (DBG_L2, "Neues Dat Format detected!"); break;
00345 case 0xABCD: PI_DEBUG (DBG_L2, "Altes Dat Format (PMSTM) detected!"); break;
00346 case 0xCEAB:
00347 PI_DEBUG (DBG_L2, "Neues Dat Format [LE on BE] detected!\n"
00348 << "-- But sorry, wrong endianess detected, I'm not handling this yet. --");
00349 dialog = gtk_message_dialog_new (NULL,
00350 GTK_DIALOG_DESTROY_WITH_PARENT,
00351 GTK_MESSAGE_INFO,
00352 GTK_BUTTONS_OK,
00353 N_("New 'dat' format [LE on BE] detected!\n"
00354 "But sorry, wrong endianess detected, "
00355 "I'm not handling this yet."));
00356 gtk_dialog_run (GTK_DIALOG (dialog));
00357 gtk_widget_destroy (dialog);
00358
00359 byteswap = TRUE;
00360 f.close ();
00361 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00362 break;
00363 case 0xCDAB:
00364 PI_DEBUG (DBG_L2, "Altes Dat Format (PMSTM) [LE on BE] detected!\n"
00365 << "-- But sorry, wrong endianess detected, I'm not handling this yet. --");
00366 dialog = gtk_message_dialog_new (NULL,
00367 GTK_DIALOG_DESTROY_WITH_PARENT,
00368 GTK_MESSAGE_INFO,
00369 GTK_BUTTONS_OK,
00370 N_("New 'dat' format [LE on BE] detected!\n"
00371 "But sorry, wrong endianess detected, "
00372 "I'm not handling this yet."));
00373 gtk_dialog_run (GTK_DIALOG (dialog));
00374 gtk_widget_destroy (dialog);
00375
00376 byteswap = TRUE;
00377 f.close ();
00378 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00379 break;
00380 case 0:
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390 PI_DEBUG (DBG_L2, "Malformed dat format Header detected!");
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 f.close ();
00401 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00402 break;
00403 default:
00404 f.close ();
00405 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00406 break;
00407 }
00408
00409
00410 time_t t;
00411 time(&t);
00412 gchar *tmp = g_strconcat ((ctime(&t)), " (Imported)", NULL); scan->data.ui.SetDateOfScan (tmp); g_free (tmp);
00413 scan->data.ui.SetName (name);
00414 scan->data.ui.SetOriginalName (name);
00415 scan->data.ui.SetType ("old G-dat");
00416 scan->data.ui.SetUser (Kopf.UserName);
00417
00418 FileList = g_string_new ("Imported by GXSM from old dat filetype.\n");
00419 g_string_sprintfa (FileList, "Original Filename: %s\n", name);
00420 g_string_append (FileList, "Original Kopf.comment:\n");
00421 g_string_append (FileList, Kopf.comment);
00422 scan->data.ui.SetComment (FileList->str);
00423 g_string_free(FileList, TRUE);
00424 FileList=NULL;
00425
00426 scan->data.s.nx = Kopf.nx[pcnt];
00427 scan->data.s.ny = Kopf.ny[pcnt];
00428 scan->data.s.dx = Kopf.dx[pcnt]*Kopf.DAtoAng_X;
00429 scan->data.s.dy = Kopf.dy[pcnt]*Kopf.DAtoAng_Y;
00430 scan->data.s.dz = Kopf.DAtoAng_Z;
00431 scan->data.s.rx = scan->data.s.nx*scan->data.s.dx;
00432 scan->data.s.ry = scan->data.s.ny*scan->data.s.dy;
00433 scan->data.s.x0 = Kopf.x_Offset[pcnt]*Kopf.DAtoAng_X;
00434 scan->data.s.y0 = Kopf.y_Offset[pcnt]*Kopf.DAtoAng_Y;
00435 scan->data.s.alpha = (double)Kopf.Rotation;
00436 scan->data.display.bright = 32.;
00437 scan->data.display.vrange_z = Contrast_to_VRangeZ (Kopf.greyfac, scan->data.s.dz);
00438 scan->data.display.voffset_z = 0.;
00439
00440 scan->mem2d->Resize (scan->data.s.nx, scan->data.s.ny);
00441 scan->mem2d->DataRead (f);
00442 scan->mem2d->data->MkXLookup (-scan->data.s.rx/2., scan->data.s.rx/2.);
00443 scan->mem2d->data->MkYLookup (-scan->data.s.ry/2., scan->data.s.ry/2.);
00444
00445 if (f.fail ()){
00446 f.close ();
00447 return status=FIO_READ_ERR;
00448 }
00449 f.close();
00450 return status=FIO_OK;
00451 }
00452
00453 FIO_STATUS Gdat_ImExportFile::Write(){
00454 int pcnt=0;
00455 ofstream f;
00456
00457 PI_DEBUG (DBG_L2, "writing >" << name << "<");
00458
00459 if (name == NULL) return FIO_NO_NAME;
00460
00461 f.open(name, ios::out);
00462 if (!f.good())
00463 return status=FIO_OPEN_ERR;
00464
00465
00466 f.seekp(0, ios::beg);
00467
00468
00469 scan->data.ui.SetName (name);
00470 Kopf.ScMode = TopoGraphic;
00471 Kopf.Kennung = 0xABCE;
00472 if(strlen(scan->data.ui.user) > 39)
00473 XSM_SHOW_ALERT(HINT_WARN, HINT_UNAME_TRUNC, " ", 0);
00474 if(strlen(scan->data.ui.comment) > 255)
00475 XSM_SHOW_ALERT(HINT_WARN, HINT_COMMENT_TRUNC, " ", 0);
00476 strncpy(Kopf.UserName, scan->data.ui.user, 39);
00477 strncpy(Kopf.comment, scan->data.ui.comment, 255);
00478 Kopf.nx[pcnt] = scan->data.s.nx;
00479 Kopf.ny[pcnt] = scan->data.s.ny;
00480
00481 Kopf.dx[pcnt] = R2INT(scan->data.s.dx/gapp->xsm->Inst->XResolution());
00482 Kopf.dy[pcnt] = R2INT(scan->data.s.dy/gapp->xsm->Inst->YResolution());
00483 Kopf.x_Offset[pcnt] = R2INT(scan->data.s.x0/gapp->xsm->Inst->XResolution());
00484 Kopf.y_Offset[pcnt] = R2INT(scan->data.s.y0/gapp->xsm->Inst->XResolution());
00485 Kopf.DAtoAng_X = gapp->xsm->Inst->XResolution();
00486 Kopf.DAtoAng_Y = gapp->xsm->Inst->YResolution();
00487 Kopf.DAtoAng_Z = gapp->xsm->Inst->ZResolution();
00488 Kopf.Rotation = (short)(rint(scan->data.s.alpha));
00489 Kopf.brightfac = scan->data.display.bright;
00490 Kopf.greyfac = VRangeZ_to_Contrast (scan->data.display.vrange_z, scan->data.s.dz);
00491
00492 Kopf.forw_delay=10L;
00493 Kopf.back_delay=10L;
00494 Kopf.NumOfTopAve=1;
00495
00496
00497 f.write((const char*)&Kopf, sizeof(HEADER));
00498 if(f.fail()){
00499 f.close();
00500 return status=FIO_WRITE_ERR;
00501 }
00502
00503 scan->mem2d->DataWrite(f);
00504
00505 if(f.fail()){
00506 f.close();
00507 return status=FIO_WRITE_ERR;
00508 }
00509 f.close();
00510 return status=FIO_OK;
00511 }
00512
00513
00514
00515
00516
00517
00518 static void g_dat_im_export_filecheck_load_callback (gpointer data ){
00519 gchar **fn = (gchar**)data;
00520 if (*fn){
00521 PI_DEBUG (DBG_L2, "checking >" << *fn << "<" );
00522
00523 Scan *dst = gapp->xsm->GetActiveScan();
00524 if(!dst){
00525 gapp->xsm->ActivateFreeChannel();
00526 dst = gapp->xsm->GetActiveScan();
00527 }
00528 Gdat_ImExportFile fileobj (dst, *fn);
00529
00530 FIO_STATUS ret = fileobj.Read();
00531 if (ret != FIO_OK){
00532
00533 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00534 *fn=NULL;
00535
00536 gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE);
00537 PI_DEBUG (DBG_L2, "Read Error " << ((int)ret) );
00538 }else{
00539
00540 *fn=NULL;
00541
00542
00543 gapp->xsm->ActiveScan->GetDataSet(gapp->xsm->data);
00544 gapp->spm_update_all();
00545 dst->draw();
00546 }
00547 }else{
00548 PI_DEBUG (DBG_L2, "Skipping" << *fn << "<" );
00549 }
00550 }
00551
00552 static void g_dat_im_export_filecheck_save_callback (gpointer data ){
00553 gchar **fn = (gchar**)data;
00554 if (*fn){
00555 Scan *src;
00556 PI_DEBUG (DBG_L2, "Saving/(checking) >" << *fn << "<" );
00557
00558 Gdat_ImExportFile fileobj (src = gapp->xsm->GetActiveScan(), *fn);
00559
00560 FIO_STATUS ret;
00561 ret = fileobj.Write();
00562
00563 if(ret != FIO_OK){
00564
00565 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00566 *fn=NULL;
00567 PI_DEBUG (DBG_L2, "Write Error " << ((int)ret) );
00568 }else{
00569
00570 *fn=NULL;
00571 }
00572 }else{
00573 PI_DEBUG (DBG_L2, "Skipping >" << *fn << "<" );
00574 }
00575 }
00576
00577
00578
00579 static void g_dat_im_export_import_callback(GtkWidget *w, void *data){
00580 gchar **help = g_strsplit (g_dat_im_export_pi.help, ",", 2);
00581 gchar *dlgid = g_strconcat (g_dat_im_export_pi.name, "-import", NULL);
00582 gchar *fn = gapp->file_dialog(help[0], NULL, file_mask, NULL, dlgid);
00583 g_strfreev (help);
00584 g_free (dlgid);
00585 g_dat_im_export_filecheck_load_callback (&fn );
00586 }
00587
00588 static void g_dat_im_export_export_callback(GtkWidget *w, void *data){
00589 gchar **help = g_strsplit (g_dat_im_export_pi.help, ",", 2);
00590 gchar *dlgid = g_strconcat (g_dat_im_export_pi.name, "-export", NULL);
00591 gchar *fn = gapp->file_dialog(help[1], NULL, file_mask, NULL, dlgid);
00592 g_strfreev (help);
00593 g_free (dlgid);
00594 g_dat_im_export_filecheck_save_callback (&fn );
00595 }