/*Compute the date corresponding to this number of seconds since midnight, Jan 1, 1900. This is sub-millisecond accurate; but ignores leap seconds. */ void sec2date(double secs,julian_date *date,hms_time *time) { int year=1900; while (secs>=date_getDaysInYear(year)*DAY2SEC) secs-=date_getDaysInYear(year++)*DAY2SEC; date->year=year; date->jd=1+(int)(secs/DAY2SEC); secs-=(date->jd-1)*DAY2SEC; date_sec2hms(secs,time); }
// Compute date corresponding to seconds since midnight, Jan 1, 1985. void seconds2date(double seconds, ymd_date *date, hms_time *time) { julian_date jd; int year = 1985; double secs = seconds; while (secs >= date_getDaysInYear(year)*DAY2SEC) secs -= date_getDaysInYear(year++)*DAY2SEC; jd.year = year; jd.jd = 1 + (int)(secs/DAY2SEC); secs -= (jd.jd - 1)*DAY2SEC; date_sec2hms(secs, time); date_jd2ymd(&jd, date); }
/*Get ESA-Modified Julian Day given conventional year and julian day. This is defined as "Days after Jan. 1, 1950".*/ int date_getMJD(julian_date *in) { int curYear=1950; int curDay=0; while (curYear<in->year) curDay+=date_getDaysInYear(curYear++); return curDay+in->jd-1; }
/*Compute the number of seconds since midnight, Jan 1, 1900. This is sub-millisecond accurate; but ignores leap seconds. */ double date2sec(julian_date *date,hms_time *time) { double ret=date_hms2sec(time); int year; for (year=1900;year<date->year;year++) ret+=date_getDaysInYear(year)*DAY2SEC; ret+=(date->jd-1)*DAY2SEC; return ret; }
// Compute corresponding seconds from midnight, Jan 1, 1985. double date2seconds(julian_date *date, double sec) { double ret = sec; int year; for (year=1985; year<date->year; year++) ret += date_getDaysInYear(year)*DAY2SEC; ret += (date->jd-1)*DAY2SEC; return ret; }
/*Get julian day given month/day.*/ void date_ymd2jd(ymd_date *in,julian_date *out) { int *monthStart; int daysInYear=date_getDaysInYear(in->year); if ((in->month>12)||(in->day>31)) {printf("ERROR! Invalid month '%d' or day '%d' passed to date_ymd2jd!\n",in->month,in->day);exit(1);} if (daysInYear==366) monthStart=monthStart_leap; else monthStart=monthStart_nonleap; out->year=in->year; out->jd=monthStart[in->month-1]+(in->day-1); }
/* Calculate seconds elapsed from given refYear to the seekYear, seekDay (julian day), and seekTime ----------------------------------------------------*/ double timeSince(ymd_date *seekDate, hms_time *seekTime, int refYear) { double totTime; int totDays=0, year; julian_date julDay; date_ymd2jd(seekDate,&julDay); for (year = refYear; year < julDay.year; year++) totDays += date_getDaysInYear(year); totDays += julDay.jd; totTime = totDays * 24.0 * 3600.0 + date_hms2sec(seekTime); return(totTime); }
void date_jd2ymd(julian_date *in,ymd_date *out) { int *monthStart; int currMonth; int daysInYear=date_getDaysInYear(in->year); if (in->jd>daysInYear) {printf("ERROR! Invalid julian day '%d' passed to date_jd2ymd!\n",in->jd);exit(1);} if (daysInYear==366) monthStart=monthStart_leap; else monthStart=monthStart_nonleap; for (currMonth=1;monthStart[currMonth]<=in->jd;currMonth++) {} out->year=in->year; out->month=currMonth; out->day=in->jd-monthStart[currMonth-1]+1; }
/*Return number of days between beginnings of the given images. Computes s2->time-s1->time.*/ double get_days(meta_state_vectors *s1,meta_state_vectors *s2) { double day_diff=0.0; /*First off, find the image with the smaller year*/ int currYear,endYear; int swap_dir=1;/*1->forward; -1->backward*/ if (s1->year>s2->year) {endYear=s1->year;currYear=s2->year;swap_dir=-1;} else {endYear=s2->year;currYear=s1->year;swap_dir=1;} /*Next, step along years until year difference is taken care of*/ while (currYear<endYear) day_diff+=date_getDaysInYear(currYear++)*swap_dir; /*Now compensate for the day and time difference*/ day_diff+=(s2->julDay-s1->julDay); day_diff+=(s2->second-s1->second)/(24.0*60*60); return day_diff; }
static void rgps_grid2cell(char *line, rgps_grid_t *grid, int nGrids, rgps_cell_t *cell, int nCells, dbf_header_t *dbf, FILE *fpOut) { char **col; int ii, nCols, nCoords=0, cell_id=0, obs_year; double obs_time, x_disp, y_disp; // Extract information from line split_into_array(line, ',', &nCols, &col); for (ii=0; ii<nCols; ii++) { if (dbf[ii].format == CSV_STRING) dbf[ii].sValue = STRDUP(col[ii]); else if (dbf[ii].format == CSV_DOUBLE) dbf[ii].fValue = atof(col[ii]); else if (dbf[ii].format == CSV_INTEGER) dbf[ii].nValue = atoi(col[ii]); if (strcmp_case(dbf[ii].shape, "CELL_ID") == 0) cell_id = dbf[ii].nValue; else if (strcmp_case(dbf[ii].shape, "OBS_YEAR") == 0) obs_year = dbf[ii].nValue; else if (strcmp_case(dbf[ii].shape, "OBS_TIME") == 0) { obs_time = dbf[ii].fValue; if (obs_year > grid[0].obs_year) obs_time += date_getDaysInYear(grid[0].obs_year); } else if (strcmp_case(dbf[ii].shape, "X_DISP") == 0) x_disp = dbf[ii].fValue; else if (strcmp_case(dbf[ii].shape, "Y_DISP") == 0) y_disp = dbf[ii].fValue; } free_char_array(&col, nCols); // Extract information from connectivity table int *grid_id = (int *) MALLOC(sizeof(int)*50); double *grid_order = (double *) MALLOC(sizeof(double)*50); size_t *p = (size_t *) MALLOC(sizeof(size_t)*50); for (ii=0; ii<nCells; ii++) { if (cell_id == cell[ii].cell_id) { grid_id[nCoords] = cell[ii].grid_id; grid_order[nCoords] = cell[ii].cell_id + (double) cell[ii].order / 100; nCoords++; } } gsl_sort_index(p, grid_order, 1, nCoords); double disp_mag = sqrt(x_disp*x_disp + y_disp*y_disp); // Extract grid information from motion product int kk, index; double diff, grid_time, birth_time, death_time; char image_id[30]; char *tmp = (char *) MALLOC(sizeof(char)*25); char *coordStr = (char *) MALLOC(sizeof(char)*50*nCoords); strcpy(coordStr, ""); for (kk=0; kk<nCoords; kk++) { index = -1; for (ii=0; ii<nGrids; ii++) { grid_time = grid[ii].obs_time; if (grid[ii].obs_year > grid[0].obs_year) grid_time += date_getDaysInYear(grid[0].obs_year); diff = fabs(grid_time - obs_time); if ((grid_id[p[kk]] == grid[ii].gpid) && (diff < 0.01)) { birth_time = grid[ii].birth_time; if (grid[ii].birth_year > grid[0].obs_year) birth_time += date_getDaysInYear(grid[0].obs_year); index = ii; } } if (index > 0 && birth_time <= grid_time) { sprintf(tmp, ",%.4f,%.4f", grid[index].x, grid[index].y); strcat(coordStr, tmp); strcpy(image_id, grid[index].image_id); } } // Check cell death time int n, cell_death_year; double cell_death_time; split_into_array(line, ',', &n, &col); for (ii=0; ii<nCells; ii++) { if (cell[ii].cell_id == atoi(col[0])) { cell_death_time = death_time = cell[ii].death_time; cell_death_year = cell[ii].death_year; if (cell[ii].death_year > cell[0].birth_year) death_time += date_getDaysInYear(cell[0].birth_year); if (cell[ii].death_year == -1) death_time = -1; } } if (death_time < 0 || obs_time < (death_time + 0.01)) { fprintf(fpOut, "%.6f,%s,%s,%s", obs_time, col[0], col[1], col[2]); fprintf(fpOut, ",%s,%d,%.6f", col[3], cell_death_year, cell_death_time); for (kk=4; kk<n; kk++) fprintf(fpOut, ",%s", col[kk]); fprintf(fpOut, ",%s,%.6f%s\n", image_id, disp_mag, coordStr); } free_char_array(&col, n); // Clean up FREE(grid_id); FREE(grid_order); FREE(p); FREE(tmp); FREE(coordStr); return; }