DAS  3.1.6 - 18/09/2017
Utils.c
Go to the documentation of this file.
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <malloc.h>
6 #include <math.h>
7 #include <io.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include "mgui.h"
11 #include "DAS_Spatram.h"
12 #include "dcl.h"
13 #include "dil.h"
14 #include "bil.h"
15 #include "DOAS.h"
16 #include "nrutil.h"
17 #include "Marq.h"
18 
19 
20 
27 bil BIL; //Bortul Library Structure
29 
30 
31 #define TINY 1.0e-25 // A small number.
32 #define FREERETURN {free_vector(d,1,n);free_vector(c,1,n);return;}
33 #define PI 3.1415926
34 
35 
36 
37 /*Given arrays xa[1..n] and ya[1..n], and given a value x, this routine
38  returns a value y, and an error estimate dy.
39  If P(x) is the polynomial of degree N - 1 such that P(xai) = yai, i =
40  1, . . . , n, then the returned value y = P(x).
41 */
42 void polint(float xa[], float ya[], int n, float x, float *y, float *dy)
43 {
44  int i,m,ns=1;
45  float den,dif,dift,ho,hp,w;
46  float *c,*d;
47  dif=(float)fabs(x-xa[1]);
48  c=vector(1,n);
49  d=vector(1,n);
50  for (i=1;i<=n;i++)
51  { //Here we .nd the index ns of the closest table entry,
52  if((dift=(float)fabs(x-xa[i])) < dif)
53  {
54  ns=i;
55  dif=dift;
56  }
57  c[i]=ya[i]; //and initialize the tableau of c’s and d’s.
58  d[i]=ya[i];
59  }
60  *y=ya[ns--]; //This is the initial approximation to y.
61  for (m=1;m<n;m++)
62  { //For each column of the tableau,
63  for(i=1;i<=n-m;i++)
64  { //we loop over the current c’s and d’s and update them.
65  ho=xa[i]-x;
66  hp=xa[i+m]-x;
67  w=c[i+1]-d[i];
68  if((den=ho-hp) == 0.0) //This error can occur only if two input
69  //xa’s are (to within roundo.) identical.
70  nrerror("Error in routine polint");
71  den=w/den;
72  d[i]=hp*den; //Here the c’s and d’s are updated.
73  c[i]=ho*den;
74  }
75  *y += (*dy=(2*ns < (n-m) ? c[ns+1] : d[ns--]));
76  /*
77  After each column in the tableau is completed, we decide which correction,
78  c or d, we want to add to our accumulating value of y,
79  i.e., which path to take through the tableau forking up or down.
80  We do this in such a way as to take the most straight
81  line route through the tableau to its apex, updating ns accordingly ù to keep track of where we are. This route keeps the partial approximations centered (insofar as possible) on the target x. The last dy added is thus the error indication. */ } free_vector(d,1,n); free_vector(c,1,n); } int polintlambda(void) { int i, l = 1, linit; float dy,y; FILE *nf; float *xa,*ya; xa = (float *)dvector(1,137); ya = (float *)dvector(1,137) ; if((nf = fopen("In.dat", "r")) == NULL) nrerror("In.dat not found\n"); while(!feof(nf)) { fscanf(nf, "%f%f", &xa[l], &ya[l]); printf("%d %12.6f %12.6f \n", l, xa[l],ya[l]); l++; } fclose(nf); linit = (int) xa[1]; nf = fopen("Is.dat", "w"); for (i=1; i<l-2;i++) { polint( &xa[i], &ya[i],2,(float) i+linit,&y,&dy); printf("\n%d %12.6f %12.6f ", i, xa[i],ya[i]); printf("\n%d %d %12.6f %12.6f", i, i+linit,y,dy); printf("\n**************"); fprintf(nf, "%12.6lf %12.6lf %d %12.6lf \n",xa[i],ya[i], i+linit, y); } fclose(nf); return 0; } /*************************************************************/ void D_CloseVR(MOBJECT p, void *od, void *ad) { MShellUnrealize(p); MShellDestroy(p); } int D_VRip = -1, D_VOpen = 0; int DD_VRip[16]; int DD_VOpen[16]; void __stdcall DD_VRange(const char *str, int val, int Port) { char msg[16]; static MOBJECT sts[16], shell[16]; if(D_VRip < 0 && val == 100) return; if(D_VRip < 0 && val > 0) return; if(val == 0) { if(D_VOpen) return; D_VOpen = 1; D_VRip++; shell[D_VRip] = MCreateShell(str, SF_NO_MAXIMIZE | SF_NO_MINIMIZE | SF_SAVEUNDER); MObjectSetBackgroundRGB(shell[D_VRip], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]); sts[D_VRip] = MCreateStatusBar(shell[D_VRip], " 0 %", 100, HELV_SMALL); MObjectSetBackgroundRGB(sts[D_VRip], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]); MStatusBarSetSliderRGB(sts[D_VRip], 0, 255, 0); MObjectSetCompositeHeight(sts[D_VRip], 20); MShellSetWMCloseCallback(shell[D_VRip], D_CloseVR, 0L); //MShellRealizeXY(shell[D_VRip], ShLoc[1][0], ShLoc[1][1]); MShellRealize(shell[D_VRip]); MObjectResize(shell[D_VRip], 300, 35); // Chiama MGUI per modificare size shell MLoopWhileEvents(0); } else { if(val == 100) { MShellUnrealize(shell[D_VRip]); MShellDestroy(shell[D_VRip]); D_VRip--; D_VOpen = 0; } else { sprintf(msg, "%d %%", val); MObjectSetText(sts[D_VRip], msg); MStatusBarSetPos(sts[D_VRip], val); } } } void D_VRange(char *str, int val, int r, int g, int b) { char msg[16]; static MOBJECT sts[16], shell[16]; if(D_VRip < 0 && val == 100) return; if(D_VRip < 0 && val > 0) return; if(val == 0) { if(D_VOpen) return; D_VOpen = 1; D_VRip++; shell[D_VRip] = MCreateShell(str, SF_NO_MAXIMIZE | SF_NO_MINIMIZE | SF_SAVEUNDER); MObjectSetBackgroundRGB(shell[D_VRip], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]); sts[D_VRip] = MCreateStatusBar(shell[D_VRip], " 0 %", 100, HELV_SMALL); MObjectSetBackgroundRGB(sts[D_VRip], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]); MStatusBarSetSliderRGB(sts[D_VRip], r, g, b); MObjectSetCompositeHeight(sts[D_VRip], 20); MShellSetWMCloseCallback(shell[D_VRip], D_CloseVR, 0L); //MShellRealizeXY(shell[D_VRip], ShLoc[1][0], ShLoc[1][1]); MShellRealize(shell[D_VRip]); MObjectResize(shell[D_VRip], 300, 35); // Chiama MGUI per modificare size shell MLoopWhileEvents(0); } else { if(val == 100) { MShellUnrealize(shell[D_VRip]); MShellDestroy(shell[D_VRip]); D_VRip--; D_VOpen = 0; } else { sprintf(msg, "%d %%", val); MObjectSetText(sts[D_VRip], msg); MStatusBarSetPos(sts[D_VRip], val); } MLoopWhileEvents(0); } } void D_VRangeSim(int id, char *str, int val, int r, int g, int b, int x, int y) { char msg[16]; static MOBJECT sts[16][16], shell[16][16]; if(DD_VRip[id] < 0 && val == 100) return; if(DD_VRip[id] < 0 && val > 0) return; if(val == 0) { if(DD_VOpen[id]) return; DD_VOpen[id] = 1; DD_VRip[id]++; shell[id][DD_VRip[id]] = MCreateShell(str, SF_NO_MAXIMIZE | SF_NO_MINIMIZE | SF_SAVEUNDER); MObjectSetBackgroundRGB(shell[id][DD_VRip[id]], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]); sts[id][DD_VRip[id]] = MCreateStatusBar(shell[id][DD_VRip[id]], " 0 %", 100, HELV_SMALL); MObjectSetBackgroundRGB(sts[id][DD_VRip[id]], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]); MStatusBarSetSliderRGB(sts[id][DD_VRip[id]], r, g, b); MObjectSetCompositeHeight(sts[id][DD_VRip[id]], 20); MShellSetWMCloseCallback(shell[id][DD_VRip[id]], D_CloseVR, 0L); MShellRealizeXY(shell[id][DD_VRip[id]], x, y); MObjectResize(shell[id][DD_VRip[id]], 300, 35); // Chiama MGUI per modificare size shell MLoopWhileEvents(0); } else { if(val >= 100) { MShellUnrealize(shell[id][DD_VRip[id]]); MShellDestroy(shell[id][DD_VRip[id]]); DD_VRip[id]--; DD_VOpen[id] = 0; } else { sprintf(msg, "%d %%", val); MObjectSetText(sts[id][DD_VRip[id]], msg); MStatusBarSetPos(sts[id][DD_VRip[id]], val); } } } double CalcFluxIndex( unsigned short mis[], int dx, int dy, float te) { int x; long sumdata = 0; double average = 0; double fluxi = 0; for(x = 200; x < dx - 200 ; x++) { sumdata += mis[ dy * dx + x]; } average = sumdata / (double) (dx - 400); fluxi = average / (double)(te * 1000000); return fluxi; } int DB_CalcFluxI(char *nomefile) { FILE *fs; FILE *fd; char buf[512],str[512]; int er = 0, i, k; char string[4] = ".FIN"; int fb; double fluxindex; char filedatan[256]; long numspettri; // Lettura file elenco i=0; fs = fopen(nomefile, "r"); if(fs < 0) return 1; while( !feof( fs ) ) { fgets(str, 80, fs); i++; } fclose(fs); numspettri = i * SAVECNT; D_VRange("Flux Index Calculation",0, 0, 222, 235); fs = fopen(nomefile, "r"); if(fs < 0) return 2; //Read the file names k = 0; while( !feof( fs ) ) { fscanf(fs, "%s", filedatan); // build the complete file name // strcpy(buf, BIL.DFILEM.WORKDIRECTORY); strcat(buf, filedatan); fb = open(filedatan, O_RDONLY | O_BINARY, S_IREAD ); while( !eof( fb ) ) { MLoopWhileEvents(2); k ++; if((k % 10) == 0 && k > 0) D_VRange(NULL, (long) k * 100 / numspettri, 0, 222, 235 ); er = ReadHeader_BIN(fb); if(BIL.DPLOT.ImatGraf == 0) BIL.DPLOT.ImatGraf = AllocUSMat(IDX, IDY); ReadIMG_BIN(fb, BIL.DPLOT.ImatGraf, IDX, IDY); if( (strcmp(BIL.SPH.mirpos,"HorizDX") == 0) | (strcmp(BIL.SPH.mirpos,"Zenith") == 0) ) { //Filter Data //if (BIL.SPH.ccdwx - (BIL.SPH.ccdex +1) != 0) // se i pixel del sensore sono piu' di quelli in param.ini // BlindPix_1 = BIL.SPH.ccdwx - (BIL.SPH.ccdex +1) ; //FilterData(BIL.DPLOT.ImatGraf, IDX, IDY, 15, BlindPix_1); fluxindex = CalcFluxIndex( BIL.DPLOT.ImatGraf, IDX , IDY/2, (float)BIL.SPH.ccdtexp); sprintf(buf,"%d.FIN", BIL.SPH.wavelen); strcpy(str, BIL.DFILEM.WORKDIRECTORY); strcat(str,buf); //MMessageDialog("Full FileName:", str, "ok", NULL); fd = fopen(str, "a"); if(fd < 0) return 1; sprintf(str,"%d/%02d/%02d %02d:%02d:%02d %d %lf\n", BIL.SPH.DATE.da_year, BIL.SPH.DATE.da_mon, BIL.SPH.DATE.da_day, BIL.SPH.TIME.ti_hour, BIL.SPH.TIME.ti_min, BIL.SPH.TIME.ti_sec, BIL.SPH.wavelen, fluxindex); fprintf(fd, "%s", str); fclose(fd); } } // D_VRange(NULL, 100, 128, 254, 128); close (fb); } D_VRange(NULL, 100, 0,0,0); fclose(fs); return 0; } int DT_ColFormat(void) { int er =0; char fname[128]; char buff[_MAX_PATH]; // int l; FB_PREFS fbp; fname[0] = '\0'; buff[0] = '\0'; memset (&fbp, 0, sizeof (fbp)); fbp.change_cwd = True; fbp.allow_dir_create = True; fbp.allow_dir_rename = True; fbp.allow_dir_delete = True; fbp.transparent_tree = False; fbp.font = HELV_SMALL; fbp.button_font = HELV_SMALL; //Set Img file sprintf(buff, "%s\\%s", BIL.DFILEM.HOMEDIRECTORY, "sys\\Tile2.bmp" ); fbp.bg_image_file =buff; /* BIL.DFILEM.DBFILENAME[0] = 0; MDCreateFM_Shell(FOPEN, FDAT); //Open File, Dat ext. if(BIL.DFILEM.DBFILENAME[0] == 0) return 1; DB_Column(BIL.DFILEM.DBFILENAME); */ if(DAS.DOption.savingmode) er = MFileBrowseExt ("Select File for Column Format", "*.bif", fname, BIL.DFILEM.DATADIRECTORY, &fbp); else er = MFileBrowseExt ("Select File for column Format", "*.dat", fname, BIL.DFILEM.DATADIRECTORY, &fbp); if(er) { sprintf(BIL.DFILEM.DBFILENAME,"%s%s",BIL.DFILEM.DATADIRECTORY, fname); //l = strlen(fname); //strncpy(buff, fname+l-4, 4); //if( (strcmp(fname+l-4,".dat") == 0) |(strcmp(fname+l-4,".Dat") == 0) ) // BIL.DFILEM.FKIND = FDAT; //else if( (strcmp(fname+l-4,".bif") == 0) | (strcmp(fname+l-4,".Bif") == 0) ) // BIL.DFILEM.FKIND = FBIF; //else //{ // MMessageDialog("DAS information", "Unsupported format!!", " Ok ", NULL); // return 1; //} DB_Column(BIL.DFILEM.DBFILENAME); } else return 1; return 0; } int DT_FluxIndex(void) { BIL.DFILEM.DBFILENAME[0] = 0; MDCreateFM_Shell(FOPEN, FELE); //Open File, Ele ext. if(BIL.DFILEM.DBFILENAME[0] == 0) return 1; DB_CalcFluxI(BIL.DFILEM.DBFILENAME); return 0; } void D_PrintSpectrum(FILE *fff) { int x, y; for(y = 0; y < IDY; y++) { for(x = 0; x < IDX; x++) { if((x % 10) == 0) { if(y == 0 && x == 0) { } else fprintf(fff, "\n", NULL); } fprintf(fff, "%5u ", BIL.DPLOT.ImatGraf[y * IDX + x]); } } fprintf(fff, "\n", NULL); } void D_PrintBILSPH_BIN(int fd) { unsigned long info = 0; info =0; info += write(fd, BIL.SPH.location, sizeof(BIL.SPH.location)); info += write(fd, &BIL.SPH.latitude, sizeof(BIL.SPH.latitude)); info += write(fd, &BIL.SPH.longitude, sizeof(BIL.SPH.longitude)); info += write(fd, &BIL.SPH.altitude, sizeof(BIL.SPH.altitude)); info += write(fd, &BIL.SPH.sza, sizeof(BIL.SPH.sza)); info += write(fd, &BIL.SPH.DATE, sizeof(struct ddate)); info += write(fd, &BIL.SPH.TIME, sizeof(struct dtime)); info += write(fd, BIL.SPH.mirpos, sizeof(BIL.SPH.mirpos)); info += write(fd, &BIL.SPH.wavelen, sizeof(BIL.SPH.wavelen)); info += write(fd, BIL.SPH.filpos, sizeof(BIL.SPH.filpos)); info += write(fd, &BIL.SPH.ccdtemp, sizeof(BIL.SPH.ccdtemp)); info += write(fd, &BIL.SPH.ccdtexp, sizeof(BIL.SPH.ccdtexp)); info += write(fd, &BIL.SPH.ccdmax, sizeof(BIL.SPH.ccdmax)); info += write(fd, &BIL.SPH.imgtype, sizeof(BIL.SPH.imgtype)); info += write(fd, &BIL.SPH.ccdwx, sizeof(BIL.SPH.ccdwx)); info += write(fd, &BIL.SPH.ccdwy, sizeof(BIL.SPH.ccdwy)); info += write(fd, &BIL.SPH.ccdsx, sizeof(BIL.SPH.ccdsx)); info += write(fd, &BIL.SPH.ccdsy, sizeof(BIL.SPH.ccdsy)); info += write(fd, &BIL.SPH.ccdex, sizeof(BIL.SPH.ccdex)); info += write(fd, &BIL.SPH.ccdey, sizeof(BIL.SPH.ccdey)); info += write(fd, &BIL.SPH.ccdvb, sizeof(BIL.SPH.ccdvb)); info += write(fd, &BIL.SPH.ccdnr, sizeof(BIL.SPH.ccdnr)); info += write(fd, &BIL.SPH.dummy1, sizeof(BIL.SPH.dummy1)); info += write(fd, &BIL.SPH.dummy2, sizeof(BIL.SPH.dummy2)); info += write(fd, &BIL.SPH.dummy3, sizeof(BIL.SPH.dummy3)); info += write(fd, &BIL.SPH.dummy4, sizeof(BIL.SPH.dummy4)); info += write(fd, &BIL.SPH.comment, sizeof(BIL.SPH.comment)); } void D_PrintBILSPH(FILE *fff) { fprintf(fff, "LOC: %s ", BIL.SPH.location); //Name of the station fprintf(fff, "LAT: %.2lf ", BIL.SPH.latitude);//Station latitude fprintf(fff, "LON: %.2lf ", BIL.SPH.longitude);//Station latitude fprintf(fff, "ALT[m/asl]: %u ", BIL.SPH.altitude);//Station altitude fprintf(fff, "SZA: %.2lf\n", BIL.SPH.sza);//SZA fprintf(fff, "%d/%02d/%02d ", BIL.SPH.DATE.da_year, BIL.SPH.DATE.da_mon, BIL.SPH.DATE.da_day); fprintf(fff, "%02d:%02d:%02d LT ",BIL.SPH.TIME.ti_hour, BIL.SPH.TIME.ti_min,BIL.SPH.TIME.ti_sec); fprintf(fff, "%s ", BIL.SPH.mirpos); fprintf(fff, "%d ", BIL.SPH.wavelen); fprintf(fff, "%s ", BIL.SPH.filpos); fprintf(fff, "%.2lf ", BIL.SPH.ccdtemp); fprintf(fff, "%.2lf ", BIL.SPH.ccdtexp); fprintf(fff, "%5u ", BIL.SPH.ccdmax); fprintf(fff, "%5u\n", BIL.SPH.ccdmin); // Image format fprintf(fff, "%d ", BIL.SPH.imgtype); fprintf(fff, "%d ", BIL.SPH.ccdwx); fprintf(fff, "%d ", BIL.SPH.ccdwy); fprintf(fff, "%d ", BIL.SPH.ccdsx); fprintf(fff, "%d ", BIL.SPH.ccdsy); fprintf(fff, "%d ", BIL.SPH.ccdex); fprintf(fff, "%d ", BIL.SPH.ccdey); fprintf(fff, "%d ", BIL.SPH.ccdvb); fprintf(fff, "%d ", BIL.SPH.ccdnr); fprintf(fff, "%d ", BIL.SPH.dummy1); fprintf(fff, "%d ", BIL.SPH.dummy2); fprintf(fff, "%d ", BIL.SPH.dummy3); fprintf(fff, "%d\n", BIL.SPH.dummy4); fprintf(fff, "%s\n", BIL.SPH.comment); } void D_Print_BIL_H_ASCII(int mod, FILE *fff) { fprintf(fff, "LOC: %s ", BIL.SPH.location); //Name of the station fprintf(fff, "LAT: %.2lf ", BIL.SPH.latitude);//Station latitude fprintf(fff, "LON: %.2lf ", BIL.SPH.longitude);//Station latitude fprintf(fff, "ALT[m/asl]: %u ", BIL.SPH.altitude);//Station altitude fprintf(fff, "SZA: %.2lf\n", BIL.SPH.sza);//SZA fprintf(fff, "%d/%02d/%02d ", BIL.SPH.DATE.da_year, BIL.SPH.DATE.da_mon, BIL.SPH.DATE.da_day); fprintf(fff, "%02d:%02d:%02d LT ",BIL.SPH.TIME.ti_hour, BIL.SPH.TIME.ti_min,BIL.SPH.TIME.ti_sec); fprintf(fff, "%s ", BIL.SPH.mirpos); fprintf(fff, "%d ", BIL.SPH.wavelen); fprintf(fff, "%s ", BIL.SPH.filpos); fprintf(fff, "%.2lf ", BIL.SPH.ccdtemp); fprintf(fff, "%.2lf ", BIL.SPH.ccdtexp); fprintf(fff, "%5u ", BIL.SPH.ccdmax); fprintf(fff, "%5u\n", BIL.SPH.ccdmin); // Image format fprintf(fff, "%d ", BIL.SPH.imgtype); fprintf(fff, "%d ", BIL.SPH.ccdwx); fprintf(fff, "%d ", BIL.SPH.ccdwy); fprintf(fff, "%d ", BIL.SPH.ccdsx); fprintf(fff, "%d ", BIL.SPH.ccdsy); fprintf(fff, "%d ", BIL.SPH.ccdex); fprintf(fff, "%d ", BIL.SPH.ccdey); fprintf(fff, "%d ", BIL.SPH.ccdvb); fprintf(fff, "%d ", BIL.SPH.ccdnr); fprintf(fff, "%d ", BIL.SPH.dummy1); fprintf(fff, "%d ", BIL.SPH.dummy2); fprintf(fff, "%d ", BIL.SPH.dummy3); fprintf(fff, "%d\n", BIL.SPH.dummy4); fprintf(fff, "%s\n", BIL.SPH.comment); } int DT_CorrHeader() { FILE *fs, *fd, *fc; char buf[256],str[256]; int er = 0, i, k; char filedatan[32]; long numspettri; BIL.DFILEM.DBFILENAME[0] = 0; MDCreateFM_Shell(FOPEN, FELE); //Open File, Ele ext. if(BIL.DFILEM.DBFILENAME[0] == 0) return 0; // Lettura file elenco i=0; fs = fopen(BIL.DFILEM.DBFILENAME, "r"); if(fs < 0) return 1; //Prima lettura file elenco per determinare approssimativamente in numero di spettri presenti while( !feof( fs ) ) { fgets(str, 80, fs); i++; } fclose(fs); numspettri = i * SAVECNT; D_VRange("Header file correction",0, 0, 222, 235); fs = fopen(BIL.DFILEM.DBFILENAME, "r"); if(fs < 0) return 1; //Read the file names k = 0; while( !feof( fs ) ) { fscanf(fs, "%s", filedatan); // build the complete file name strcpy(buf, BIL.DFILEM.WORKDIRECTORY); strcat(buf, filedatan); fc = fopen(buf, "r"); if(fc < 0) return 1; while( !feof( fc ) ) { MLoopWhileEvents(0); k ++; if((k % 10) == 0 && k > 0) D_VRange(NULL, (long) k * 100 / numspettri, 0, 222, 235 ); er = ReadHeaderOldF(fc); //Modifica alcuni parametri del Header BIL.SPH.ccdwx = 1024; BIL.SPH.ccdwy = 254; BIL.SPH.ccdsx = 0; BIL.SPH.ccdsy = 0; BIL.SPH.ccdex = 1023; BIL.SPH.ccdey = 253; if(BIL.DPLOT.ImatGraf == 0) BIL.DPLOT.ImatGraf = AllocUSMat(IDX, IDY); ReadIMG(fc, BIL.DPLOT.ImatGraf, IDX, IDY); sprintf(buf,"%s\\%s", "CORRECTED",filedatan); strcpy(str, BIL.DFILEM.WORKDIRECTORY); strcat(str,buf); //MMessageDialog("Full FileName:", str, "ok", NULL); fd = fopen(str, "a"); if(fd < 0) return 1; //Print D_PrintBILSPH(fd); D_PrintSpectrum(fd); fclose(fd); } fclose(fc); } D_VRange(NULL, 100, 0,0,0); fclose(fs); if(!BIL.DPLOT.ImatGraf == 0) { BIL.DPLOT.ImatGraf = DeallocUSMat(BIL.DPLOT.ImatGraf); BIL.DPLOT.ImatOrig = DeallocUSMat(BIL.DPLOT.ImatOrig); } return 0; } void ShiftImg(int hp, int vp) { int x, y; for(y = 0; y < vp; y++) { for(x = 0; x < hp; x++) { if((x==0) & (y!=0)) BIL.DPLOT.ImatGraf[y * IDX + x] =BIL.DPLOT.ImatGraf[y * IDX + x+2]/2 + BIL.DPLOT.ImatGraf[y * IDX + x+3]/2; else if((x==1) & (y!=0)) BIL.DPLOT.ImatGraf[y * IDX + x] =BIL.DPLOT.ImatGraf[y * IDX + x+1]/2 + BIL.DPLOT.ImatGraf[y * IDX + x+3]/2; else BIL.DPLOT.ImatGraf[y * IDX + x] =BIL.DPLOT.ImatGraf[y * IDX + x]; } } } int DT_CorrFileEvora(void) { FILE *fs, *fd, *fc; char buf[256],str[256]; int er = 0, i, k; char filedatan[32]; long numspettri; BIL.DFILEM.DBFILENAME[0] = 0; MDCreateFM_Shell(FOPEN, FELE); //Open File, Ele ext. if(BIL.DFILEM.DBFILENAME[0] == 0) return 0; // Lettura file elenco i=0; fs = fopen(BIL.DFILEM.DBFILENAME, "r"); if(fs < 0) return 1; //Prima lettura file elenco per determinare approssimativamente in numero di spettri presenti while( !feof( fs ) ) { fgets(str, 80, fs); i++; } fclose(fs); numspettri = i * SAVECNT; D_VRange("Header file correction",0, 0, 222, 235); fs = fopen(BIL.DFILEM.DBFILENAME, "r"); if(fs < 0) return 1; //Read the file names k = 0; while( !feof( fs ) ) { fscanf(fs, "%s", filedatan); // build the complete file name strcpy(buf, BIL.DFILEM.WORKDIRECTORY); strcat(buf, filedatan); fc = fopen(buf, "r"); if(fc < 0) return 1; while( !feof( fc ) ) { MLoopWhileEvents(0); k ++; if((k % 10) == 0 && k > 0) D_VRange(NULL, (long) k * 100 / numspettri, 0, 222, 235 ); er = ReadHeaderOnly(fc); //Modifica alcuni parametri del Header BIL.SPH.ccdwx = 1024; BIL.SPH.ccdwy = 254; BIL.SPH.ccdsx = 0; BIL.SPH.ccdsy = 0; BIL.SPH.ccdex = 1023; BIL.SPH.ccdey = 253; if(BIL.DPLOT.ImatGraf == 0) BIL.DPLOT.ImatGraf = AllocUSMat(IDX, IDY); ReadIMG(fc, BIL.DPLOT.ImatGraf, IDX, IDY); ShiftImg(IDX, IDY); sprintf(buf,"%s\\%s", "CORRECTED",filedatan); strcpy(str, BIL.DFILEM.WORKDIRECTORY); strcat(str,buf); //MMessageDialog("Full FileName:", str, "ok", NULL); fd = fopen(str, "a"); if(fd < 0) return 1; //Print D_PrintBILSPH(fd); D_PrintSpectrum(fd); fclose(fd); } fclose(fc); } D_VRange(NULL, 100, 0,0,0); fclose(fs); if(!BIL.DPLOT.ImatGraf == 0) { BIL.DPLOT.ImatGraf = DeallocUSMat(BIL.DPLOT.ImatGraf); BIL.DPLOT.ImatOrig = DeallocUSMat(BIL.DPLOT.ImatOrig); } return 0; } int Model_read(char *buff) { FILE *fs; fs = fopen(BIL.DFILEM.DBFILENAME, "r"); if(fs < 0) return 1; while( !feof( fs ) ) { // fscanf(fs, "%s", str); //comment } fclose(fs); return 0; } int DT_LinkSpectra(void) { BIL.DFILEM.DBFILENAME[0] = 0; MDCreateFM_Shell(FOPEN, FMOD); //Open File, mod ext. if(BIL.DFILEM.DBFILENAME[0] == 0) return 1; Model_read(BIL.DFILEM.DBFILENAME); return 0; }
82  to keep track of where we are.
83  This route keeps the partial approximations centered (insofar as possible)
84  on the target x. The last dy added is thus the error indication.
85  */
86  }
87  free_vector(d,1,n);
88  free_vector(c,1,n);
89 }
90 
91 
92 
93 
94 int polintlambda(void)
95 {
96 
97  int i, l = 1, linit;
98  float dy,y;
99  FILE *nf;
100  float *xa,*ya;
101 
102  xa = (float *)dvector(1,137);
103  ya = (float *)dvector(1,137) ;
104 
105 
106  if((nf = fopen("In.dat", "r")) == NULL)
107  nrerror("In.dat not found\n");
108  while(!feof(nf))
109  {
110  fscanf(nf, "%f%f", &xa[l], &ya[l]);
111  printf("%d %12.6f %12.6f \n", l, xa[l],ya[l]);
112 
113  l++;
114  }
115  fclose(nf);
116  linit = (int) xa[1];
117 
118  nf = fopen("Is.dat", "w");
119 
120  for (i=1; i<l-2;i++)
121  {
122  polint( &xa[i], &ya[i],2,(float) i+linit,&y,&dy);
123  printf("\n%d %12.6f %12.6f ", i, xa[i],ya[i]);
124  printf("\n%d %d %12.6f %12.6f", i, i+linit,y,dy);
125  printf("\n**************");
126 
127  fprintf(nf, "%12.6lf %12.6lf %d %12.6lf \n",xa[i],ya[i], i+linit, y);
128 
129  }
130 
131  fclose(nf);
132 
133  return 0;
134 
135 }
136 
137 
138 /*************************************************************/
139 
140 void D_CloseVR(MOBJECT p, void *od, void *ad)
141 {
142  MShellUnrealize(p);
143  MShellDestroy(p);
144 
145 
146 }
147 
148 int D_VRip = -1, D_VOpen = 0;
149 
150 int DD_VRip[16];
151 int DD_VOpen[16];
152 
153 void __stdcall DD_VRange(const char *str, int val, int Port)
154 {
155 
156 
157  char msg[16];
158  static MOBJECT sts[16], shell[16];
159  if(D_VRip < 0 && val == 100)
160  return;
161  if(D_VRip < 0 && val > 0)
162  return;
163  if(val == 0)
164  {
165  if(D_VOpen) return;
166  D_VOpen = 1;
167  D_VRip++;
169  MObjectSetBackgroundRGB(shell[D_VRip], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]);
170  sts[D_VRip] = MCreateStatusBar(shell[D_VRip], " 0 %", 100, HELV_SMALL);
171  MObjectSetBackgroundRGB(sts[D_VRip], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]);
172  MStatusBarSetSliderRGB(sts[D_VRip], 0, 255, 0);
173  MObjectSetCompositeHeight(sts[D_VRip], 20);
174 
175  MShellSetWMCloseCallback(shell[D_VRip], D_CloseVR, 0L);
176  //MShellRealizeXY(shell[D_VRip], ShLoc[1][0], ShLoc[1][1]);
177  MShellRealize(shell[D_VRip]);
178  MObjectResize(shell[D_VRip], 300, 35);
179  // Chiama MGUI per modificare size shell
180  MLoopWhileEvents(0);
181 
182  }
183  else
184  {
185  if(val == 100)
186  {
187  MShellUnrealize(shell[D_VRip]);
188  MShellDestroy(shell[D_VRip]);
189  D_VRip--;
190  D_VOpen = 0;
191  }
192  else
193  {
194  sprintf(msg, "%d %%", val);
195  MObjectSetText(sts[D_VRip], msg);
196  MStatusBarSetPos(sts[D_VRip], val);
197  }
198  }
199 }
200 
201 
202 
203 
204 
205 
206 void D_VRange(char *str, int val, int r, int g, int b)
207 {
208  char msg[16];
209  static MOBJECT sts[16], shell[16];
210  if(D_VRip < 0 && val == 100)
211  return;
212  if(D_VRip < 0 && val > 0)
213  return;
214  if(val == 0)
215  {
216  if(D_VOpen) return;
217  D_VOpen = 1;
218  D_VRip++;
220  MObjectSetBackgroundRGB(shell[D_VRip], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]);
221  sts[D_VRip] = MCreateStatusBar(shell[D_VRip], " 0 %", 100, HELV_SMALL);
222  MObjectSetBackgroundRGB(sts[D_VRip], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]);
223  MStatusBarSetSliderRGB(sts[D_VRip], r, g, b);
224  MObjectSetCompositeHeight(sts[D_VRip], 20);
225 
226  MShellSetWMCloseCallback(shell[D_VRip], D_CloseVR, 0L);
227  //MShellRealizeXY(shell[D_VRip], ShLoc[1][0], ShLoc[1][1]);
228  MShellRealize(shell[D_VRip]);
229  MObjectResize(shell[D_VRip], 300, 35);
230  // Chiama MGUI per modificare size shell
231  MLoopWhileEvents(0);
232 
233  }
234  else
235  {
236  if(val == 100)
237  {
238  MShellUnrealize(shell[D_VRip]);
239  MShellDestroy(shell[D_VRip]);
240  D_VRip--;
241  D_VOpen = 0;
242  }
243  else
244  {
245  sprintf(msg, "%d %%", val);
246  MObjectSetText(sts[D_VRip], msg);
247  MStatusBarSetPos(sts[D_VRip], val);
248 
249  }
250  MLoopWhileEvents(0);
251 
252  }
253 }
254 
255 
256 
257 
258 void D_VRangeSim(int id, char *str, int val, int r, int g, int b, int x, int y)
259 {
260  char msg[16];
261  static MOBJECT sts[16][16], shell[16][16];
262  if(DD_VRip[id] < 0 && val == 100)
263  return;
264  if(DD_VRip[id] < 0 && val > 0)
265  return;
266  if(val == 0)
267  {
268  if(DD_VOpen[id]) return;
269  DD_VOpen[id] = 1;
270  DD_VRip[id]++;
272  MObjectSetBackgroundRGB(shell[id][DD_VRip[id]], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]);
273  sts[id][DD_VRip[id]] = MCreateStatusBar(shell[id][DD_VRip[id]], " 0 %", 100, HELV_SMALL);
274  MObjectSetBackgroundRGB(sts[id][DD_VRip[id]], DPAL.DB_BG[0], DPAL.DB_BG[1], DPAL.DB_BG[2]);
275  MStatusBarSetSliderRGB(sts[id][DD_VRip[id]], r, g, b);
276  MObjectSetCompositeHeight(sts[id][DD_VRip[id]], 20);
277 
278  MShellSetWMCloseCallback(shell[id][DD_VRip[id]], D_CloseVR, 0L);
279  MShellRealizeXY(shell[id][DD_VRip[id]], x, y);
280  MObjectResize(shell[id][DD_VRip[id]], 300, 35);
281  // Chiama MGUI per modificare size shell
282  MLoopWhileEvents(0);
283 
284  }
285  else
286  {
287  if(val >= 100)
288  {
289  MShellUnrealize(shell[id][DD_VRip[id]]);
290  MShellDestroy(shell[id][DD_VRip[id]]);
291  DD_VRip[id]--;
292  DD_VOpen[id] = 0;
293  }
294  else
295  {
296  sprintf(msg, "%d %%", val);
297  MObjectSetText(sts[id][DD_VRip[id]], msg);
298  MStatusBarSetPos(sts[id][DD_VRip[id]], val);
299  }
300  }
301 }
302 
303 
304 
305 
306 double CalcFluxIndex( unsigned short mis[], int dx, int dy, float te)
307 {
308 
309  int x;
310  long sumdata = 0;
311  double average = 0;
312  double fluxi = 0;
313 
314 
315  for(x = 200; x < dx - 200 ; x++)
316  {
317  sumdata += mis[ dy * dx + x];
318  }
319 
320  average = sumdata / (double) (dx - 400);
321 
322  fluxi = average / (double)(te * 1000000);
323 
324  return fluxi;
325 
326 }
327 
328 int DB_CalcFluxI(char *nomefile)
329 {
330 
331  FILE *fs;
332  FILE *fd;
333  char buf[512],str[512];
334  int er = 0, i, k;
335  char string[4] = ".FIN";
336  int fb;
337  double fluxindex;
338  char filedatan[256];
339  long numspettri;
340 
341 
342 // Lettura file elenco
343  i=0;
344  fs = fopen(nomefile, "r");
345  if(fs < 0) return 1;
346  while( !feof( fs ) )
347  {
348  fgets(str, 80, fs);
349  i++;
350  }
351  fclose(fs);
352 
353  numspettri = i * SAVECNT;
354 
355  D_VRange("Flux Index Calculation",0, 0, 222, 235);
356 
357 
358  fs = fopen(nomefile, "r");
359  if(fs < 0) return 2;
360 
361  //Read the file names
362  k = 0;
363  while( !feof( fs ) )
364  {
365  fscanf(fs, "%s", filedatan);
366  // build the complete file name
367 // strcpy(buf, BIL.DFILEM.WORKDIRECTORY);
368  strcat(buf, filedatan);
369 
370 
371  fb = open(filedatan, O_RDONLY | O_BINARY, S_IREAD );
372 
373  while( !eof( fb ) )
374  {
375 
376  MLoopWhileEvents(2);
377  k ++;
378  if((k % 10) == 0 && k > 0)
379  D_VRange(NULL, (long) k * 100 / numspettri, 0, 222, 235 );
380  er = ReadHeader_BIN(fb);
381 
382  if(BIL.DPLOT.ImatGraf == 0)
383  BIL.DPLOT.ImatGraf = AllocUSMat(IDX, IDY);
384 
385  ReadIMG_BIN(fb, BIL.DPLOT.ImatGraf, IDX, IDY);
386 
387  if( (strcmp(BIL.SPH.mirpos,"HorizDX") == 0) | (strcmp(BIL.SPH.mirpos,"Zenith") == 0) )
388  {
389  //Filter Data
390  //if (BIL.SPH.ccdwx - (BIL.SPH.ccdex +1) != 0) // se i pixel del sensore sono piu' di quelli in param.ini
391  // BlindPix_1 = BIL.SPH.ccdwx - (BIL.SPH.ccdex +1) ;
392 
393  //FilterData(BIL.DPLOT.ImatGraf, IDX, IDY, 15, BlindPix_1);
394 
395 
396  fluxindex = CalcFluxIndex( BIL.DPLOT.ImatGraf, IDX , IDY/2, (float)BIL.SPH.ccdtexp);
397 
398  sprintf(buf,"%d.FIN", BIL.SPH.wavelen);
399  strcpy(str, BIL.DFILEM.WORKDIRECTORY);
400  strcat(str,buf);
401  //MMessageDialog("Full FileName:", str, "ok", NULL);
402 
403  fd = fopen(str, "a");
404  if(fd < 0) return 1;
405 
406  sprintf(str,"%d/%02d/%02d %02d:%02d:%02d %d %lf\n", BIL.SPH.DATE.da_year, BIL.SPH.DATE.da_mon, BIL.SPH.DATE.da_day, BIL.SPH.TIME.ti_hour, BIL.SPH.TIME.ti_min, BIL.SPH.TIME.ti_sec, BIL.SPH.wavelen, fluxindex);
407  fprintf(fd, "%s", str);
408 
409  fclose(fd);
410  }
411 
412 
413 
414  }
415 // D_VRange(NULL, 100, 128, 254, 128);
416  close (fb);
417 
418 
419 
420  }
421 
422  D_VRange(NULL, 100, 0,0,0);
423 
424  fclose(fs);
425 
426  return 0;
427 }
428 
429 
430 
431 int DT_ColFormat(void)
432 {
433 
434  int er =0;
435  char fname[128];
436  char buff[_MAX_PATH];
437 // int l;
438 
439 
440  FB_PREFS fbp;
441 
442  fname[0] = '\0';
443  buff[0] = '\0';
444 
445  memset (&fbp, 0, sizeof (fbp));
446 
447  fbp.change_cwd = True;
448  fbp.allow_dir_create = True;
449  fbp.allow_dir_rename = True;
450  fbp.allow_dir_delete = True;
451  fbp.transparent_tree = False;
452  fbp.font = HELV_SMALL;
453  fbp.button_font = HELV_SMALL;
454  //Set Img file
455  sprintf(buff, "%s\\%s", BIL.DFILEM.HOMEDIRECTORY, "sys\\Tile2.bmp" );
456  fbp.bg_image_file =buff;
457 
458 /*
459  BIL.DFILEM.DBFILENAME[0] = 0;
460 
461  MDCreateFM_Shell(FOPEN, FDAT); //Open File, Dat ext.
462 
463  if(BIL.DFILEM.DBFILENAME[0] == 0)
464  return 1;
465 
466  DB_Column(BIL.DFILEM.DBFILENAME);
467 */
468 
470  er = MFileBrowseExt ("Select File for Column Format", "*.bif", fname, BIL.DFILEM.DATADIRECTORY, &fbp);
471  else
472  er = MFileBrowseExt ("Select File for column Format", "*.dat", fname, BIL.DFILEM.DATADIRECTORY, &fbp);
473  if(er)
474  {
475  sprintf(BIL.DFILEM.DBFILENAME,"%s%s",BIL.DFILEM.DATADIRECTORY, fname);
476  //l = strlen(fname);
477  //strncpy(buff, fname+l-4, 4);
478 
479  //if( (strcmp(fname+l-4,".dat") == 0) |(strcmp(fname+l-4,".Dat") == 0) )
480  // BIL.DFILEM.FKIND = FDAT;
481  //else if( (strcmp(fname+l-4,".bif") == 0) | (strcmp(fname+l-4,".Bif") == 0) )
482  // BIL.DFILEM.FKIND = FBIF;
483  //else
484  //{
485  // MMessageDialog("DAS information", "Unsupported format!!", " Ok ", NULL);
486  // return 1;
487  //}
489  }
490  else
491  return 1;
492 
493 
494  return 0;
495 
496 }
497 
498 
499 int DT_FluxIndex(void)
500 {
501 
502 
503 
504  BIL.DFILEM.DBFILENAME[0] = 0;
505  MDCreateFM_Shell(FOPEN, FELE); //Open File, Ele ext.
506 
507  if(BIL.DFILEM.DBFILENAME[0] == 0)
508  return 1;
509 
511 
512 
513 
514  return 0;
515 }
516 
517 void D_PrintSpectrum(FILE *fff)
518 {
519 
520  int x, y;
521 
522  for(y = 0; y < IDY; y++)
523  {
524  for(x = 0; x < IDX; x++)
525  {
526  if((x % 10) == 0)
527  {
528  if(y == 0 && x == 0)
529  {
530  }
531  else
532  fprintf(fff, "\n", NULL);
533  }
534  fprintf(fff, "%5u ", BIL.DPLOT.ImatGraf[y * IDX + x]);
535  }
536  }
537  fprintf(fff, "\n", NULL);
538 
539 }
540 void D_PrintBILSPH_BIN(int fd)
541 {
542 
543 
544  unsigned long info = 0;
545 
546 
547  info =0;
548 
549  info += write(fd, BIL.SPH.location, sizeof(BIL.SPH.location));
550  info += write(fd, &BIL.SPH.latitude, sizeof(BIL.SPH.latitude));
551  info += write(fd, &BIL.SPH.longitude, sizeof(BIL.SPH.longitude));
552  info += write(fd, &BIL.SPH.altitude, sizeof(BIL.SPH.altitude));
553  info += write(fd, &BIL.SPH.sza, sizeof(BIL.SPH.sza));
554  info += write(fd, &BIL.SPH.DATE, sizeof(struct ddate));
555  info += write(fd, &BIL.SPH.TIME, sizeof(struct dtime));
556 
557  info += write(fd, BIL.SPH.mirpos, sizeof(BIL.SPH.mirpos));
558  info += write(fd, &BIL.SPH.wavelen, sizeof(BIL.SPH.wavelen));
559  info += write(fd, BIL.SPH.filpos, sizeof(BIL.SPH.filpos));
560 
561  info += write(fd, &BIL.SPH.ccdtemp, sizeof(BIL.SPH.ccdtemp));
562  info += write(fd, &BIL.SPH.ccdtexp, sizeof(BIL.SPH.ccdtexp));
563  info += write(fd, &BIL.SPH.ccdmax, sizeof(BIL.SPH.ccdmax));
564 
565  info += write(fd, &BIL.SPH.imgtype, sizeof(BIL.SPH.imgtype));
566  info += write(fd, &BIL.SPH.ccdwx, sizeof(BIL.SPH.ccdwx));
567  info += write(fd, &BIL.SPH.ccdwy, sizeof(BIL.SPH.ccdwy));
568  info += write(fd, &BIL.SPH.ccdsx, sizeof(BIL.SPH.ccdsx));
569  info += write(fd, &BIL.SPH.ccdsy, sizeof(BIL.SPH.ccdsy));
570  info += write(fd, &BIL.SPH.ccdex, sizeof(BIL.SPH.ccdex));
571  info += write(fd, &BIL.SPH.ccdey, sizeof(BIL.SPH.ccdey));
572  info += write(fd, &BIL.SPH.ccdvb, sizeof(BIL.SPH.ccdvb));
573  info += write(fd, &BIL.SPH.ccdnr, sizeof(BIL.SPH.ccdnr));
574  info += write(fd, &BIL.SPH.dummy1, sizeof(BIL.SPH.dummy1));
575  info += write(fd, &BIL.SPH.dummy2, sizeof(BIL.SPH.dummy2));
576  info += write(fd, &BIL.SPH.dummy3, sizeof(BIL.SPH.dummy3));
577  info += write(fd, &BIL.SPH.dummy4, sizeof(BIL.SPH.dummy4));
578  info += write(fd, &BIL.SPH.comment, sizeof(BIL.SPH.comment));
579 
580 }
581 void D_PrintBILSPH(FILE *fff)
582 {
583 
584  fprintf(fff, "LOC: %s ", BIL.SPH.location); //Name of the station
585  fprintf(fff, "LAT: %.2lf ", BIL.SPH.latitude);//Station latitude
586  fprintf(fff, "LON: %.2lf ", BIL.SPH.longitude);//Station latitude
587  fprintf(fff, "ALT[m/asl]: %u ", BIL.SPH.altitude);//Station altitude
588  fprintf(fff, "SZA: %.2lf\n", BIL.SPH.sza);//SZA
589 
590  fprintf(fff, "%d/%02d/%02d ", BIL.SPH.DATE.da_year, BIL.SPH.DATE.da_mon, BIL.SPH.DATE.da_day);
591  fprintf(fff, "%02d:%02d:%02d LT ",BIL.SPH.TIME.ti_hour, BIL.SPH.TIME.ti_min,BIL.SPH.TIME.ti_sec);
592  fprintf(fff, "%s ", BIL.SPH.mirpos);
593  fprintf(fff, "%d ", BIL.SPH.wavelen);
594  fprintf(fff, "%s ", BIL.SPH.filpos);
595  fprintf(fff, "%.2lf ", BIL.SPH.ccdtemp);
596  fprintf(fff, "%.2lf ", BIL.SPH.ccdtexp);
597  fprintf(fff, "%5u ", BIL.SPH.ccdmax);
598  fprintf(fff, "%5u\n", BIL.SPH.ccdmin);
599 
600 
601  // Image format
602  fprintf(fff, "%d ", BIL.SPH.imgtype);
603  fprintf(fff, "%d ", BIL.SPH.ccdwx);
604  fprintf(fff, "%d ", BIL.SPH.ccdwy);
605  fprintf(fff, "%d ", BIL.SPH.ccdsx);
606  fprintf(fff, "%d ", BIL.SPH.ccdsy);
607  fprintf(fff, "%d ", BIL.SPH.ccdex);
608  fprintf(fff, "%d ", BIL.SPH.ccdey);
609  fprintf(fff, "%d ", BIL.SPH.ccdvb);
610  fprintf(fff, "%d ", BIL.SPH.ccdnr);
611  fprintf(fff, "%d ", BIL.SPH.dummy1);
612  fprintf(fff, "%d ", BIL.SPH.dummy2);
613  fprintf(fff, "%d ", BIL.SPH.dummy3);
614  fprintf(fff, "%d\n", BIL.SPH.dummy4);
615  fprintf(fff, "%s\n", BIL.SPH.comment);
616 
617 
618 }
619 
620 
621 void D_Print_BIL_H_ASCII(int mod, FILE *fff)
622 {
623 
624  fprintf(fff, "LOC: %s ", BIL.SPH.location); //Name of the station
625  fprintf(fff, "LAT: %.2lf ", BIL.SPH.latitude);//Station latitude
626  fprintf(fff, "LON: %.2lf ", BIL.SPH.longitude);//Station latitude
627  fprintf(fff, "ALT[m/asl]: %u ", BIL.SPH.altitude);//Station altitude
628  fprintf(fff, "SZA: %.2lf\n", BIL.SPH.sza);//SZA
629 
630  fprintf(fff, "%d/%02d/%02d ", BIL.SPH.DATE.da_year, BIL.SPH.DATE.da_mon, BIL.SPH.DATE.da_day);
631  fprintf(fff, "%02d:%02d:%02d LT ",BIL.SPH.TIME.ti_hour, BIL.SPH.TIME.ti_min,BIL.SPH.TIME.ti_sec);
632  fprintf(fff, "%s ", BIL.SPH.mirpos);
633  fprintf(fff, "%d ", BIL.SPH.wavelen);
634  fprintf(fff, "%s ", BIL.SPH.filpos);
635  fprintf(fff, "%.2lf ", BIL.SPH.ccdtemp);
636  fprintf(fff, "%.2lf ", BIL.SPH.ccdtexp);
637  fprintf(fff, "%5u ", BIL.SPH.ccdmax);
638  fprintf(fff, "%5u\n", BIL.SPH.ccdmin);
639 
640 
641  // Image format
642  fprintf(fff, "%d ", BIL.SPH.imgtype);
643  fprintf(fff, "%d ", BIL.SPH.ccdwx);
644  fprintf(fff, "%d ", BIL.SPH.ccdwy);
645  fprintf(fff, "%d ", BIL.SPH.ccdsx);
646  fprintf(fff, "%d ", BIL.SPH.ccdsy);
647  fprintf(fff, "%d ", BIL.SPH.ccdex);
648  fprintf(fff, "%d ", BIL.SPH.ccdey);
649  fprintf(fff, "%d ", BIL.SPH.ccdvb);
650  fprintf(fff, "%d ", BIL.SPH.ccdnr);
651  fprintf(fff, "%d ", BIL.SPH.dummy1);
652  fprintf(fff, "%d ", BIL.SPH.dummy2);
653  fprintf(fff, "%d ", BIL.SPH.dummy3);
654  fprintf(fff, "%d\n", BIL.SPH.dummy4);
655  fprintf(fff, "%s\n", BIL.SPH.comment);
656 
657 
658 }
659 
660 
661 
663 {
664 
665  FILE *fs, *fd, *fc;
666  char buf[256],str[256];
667  int er = 0, i, k;
668 
669  char filedatan[32];
670  long numspettri;
671 
672 
673  BIL.DFILEM.DBFILENAME[0] = 0;
674  MDCreateFM_Shell(FOPEN, FELE); //Open File, Ele ext.
675 
676  if(BIL.DFILEM.DBFILENAME[0] == 0)
677  return 0;
678 
679 // Lettura file elenco
680  i=0;
681  fs = fopen(BIL.DFILEM.DBFILENAME, "r");
682  if(fs < 0) return 1;
683  //Prima lettura file elenco per determinare approssimativamente in numero di spettri presenti
684  while( !feof( fs ) )
685  {
686  fgets(str, 80, fs);
687  i++;
688  }
689  fclose(fs);
690 
691  numspettri = i * SAVECNT;
692 
693  D_VRange("Header file correction",0, 0, 222, 235);
694 
695 
696  fs = fopen(BIL.DFILEM.DBFILENAME, "r");
697  if(fs < 0) return 1;
698 
699  //Read the file names
700  k = 0;
701  while( !feof( fs ) )
702  {
703  fscanf(fs, "%s", filedatan);
704  // build the complete file name
705  strcpy(buf, BIL.DFILEM.WORKDIRECTORY);
706  strcat(buf, filedatan);
707 
708  fc = fopen(buf, "r");
709  if(fc < 0) return 1;
710 
711  while( !feof( fc ) )
712  {
713  MLoopWhileEvents(0);
714  k ++;
715  if((k % 10) == 0 && k > 0)
716  D_VRange(NULL, (long) k * 100 / numspettri, 0, 222, 235 );
717  er = ReadHeaderOldF(fc);
718  //Modifica alcuni parametri del Header
719  BIL.SPH.ccdwx = 1024;
720  BIL.SPH.ccdwy = 254;
721  BIL.SPH.ccdsx = 0;
722  BIL.SPH.ccdsy = 0;
723  BIL.SPH.ccdex = 1023;
724  BIL.SPH.ccdey = 253;
725 
726  if(BIL.DPLOT.ImatGraf == 0)
727  BIL.DPLOT.ImatGraf = AllocUSMat(IDX, IDY);
728 
729  ReadIMG(fc, BIL.DPLOT.ImatGraf, IDX, IDY);
730 
731 
732  sprintf(buf,"%s\\%s", "CORRECTED",filedatan);
733  strcpy(str, BIL.DFILEM.WORKDIRECTORY);
734  strcat(str,buf);
735  //MMessageDialog("Full FileName:", str, "ok", NULL);
736  fd = fopen(str, "a");
737  if(fd < 0) return 1;
738 
739  //Print
740  D_PrintBILSPH(fd);
741  D_PrintSpectrum(fd);
742 
743  fclose(fd);
744 
745 
746  }
747  fclose(fc);
748 
749  }
750 
751  D_VRange(NULL, 100, 0,0,0);
752 
753  fclose(fs);
754 
755  if(!BIL.DPLOT.ImatGraf == 0)
756  {
759 
760  }
761  return 0;
762 }
763 
764 
765 void ShiftImg(int hp, int vp)
766 {
767 
768  int x, y;
769 
770  for(y = 0; y < vp; y++)
771  {
772  for(x = 0; x < hp; x++)
773  {
774  if((x==0) & (y!=0))
775  BIL.DPLOT.ImatGraf[y * IDX + x] =BIL.DPLOT.ImatGraf[y * IDX + x+2]/2 + BIL.DPLOT.ImatGraf[y * IDX + x+3]/2;
776  else if((x==1) & (y!=0))
777  BIL.DPLOT.ImatGraf[y * IDX + x] =BIL.DPLOT.ImatGraf[y * IDX + x+1]/2 + BIL.DPLOT.ImatGraf[y * IDX + x+3]/2;
778  else
779  BIL.DPLOT.ImatGraf[y * IDX + x] =BIL.DPLOT.ImatGraf[y * IDX + x];
780  }
781  }
782 
783 
784 }
785 
787 {
788 
789  FILE *fs, *fd, *fc;
790  char buf[256],str[256];
791  int er = 0, i, k;
792 
793  char filedatan[32];
794  long numspettri;
795 
796 
797  BIL.DFILEM.DBFILENAME[0] = 0;
798  MDCreateFM_Shell(FOPEN, FELE); //Open File, Ele ext.
799 
800  if(BIL.DFILEM.DBFILENAME[0] == 0)
801  return 0;
802 
803 // Lettura file elenco
804  i=0;
805  fs = fopen(BIL.DFILEM.DBFILENAME, "r");
806  if(fs < 0) return 1;
807  //Prima lettura file elenco per determinare approssimativamente in numero di spettri presenti
808  while( !feof( fs ) )
809  {
810  fgets(str, 80, fs);
811  i++;
812  }
813  fclose(fs);
814 
815  numspettri = i * SAVECNT;
816 
817  D_VRange("Header file correction",0, 0, 222, 235);
818 
819 
820  fs = fopen(BIL.DFILEM.DBFILENAME, "r");
821  if(fs < 0) return 1;
822 
823  //Read the file names
824  k = 0;
825  while( !feof( fs ) )
826  {
827  fscanf(fs, "%s", filedatan);
828  // build the complete file name
829  strcpy(buf, BIL.DFILEM.WORKDIRECTORY);
830  strcat(buf, filedatan);
831 
832  fc = fopen(buf, "r");
833  if(fc < 0) return 1;
834 
835  while( !feof( fc ) )
836  {
837  MLoopWhileEvents(0);
838  k ++;
839  if((k % 10) == 0 && k > 0)
840  D_VRange(NULL, (long) k * 100 / numspettri, 0, 222, 235 );
841  er = ReadHeaderOnly(fc);
842  //Modifica alcuni parametri del Header
843  BIL.SPH.ccdwx = 1024;
844  BIL.SPH.ccdwy = 254;
845  BIL.SPH.ccdsx = 0;
846  BIL.SPH.ccdsy = 0;
847  BIL.SPH.ccdex = 1023;
848  BIL.SPH.ccdey = 253;
849 
850 
851  if(BIL.DPLOT.ImatGraf == 0)
852  BIL.DPLOT.ImatGraf = AllocUSMat(IDX, IDY);
853 
854 
855 
856  ReadIMG(fc, BIL.DPLOT.ImatGraf, IDX, IDY);
857 
858 
859  ShiftImg(IDX, IDY);
860 
861 
862  sprintf(buf,"%s\\%s", "CORRECTED",filedatan);
863  strcpy(str, BIL.DFILEM.WORKDIRECTORY);
864  strcat(str,buf);
865  //MMessageDialog("Full FileName:", str, "ok", NULL);
866  fd = fopen(str, "a");
867  if(fd < 0) return 1;
868 
869  //Print
870  D_PrintBILSPH(fd);
871  D_PrintSpectrum(fd);
872 
873  fclose(fd);
874 
875 
876  }
877  fclose(fc);
878 
879  }
880 
881  D_VRange(NULL, 100, 0,0,0);
882 
883  fclose(fs);
884 
885  if(!BIL.DPLOT.ImatGraf == 0)
886  {
889  }
890  return 0;
891 }
892 
893 int Model_read(char *buff)
894 {
895 
896  FILE *fs;
897 
898 
899  fs = fopen(BIL.DFILEM.DBFILENAME, "r");
900  if(fs < 0) return 1;
901 
902  while( !feof( fs ) )
903  {
904 // fscanf(fs, "%s", str); //comment
905 
906 
907  }
908 
909 
910  fclose(fs);
911  return 0;
912 }
913 
914 
915 int DT_LinkSpectra(void)
916 {
917 
918  BIL.DFILEM.DBFILENAME[0] = 0;
919  MDCreateFM_Shell(FOPEN, FMOD); //Open File, mod ext.
920 
921  if(BIL.DFILEM.DBFILENAME[0] == 0)
922  return 1;
923 
925 
926  return 0;
927 
928 
929 }
int DB_CalcFluxI(char *nomefile)
Definition: Utils.c:328
struct ddate DATE
Definition: bildef.h:134
void MObjectSetBackgroundRGB(MOBJECT obj, int r, int g, int b)
int D_VRip
Definition: Utils.c:148
char comment[255]
Definition: bildef.h:157
unsigned int ccdmax
Definition: bildef.h:142
int IDX
Number of sensible horizontal pixels.
Definition: DAS_Spat.c:118
palette DPAL
Definition: Utils.c:28
int ReadHeaderOnly(FILE *fd)
read the ASCII header of the stored spectrum
Definition: Save.c:704
void MLoopWhileEvents(int discard)
optionini DOption
Options for DAS execution.
int savingmode
Flag to set saving mode: 0=ASCII, 1=Binary.
void MShellRealize(MOBJECT obj)
CARD8 change_cwd
Definition: Mguidefs.h:1485
#define O_BINARY
Definition: Dildef.h:201
#define SF_NO_MAXIMIZE
Definition: Mguidefs.h:1034
int DT_CorrFileEvora(void)
Used to correct some data files.
Definition: Utils.c:786
CARD8 allow_dir_create
Definition: Mguidefs.h:1487
Control Panel and push buttons colors .
Definition: bildef.h:83
MTFont button_font
Definition: Mguidefs.h:1501
CARD8 allow_dir_delete
Definition: Mguidefs.h:1493
#define FMOD
Definition: bildef.h:228
char HOMEDIRECTORY[_MAX_PATH]
Definition: bildef.h:200
MTFont font
Definition: Mguidefs.h:1499
#define False
Definition: Mguidefs.h:236
void MStatusBarSetPos(MOBJECT p, int pos)
das DAS
Definition: Chktemp.c:26
unsigned int altitude
Altitude of the Station.
Definition: bildef.h:131
char DATADIRECTORY[_MAX_PATH]
Definition: bildef.h:201
int D_VOpen
Definition: Utils.c:148
Definition: Dildef.h:423
#define SF_NO_MINIMIZE
Definition: Mguidefs.h:1036
void polint(float xa[], float ya[], int n, float x, float *y, float *dy)
Definition: Utils.c:42
spectrumheader SPH
Spectrum Header Structure.
Definition: bildef.h:256
void ReadIMG(FILE *file, unsigned short *mat, int hp, int vp)
Read ASCII Image (Spectral Data only) .
Definition: Spat_Plot.c:1206
void D_VRange(char *str, int val, int r, int g, int b)
Create and display the status bar.
Definition: Utils.c:206
void MShellUnrealize(MOBJECT obj)
unsigned char ti_hour
Definition: bildef.h:79
void MObjectSetText(MOBJECT obj, const char *text)
int DT_ColFormat(void)
Callback for Column format.
Definition: Utils.c:431
float * vector()
bil BIL
Definition: Utils.c:27
void MShellDestroy(MOBJECT obj)
File Browser Preferences. Used by the Utility functions MFileBowserExt() and MDirBowserExt().
Definition: Mguidefs.h:1482
int DT_LinkSpectra(void)
For the linked spectra (..on going!!!)
Definition: Utils.c:915
CARD8 transparent_tree
Definition: Mguidefs.h:1495
d_fileman DFILEM
Definition: bildef.h:257
void D_PrintSpectrum(FILE *fff)
Print Spectrum.
Definition: Utils.c:517
#define SAVECNT
maximum number of spectra in one file
MOBJECT MCreateStatusBar(MOBJECT parent, const char *text, int max, MTFont font)
double * dvector()
unsigned short * ImatOrig
Definition: bildef.h:180
void __stdcall DD_VRange(const char *str, int val, int Port)
Definition: Utils.c:153
void MObjectSetCompositeHeight(MOBJECT obj, int h)
unsigned char ti_sec
Definition: bildef.h:81
d_view DPLOT
Definition: bildef.h:254
#define HELV_SMALL
Definition: Mguidefs.h:881
char filpos[5]
Definition: bildef.h:139
unsigned short * DeallocUSMat(unsigned short *mat)
Definition: Spat_Plot.c:145
char DBFILENAME[_MAX_PATH]
Definition: bildef.h:204
int ReadHeader_BIN(int fd)
read the BINARY header of the stored spectrum
Definition: Spat_Plot.c:1798
char mirpos[8]
Definition: bildef.h:137
struct dtime TIME
Definition: bildef.h:133
MOBJECT MDCreateFM_Shell(int mod, int fmt)
Definition: DFileMan.c:407
int MFileBrowseExt(const char *t, const char *ext, char *fname, char *dname, FB_PREFS *prefs)
void D_PrintBILSPH(FILE *fff)
Print Header.
Definition: Utils.c:581
void ReadIMG_BIN(int fd, unsigned short *mat, int hp, int vp)
read the BINARY stored spectrum
Definition: Spat_Plot.c:1166
void MStatusBarSetSliderRGB(MOBJECT p, int r, int g, int b)
int IDY
Number of sensible vertical pixels.
Definition: DAS_Spat.c:119
void ShiftImg(int hp, int vp)
Used to correct some data files.
Definition: Utils.c:765
double CalcFluxIndex(unsigned short mis[], int dx, int dy, float te)
calculates the Flux Index of a measurement
Definition: Utils.c:306
void MShellRealizeXY(MOBJECT obj, int x, int y)
Function prototypes.
unsigned char Port
Definition: bildef.h:77
const char * bg_image_file
Definition: Mguidefs.h:1519
#define _MAX_PATH
Definition: Dildef.h:202
MOBJECT MCreateShell(const char *title, int flags)
short DB_BG[3]
Default Background Color.
unsigned short * ImatGraf
Definition: bildef.h:176
Function prototypes.
#define FOPEN
Definition: bildef.h:217
float ccdtexp
Definition: bildef.h:140
int Model_read(char *buff)
Definition: Utils.c:893
static double p
Definition: SOLPOS.C:131
int DT_CorrHeader()
Used to correct the header in some data files.
Definition: Utils.c:662
unsigned char ti_min
Definition: bildef.h:78
Bortul Image Library Structure .
Definition: bildef.h:250
#define SF_SAVEUNDER
Definition: Mguidefs.h:1015
float longitude
Longitude of the Station.
Definition: bildef.h:130
unsigned char da_mon
Definition: bildef.h:86
void free_vector()
char WORKDIRECTORY[_MAX_PATH]
Definition: bildef.h:202
float latitude
Latitude of the Station.
Definition: bildef.h:129
int DT_FluxIndex(void)
Callback for Flux Index Calculation.
Definition: Utils.c:499
void * MOBJECT
Definition: Mguidefs.h:192
unsigned char da_day
Definition: bildef.h:85
#define True
Definition: Mguidefs.h:235
char location[4]
Name of the Station (BLQ, LPA, EVR, TNB, DMC.....)
Definition: bildef.h:128
int DD_VOpen[16]
Definition: Utils.c:151
short int da_year
Definition: bildef.h:84
void D_PrintBILSPH_BIN(int fd)
Print on file the binary header of the measured spectrum.
Definition: Utils.c:540
float ccdtemp
Definition: bildef.h:141
CARD8 allow_dir_rename
Definition: Mguidefs.h:1490
int DD_VRip[16]
Definition: Utils.c:150
#define FELE
Definition: bildef.h:223
unsigned short * AllocUSMat(int hp, int vp)
Definition: Spat_Plot.c:1994
void D_VRangeSim(int id, char *str, int val, int r, int g, int b, int x, int y)
init progress bar during simultaneous positioning
Definition: Utils.c:258
int polintlambda(void)
Definition: Utils.c:94
void MShellSetWMCloseCallback(MOBJECT p, WM_CLOSE_CB cb, void *a_data)
unsigned int ccdmin
Definition: bildef.h:143
int ReadHeaderOldF(FILE *fd)
read the ASCII header of the stored spectrum (OLD FORMAT - replaced by ReadHeaderOnly(FILE *fd) ...
Definition: Save.c:751
int DB_Column(char *sourcefilename)
Column format.
Definition: Save.c:1109
float sza
Solar Zenith Angle.
Definition: bildef.h:132
void D_Print_BIL_H_ASCII(int mod, FILE *fff)
Definition: Utils.c:621
void nrerror()
void D_CloseVR(MOBJECT p, void *od, void *ad)
Callback closing the status bar.
Definition: Utils.c:140
void MObjectResize(MOBJECT obj, int w, int h)
______________________________________________________________________________________
Generated on Mon Sep 18 2017 11:44:10 for DAS - Rel. 3.1.6 - 18/09/2017.