void day_to_date(int year, int day, char st[]) { char *month_name[] = {"DUM", "JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}; int i; int *day_month; day_month = ivector(1, 12); day_month[1] = 31, day_month[2] = 28, day_month[3] = 31; day_month[4] = 30, day_month[5] = 31, day_month[6] = 30; day_month[7] = 31, day_month[8] = 31, day_month[9] = 30; day_month[10] = 31, day_month[11] = 30, day_month[12] = 31; if(fmod(year, 4.0) == 0) day_month[2] = 29; for(i = 1; i <= 12; i++){ if(day <= day_month[i]){ sprintf(st, "%4d%s%02d", year, month_name[i], day); break; } else{ day -= day_month[i]; } } } /* ---------------------------------------------------------- */ int file_exist(char file[]) { FILE *fp; fp = fopen(file, "r"); if(fp == NULL) return 0; else{ fclose(fp); return 1; } } /* ---------------------------------------------------------- */ int number_rows(char file_list[]) { int i, n_row; char file[16], st[80]; FILE *fp; i = 0; while((file_list[i] != '\0') && (file_list[i] != ';')){ file[i] = file_list[i]; i++; } file[i] = '\0'; fp = fopen(file, "r"); do{ fscanf(fp, "%s", &st); } while(strcmp(st, "Number_of_rows") != 0); fscanf(fp, "%d", &n_row); fclose(fp); return n_row; } /* ---------------------------------------------------------- */ int number_files(char file_list[]) { int i, n_file; n_file = 1; i = 0; while(file_list[i] != '\0'){ if(file_list[i] == ';') n_file++; i++; } return n_file; } /* ---------------------------------------------------------- */ int calc_signal(char file_list[], int n_row, struct scan_header *hd, float **sig, char *sig_unit) { int i, j; int n_file, i_file, i_char; int **label; float **data, **r_data; float sum, sum2; char file[16]; char st[80]; struct scan_header temp; struct label_unit unit; FILE *fp; n_file = number_files(file_list); //number of files in list data = matrix(1, n_row, 1, 1 + 2*n_file); //allocate for data r_data = matrix(1, n_row, 1, 3); hd->start_wl = -9999; //initialize for header values hd->stop_wl = -9999; //specific to each instrument hd->inc_wl = -9999; hd->dark_sig = -9999; hd->filter_temp = -9999; hd->detect_temp = -9999; hd->motor_temp = -9999; hd->housing_temp = -9999; hd->pmt_volt = -9999; hd->int_time = -9999; hd->dead_time = -9999; i_char = 0; for(i_file = 1; i_file <= n_file; i_file++){ //loop through files j = 0; //read file from list if(i_file != n_file){ while(file_list[i_char] != ';'){ file[j] = file_list[i_char]; i_char++; j++; } file[j] = '\0'; i_char++; } else{ while(file_list[i_char] != '\0'){ file[j] = file_list[i_char]; i_char++; j++; } file[j] = '\0'; i_char++; } fp = fopen(file, "r"); //open file if(i_file == 1){ read_scan_header(fp, hd); //read header } else{ read_scan_header(fp, &temp); //if multiple files, read header hd->stop_time = temp.stop_time; //and update values if(hd->dark_sig > -9999) hd->dark_sig += temp.dark_sig; if(hd->filter_temp > -9999) hd->filter_temp += temp.filter_temp; if(hd->detect_temp > -9999) hd->detect_temp += temp.detect_temp; if(hd->motor_temp > -9999) hd->motor_temp += temp.motor_temp; if(hd->housing_temp > -9999) hd->housing_temp += temp.housing_temp; } label = imatrix(1, hd->n_col, 1, 64); //allocate for labels read_labels(fp, hd->n_col, label, &unit); //read labels strcpy(sig_unit, unit.sig_unit); //set signal unit read_data(fp, n_row, 3, r_data); //read data if(i_file == 1){ //update wavelength for(i = 1; i <= n_row; i++){ data[i][1] = r_data[i][1]; } } for(i = 1; i <= n_row; i++){ //update signals and uncertainty data[i][i_file+1] = r_data[i][2]; data[i][i_file+n_file+1] = r_data[i][3]; } fclose(fp); //close file } for(i = 1; i <= n_row; i++){ //calculate average signal sig[i][1] = data[i][1]; sum = 0, sum2 = 0; for(j = 1; j <= n_file; j++){ sum += data[i][j+1]; sum2 += data[i][j+1]*data[i][j+1]; } sig[i][2] = sum/(float)n_file; //average signal if(n_file == 1){ //signal uncertainty sig[i][3] = data[i][3]; } else{ if(sig[i][2] != 0){ sig[i][3] = sqrt((sum2 - 2*sig[i][2]*sum + n_file*sig[i][2]*sig[i][2])/(float)(n_file - 1))/sig[i][2]; } else{ sig[i][3] = 0; } } } //header values if(hd->dark_sig > -9999) hd->dark_sig /= (float)n_file; if(hd->filter_temp > -9999) hd->filter_temp /= (float)n_file; if(hd->detect_temp > -9999) hd->detect_temp /= (float)n_file; if(hd->motor_temp > -9999) hd->motor_temp /= (float)n_file; if(hd->housing_temp > -9999) hd->housing_temp /= (float)n_file; free_matrix(data, 1, n_row, 1, 1+2*n_file); free_matrix(r_data, 1, n_row, 1, 3); return 0; }