//初始化闹钟 //以1970年1月1日为基准 //1970~2099年为合法年份 //syear,smon,sday,hour,min,sec:闹钟的年月日时分秒 //返回值:0,成功;其他:错误代码. u8 RTC_Alarm_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec) { u16 t; u32 seccount=0; if(syear<1970||syear>2099)return 1; for(t=1970;t<syear;t++) //把所有年份的秒钟相加 { if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数 else seccount+=31536000; //平年的秒钟数 } smon-=1; for(t=0;t<smon;t++) //把前面月份的秒钟数相加 { seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加 if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数 } seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加 seccount+=(u32)hour*3600;//小时秒钟数 seccount+=(u32)min*60; //分钟秒钟数 seccount+=sec;//最后的秒钟加上去 //设置时钟 RCC->APB1ENR|=1<<28;//使能电源时钟 RCC->APB1ENR|=1<<27;//使能备份时钟 PWR->CR|=1<<8; //取消备份区写保护 //上面三步是必须的! RTC->CRL|=1<<4; //允许配置 RTC->ALRL=seccount&0xffff; RTC->ALRH=seccount>>16; RTC->CRL&=~(1<<4);//配置更新 while(!(RTC->CRL&(1<<5)));//等待RTC寄存器操作完成 return 0; }
u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec) { u16 t; u32 seccount=0; if(syear<1970||syear>2099)return 1; for(t=1970;t<syear;t++) //把所有年份的秒钟相加 { if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数 else seccount+=31536000; //平年的秒钟数 } smon-=1; for(t=0;t<smon;t++) //把前面月份的秒钟数相加 { seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加 if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数 } seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加 seccount+=(u32)hour*3600;//小时秒钟数 seccount+=(u32)min*60; //分钟秒钟数 seccount+=sec;//最后的秒钟加上去 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟 PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问 RTC_SetCounter(seccount); //设置RTC计数器的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 return 0; }
/** * @brief Update time * @param none * @retval None */ u8 Time_Update(u16 syear, u8 smon, u8 sday, u8 hour, u8 min, u8 sec) { u16 t; u32 seccount = 0; if (syear < 1970 || syear>2099)return 1; for (t = 1970; t < syear; t++) //把所有年份的秒钟相加 { if (Is_Leap_Year(t))seccount += 31622400; //闰年的秒钟数 else seccount += 31536000; //平年的秒钟数 } smon -= 1; for (t = 0; t < smon; t++) //把前面月份的秒钟数相加 { seccount += (u32)mon_table[t] * 86400; //月份秒钟数相加 if (Is_Leap_Year(syear) && t == 1)seccount += 86400; //闰年2月份增加一天的秒钟数 } seccount += (u32)(sday - 1) * 86400; //把前面日期的秒钟数相加 seccount += (u32)hour * 3600; //小时秒钟数 seccount += (u32)min * 60; //分钟秒钟数 seccount += sec; //最后的秒钟加上去 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 RTC_SetCounter(seccount); //设置RTC计数器的值 RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 return 0; }
//设置时钟 //把输入的时钟转换为秒钟 //以1970年1月1日为基准 //1970~2099年为合法年份 //返回值:0,成功;其他:错误代码. //月份数据表 //unsigned char const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5}; //月修正数据表 //平年的月份日期表 //const unsigned char mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31}; unsigned char RTC_Set(tim timer) { unsigned short t; unsigned int seccount=0; if(timer.year<1970||timer.year>2099)return 1; for(t=1970;t<timer.year;t++) //把所有年份的秒钟相加 { if(Is_Leap_Year(t)!=0)seccount+=31622400;//闰年的秒钟数 else seccount+=31536000; //平年的秒钟数 } timer.month-=1; for(t=0;t<timer.month;t++) //把前面月份的秒钟数相加 { seccount+=(unsigned int)mon_table[t]*86400;//月份秒钟数相加 if(Is_Leap_Year(timer.year)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数 } seccount+=(unsigned int)(timer.date-1)*86400;//把前面日期的秒钟数相加 seccount+=(unsigned int)timer.hour*3600;//小时秒钟数 seccount+=(unsigned int)timer.min*60; //分钟秒钟数 seccount+=timer.sec;//最后的秒钟加上去 //设置时钟 RCC->APB1ENR|=1<<28;//使能电源时钟 RCC->APB1ENR|=1<<27;//使能备份时钟 PWR->CR|=1<<8; //取消备份区写保护 //上面三步是必须的! RTC->CRL|=1<<4; //允许配置 RTC->CNTL=seccount&0xffff; RTC->CNTH=seccount>>16; RTC->CRL&=~(1<<4);//配置更新 while(!(RTC->CRL&(1<<5)));//等待RTC寄存器操作完成 return 0; }
//得到当前的时间 //返回值:0,成功;其他:错误代码. u8 RTC_Get(void) { static u16 daycnt=0; u32 timecount=0; u32 temp=0; u16 temp1=0; timecount=RTC->CNTH;//得到计数器中的值(秒钟数) timecount<<=16; timecount+=RTC->CNTL; temp=timecount/86400; //得到天数(秒钟数对应的) if(daycnt!=temp)//超过一天了 { daycnt=temp; temp1=1970; //从1970年开始 while(temp>=365) { if(Is_Leap_Year(temp1))//是闰年 { if(temp>=366)temp-=366;//闰年的秒钟数 else break; } else temp-=365; //平年 temp1++; } timer.w_year=temp1;//得到年份 temp1=0; while(temp>=28)//超过了一个月 { if(Is_Leap_Year(timer.w_year)&&temp1==1)//当年是不是闰年/2月份 { if(temp>=29)temp-=29;//闰年的秒钟数 else break; } else { if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年 else break; } temp1++; } timer.w_month=temp1+1;//得到月份 timer.w_date=temp+1; //得到日期 } temp=timecount%86400; //得到秒钟数 timer.hour=temp/3600; //小时 timer.min=(temp%3600)/60; //分钟 timer.sec=(temp%3600)%60; //秒钟 timer.week=RTC_Get_Week(timer.w_year,timer.w_month,timer.w_date);//获取星期 return 0; }
unsigned char RTC_Get_Time(tim *timer) { static unsigned short daycnt=0; unsigned int timecount=0; unsigned int temp=0; unsigned short temp1=0; RTC_WaitForLastTask(); timecount=RTC->CNTH;//得到计数器中的值(秒钟数) timecount<<=16; timecount+=RTC->CNTL; temp=timecount/86400; //得到天数(秒钟数对应的) if(daycnt!=temp)//超过一天了 { daycnt=temp; temp1=1970; //从1970年开始 while(temp>=365) { if(Is_Leap_Year(temp1))//是闰年 { if(temp>=366)temp-=366;//闰年的秒钟数 else {temp1++;break;} } else temp-=365; //平年 temp1++; } timer->year=temp1;//得到年份 temp1=0; while(temp>=28)//超过了一个月 { if(Is_Leap_Year(timer->year)&&temp1==1)//当年是不是闰年/2月份 { if(temp>=29)temp-=29;//闰年的秒钟数 else break; } else { if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年 else break; } temp1++; } timer->month=temp1+1;//得到月份 timer->date=temp+1; //得到日期 } temp=timecount%86400; //得到秒钟数 timer->hour=temp/3600; //小时 timer->min=(temp%3600)/60; //分钟 timer->sec=(temp%3600)%60; //秒钟 //timer->sec = 0; //秒钟 timer->week=RTC_Get_Week(timer->year,timer->month,timer->date);//获取星期 return 0; }
//得到当前的时间 //返回值:0,成功;其他:错误代码. u8 Time_Get(void) { static u16 daycnt = 0; u32 timecount = 0; u32 temp = 0; u16 temp1 = 0; xSemaphoreTake(RTCStructMutex, portMAX_DELAY); timecount = RTC_GetCounter(); //获得 RTC 计数器值(秒钟数) temp = timecount / 86400; //得到天数(秒钟数对应的) if (daycnt != temp)//超过一天了 { daycnt = temp; temp1 = 1970; //从1970年开始 while (temp >= 365) { if (Is_Leap_Year(temp1))//是闰年 { if (temp >= 366)temp -= 366;//闰年的秒钟数 else { temp1++; break; } } else temp -= 365; //平年 temp1++; } RTCTime.w_year = temp1;//得到年份 temp1 = 0; while (temp >= 28)//超过了一个月 { if (Is_Leap_Year(RTCTime.w_year) && temp1 == 1)//当年是不是闰年/2月份 { if (temp >= 29)temp -= 29;//闰年的秒钟数 else break; } else { if (temp >= mon_table[temp1])temp -= mon_table[temp1];//平年 else break; } temp1++; } RTCTime.w_month = temp1 + 1;//得到月份 RTCTime.w_date = temp + 1; //得到日期 } temp = timecount % 86400; //得到秒钟数 RTCTime.hour = temp / 3600; //小时 RTCTime.min = (temp % 3600) / 60; //分钟 RTCTime.sec = (temp % 3600) % 60; //秒钟 RTCTime.week = RTC_Get_Week(RTCTime.w_year, RTCTime.w_month, RTCTime.w_date);//获取星期 xSemaphoreGive(RTCStructMutex); return 0; }
int Get_Day_Of_Year(int i_iYear, int i_iMonth, int i_iDay) { int iDay = i_iDay; switch (i_iMonth) { case 12: iDay += 30; // November case 11: iDay += 31; // October case 10: iDay += 30; // September case 9: iDay += 31; // August case 8: iDay += 31; // July case 7: iDay += 30; // June case 6: iDay += 31; // May case 5: iDay += 30; // April case 4: iDay += 31; // March case 3: iDay += 28; // February if (Is_Leap_Year(i_iYear)) iDay ++; // February - leap year case 2: iDay += 31; // January } return iDay; }
char RTC_Check(tim timer) { if(Is_Leap_Year(timer.year)) { if((timer.month == 2) && (timer.date >mon_table[timer.month-1])) { return ERROR; // GUI_MessageBox("请输入正确的日期","error!!",0); } else { if(timer.date > mon_table[timer.month-1]) { return ERROR; /// GUI_MessageBox("请输入正确的日期","error!!",0); } } } else { if(timer.date > mon_table[timer.month-1]) { return ERROR; // GUI_MessageBox("请输入正确的日期","error!!",0); } } return SUCCESS; }
u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec) { u16 t; u32 seccount=0; if(syear<1970||syear>2099)return 1; for(t=1970;t<syear;t++) //把所有年份的秒钟相加 { if(Is_Leap_Year(t))seccount+=31622400;//闰年的秒钟数 else seccount+=31536000; //平年的秒钟数 } smon-=1; for(t=0;t<smon;t++) //把前面月份的秒钟数相加 { seccount+=(u32)mon_table[t]*86400;//月份秒钟数相加 if(Is_Leap_Year(syear)&&t==1)seccount+=86400;//闰年2月份增加一天的秒钟数 } seccount+=(u32)(sday-1)*86400;//把前面日期的秒钟数相加 seccount+=(u32)hour*3600;//小时秒钟数 seccount+=(u32)min*60; //分钟秒钟数 seccount+=sec;//最后的秒钟加上去 //设置时钟 //RCC->APB1ENR|=1<<28;//使能电源时钟 //RCC->APB1ENR|=1<<27;//使能备份时钟 //PWR->CR|=1<<8; //取消备份区写保护 //上面三步是必须的! //RTC->CRL|=1<<4; //允许配置 //RTC->CNTL=seccount&0xffff; //RTC->CNTH=seccount>>16; //RTC->CRL&=~(1<<4);//配置更新 //while(!(RTC->CRL&(1<<5)));//等待RTC寄存器操作完成 /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 /* Change the current time */ RTC_SetCounter(seccount); //设置RTC计数器的值 /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成 return 0; }
/** * @brief It asks user to change Date * @return status */ uint8_t BSP_RTC_Change_Date (void) { uint8_t DaysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; RTC_DateTypeDef D; D.WeekDay = RTC_WEEKDAY_MONDAY; D.Month = RTC_MONTH_MAY; D.Date = 1; D.Year = 00; uint8_t Daysthismonth; uint8_t DateBuffer[1]; char cp; /* input from keyboard */ char RTCdata; /* buffer */ uint8_t DateFlag=0; /* flag for date data is available */ uint8_t count = 0; /* input character counter */ uint8_t State = 0; /* state for dd=1, mm=2, ccyy=3 */ uint8_t digit = 0; /* indication for 1st, 2nd, 3rd and 4th digit */ /* for dd, mm, yycc */ uint8_t MaxCount = 8; /* maximum no. of digits */ char AsciiLSB; /* temp storage for LSB ascii input */ char AsciiMSB; /* temp storage for MSB ascii input */ uint8_t DaysThisMonth; /* number of days for the given month */ while(1) { cp = ugetche(BLOCKING); /* get input character */ if(cp == In_ESC) /* if ESCAPE pressed then exit */ { return (cp); } else if (cp == In_CR) /* CARRIAGE RETURN ? */ { if(count==0) /* any characters at all ? */ { continue; /* no, so get another character */ } if(DateFlag==1) { /* update real time clock */ /* Read RTC */ Year[0] = ((DateBuffer[0] & 0xFF) * 100); Year[0] += (DateBuffer[1] & 0xFF); D.Year = DateBuffer[1]; HAL_RTC_SetDate(&hrtc_bsp, &D, FORMAT_BIN); return(0); } else { break; /* check for any character */ } } else if (cp == In_DELETE || cp == In_BACKSPACE) /* delete or back space */ { if(count==0) /* any characters entered */ { continue; /* no, so get another character */ } if(State == 2) /* State 2 contains 4 digits */ { if(digit == 0) { uErase_Backslash(); digit = 1; /* now its point to 2nd digit of month */ State--; /* point to month's state */ count--; /* decrement character count */ continue; } else { uErase_Char_With_UnderScore(); digit--; /* point to 2nd digit */ count--; /* decrement count */ continue; /* go back and get another input */ } } else { if(digit ==0) { uErase_Backslash(); State--; /* point to month's state */ digit = 1; /* now its point to 2nd digit of month */ count--; /* decrement character count */ continue; } else { uErase_Char_With_UnderScore(); digit = 0; /* point to 1st digit */ count--; /* decrement count */ continue; /* goback and get another input */ } } } else if (cp>= '0' && cp <='9') /* is character between 0 to 9? */ { RTCdata = cp; /****** check date ****/ if(count < MaxCount) { if(State == 0) { if(digit==0) { uprintf("%c", RTCdata); /* echo 1st character */ AsciiMSB = RTCdata; /* store 1st byte MSB */ count++; /* increment character count,will be 1 */ digit++; /* increment digit, will be 1 */ continue; } else { uprintf("%c", RTCdata); /* echo 2nd character */ AsciiLSB = RTCdata; /* store 2nd byte LSB */ RTCdata = ((AsciiMSB & 0x0F) * 10); /* store nibble */ RTCdata += (AsciiLSB & 0x0F); /* add to nibble, integer */ if(RTCdata >= 1 && RTCdata <=31) /* check date range ( 1-31) */ { D.Date = RTCdata; /* store DOM in DateBuffer[0] */ State++; /* increment to mm state */ count++; /* increment character count,will be 2 */ digit = 0; /* set digit to zero */ uprintf("/"); /* and write '/' on the screen */ continue; /* continue for minutes */ } else { uErase_And_RingTheBell(); continue; } } } /************************* check month *************************/ if(State == 1) { if(digit==0) { uprintf("%c", RTCdata); /* echo 1st character */ AsciiMSB = RTCdata; /* store 1st byte MSB */ count++; /* increment character count,will be 3 */ digit++; /* increment digit, will be 1 */ continue; } else { uprintf("%c", RTCdata); /* echo 2nd character */ AsciiLSB = RTCdata; /* store 2nd byte LSB */ RTCdata = ((AsciiMSB & 0x0F) * 10); /* store nibble */ RTCdata += (AsciiLSB & 0x0F); /* add to nibble, integer */ if(RTCdata >= 1 && RTCdata <=12) /* check month range ( 1-12) */ { D.Month = RTCdata; /* store mm in time buffer */ State++; /* increment to ss state */ count++; /* increment character count,will be 4 */ digit = 0; /* set digit to zero */ uprintf("/"); /* and write '/' on the screen */ continue; /* continue for seconds */ } else { uErase_And_RingTheBell(); continue; } } } /********** check year and century *********/ if(State == 2) { if(digit == 0) { uprintf("%c", RTCdata); /* echo 1st character */ AsciiMSB = RTCdata; /* store 1st byte MSB */ count++; /* increment character count,will be 5 */ digit++; /* increment digit no. */ continue; } if(digit == 1) { uprintf("%c", RTCdata); /* echo 2nd character */ AsciiLSB = RTCdata; /* store 2nd byte LSB */ RTCdata = ((AsciiMSB & 0x0F) * 10); /* store nibble */ RTCdata += (AsciiLSB & 0x0F); /* add to nibble, integer */ if(RTCdata >= 0 && RTCdata <=40) /* check century range (0-39) */ { DateBuffer[0] = RTCdata; /* store in century buffer */ count++; /* increment character count,will be 6 */ digit++; /* increment digit number, will be 3 */ continue; } else { uErase_Char_With_UnderScore(); uprintf("\7"); /* ring the bell */ continue; } } /********** check year ************/ if(digit == 2) { uprintf("%c", RTCdata); /* echo 1st character */ AsciiMSB = RTCdata; /* store 1st byte MSB */ count++; /* increment character count,will be 7 */ digit++; /* increment digit no. */ continue; } if(digit == 3) { uprintf("%c", RTCdata); /* echo 2nd character */ AsciiLSB = RTCdata; /* store 2nd byte LSB */ RTCdata = ((AsciiMSB & 0x0F) * 10); /* store nibble */ RTCdata += (AsciiLSB & 0x0F); /* add to nibble, integer */ if(DateBuffer[0] == 40) { if(RTCdata <=95) /* check year range (00-95) */ { DateBuffer[1] = RTCdata; /* store in year buffer */ DateFlag = 1; /* Time data is ready for RTC */ count++; /* increment character count,will be 8 */ digit++; /* increment digit number, will be 4 */ continue; } else { uErase_Char_With_UnderScore(); uprintf("\7"); /* ring the bell */ continue; } } if(RTCdata <=99) /* check year range (00-99) */ { DateBuffer[1] = RTCdata; /* store in year buffer */ DaysThisMonth = DaysPerMonth[D.Month- 1]; if(Is_Leap_Year(DateBuffer[1], DateBuffer[0]) && (D.Month == 2)) { ++DaysThisMonth; } if(D.Date > DaysThisMonth) { D.Date = DaysThisMonth; } DateFlag = 1; /* Time data is ready for RTC */ count++; /* increment character count,will be 8 */ digit++; /* increment digit number, will be 4 */ continue; } else { uErase_Char_With_UnderScore(); uprintf("\7"); /* ring the bell */ continue; } } } continue; /* number of char is more than reqd */ /* so go back to find the decision */ } else { uprintf("%c", cp); /* echo character */ uErase_Char(); uprintf("\7"); /* ring the bell */ continue; } } } return(0); }
int main(int i_iArg_Count,const char * i_lpszArg_Values[]) { double dFit_Range_Min_WL,dFit_Range_Max_WL; bool bData_valid; char lpszTarget_Path[256]; char lpszTarget_File[256]; char lpszTarget_Complete_Path[256] = {""}; char lpszOutput_File_Prefix[256] = {""}; char lpszOutput_Filename[256]; char lpszTitle[256]; bool bData_Valid = false; xrsrand(); char lpszFitRegion[32]; char lpszFitModel[128]; if (i_iArg_Count < 3) { printf("Must specify file to do temp fitting.\n"); exit(1); } unsigned int uiPath_Idx = 1; while (i_lpszArg_Values[uiPath_Idx][0] == '-') uiPath_Idx++; int iYear_BMax = atoi(i_lpszArg_Values[uiPath_Idx]) / 10000; int iMonth_BMax = (atoi(i_lpszArg_Values[uiPath_Idx]) % 10000) / 100; int iDay_BMax = (atoi(i_lpszArg_Values[uiPath_Idx]) % 100); int iDay_Of_Year_BMax = Get_Day_Of_Year(iYear_BMax,iMonth_BMax,iDay_BMax); if (iYear_BMax < 1970) { printf("Usage:\n"); printf("psfit YYYYMMDD <file>\n"); printf("Date indicates day of Bmax\n"); exit(1); } uiPath_Idx++; while (i_lpszArg_Values[uiPath_Idx][0] == '-') uiPath_Idx++; strcpy(lpszTarget_Complete_Path,i_lpszArg_Values[uiPath_Idx]); strcpy(lpszTarget_Path,lpszTarget_Complete_Path); unsigned int uiI = strlen(lpszTarget_Path); while (lpszTarget_Path[uiI] != '/' && uiI > 0) uiI--; if (uiI != 0) { uiI++; lpszTarget_Path[uiI] = 0;// } else strcpy(lpszTarget_Path,"./"); strcpy(lpszTarget_File,&lpszTarget_Complete_Path[uiI]); sprintf(lpszOutput_File_Prefix,"%s%s/%s",lpszTarget_Path,"Temp",lpszTarget_File); ES::Spectrum cTarget = ES::Spectrum::create_from_ascii_file( lpszTarget_Complete_Path ); ES::Spectrum cGenerated = ES::Spectrum::create_from_ascii_file( lpszTarget_Complete_Path ); double dTemp; CompareFits(cTarget, cGenerated,dTemp); sprintf(lpszTitle,"Tps %.3f\n",dTemp); epsplot::PAGE_PARAMETERS cPlot_Parameters; epsplot::DATA cPlot; cPlot_Parameters.m_uiNum_Columns = 1; cPlot_Parameters.m_uiNum_Rows = 1; cPlot_Parameters.m_dWidth_Inches = 11.0; cPlot_Parameters.m_dHeight_Inches = 8.5; unsigned int uiX_Axis = cPlot.Set_X_Axis_Parameters( "Wavelength [A]", false, false, false, 0.0, false, 0.0); unsigned int uiY_Axis = cPlot.Set_Y_Axis_Parameters( "Flux", false, false, false, 5.0, false, 60.0); // cPlot.Set_Num_Plots(2); cPlot.Set_Plot_Title(lpszTitle,18.0); double dStipple[2] = {16,16}; // long long dash cPlot.Define_Custom_Stipple(epsplot::STPL_CUSTOM_1,dStipple,2); double dTarget_Minima_WL, dTarget_Minima_Flux, dFit_Minima_WL, dFit_Minima_Flux; Get_Normalization_Fluxes(cTarget, cGenerated, cTarget.wl(0), cTarget.wl(cTarget.size() - 1), dTarget_Minima_Flux, dFit_Minima_Flux); printf("Plot full\n"); Plot(cPlot_Parameters, cPlot, "%s.tempfit.full.eps", lpszOutput_File_Prefix, cTarget, cGenerated, dTarget_Minima_Flux, dFit_Minima_Flux,cTarget.wl(0), cTarget.wl(cTarget.size()- 1), uiX_Axis, uiY_Axis); // Plot(cPlot_Parameters, cPlot, "%s.tempfit.full.eps", lpszOutput_File_Prefix, cTarget, cGenerated, dTarget_Minima_Flux, dFit_Minima_Flux,7000.0,9000.0); sprintf(lpszOutput_Filename,"Temp/tempfit.data",lpszOutput_File_Prefix); FILE * fileOut = fopen(lpszOutput_Filename,"rt"); if (!fileOut) { fileOut = fopen(lpszOutput_Filename,"wt"); fprintf(fileOut,"File, Days (Bmax), Temp (kK), <L>\n"); } else { fclose(fileOut); fileOut = fopen(lpszOutput_Filename,"at"); } if (fileOut) { const char * lpszCursor = i_lpszArg_Values[uiPath_Idx]; while (lpszCursor[0] != '-' && lpszCursor[0] != 0) lpszCursor++; lpszCursor++; int iYear_Curr = atoi(lpszCursor) / 10000; int iMonth_Curr = (atoi(lpszCursor) % 10000) / 100; int iDay_Curr = (atoi(lpszCursor) % 100); int iDay_Of_Year_Curr = Get_Day_Of_Year(iYear_Curr,iMonth_Curr,iDay_Curr); double dDay_Rel = (iDay_Of_Year_Curr - iDay_Of_Year_BMax); while (iYear_Curr < iYear_BMax) { if (Is_Leap_Year(iYear_Curr)) dDay_Rel -= 366; else dDay_Rel -= 365; iYear_Curr++; } while (iYear_Curr > iYear_BMax) { if (Is_Leap_Year(iYear_Curr)) dDay_Rel += 366; else dDay_Rel += 365; iYear_Curr--; } unsigned int uiIdx = 0; double dLuminosity_Target = 0.0; while (uiIdx < cTarget.size() && cTarget.wl(uiIdx) < FIT_BLUE_WL) uiIdx++; while (uiIdx < cTarget.size() && cTarget.wl(uiIdx) < FIT_RED_WL) { double dDelta_Lambda; if (uiIdx > 0 && uiIdx < (cTarget.size() - 1)) dDelta_Lambda = (cTarget.wl(uiIdx + 1) - cTarget.wl(uiIdx - 1)) * 0.5; else if (uiIdx == 0) dDelta_Lambda = cTarget.wl(uiIdx + 1) - cTarget.wl(uiIdx); else if (uiIdx == (cTarget.size() - 1)) dDelta_Lambda = cTarget.wl(uiIdx) - cTarget.wl(uiIdx - 1); dLuminosity_Target += cTarget.flux(uiIdx) * dDelta_Lambda; uiIdx++; } dLuminosity_Target /= dTarget_Minima_Flux; if (cTarget.wl(0) > FIT_BLUE_WL) dLuminosity_Target /= (FIT_RED_WL - cTarget.wl(0)); else dLuminosity_Target /= (FIT_RED_WL - FIT_BLUE_WL); fprintf(fileOut,"%s, %.0f, %.1f, %.1e\n",i_lpszArg_Values[uiPath_Idx], dDay_Rel, dTemp,dLuminosity_Target); fclose(fileOut); } // double dBest_Fit = Get_Fit(cTarget, cGenerated, cTarget.wl(0), cTarget.wl(cTarget.size() - 1)); double dBest_Fit = Get_Fit(cTarget, cGenerated, FIT_BLUE_WL, FIT_RED_WL); printf("Overall Fit: %.2e\n",dBest_Fit); sprintf(lpszOutput_Filename,"%s.tempfit.csv",lpszOutput_File_Prefix); fileOut = fopen(lpszOutput_Filename,"wt"); for (unsigned int uiI = 0; uiI < cGenerated.size(); uiI++) fprintf(fileOut, "%f %f %f\n",cGenerated.wl(uiI), cGenerated.flux(uiI), cGenerated.flux_error(uiI)); fclose(fileOut); return 0; }
int main(int i_iArg_Count,const char * i_lpszArg_Values[]) { if (i_iArg_Count > 1) { if (!(i_iArg_Count & 0x01)) // even number of parameters { int iYear_BMax = atoi(i_lpszArg_Values[1]) / 10000; int iMonth_BMax = (atoi(i_lpszArg_Values[1]) % 10000) / 100; int iDay_BMax = (atoi(i_lpszArg_Values[1]) % 100); int iDay_Of_Year_BMax = Get_Day_Of_Year(iYear_BMax,iMonth_BMax,iDay_BMax); FILE * fileOut = fopen("fit.user.other.csv","wt"); fprintf(fileOut,"model, Date, Days (Bmax), Model day, PS velocity, PS temp, PS scaling, HVF scaling"); for (unsigned int uiI = 1; uiI <= 6; uiI++) { fprintf(fileOut,", Fit Raw Moment %i, Fit Central Moment %i, Fit Standardized Moment %i",uiI,uiI,uiI); } fprintf(fileOut,"\n"); for (unsigned int uiI = 2; uiI < i_iArg_Count; uiI+=2) { const char * lpszDate_Cursor = i_lpszArg_Values[uiI] + 9; char lpszDate_String[9]; strncpy(lpszDate_String,lpszDate_Cursor,8); lpszDate_String[8] = 0; int iYear_Curr = atoi(lpszDate_String) / 10000; int iMonth_Curr = (atoi(lpszDate_String) % 10000) / 100; int iDay_Curr = (atoi(lpszDate_String) % 100); int iDay_Of_Year_Curr = Get_Day_Of_Year(iYear_Curr,iMonth_Curr,iDay_Curr); ES::Spectrum cTarget = ES::Spectrum::create_from_ascii_file( i_lpszArg_Values[uiI] ); ES::Spectrum cGenerated = ES::Spectrum::create_from_ascii_file( i_lpszArg_Values[uiI + 1] ); // printf("%i %i\n",cTarget.size(),cGenerated.size()); double * lpdRaw_Moments = NULL, *lpdCentral_Moments = NULL, *lpdStandardized_Moments = NULL; double dFit_Range_Min_WL = 7750.0; double dFit_Range_Max_WL = 8400.0; double dTarget_Minima_WL, dTarget_Minima_Flux, dFit_Minima_WL, dFit_Minima_Flux; // Find the minima of the line used for fitting // Find_Flux_Minimum(cTarget, dTarget_Minima_WL, dTarget_Minima_Flux,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Blue_Maxima(cTarget, dTarget_Minima_WL, dTarget_Minima_Flux,dFit_Range_Min_WL, dFit_Range_Max_WL); // Find_Flux_At_WL(cFit_Info.cBest_Fit_Spectrum,dTarget_Minima_WL, dNormalization_Flux_Fit); // Find_Flux_Minimum(cGenerated,dFit_Minima_WL, dFit_Minima_Flux,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Blue_Maxima(cGenerated,dFit_Minima_WL, dFit_Minima_Flux,dFit_Range_Min_WL, dFit_Range_Max_WL); Get_Fit_Moments_2(cTarget, cGenerated, dFit_Range_Min_WL,dFit_Range_Max_WL, 6, lpdRaw_Moments, lpdCentral_Moments, lpdStandardized_Moments,dTarget_Minima_Flux,dFit_Minima_Flux); unsigned int uiStart_Idx_Target = 0, uiStart_Idx_Generated = 0; double * lpdTarget_WL = NULL, *lpdTarget_Flux = NULL, *lpdGenerated_WL = NULL, *lpdGenerated_Flux = NULL; unsigned int uiNum_Points_Target, uiNum_Points_Generated; Get_Spectra_Data(cTarget, lpdTarget_WL, lpdTarget_Flux, uiNum_Points_Target, dFit_Range_Min_WL, dFit_Range_Max_WL); Get_Spectra_Data(cGenerated, lpdGenerated_WL, lpdGenerated_Flux, uiNum_Points_Generated, dFit_Range_Min_WL, dFit_Range_Max_WL); while (lpdGenerated_WL[uiStart_Idx_Generated] < lpdTarget_WL[0] && uiStart_Idx_Generated < uiNum_Points_Generated) uiStart_Idx_Generated++; while (lpdTarget_WL[uiStart_Idx_Target] < lpdGenerated_WL[0] && uiStart_Idx_Target < uiNum_Points_Target) uiStart_Idx_Target++; unsigned int uiNum_Points_Error = uiNum_Points_Target - uiStart_Idx_Target; double * lpdError = new double[uiNum_Points_Error]; for (unsigned int uiJ = 0; uiJ < 9; uiJ++) { double dNormalization_Target,dNormalization_Generated; double dTarget_Minima_WL_Lcl, dTarget_Minima_Flux_Lcl,dFit_Minima_WL_Lcl,dFit_Minima_Flux_Lcl; //printf("Calc Norm\n"); switch (uiJ) { case 0: // Individual at blue individual max Find_Blue_Maxima(cTarget, dTarget_Minima_WL_Lcl, dTarget_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Blue_Maxima(cGenerated,dFit_Minima_WL_Lcl, dFit_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dFit_Minima_Flux_Lcl; break; case 1: // target flux at generated blue max Find_Blue_Maxima(cGenerated,dFit_Minima_WL_Lcl, dFit_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Flux_At_WL(cTarget,dFit_Minima_WL_Lcl,dTarget_Minima_Flux_Lcl); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dTarget_Minima_Flux_Lcl; break; case 2: // individual flux at generated blue max Find_Blue_Maxima(cGenerated,dFit_Minima_WL_Lcl, dFit_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Flux_At_WL(cTarget,dFit_Minima_WL_Lcl,dTarget_Minima_Flux_Lcl); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dFit_Minima_Flux_Lcl; break; case 3: // Individual at individual minima Find_Flux_Minimum(cTarget, dTarget_Minima_WL_Lcl, dTarget_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Flux_Minimum(cGenerated,dFit_Minima_WL_Lcl, dFit_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dFit_Minima_Flux_Lcl; break; case 4: // target flux at target minimum Find_Flux_Minimum(cTarget,dTarget_Minima_WL_Lcl,dTarget_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dTarget_Minima_Flux_Lcl; break; case 5: // individual flux at generated blue max Find_Flux_Minimum(cTarget,dTarget_Minima_WL_Lcl,dTarget_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Flux_At_WL(cGenerated,dTarget_Minima_WL_Lcl, dFit_Minima_Flux_Lcl); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dFit_Minima_Flux_Lcl; break; case 6: // Individual at blue individual max Find_Red_Maxima(cTarget, dTarget_Minima_WL_Lcl, dTarget_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Red_Maxima(cGenerated,dFit_Minima_WL_Lcl, dFit_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dFit_Minima_Flux_Lcl; break; case 7: // target flux at generated blue max Find_Red_Maxima(cGenerated,dFit_Minima_WL_Lcl, dFit_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Flux_At_WL(cTarget,dFit_Minima_WL_Lcl,dTarget_Minima_Flux_Lcl); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dTarget_Minima_Flux_Lcl; break; case 8: // individual flux at generated blue max Find_Red_Maxima(cGenerated,dFit_Minima_WL_Lcl, dFit_Minima_Flux_Lcl,dFit_Range_Min_WL, dFit_Range_Max_WL); Find_Flux_At_WL(cTarget,dFit_Minima_WL_Lcl,dTarget_Minima_Flux_Lcl); dNormalization_Target = 1.0 / dTarget_Minima_Flux_Lcl; dNormalization_Generated = 1.0 / dFit_Minima_Flux_Lcl; break; } //printf("Norm\n"); unsigned int uiGen_Start_Index = uiStart_Idx_Generated; unsigned int uiLower_Bound,uiUpper_Bound; for (unsigned int uiK = 0; uiK < uiNum_Points_Error; uiK++) { //printf("Bracket\n"); Bracket_Wavelength(cGenerated, lpdTarget_WL[uiK + uiStart_Idx_Target], uiLower_Bound, uiUpper_Bound, uiGen_Start_Index); uiGen_Start_Index = uiUpper_Bound; //printf("Target\n"); double dTarget = lpdTarget_Flux[uiK + uiStart_Idx_Target] * dNormalization_Target; //printf("Generated\n"); double dGenerated = Interpolate_Flux(cGenerated, lpdTarget_WL[uiK + uiStart_Idx_Target], uiLower_Bound, uiUpper_Bound) * dNormalization_Generated; lpdError[uiK] = dTarget - dGenerated; } double lpdMoments[3]; Get_Raw_Moments(lpdError, uiNum_Points_Error, 3, lpdMoments); printf("%i: %.2e %.2e %.2e\n",uiJ,fabs(lpdMoments[0]),fabs(lpdMoments[1]),fabs(lpdMoments[2])); } delete [] lpdError; delete [] lpdTarget_WL; delete [] lpdTarget_Flux; delete [] lpdGenerated_WL; delete [] lpdGenerated_Flux; double dDay_Rel = (iDay_Of_Year_Curr - iDay_Of_Year_BMax); while (iYear_Curr < iYear_BMax) { if (Is_Leap_Year(iYear_Curr)) dDay_Rel -= 366; else dDay_Rel -= 365; iYear_Curr++; } while (iYear_Curr > iYear_BMax) { if (Is_Leap_Year(iYear_Curr)) dDay_Rel += 366; else dDay_Rel += 365; iYear_Curr--; } fprintf(fileOut,"Jeff, %s, %.0f, n/a, n/a, n/a, n/a, n/a",lpszDate_String,dDay_Rel); for (unsigned int uiJ = 0; uiJ < 6; uiJ++) fprintf(fileOut,", %.17e, %.17e, %.17e",lpdRaw_Moments[uiJ], lpdCentral_Moments[uiJ], lpdStandardized_Moments[uiJ]); fprintf(fileOut,"\n"); epsplot::PAGE_PARAMETERS cPlot_Parameters; epsplot::DATA cPlot; cPlot_Parameters.m_uiNum_Columns = 1; cPlot_Parameters.m_uiNum_Rows = 1; cPlot_Parameters.m_dWidth_Inches = 11.0; cPlot_Parameters.m_dHeight_Inches = 8.5; unsigned int uiX_Axis = cPlot.Set_X_Axis_Parameters( "Wavelength [A]", false, false, false, 0.0, false, 0.0); unsigned int uiY_Axis = cPlot.Set_Y_Axis_Parameters( "Intensity", false, false, false, 5.0, false, 60.0); // cPlot.Set_Num_Plots(2); cPlot.Set_Plot_Title(NULL);//bNo_Title ? NULL : lpszTitle,18.0); // printf("Plot full\n"); Plot(cPlot_Parameters, cPlot, "%s.full.eps", i_lpszArg_Values[uiI + 1], cTarget, cGenerated, dTarget_Minima_Flux, dFit_Minima_Flux,cTarget.wl(0), cTarget.wl(cTarget.size() - 1), uiX_Axis, uiY_Axis); // printf("Plot Ca NIR\n"); Plot(cPlot_Parameters, cPlot, "%s.CaNIR.eps", i_lpszArg_Values[uiI + 1], cTarget, cGenerated, dTarget_Minima_Flux, dFit_Minima_Flux,7000.0, 9000.0, uiX_Axis, uiY_Axis); // printf("Plot Ca H&K\n"); Plot(cPlot_Parameters, cPlot, "%s.CaHK.eps", i_lpszArg_Values[uiI + 1], cTarget, cGenerated, dTarget_Minima_Flux, dFit_Minima_Flux,3000.0, 5000.0, uiX_Axis, uiY_Axis); // printf("Plot Si 6355\n"); Plot(cPlot_Parameters, cPlot, "%s.Si6355.eps", i_lpszArg_Values[uiI + 1], cTarget, cGenerated, dTarget_Minima_Flux, dFit_Minima_Flux,5000.0, 7000.0, uiX_Axis, uiY_Axis); } fclose(fileOut); } } return 0; }