INTEGER getcal_(char *description, char *units, DOUBLE_PRECISION *low, DOUBLE_PRECISION *high, DOUBLE_PRECISION *scale, INTEGER *caltype) { if (getcal(fcstring(description), fcstring(units), &cinfo) == 0) { *low = cinfo.low; *high = cinfo.high; *scale = cinfo.scale; *caltype = cinfo.caltype; return (0L); } else return (-1L); }
/*======================================================================================== * Turns the passed value into a Y/M/D date */ int utCalendar2_cal( double val, ut_unit *dataunits, int *year, int *month, int *day, int *hour, int *minute, double *second, const char *calendar_name ) { int jdnew, ndinc, ierr, iorig, iround; double fdays, extra_seconds, tot_extra_seconds; int ndays; calcalcs_cal *cal2use; /* Following vars are saved between invocations and reused if the * passed units are the same as last time. */ static ut_unit *prev_units=NULL; static cv_converter *conv_user_units_to_days=NULL; static int y0, mon0, d0, h0, min0, jday0; static double s0, extra_seconds0; static char *prev_calendar=NULL; if( dataunits == NULL ) { fprintf( stderr, "Error, utCalendar2 passed a NULL units\n" ); return( UT_ENOINIT ); } if( have_initted == 0 ) initialize( ut_get_system(dataunits) ); /* Get the calendar we will be using, based on the passed name */ cal2use = getcal( calendar_name ); if( cal2use == NULL ) { unknown_cal_emit_warning( calendar_name ); cal2use = getcal( "Standard" ); } /* See if we are being passed the same units and calendar as last time. If so, * we can optimize by not recomputing all this junk */ if( (prev_units != NULL) && (prev_calendar != NULL) && (strcmp(prev_calendar,cal2use->name)==0) && (ut_compare( prev_units, dataunits ) == 0)) { /* Units and calendar are same as used last call */ } else { /* Units/calendar combo are different from previously saved units, must redo calcs */ if( prev_units != NULL ) ut_free( prev_units ); if( prev_calendar != NULL ) free( prev_calendar ); /* Get origin day of the data units */ get_origin( dataunits, &y0, &mon0, &d0, &h0, &min0, &s0 ); /* Note: static vars */ /* Number of seconds into the specified origin day */ extra_seconds0 = h0*3600.0 + min0*60.0 + s0; /* Note: static vars */ /* Convert the origin day to Julian Day number in the specified calendar */ if( (ierr = ccs_date2jday( cal2use, y0, mon0, d0, &jday0 )) != 0 ) { fprintf( stderr, "Error in utCalendar2: %s\n", ccs_err_str(ierr) ); return( UT_EINVALID ); } /* Get converter from user-specified units to "days" */ if( conv_user_units_to_days != NULL ) cv_free( conv_user_units_to_days ); conv_user_units_to_days = get_user_to_day_converter( dataunits, y0, mon0, d0, h0, min0, s0 ); /* Save these units so we can reuse our time-consuming * calculations next time if they are the same units */ prev_units = ut_clone( dataunits ); if( ut_compare( prev_units, dataunits ) != 0 ) { fprintf( stderr, "error, internal error in udunits2 library found in routine utCalendar2: a clone of the user's units does not equal the original units!\n" ); exit(-1); } prev_calendar = (char *)malloc( sizeof(char) * (strlen(cal2use->name)+1 )); strcpy( prev_calendar, cal2use->name ); } /* Convert user value of offset to floating point days */ fdays = cv_convert_double( conv_user_units_to_days, val ); /* Get integer number of days and seconds past that */ ndays = fdays; extra_seconds = (fdays - ndays)*86400.0; /* Get new Julian day */ jdnew = jday0 + ndays; /* Handle the sub-day part */ tot_extra_seconds = extra_seconds0 + extra_seconds; ndinc = tot_extra_seconds / 86400.0; jdnew += ndinc; tot_extra_seconds -= ndinc*86400.0; if( tot_extra_seconds < 0.0 ) { tot_extra_seconds += 86400.0; jdnew--; } /* Convert to a date */ if( (ierr = ccs_jday2date( cal2use, jdnew, year, month, day )) != 0 ) { fprintf( stderr, "Error in utCalendar2: %s\n", ccs_err_str(ierr) ); return( UT_EINVALID ); } *hour = tot_extra_seconds / 3600.0; tot_extra_seconds -= *hour * 3600.0; *minute = tot_extra_seconds / 60.0; tot_extra_seconds -= *minute * 60.0; *second = tot_extra_seconds; /* Handle the rouding issues */ iorig = *second; /* Integer conversion */ iround = *second + sec_rounding_value; if( iround > iorig ) { /* printf( "rounding alg invoked, orig date: %04d-%02d-%02d %02d:%02d:%.20lf\n", *year, *month, *day, *hour, *minute, *second ); */ *second = (double)iround; if( *second >= 60.0 ) { *second -= 60.0; *minute += 1.0; if( *minute >= 60.0 ) { *minute -= 60.0; *hour += 1.0; if( *hour >= 24.0 ) { *hour -= 24.0; if( (ierr = ccs_jday2date( cal2use, jdnew+1, year, month, day )) != 0 ) { fprintf( stderr, "Error in utCalendar2: %s\n", ccs_err_str(ierr) ); return( UT_EINVALID ); } } } } /* printf( "after rounding alg here is new date: %04d-%02d-%02d %02d:%02d:%02.20lf\n", *year, *month, *day, *hour, *minute, *second ); */ } return(0); }
/*======================================================================================== * Turn the passed Y/M/D date into a value in the user's units */ int utInvCalendar2_cal( int year, int month, int day, int hour, int minute, double second, ut_unit *user_unit, double *value, const char *calendar_name ) { int jday, ierr, diff_in_days; double fdiff_in_days, val_days, val_partdays, fdiff_in_partdays, fpartday; calcalcs_cal *cal2use; /* Following vars are static and retained between invocations for efficiency */ static ut_unit *prev_units=NULL; static int y0, mon0, d0, h0, min0, jday0; static double s0, fpartday0; static cv_converter *conv_days_to_user_units=NULL; static char *prev_calendar=NULL; if( have_initted == 0 ) initialize( ut_get_system(user_unit) ); /* Get the calendar we will be using, based on the passed name */ cal2use = getcal( calendar_name ); if( cal2use == NULL ) { unknown_cal_emit_warning( calendar_name ); cal2use = getcal( "Standard" ); } if( (prev_units != NULL) && (prev_calendar != NULL) && (strcmp(prev_calendar,cal2use->name)==0) && (ut_compare( prev_units, user_unit ) == 0)) { /* Units are same as used last call */ } else { if( prev_units != NULL ) ut_free( prev_units ); if( prev_calendar != NULL ) free( prev_calendar ); if( conv_days_to_user_units != NULL ) cv_free( conv_days_to_user_units ); /* Get origin day of the data units */ get_origin( user_unit, &y0, &mon0, &d0, &h0, &min0, &s0 ); /* Note: static vars */ /* Convert the origin day to Julian Day number in the specified calendar */ if( (ierr = ccs_date2jday( cal2use, y0, mon0, d0, &jday0 )) != 0 ) { fprintf( stderr, "Error in utCalendar2: %s\n", ccs_err_str(ierr) ); return( UT_EINVALID ); } /* Get the origin's HMS in fractional (floating point) part of a Julian day */ fpartday0 = (double)h0/24.0 + (double)min0/1440.0 + s0/86400.0; /* Get converter for turning days into user's units */ conv_days_to_user_units = get_day_to_user_converter( user_unit, y0, mon0, d0, h0, min0, s0 ); /* Save these units so we can reuse our time-consuming * calculations next time if they are the same units */ prev_units = ut_clone( user_unit ); if( ut_compare( prev_units, user_unit ) != 0 ) { fprintf( stderr, "error, internal error in udunits2 library found in routine utInvCalendar2: a clone of the user's units does not equal the original units!\n" ); exit(-1); } prev_calendar = (char *)malloc( sizeof(char) * (strlen(cal2use->name)+1 )); strcpy( prev_calendar, cal2use->name ); } /* Turn passed date into a Julian day */ if( (ierr = ccs_date2jday( cal2use, year, month, day, &jday )) != 0 ) { fprintf( stderr, "Error in utInvCalendar2: %s\n", ccs_err_str(ierr) ); return( UT_EINVALID ); } /* jday and jday0 can be very large and nearly equal, so we difference * them first to keep the precision high */ diff_in_days = jday - jday0; fdiff_in_days = (double)diff_in_days; /* Get the fractional (floating point) part of a Julian day difference */ fpartday = (double)hour/24.0 + (double)minute/1440.0 + second/86400.0; fdiff_in_partdays = fpartday - fpartday0; /* Convert days and partial days to user's units */ val_days = cv_convert_double( conv_days_to_user_units, fdiff_in_days ); val_partdays = cv_convert_double( conv_days_to_user_units, fdiff_in_partdays ); /* Hopefully this will minimize the roundoff errors */ *value = val_days + val_partdays; return(0); }
main(int argc, char *argv[]) { extern int ns; float delta_t,wd,b0,b1,b2,b3,chisq,fov; int size,nf,ne,wd2,nord,maxproj; int j; float wrkbuff0[maxsize],wrkbuff1[maxsize],wrkbuff2[maxsize],wrkbuff3[maxsize]; FILE fil_out; shims shim_val[maxshim]; float shim_cal[(maxshim+1)*maxshim]; poly_coeff a1,a2,a3,chi; char ch; char string[100], fil_in[100], logfile[255], errfile[255], calfile[255]; FILE *fp, *fp1; fprintf(stderr," %d arguments passed to %s\n",argc,argv[0]); if (argc == 13) { ns=3; } if (argc == 16) { ns=6; } strcpy(fil_in,argv[1]); getdata(&size,&maxproj,&wrkbuff2[0],fil_in); delta_t = (float)atof(argv[2]); nord = atoi(argv[3]); fov = atof(argv[4]); strcpy(logfile,getenv("vnmruser")); strcat(logfile,"/shims/fastmap.log"); fp = fopen(logfile,"a"); fprintf(fp," the following units are in Hz/cm^n \n"); fprintf(fp,"nf j a0 a1 a2 a3 (RMSD)\n"); fclose(fp); fprintf(stderr," the following units are in Hz/cm^n \n"); fprintf(stderr,"nf j a0 a1 a2 a3 (RMSD)\n"); #ifdef DEBUG fprintf(stderr,"Before the first for instruction : nf = %d and ns = %d\n", nf, ns); #endif for (nf = 0; nf<ns; nf++) { a1[nf] = 0.0; a2[nf] = 0.0; a3[nf] = 0.0; chi[nf] = 0.0; fprintf(stderr,"ne value : %d and maxproj value : %d \n",ne,maxproj); for (ne=1; ne<maxproj/ns; ne++) { wd = atof(argv[5+nf]); wd2 = (int)(size*wd*0.25); getphase(size,nf,wd2,&wrkbuff1[0],&wrkbuff2[0],(int)(nf*maxproj/ns),(int)(nf*maxproj/ns+ne)); #ifdef DEBUG fprintf(stderr,"%s %f %d %d \n",fil_in,delta_t,wd2,nord); #endif fprintf(stderr,"%d %+7.4f ",nf,ne*delta_t); #ifdef DEBUG fprintf(stderr,"Before calpolycoeff : wd2=%d , nf=%d, size=%d, fov=%f, nord=%d \n",wd2, nf, size, fov, nord); #endif calpolycoeff(wd2,(nf+1),size,fov,delta_t*ne,wrkbuff1,&wrkbuff0[0],&b0,&b1,&b2,&b3,&chisq,nord); #ifdef DEBUG fprintf(stderr,"After calpolycoeff and before the if\n"); #endif if (ne>0) { for (j = 0; j<2*wd2; j++) { wrkbuff3[(nf*maxproj/ns+ne)*size+2*j] = wrkbuff0[(nf*2+1)*size+2*j]; wrkbuff3[(nf*maxproj/ns+ne)*size+2*j+1] = wrkbuff0[(nf*2)*size+2*j]; } } chisq*=chisq; a1[nf]=a1[nf]+b1/chisq; a2[nf]=a2[nf]+b2/chisq; a3[nf]=a3[nf]+b3/chisq; chi[nf]=chi[nf]+1/chisq; if (((delta_t<0.0001) || (chisq>2.0)) && (ne == 1)) { strcpy(errfile,getenv("vnmruser")); strcat(errfile,"/shims/fastmap.err"); fp1 = fopen(errfile,"a"); if (chisq>1.0E04) { fprintf(fp1,"ERROR: Not enough Sensitivity in the %dth projection \n Check position, Rx gain and tpwr\n",nf+1); } if ((delta_t<0.0001) && (nf<1)) { fprintf(fp1,"ERROR: Not enough B0 encoding : Check tau \n"); } if ((chisq>2.25) && (chisq<1.000001E04)) { fprintf(fp1,"ERROR: RMSD Phase error in the %dth projection is above 1.5 Hz\n Check tpwr, tau, sensitivity and B0 homogeneity \n",nf+1); } fclose(fp1); } } a1[nf]=a1[nf]/chi[nf]; a2[nf]=a2[nf]/chi[nf]; a3[nf]=a3[nf]/chi[nf]; } fprintf(stderr," AFTER the FOR loop ...\n"); if (maxproj/ns>2) { for (nf = 0; nf<ns; nf++) fprintf(stderr," %d %+6.3f %+6.3f %+6.3f\n",nf, a1[nf],a2[nf],a3[nf]); } putdata(&size,&wrkbuff3[0],fil_in); printf("\n"); strcpy(calfile,getenv("FASTMAP")); strcat(calfile,"/calib/."); strcat(calfile,argv[5+ns]); getcal(calfile,&shim_cal[0]); cal_shim(&argv[7+ns],a1,a2,&shim_val[0],&shim_cal[0]); printf("\n"); }
int sup_dgs () { /* declarations */ char str[STRLEN]; int i, i1, i2, i7, i8; FILE *fp; void getcal(char *); //char file_name[]="./dgscal.dat.350"; // place this is sort directory char file_name[]="./dgscal.dat"; // place this is sort directory gDirectory->mkdir("dgs")->cd(); // functions for making root histograms //TH2F *make2D(const char*, int, int, int, int, int, int); //TH1D *make1D(const char*, int ,int ,int); // 2-D's for Rate //hEventCounter = make1D("EvntCounter",14400,0,14400); // Good for 4 hours if Counts/sec //hGeCounter = make2D("GeCounter",14400,0,14400,110,1,111); //hBGOCounter = make2D("BGOCounter",14400,0,14400,110,1,111); // 2-D's for Energy //hEhiRaw = make2D("EhiRaw",16384,0,16384,110,1,111); //hEhiCln = make2D("EhiCln",16384,0,16384,110,1,111); //hEhiDrty = make2D("EhiDrty",16384,0,16384,110,1,111); // 2-D's for Tacs //hGeBGO_DT = make2D("GeBGO_DT",400,-200,200,110,1,111); // 2-D's for PZ and Baseline //hTrB = make2D("TrBase",4096,0,4096,110,1,111); //hFwB = make2D("FwBase",4096,0,4096,110,1,111); // for (int i = 1; i < NGE+1; i++ ){ // sprintf(str, "E_TrB%i", i) ; //hE_TrB[i] = make2D(str,2500,0,5000,1024,0,8192); // } /* list what we have */ //printf (" we have define the following spectra:\n"); Pars.wlist = gDirectory->GetList (); //Pars.wlist->Print (); /* -------------------- */ /* read in the map file */ /* -------------------- */ for (i = 0; i < NCHANNELS; i++) { tlkup[i] = NOTHING; tid[i] = NOTHING; }; fp = fopen ("map.dat", "r"); if (fp == NULL) { printf ("need a \"map.dat\" file to run\n"); //system("./mkMap > map.dat"); //printf("just made you one...\n"); //fp = fopen ("map.dat", "r"); //assert(fp != NULL); }; printf ("\nmapping - started\n"); i2 = fscanf (fp, "\n%i %i %i %s", &i1, &i7, &i8, str); printf ("%i %i %i %s\n", i1, i7, i8, str); while (i2 == 4) { tlkup[i1] = i7; tid[i1] = i8; i2 = fscanf (fp, "\n%i %i %i %s", &i1, &i7, &i8, str); //printf ("%i %i %i %s\n", i1, i7, i8, str); }; fclose (fp); printf ("\nmapping - complete!!\n"); // Set Default Calibration parameters for (i = 0; i < NGE+1; i++) { printf ("\nsup_dgs %i \n",i); ehigain[i] = 1.0; ehioffset[i] = 0.0; ehiPZ[i]=1.0; ehibase[i]=0.0; }; // This is the DGS calibration file getcal(file_name); printf ("\nsup_dgs done!!\n"); return(0); }
int fetchsignals(void) { int first = 1, framelen, i, imax, imin, j, *m, *mp, n; WFDB_Calinfo cal; WFDB_Sample **sb, **sp, *sbo, *spo, *v; WFDB_Time t, ts0, tsf; /* Do nothing if no samples were requested. */ if (nosig < 1 || t0 >= tf) return (0); /* Open the signal calibration database. */ (void)calopen("wfdbcal"); if (tfreq != ffreq) { ts0 = (WFDB_Time)(t0*tfreq/ffreq + 0.5); tsf = (WFDB_Time)(tf*tfreq/ffreq + 0.5); } else { ts0 = t0; tsf = tf; } /* Allocate buffers and buffer pointers for each selected signal. */ SUALLOC(sb, nsig, sizeof(WFDB_Sample *)); SUALLOC(sp, nsig, sizeof(WFDB_Sample *)); for (n = framelen = 0; n < nsig; framelen += s[n++].spf) if (sigmap[n] >= 0) { SUALLOC(sb[n], (int)((tf-t0)*s[n].spf + 0.5), sizeof(WFDB_Sample)); sp[n] = sb[n]; } /* Allocate a frame buffer and construct the frame map. */ SUALLOC(v, framelen, sizeof(WFDB_Sample)); /* frame buffer */ SUALLOC(m, framelen, sizeof(int)); /* frame map */ for (i = n = 0; n < nsig; n++) { for (j = 0; j < s[n].spf; j++) m[i++] = sigmap[n]; } for (imax = framelen-1; imax > 0 && m[imax] < 0; imax--) ; for (imin = 0; imin < imax && m[imin] < 0; imin++) ; /* Fill the buffers. */ isigsettime(t0); for (t = t0; t < tf && getframe(v) > 0; t++) for (i = imin, mp = m + imin; i <= imax; i++, mp++) if ((n = *mp) >= 0) *(sp[n]++) = v[i]; /* Generate output. */ printf(" { \"signal\":\n [\n"); for (n = 0; n < nsig; n++) { if (sigmap[n] >= 0) { char *p; int delta, prev; if (!first) printf(",\n"); else first = 0; printf(" { \"name\": %s,\n", p = strjson(sname[n])); SFREE(p); if (s[n].units) { printf(" \"units\": %s,\n", p = strjson(s[n].units)); SFREE(p); } else printf(" \"units\": \"mV\",\n"); printf(" \"t0\": %ld,\n", (long)ts0); printf(" \"tf\": %ld,\n", (long)tsf); printf(" \"gain\": %g,\n", s[n].gain ? s[n].gain : WFDB_DEFGAIN); printf(" \"base\": %d,\n", s[n].baseline); printf(" \"tps\": %d,\n", (int)(tfreq/(ffreq*s[n].spf)+0.5)); if (getcal(sname[n], s[n].units, &cal) == 0) printf(" \"scale\": %g,\n", cal.scale); else printf(" \"scale\": 1,\n"); printf(" \"samp\": [ "); for (sbo = sb[n], prev = 0, spo = sp[n]-1; sbo < spo; sbo++) { delta = *sbo - prev; printf("%d,", delta); prev = *sbo; } printf("%d ]\n }", *sbo - prev); } } printf("\n ]%s", nann ? ",\n" : "\n }\n"); flushcal(); for (n = 0; n < nsig; n++) SFREE(sb[n]); SFREE(sb); SFREE(sp); SFREE(v); SFREE(m); return (1); /* output was written */ }