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 #include <gtk/gtk.h>
00065 #include "config.h"
00066 #include "gxsm/plugin.h"
00067 #include "gxsm/dataio.h"
00068 #include "gxsm/action_id.h"
00069 #include "gxsm/util.h"
00070
00071 #ifndef WORDS_BIGENDIAN
00072 #define WORDS_BIGENDIAN 0
00073 #endif
00074
00075 using namespace std;
00076
00077
00078
00079 static void UK2k_import_init (void);
00080 static void UK2k_import_query (void);
00081 static void UK2k_import_about (void);
00082 static void UK2k_import_configure (void);
00083 static void UK2k_import_cleanup (void);
00084
00085 static void UK2k_import_filecheck_load_callback (gpointer data );
00086
00087
00088 static void UK2k_import_import_callback (GtkWidget *w, void *data);
00089
00090
00091
00092 GxsmPlugin UK2k_import_pi = {
00093 NULL,
00094 NULL,
00095 0,
00096 NULL,
00097
00098
00099 "UK2k_import",
00100 NULL,
00101
00102 "Import of UK2k data format.",
00103 "Juan de la Figuera",
00104 N_("File/Import"),
00105 N_("..."),
00106 NULL,
00107 "This plugin is respnsible for reading UK2000 v3.4 images, so far no spectroscopy, and only one image",
00108 NULL,
00109 NULL,
00110 UK2k_import_init,
00111 UK2k_import_query,
00112
00113
00114 UK2k_import_about,
00115
00116
00117 UK2k_import_configure,
00118
00119
00120 NULL,
00121
00122
00123 UK2k_import_cleanup
00124 };
00125
00126
00127 static const char *about_text = N_("Gxsm UK2k Data File Import Plugin\n\n"
00128 "This plugin reads in UK2k datafiles."
00129 );
00130
00131
00132
00133 GxsmPlugin *get_gxsm_plugin_info ( void ){
00134 UK2k_import_pi.description = g_strdup_printf(N_("Gxsm UK2k_import plugin %s"), VERSION);
00135 return &UK2k_import_pi;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 static void UK2k_import_query(void)
00147 {
00148 static GnomeUIInfo menuinfo_Import[] = {
00149 { GNOME_APP_UI_ITEM,
00150 N_("UK2k Import"), N_("Import of the UK2k format"),
00151 (gpointer) UK2k_import_import_callback, NULL,
00152 NULL, GNOME_APP_PIXMAP_STOCK, GNOME_STOCK_MENU_OPEN,
00153 0, GDK_CONTROL_MASK, NULL },
00154 GNOMEUIINFO_END
00155 };
00156
00157 gnome_app_insert_menus (
00158 GNOME_APP(UK2k_import_pi.app->getApp()),
00159 N_("File/Import/"),
00160 menuinfo_Import
00161 );
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 if(UK2k_import_pi.status) g_free(UK2k_import_pi.status);
00179 UK2k_import_pi.status = g_strconcat (
00180 N_("Plugin query has attached "),
00181 UK2k_import_pi.name,
00182 N_(": File IO Filters are ready to use"),
00183 NULL);
00184
00185
00186
00187
00188 UK2k_import_pi.app->ConnectPluginToLoadFileEvent (UK2k_import_filecheck_load_callback);
00189
00190 }
00191
00192
00193
00194
00195
00196
00197
00198
00199 static void UK2k_import_init(void)
00200 {
00201 PI_DEBUG (DBG_L2, "UK2k_import Plugin Init" );
00202 }
00203
00204
00205 static void UK2k_import_about(void)
00206 {
00207 const gchar *authors[] = { UK2k_import_pi.authors, NULL};
00208 gtk_widget_show(gnome_about_new ( UK2k_import_pi.name,
00209 VERSION,
00210 N_("(C) 2001 the Free Software Foundation"),
00211 about_text,
00212 authors,
00213 NULL, NULL, NULL
00214 ));
00215 }
00216
00217
00218 static void UK2k_import_configure(void)
00219 {
00220 if(UK2k_import_pi.app)
00221 UK2k_import_pi.app->message("UK2k_import Plugin Configuration");
00222 }
00223
00224
00225 static void UK2k_import_cleanup(void)
00226 {
00227 PI_DEBUG (DBG_L2, "UK2k_import Plugin Cleanup" );
00228 gnome_app_remove_menus (GNOME_APP (UK2k_import_pi.app->getApp()),
00229 N_("File/Import/UK2k Import"), 1);
00230
00231
00232
00233 }
00234
00235
00236 class UK2k_ImExportFile : public Dataio{
00237 public:
00238 UK2k_ImExportFile(Scan *s, const char *n) : Dataio(s,n){ };
00239 virtual FIO_STATUS Read();
00240 virtual FIO_STATUS Write();
00241 private:
00242
00243 FIO_STATUS spmReadDat(const char *fname);
00244
00245 FIO_STATUS spmReadPar(const gchar *fname, const gchar *fsuffix);
00246 };
00247
00248
00249 FIO_STATUS UK2k_ImExportFile::Read(){
00250
00251 FIO_STATUS ret = FIO_OK;
00252 gchar *fdatname = (gchar*) name;
00253 gchar *fparname=NULL;
00254 gchar *fbasename=NULL;
00255 gchar *fsuffix=NULL;
00256
00257
00258 if (fdatname == NULL || strlen(fdatname) < 4) {
00259 PI_DEBUG (DBG_L2, "UK2k: problem with filename lenght " << strlen(fdatname) );
00260 return FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00261 }
00262
00263
00264
00265
00266 fbasename = g_strndup (name, strlen(name)-4);
00267 fsuffix = g_strdup (name+strlen(name)-4);
00268 fparname = g_strconcat (fbasename, ".stp", NULL);
00269
00270 if (!strncmp(fsuffix,".STD",4))
00271 fparname= g_strconcat(fbasename,".STP",NULL);
00272
00273
00274 if ( strncmp(fsuffix,".STD", 4) && strncmp(fsuffix,".std", 4) ) {
00275 PI_DEBUG (DBG_L2, "UK2k: problem with extension " << fsuffix );
00276 ret = FIO_NOT_RESPONSIBLE_FOR_THAT_FILE;
00277 }
00278 else{
00279
00280
00281 std::ifstream f;
00282 f.open(fdatname, std::ios::in | std::ios::binary);
00283 if(!f.good()){
00284 PI_DEBUG (DBG_L2, "UK2k_ImExportFile::Read: data file error" );
00285 ret = status = FIO_OPEN_ERR;
00286 }
00287 f.close();
00288 f.open(fparname, std::ios::in);
00289 if(!f.good()){
00290 PI_DEBUG (DBG_L2, "Uk2k_ImExportFile::Read: parameter file error" );
00291 ret = status = FIO_OPEN_ERR;
00292 }
00293 f.close();
00294
00295 PI_DEBUG (DBG_L2, "UK2k_ImExportFile::Read: " << fdatname <<
00296 " is a UK2000 File!" );
00297
00298
00299
00300 if ( ret == FIO_OK )
00301 ret = spmReadPar (fparname, fsuffix);
00302
00303
00304 if ( ret == FIO_OK ) {
00305 PI_DEBUG (DBG_L2, "UK2k: Read parameter file OK!" );
00306 if ( scan->data.s.nvalues == 1 )
00307 ret = spmReadDat (fdatname);
00308 }
00309 }
00310
00311 g_free (fbasename);
00312 g_free (fparname);
00313 g_free (fsuffix);
00314
00315 scan->mem2d->data->MkYLookup(scan->data.s.y0, scan->data.s.y0+scan->data.s.ry);
00316 scan->mem2d->data->MkXLookup(scan->data.s.x0-scan->data.s.rx/2., scan->data.s.x0+scan->data.s.rx/2.);
00317 scan->mem2d->data->MkVLookup (-10., 10.);
00318
00319 return ret;
00320 }
00321
00322
00323 FIO_STATUS UK2k_ImExportFile::spmReadDat(const gchar *fname)
00324 {
00325
00326 scan->data.ui.SetName (fname);
00327 scan->data.ui.SetOriginalName (fname);
00328
00329
00330 scan->mem2d->Resize (
00331 scan->data.s.nx,
00332 scan->data.s.ny,
00333 ZD_SHORT
00334 );
00335
00336 PI_DEBUG (DBG_L2, "UK2k_ImExportFile: Resize done." );
00337
00338
00339
00340 short *buf;
00341 if(!(buf = g_new(short, scan->data.s.nx*scan->data.s.ny)))
00342 return FIO_NO_MEM;
00343
00344
00345 ifstream f;
00346 f.open(fname, ios::in | ios::binary);
00347 if(f.good())
00348 f.read((char*)buf, sizeof(short) * scan->data.s.nx * scan->data.s.ny);
00349 else {
00350 g_free(buf);
00351 return FIO_OPEN_ERR;
00352 }
00353 f.close();
00354
00355
00356 short *pt = buf;
00357 char *cpt, low;
00358 for (int j=0; j < scan->data.s.ny; ++j)
00359 for (int i=0; i < scan->data.s.nx; ++i, ++pt) {
00360
00361 if (WORDS_BIGENDIAN) {
00362 cpt = (char*)pt;
00363 low = cpt[0];
00364 cpt[0] = cpt[1];
00365 cpt[1] = low;
00366 }
00367 if (*pt>0) {*pt-=32767;}
00368 else { *pt+=32767;}
00369
00370 scan->mem2d->PutDataPkt ((double)*pt, i, scan->data.s.ny-j-1);
00371 }
00372
00373 g_free (buf);
00374
00375 PI_DEBUG (DBG_L2, "UK2k_ImExportFile: Import done." );
00376
00377
00378 return FIO_OK;
00379 }
00380
00381
00382 FIO_STATUS UK2k_ImExportFile::spmReadPar(const gchar *fname, const gchar *fsuffix)
00383 {
00384 char valid = FALSE;
00385 char idate[80],itime[80];
00386
00387
00388
00389
00390 scan->data.s.ntimes = 1;
00391 scan->data.s.nvalues = 1;
00392
00393
00394 if(getlogin()){
00395 scan->data.ui.SetUser (getlogin());
00396 }
00397 else{
00398 scan->data.ui.SetUser ("unkonwn user");
00399 }
00400
00401
00402 scan->data.ui.SetType ("UK2000 v3.4 Type: SHT ");
00403
00404 gchar *comm = g_strconcat ("Imported by UK2k import plugin from: ",
00405 fname,
00406 NULL);
00407 scan->data.ui.SetComment (comm);
00408 g_free (comm);
00409 PI_DEBUG (DBG_L2, " comment: " << scan->data.ui.comment );
00410
00411
00412 scan->data.s.dx = 1;
00413 scan->data.s.dy = 1;
00414 scan->data.s.dz = 1;
00415 scan->data.s.rx = scan->data.s.nx;
00416 scan->data.s.ry = scan->data.s.ny;
00417 scan->data.s.x0 = 0;
00418 scan->data.s.y0 = 0;
00419 scan->data.s.alpha = 0;
00420
00421
00422 scan->data.display.cpshigh = 1e3;
00423 scan->data.display.cpslow = 1.;
00424 scan->data.display.cnttime = 1.;
00425
00426
00427
00428
00429
00430 std::ifstream f;
00431 f.open(fname, std::ios::in);
00432 if(!f.good())
00433 return FIO_OPEN_ERR;
00434
00435
00436
00437 gchar linebuf[100];
00438 while (!f.eof()) {
00439
00440 f.getline (linebuf, 100);
00441
00442
00443 if (!strncmp (linebuf, "software Uscan2000 3.4",24)) {
00444 valid=TRUE;
00445 }
00446
00447 if (!strncmp (linebuf, "date",4)) {
00448 sscanf (linebuf, "%*[^0-9]%80s", idate);
00449 PI_DEBUG (DBG_L2, "UK2k date:" << idate );
00450 }
00451
00452 if (!strncmp (linebuf, "time",4)) {
00453 sscanf (linebuf, "%*[^0-9]%80s", itime);
00454 PI_DEBUG (DBG_L2, "UK2k time: " << itime );
00455 }
00456
00457
00458
00459 if ( !strncmp (linebuf, "length_x", 8) ) {
00460 double rx;
00461 sscanf (linebuf, "%*[^0-9]%lf", &rx);
00462 scan->data.s.rx = rx;
00463 PI_DEBUG (DBG_L2, "UK2k rx = " << scan->data.s.rx );
00464 }
00465
00466
00467 if ( !strncmp (linebuf, "length_y", 8) ) {
00468 double ry;
00469 sscanf (linebuf, "%*[^0-9]%lf", &ry);
00470 scan->data.s.ry = ry;
00471 PI_DEBUG (DBG_L2, "UK2k ry = " << scan->data.s.ry );
00472 }
00473
00474
00475 if ( !strncmp (linebuf, "samples_x", 9) ) {
00476 int nx;
00477 sscanf (linebuf, "%*[^0-9]%d", &nx);
00478 scan->data.s.nx = nx;
00479 PI_DEBUG (DBG_L2, "UK2k nx = " << scan->data.s.nx );
00480 }
00481
00482
00483 if ( !strncmp (linebuf, "samples_y", 9) ) {
00484 int ny;
00485 sscanf (linebuf, "%*[^0-9]%d", &ny);
00486 scan->data.s.ny = ny;
00487 PI_DEBUG (DBG_L2, "UK2k ny = " << scan->data.s.ny );
00488 }
00489
00490
00491
00492 if ( !strncmp (linebuf, "scan_rot", 8) ) {
00493 double alf;
00494 sscanf (linebuf, "%*[^0-9-]%lf", &alf);
00495 scan->data.s.alpha = alf;
00496 PI_DEBUG (DBG_L2, "UK2k alpha = " << scan->data.s.alpha );
00497 }
00498
00499
00500 if ( !strncmp (linebuf, "start_x", 7) ) {
00501 double x0;
00502 sscanf (linebuf, "%*[^0-9-]%lf", &x0);
00503 scan->data.s.x0 = x0;
00504 PI_DEBUG (DBG_L2, "UK2k x0 = " << scan->data.s.x0 );
00505 }
00506
00507
00508 if ( !strncmp (linebuf, "start_y", 7) ) {
00509 double y0;
00510 sscanf (linebuf, "%*[^0-9-]%lf", &y0);
00511 scan->data.s.y0 = y0;
00512 PI_DEBUG (DBG_L2, "UK2k y0 = " << scan->data.s.y0 );
00513 }
00514
00515
00516 if ( !strncmp (linebuf, "servo_range", 11) ) {
00517 double dz;
00518 sscanf (linebuf, "%*[^0-9-]%lf", &dz);
00519
00520 dz/=16384;
00521 PI_DEBUG (DBG_L2, "UK2k z unit: nm" );
00522 PI_DEBUG (DBG_L2, "UK2k dz:" << dz );
00523 UnitObj *zu = gapp->xsm->MakeUnit ("nm","nm");
00524 scan->data.s.dz = zu->Usr2Base(dz);
00525 scan->data.SetZUnit(zu);
00526 delete zu;
00527 }
00528
00529
00530 if ( !strncmp (linebuf, "z_bias", 6) ) {
00531 double ugap;
00532 sscanf (linebuf, "%*[^0-9-]%lf", &ugap);
00533 scan->data.s.Bias = ugap/1000.0;
00534 PI_DEBUG (DBG_L2, "UK2k ugap = " << scan->data.s.Bias );
00535 }
00536
00537
00538
00539 if ( !strncmp (linebuf, "setpoint", 8) ) {
00540 double iset;
00541 sscanf (linebuf, "%*[^0-9-]%lf", &iset);
00542 scan->data.s.Current = iset;
00543 PI_DEBUG (DBG_L2, "UK2k iset = " << scan->data.s.Current );
00544 }
00545
00546 }
00547
00548 f.close();
00549
00550 gchar *tmp=g_strconcat (itime, " ", idate, NULL);
00551 scan->data.ui.SetDateOfScan (tmp);
00552 g_free (tmp);
00553
00554
00555 scan->data.s.dx = scan->data.s.rx / scan->data.s.nx;
00556 PI_DEBUG (DBG_L2, " dx = " << scan->data.s.dx );
00557
00558
00559 scan->data.s.dy = scan->data.s.ry/scan->data.s.ny;
00560 PI_DEBUG (DBG_L2, " dy = " << scan->data.s.dy );
00561
00562
00563
00564
00565
00566 return FIO_OK;
00567 }
00568
00569
00570
00571
00572 FIO_STATUS UK2k_ImExportFile::Write()
00573 {
00574 return FIO_OK;
00575 }
00576
00577
00578
00579
00580
00581
00582
00583 static void UK2k_import_filecheck_load_callback (gpointer data ){
00584 gchar **fn = (gchar**)data;
00585 if (*fn){
00586 PI_DEBUG (DBG_L2, "Check File: UK2k_import_filecheck_load_callback called with >"
00587 << *fn << "<" );
00588
00589 Scan *dst = gapp->xsm->GetActiveScan();
00590 if(!dst){
00591 gapp->xsm->ActivateFreeChannel();
00592 dst = gapp->xsm->GetActiveScan();
00593 }
00594 UK2k_ImExportFile fileobj (dst, *fn);
00595
00596 FIO_STATUS ret = fileobj.Read();
00597 if (ret != FIO_OK){
00598
00599 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE) {
00600 *fn=NULL;
00601 PI_DEBUG (DBG_L2, "UK2k: Read Error " << ((int)ret) << "!!!!!!!!" );
00602 } else
00603 PI_DEBUG (DBG_L2, "UK2k: Not a UK2k file!" );
00604
00605 gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE);
00606 }else{
00607
00608 *fn=NULL;
00609
00610
00611 gapp->xsm->ActiveScan->GetDataSet(gapp->xsm->data);
00612 gapp->spm_update_all();
00613 dst->draw();
00614 }
00615 }else{
00616 PI_DEBUG (DBG_L2, "UK2k_import_filecheck_load: Skipping" );
00617 }
00618 }
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648 static void UK2k_import_import_callback(GtkWidget *w, void *data){
00649
00650 gchar *fn = gapp->file_dialog("UK2k Import", NULL,
00651 "*.std",
00652 NULL, "UK2k-Import");
00653
00654 PI_DEBUG (DBG_L2, "FLDLG-IM::" << fn );
00655 UK2k_import_filecheck_load_callback (&fn );
00656 }
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667