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 #include <gtk/gtk.h>
00071 #include "config.h"
00072 #include "gxsm/plugin.h"
00073 #include "gxsm/dataio.h"
00074 #include "gxsm/action_id.h"
00075
00076 using namespace std;
00077
00078
00079 static void nanoimport_init( void );
00080 static void nanoimport_about( void );
00081 static void nanoimport_configure( void );
00082 static void nanoimport_cleanup( void );
00083
00084 static void nanoimport_run(GtkWidget *w, void *data);
00085
00086 static void nano_import_filecheck_load_callback (gpointer data);
00087
00088
00089
00090 GxsmPlugin nanoimport_pi = {
00091 NULL,
00092 NULL,
00093 0,
00094 NULL,
00095
00096
00097
00098
00099 "Nanoimport",
00100
00101
00102
00103
00104
00105 NULL,
00106
00107 "Imports from Nanoscope Data",
00108
00109 "Percy Zahl",
00110
00111 N_("_File/_Import/"),
00112
00113 N_("Nano Scope"),
00114
00115 N_("Import Digital NanoScope File"),
00116
00117 "no more info",
00118 NULL,
00119 NULL,
00120
00121
00122 nanoimport_init,
00123
00124
00125 NULL,
00126
00127
00128 nanoimport_about,
00129
00130
00131 nanoimport_configure,
00132
00133
00134 nanoimport_run,
00135
00136
00137 nanoimport_cleanup
00138 };
00139
00140
00141 static const char *about_text = N_("Gxsm NanoScope Data Import Plugin\n\n"
00142 "This plugin reads in a orig. Data File\n"
00143 "from Digital Nano Scope to Gxsm.");
00144
00145
00146
00147 GxsmPlugin *get_gxsm_plugin_info ( void ){
00148 nanoimport_pi.description = g_strdup_printf(N_("Gxsm MathOneArg nanoimport plugin %s"), VERSION);
00149 return &nanoimport_pi;
00150 }
00151
00152
00153
00154
00155
00156
00157
00158 static void nanoimport_init(void)
00159 {
00160 PI_DEBUG (DBG_L2, "Nanoimport Plugin Init" );
00161 PI_DEBUG (DBG_L2, "-> connecting to load list" );
00162 gapp->ConnectPluginToLoadFileEvent (nano_import_filecheck_load_callback);
00163 }
00164
00165
00166 static void nanoimport_about(void)
00167 {
00168 const gchar *authors[] = { nanoimport_pi.authors, NULL};
00169 gtk_widget_show(gnome_about_new ( nanoimport_pi.name,
00170 VERSION,
00171 N_("(C) 2000 the Free Software Foundation"),
00172 about_text,
00173 authors,
00174 NULL, NULL, NULL
00175 ));
00176 }
00177
00178
00179 static void nanoimport_configure(void)
00180 {
00181 if(nanoimport_pi.app)
00182 nanoimport_pi.app->message("Nanoimport Plugin Configuration");
00183
00184 }
00185
00186
00187 static void nanoimport_cleanup(void)
00188 {
00189 PI_DEBUG (DBG_L2, "Nanoimport Plugin Cleanup" );
00190 }
00191
00192 class NanoScopeFile : public Dataio{
00193 public:
00194 NanoScopeFile(Scan *s, const char *n, int i=0) : Dataio(s,n){ imgindex=i; DIversion=0x00; };
00195 void SetIndex(int i){ imgindex=i; };
00196 int MultiImage(){ return DIversion < 0x04300000 ? 0:1; }
00197 virtual FIO_STATUS Read();
00198 virtual FIO_STATUS Write(){ return FIO_OK; };
00199 private:
00200 int imgindex;
00201 int DIversion;
00202 };
00203
00204 FIO_STATUS NanoScopeFile::Read(){
00205 size_t offset=0, length=0;
00206 gchar *line=NULL;
00207 double Zscale=1., Zmag=1.;
00208 double ZSens_V_to_nm=14.;
00209 double ZDAtoVolt=0.006713867;
00210 double CurrentSens_V_to_nA = 10.;
00211 int bps=2;
00212 int imgno=0;
00213
00214 ifstream f;
00215 GString *FileList=NULL;
00216
00217 f.open(name, ios::in);
00218 if(!f.good())
00219 return status=FIO_OPEN_ERR;
00220
00221
00222 scan->data.LoadValues(gapp->xsm->Inst, gapp->xsm->hardware);
00223
00224 scan->data.ui.SetName (strrchr(name,'/')?strrchr(name,'/')+1:name);
00225 scan->data.ui.SetOriginalName ("--NA--");
00226 scan->data.ui.SetUser ("somebody@nanoscope");
00227 FileList = g_string_new("Import from Nanoscope Data\n");
00228 g_string_sprintfa(FileList, "Channel No: %d\n", imgindex+1);
00229 g_string_append(FileList, "Original File Info Header follows:\n");
00230
00231 scan->data.s.rx = 1.;
00232 scan->data.s.ry = 1.;
00233 scan->data.s.nx = 1;
00234 scan->data.s.ny = 1;
00235 scan->data.s.x0 = 0.;
00236 scan->data.s.y0 = 0.;
00237 scan->data.s.alpha = 0.;
00238
00239
00240 scan->data.display.bright = 32.;
00241 scan->data.display.contrast = 0.01;
00242
00243
00244
00245
00246
00247
00248 for(; f.good();){
00249 gchar tmpline[256];
00250 f.getline(tmpline, 255);
00251 g_free (line);
00252 line = g_strstrip (g_strdup (tmpline));
00253 if (!line) continue;
00254 if (strlen(line) < 2) continue;
00255 g_string_append(FileList, line);
00256 g_string_append(FileList, "\n");
00257
00258 if(strncmp(line, "\\Version: 0x", 12) == 0){
00259 sscanf(&line[12],"%x", &DIversion);
00260 continue;
00261 }
00262 if(strncmp(line, "\\Date: ", 6) == 0){
00263 scan->data.ui.SetDateOfScan (&line[6]);
00264 continue;
00265 }
00266 if(strncmp(line, "\\Scan size: ",11) == 0){
00267 scan->data.s.rx = scan->data.s.ry = 10.*atof(&line[11]);
00268 continue;
00269 }
00270 if(strncmp(line, "\\Samps/line: ",12) == 0){
00271 scan->data.s.nx = atoi(&line[12]);
00272 continue;
00273 }
00274 if(strncmp(line, "\\Lines: ",7) == 0){
00275 scan->data.s.ny = atoi(&line[7]);
00276 continue;
00277 }
00278 if(strncmp(line, "\\X offset: ",10) == 0){
00279 scan->data.s.x0 = atof(&line[10]);
00280 continue;
00281 }
00282 if(strncmp(line, "\\Y offset: ",10) == 0){
00283 scan->data.s.y0 = atof(&line[10]);
00284 continue;
00285 }
00286 if(strncmp(line, "\\Rotate Ang.: ",13) == 0){
00287 scan->data.s.alpha = 180./M_PI*atof(&line[13]);
00288 continue;
00289 }
00290 if(strncmp(line, "\\@Sens. Zscan: V ",16) == 0){
00291 ZSens_V_to_nm = atof(&line[16]);
00292 g_string_sprintfa(FileList, "--->got ZSens: %fV/nm\n", ZSens_V_to_nm);
00293 continue;
00294 }
00295 if(strncmp(line, "\\@Sens. Current: V ",18) == 0){
00296 CurrentSens_V_to_nA = atof(&line[18]);
00297 g_string_sprintfa(FileList, "--->got CurrentSens: %fV/nA\n", CurrentSens_V_to_nA);
00298 continue;
00299 }
00300 if(strncmp(line, "\\@1:Z limit: V [Sens. Zscan] (",30) == 0){
00301 ZDAtoVolt = atof(&line[30]);
00302 g_string_sprintfa(FileList, "--->got ZDAtoVolt: %fV/LSB\n", ZDAtoVolt);
00303 continue;
00304 }
00305
00306
00307 if(strncmp(line, "\\*Ciao image list",16) == 0){
00308 break;
00309 }
00310 if(strncmp(line, "\\*File list end ",15) == 0){
00311 break;
00312 }
00313 }
00314 g_free (line);
00315 line = NULL;
00316
00317 scan->data.s.dx = scan->data.s.rx/(double)scan->data.s.nx;
00318 scan->data.s.dy = scan->data.s.ry/(double)scan->data.s.ny;
00319 scan->data.s.ntimes = 1;
00320 scan->data.s.nvalues = 1;
00321
00322 if (DIversion < 0x03000000 || !f.good())
00323 return FIO_INVALID_FILE;
00324
00325 if (DIversion < 0x04300000){
00326
00327
00328 g_string_append(FileList, "----------\nImage Information (DI Version < 0x04300000) follows:\n");
00329
00330 f.close();
00331 f.open(name, ios::in);
00332 for(; f.good();){
00333 gchar tmpline[256];
00334 f.getline(tmpline, 255);
00335 g_free (line);
00336 line = g_strstrip (g_strdup (tmpline));
00337 if (!line) continue;
00338 if (strlen(line) < 2) continue;
00339 g_string_append(FileList, line);
00340 g_string_append(FileList, "\n");
00341
00342 if(strncmp(line, "\\Data offset: ",13) == 0){
00343 offset = (size_t)atoi(&line[13]);
00344 continue;
00345 }
00346 if(strncmp(line, "\\Data length: ",13) == 0){
00347 length = (size_t)atoi(&line[13]);
00348 continue;
00349 }
00350 if(strncmp(line, "\\Z magnify image: ",17) == 0){
00351 Zmag = atof(&line[17]);
00352 continue;
00353 }
00354 if(strncmp(line, "\\Z scale: ",9) == 0){
00355 Zscale = atof(&line[9]);
00356 g_string_append(FileList, "----------\n got Z scale, stopping here reading Header.\n");
00357 break;
00358 }
00359 if(strncmp(line, "\\*File list end ",15) == 0)
00360 break;
00361 }
00362 g_free (line);
00363 line = NULL;
00364 g_string_append(FileList, "----------\nEnd of File List.\n");
00365 g_string_append(FileList,
00366 "** Note: I'm unsure about the Z-scale, I use:\n"
00367 "** Z scale * Z magnify * 0.1 = 1DA/Angstroem\n");
00368
00369 g_string_sprintfa(FileList, "--->Z magn. = %f\n", Zmag);
00370 g_string_sprintfa(FileList, "--->Z scale = %f\n", Zscale);
00371
00372 scan->data.s.dz = 10.*Zscale/65536.;
00373
00374 }else{
00375
00376
00377 while(imgno < imgindex && f.good()){
00378 gchar tmpline[256];
00379 f.getline(tmpline, 255);
00380 g_free (line);
00381 line = g_strstrip (g_strdup (tmpline));
00382 if(strncmp(line, "\\*Ciao image list",16) == 0)
00383 ++imgno;
00384 if(strncmp(line, "\\*File list end ",15) == 0)
00385 break;
00386 }
00387 g_free (line);
00388 line = NULL;
00389
00390 if( imgno < imgindex ){
00391 f.close();
00392 g_string_free(FileList, TRUE);
00393 FileList=NULL;
00394 return status=FIO_READ_ERR;
00395 }
00396
00397
00398
00399 g_string_append(FileList, "----------\nImage Channel Information follows:\n");
00400 for(; f.good();){
00401 gchar tmpline[256];
00402 f.getline(tmpline, 255);
00403 g_free (line);
00404 line = g_strstrip (g_strdup (tmpline));
00405 if (!line) continue;
00406 if (strlen(line) < 2) continue;
00407 g_string_append(FileList, line);
00408 g_string_append(FileList, "\n");
00409
00410 if(strncmp(line, "\\Data offset: ",13) == 0){
00411 offset = (size_t)atoi(&line[13]);
00412 continue;
00413 }
00414 if(strncmp(line, "\\Data length: ",13) == 0){
00415 length = (size_t)atoi(&line[13]);
00416 continue;
00417 }
00418 if(strncmp(line, "\\Bytes/pixel: ",13) == 0){
00419 bps = (size_t)atoi(&line[13]);
00420 continue;
00421 }
00422 if(strncmp(line, "\\Valid data start: ",18) == 0){
00423 continue;
00424 }
00425 if(strncmp(line, "\\Valid data len X: ",18) == 0){
00426 continue;
00427 }
00428 if(strncmp(line, "\\Valid data len Y: ",18) == 0){
00429 continue;
00430 }
00431 if(strncmp(line, "\\@2:Image Data: ",15) == 0){
00432 continue;
00433 }
00434 if(strncmp(line, "\\@2:Z scale: V [Sens.",21) == 0){
00435 gchar *pos = strchr(&line[22], ']');
00436 if(pos){
00437 *pos = 0;
00438 gchar *ScanInfo = g_strdup(&line[22]);
00439 g_string_sprintfa(FileList, "--->got ZScan Info: %s\n", ScanInfo);
00440 scan->data.ui.SetBaseName (scan->data.ui.name);
00441 ZDAtoVolt = atof(pos+3);
00442 g_string_sprintfa(FileList, "--->got ZScan Sens: %fV/LSB\n", ZDAtoVolt);
00443
00444 gchar *nameext = g_strdup_printf("%s[%d]%s",
00445 scan->data.ui.name,
00446 imgindex+1,
00447 ScanInfo);
00448 scan->data.ui.SetName (nameext);
00449 scan->data.ui.SetBaseName (nameext);
00450 g_free(nameext);
00451 g_free(ScanInfo);
00452 }
00453 else
00454 g_string_sprintfa(FileList, "---> Error finding matching ']'!\n");
00455 continue;
00456 }
00457
00458 if(strncmp(line, "\\*Ciao image list",16) == 0)
00459 break;
00460 if(strncmp(line, "\\*File list end ",15) == 0)
00461 break;
00462 }
00463 g_free (line);
00464 line = NULL;
00465 g_string_append(FileList, "----------\nEnd of File List.\n");
00466
00467
00468
00469 scan->data.s.dz = 10.*ZSens_V_to_nm*ZDAtoVolt;
00470
00471
00472 }
00473
00474 g_string_sprintfa(FileList, "--->Calculated dz = %f Ang/DAunit\n", scan->data.s.dz);
00475
00476
00477 for(gchar *p=FileList->str; *p; ++p)
00478 if (*p == 'º') *p='d';
00479
00480 gsize br, bw;
00481 GError *error = NULL;
00482
00483
00484 gchar *utf8_text = g_convert (FileList->str, -1, "UTF-8", "ISO-8859-1",
00485 &br, &bw, &error);
00486
00487 if (error != NULL){
00488 gchar *info = g_strdup_printf (
00489 "Nanoscope header to comment conversion failed:\n"
00490 "Sorry, an ISO-8859-1 to UTF-8 conversion error occured,\n"
00491 "g_convert retured an error code.\n"
00492 "Bytes read: %d, written: %d",
00493 br, bw
00494 );
00495 scan->data.ui.SetComment (info);
00496 g_free (info);
00497 g_error_free (error);
00498 } else
00499 scan->data.ui.SetComment (utf8_text);
00500
00501 g_free (utf8_text);
00502 g_string_free(FileList, TRUE);
00503 FileList=NULL;
00504
00505
00506
00507 f.seekg(offset, ios::beg);
00508
00509 scan->mem2d->Resize (scan->data.s.nx, scan->data.s.ny);
00510 scan->mem2d->DataRead (f, -1);
00511 scan->mem2d->data->MkXLookup (-scan->data.s.rx/2., scan->data.s.rx/2.);
00512 scan->mem2d->data->MkYLookup (-scan->data.s.ry/2., scan->data.s.ry/2.);
00513
00514 if(f.fail()){
00515 f.close();
00516 return status=FIO_READ_ERR;
00517 }
00518 f.close();
00519 return status=FIO_OK;
00520 }
00521
00522
00523 static void nano_import_filecheck_load_callback (gpointer data){
00524 gchar **fn = (gchar**)data;
00525 if (*fn){
00526 Scan *dst;
00527 int ret, ret0;
00528 PI_DEBUG (DBG_L2, "Check File: nano_import_filecheck_load_calback >"
00529 << *fn << "<" );
00530
00531
00532 gapp->xsm->ActivateFreeChannel();
00533 NanoScopeFile NanoFile(dst = gapp->xsm->GetActiveScan(), *fn);
00534 for(int i=0; i<3; ++i){
00535 if(!dst){
00536 gapp->xsm->ActivateFreeChannel();
00537 dst = gapp->xsm->GetActiveScan();
00538 }
00539 NanoFile.SetIndex(i);
00540 NanoFile.SetScan(dst);
00541 if((ret=NanoFile.Read()) != FIO_OK){
00542
00543 if(i==0) ret0 = ret;
00544 gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE);
00545 break;
00546 }
00547 if(i==0) ret0 = ret;
00548 gapp->xsm->ActiveScan->GetDataSet(gapp->xsm->data);
00549 gapp->xsm->AutoDisplay ();
00550 gapp->spm_update_all();
00551 dst->draw();
00552 dst=NULL;
00553 if(! NanoFile.MultiImage() ) break;
00554 }
00555
00556 if (ret != FIO_OK){
00557
00558 if (ret != FIO_NOT_RESPONSIBLE_FOR_THAT_FILE)
00559 *fn=NULL;
00560
00561 gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE);
00562 PI_DEBUG (DBG_L2, "Read Error " << ((int)ret) << "!!!!!!!!" );
00563 }else{
00564
00565 *fn=NULL;
00566
00567
00568 gapp->xsm->ActiveScan->GetDataSet(gapp->xsm->data);
00569 gapp->xsm->AutoDisplay ();
00570 gapp->spm_update_all();
00571 dst->draw();
00572 }
00573 }else{
00574 PI_DEBUG (DBG_L2, "nano_import_filecheck_load: Skipping" );
00575 }
00576 }
00577
00578
00579 static void nanoimport_run(GtkWidget *w, void *data)
00580 {
00581 Scan *dst;
00582 gchar *nfname = gapp->file_dialog("NanoScope file to load", NULL,
00583 "*", NULL, "NanoImport");
00584 if( !nfname ) return;
00585
00586 gapp->xsm->ActivateFreeChannel();
00587 NanoScopeFile NanoFile(dst = gapp->xsm->GetActiveScan(), nfname);
00588 for(int i=0; i<3; ++i){
00589 if(!dst){
00590 gapp->xsm->ActivateFreeChannel();
00591 dst = gapp->xsm->GetActiveScan();
00592 }
00593 NanoFile.SetIndex(i);
00594 NanoFile.SetScan(dst);
00595 if(NanoFile.Read() != FIO_OK){
00596
00597 gapp->xsm->SetMode(-1, ID_CH_M_OFF, TRUE);
00598 break;
00599 }
00600 gapp->xsm->ActiveScan->GetDataSet(gapp->xsm->data);
00601 gapp->xsm->AutoDisplay ();
00602 gapp->spm_update_all();
00603 dst->draw();
00604 dst=NULL;
00605 if(! NanoFile.MultiImage() ) break;
00606 }
00607 }