44#include "casu_utils.h"
51static void casu_substr(
char *in,
int ist,
int len,
char *out);
52static int casu_mjdcompare(
const cpl_frame *f1,
const cpl_frame *f2);
101 if (frame1 == NULL || frame2 == NULL)
106 if ((v1 = (
char *)cpl_frame_get_tag(frame1)) == NULL)
108 if ((v2 = (
char *)cpl_frame_get_tag(frame2)) == NULL)
150 cpl_size *labels, cpl_size nlab,
153 cpl_frameset *cur_set,*ret_set;
154 cpl_frame *cur_frame;
159 for (i = 0; i < nlab; i++) {
160 cur_set = cpl_frameset_extract(frameset,labels,(cpl_size)i);
163 cur_frame = cpl_frameset_get_position(cur_set,0);
164 cur_tag = (
char *)cpl_frame_get_tag(cur_frame);
165 if (!strcmp(cur_tag,tag)) {
167 cpl_frameset_sort(ret_set,casu_mjdcompare);
170 cpl_frameset_delete(cur_set);
207 cpl_size *labels, cpl_size nlab,
209 cpl_frameset *cur_set;
210 cpl_frame *cur_frame,*new_frame;
215 cur_frame = cpl_frameset_get_position(cur_set,0);
216 new_frame = cpl_frame_duplicate(cur_frame);
217 cpl_frameset_delete(cur_set);
246 const char *fctid =
"casu_getnpts";
248 if ((nx = (
int)cpl_image_get_size_x(in)) == -1) {
249 cpl_msg_error(fctid,
"NULL image input");
252 if ((ny = (
int)cpl_image_get_size_y(in)) == -1) {
253 cpl_msg_error(fctid,
"NULL image input");
287extern void casu_prov(cpl_propertylist *p, casu_fits **inlist,
int n,
290 char keyword[SZKEY],value[SZVAL],*fn,*base;
295 cpl_propertylist_erase_regexp(p,
"ESO DRS PROV[0-9]*",0);
297 cpl_propertylist_erase_regexp(p,
"PROV[0-9]*",0);
301 for (i = 0; i < n; i++) {
303 (void)snprintf(keyword,SZKEY,
"ESO DRS PROV%d",i+1);
306 (void)snprintf(value,SZVAL,
"%s",base);
309 (void)snprintf(keyword,SZKEY,
"PROV%d",i+1);
312 (void)snprintf(value,SZVAL,
"%s",base);
315 cpl_propertylist_update_string(p,keyword,value);
316 (void)snprintf(value,SZVAL,
"Input file # %d",i+1);
317 cpl_propertylist_set_comment(p,keyword,value);
353 t = cpl_malloc(m*
sizeof(*t));
356 while (increment > 0) {
357 for (i = increment; i < n; i++) {
359 for (k = 0; k < m; k++)
361 while ((j >= increment) && (a[0][j-increment] > t[0])) {
362 for (k = 0; k < m; k++)
363 a[k][j] = a[k][j-increment];
366 for (k = 0; k < m; k++)
372 increment = (int)((
float)increment/2.2);
400 cpl_propertylist *p2) {
406 if (p1 == NULL || p2 == NULL)
413 for (i = 0; i < cpl_propertylist_get_size(p2); i++) {
414 name = cpl_property_get_name(cpl_propertylist_get(p2,i));
415 if (cpl_propertylist_has(p1,name))
416 cpl_propertylist_erase(p1,name);
418 cpl_propertylist_append(p1,p2);
454 cpl_propertylist_update_bool(p,
"ESO DRS IMADUMMY",TRUE);
455 cpl_propertylist_set_comment(p,
"ESO DRS IMADUMMY",
456 "This is a dummy product");
487 cpl_propertylist *temp;
488 cpl_property *property;
495 if (! cpl_propertylist_has(p,oldname))
497 temp = cpl_propertylist_new();
498 cpl_propertylist_copy_property(temp,p,oldname);
499 property = cpl_propertylist_get(temp,0);
503 cpl_property_set_name(property,newname);
507 cpl_propertylist_append(p,temp);
508 cpl_propertylist_erase(p,oldname);
509 cpl_propertylist_delete(temp);
542 cpl_image_multiply_scalar(im,0.0);
607 return(cpl_propertylist_has(p,
"ESO DRS IMADUMMY"));
652extern void casu_overexp(casu_fits **fitslist,
int *n,
int ndit,
float lthr,
653 float hthr,
int ditch,
float *minv,
float *maxv,
657 double val,dndit,sum;
661 dndit = (double)ndit;
666 for (i = 0; i < *n; i++) {
668 val = cpl_image_get_median_window(im,500,500,1000,1000);
670 *minv = min(*minv,val);
671 *maxv = max(*maxv,val);
673 if (val > lthr && val < hthr) {
674 fitslist[m++] = fitslist[i];
680 for (i = m; i < *n; i++)
682 *avev = sum/(double)*n;
715 if (cpl_image_get_size_x(im1) != cpl_image_get_size_x(im2) ||
716 cpl_image_get_size_y(im1) != cpl_image_get_size_y(im2))
761 unsigned char *iflag;
766 if (*status != CASU_OK)
773 *n = cpl_frame_get_nextensions(frame);
774 *cors = cpl_malloc(*n*
sizeof(
float));
775 iflag = cpl_calloc(*n,
sizeof(iflag));
781 for (i = 0; i < *n; i++) {
782 p = cpl_propertylist_load(cpl_frame_get_filename(frame),(cpl_size)(i+1));
783 if (cpl_propertylist_has(p,
"ESO DRS IMADUMMY")) {
785 }
else if (! cpl_propertylist_has(p,
"ESO DRS MEDFLAT")) {
788 val = cpl_propertylist_get_double(p,
"ESO DRS MEDFLAT");
797 cpl_propertylist_delete(p);
807 for (i = 0; i < *n; i++) {
809 (*cors)[i] = sum/(*cors)[i];
856 (void)gettimeofday(&tv,NULL);
858 tm = gmtime_r(&(tv.tv_sec), &result);
859 sec = (float)tm->tm_sec + 1.0e-6*(
float)tv.tv_usec;
863 (void)snprintf(out,n,
"%04d-%02d-%02dT%02d:%02d:%07.4f",1900+tm->tm_year,
864 tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,sec);
899extern int casu_catpars(cpl_frame *indx,
char **catpath,
char **catname) {
902 const char *fctid =
"casu_catpars",*unk =
"unknown";
911 *catpath = cpl_strdup(cpl_frame_get_filename(indx));
912 if (access((
const char *)*catpath,R_OK) != 0) {
913 cpl_msg_error(fctid,
"Can't access index file %s",*catpath);
921 if ((p = cpl_propertylist_load(cpl_frame_get_filename(indx),0)) == NULL) {
923 cpl_msg_error(fctid,
"Can't load index file header %s",
924 cpl_frame_get_filename(indx));
931 if (cpl_propertylist_has(p,
"CATNAME")) {
932 *catname = cpl_strdup(cpl_propertylist_get_string(p,
"CATNAME"));
935 *catname = cpl_strdup(unk);
936 cpl_msg_warning(fctid,
"Property CATNAME not in index file header %s",
937 cpl_frame_get_filename(indx));
983 int nlist,
float err) {
985 float errsq,errmin,dx,dy,poserr;
992 indx = (isp + ifp)/2;
993 while (ifp-isp >= 2) {
994 if (ylist[indx] < y - err) {
997 }
else if (ylist[indx] > y - err) {
1010 for (i = isp; i < nlist; i++) {
1011 if (ylist[i] > y+err)
1015 poserr = dx*dx + dy*dy;
1016 if (poserr < errsq) {
1017 if (poserr <= errmin) {
1052 if (!strcmp(col,
"X")) {
1053 if (cpl_propertylist_has(p,
"ESO DRS XCOL"))
1054 return(cpl_propertylist_get_int(p,
"ESO DRS XCOL"));
1057 }
else if (!strcmp(col,
"Y")) {
1058 if (cpl_propertylist_has(p,
"ESO DRS YCOL"))
1059 return(cpl_propertylist_get_int(p,
"ESO DRS YCOL"));
1089 int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
1090 int offs[] = {0,5,8,11,14,17};
1091 int lns[] = {4,2,2,2,2,7};
1092 int info[5],i,night;
1100 for (i = 0; i < 5; i++) {
1101 casu_substr(dateobs,offs[i],lns[i],test);
1102 info[i] = atoi(test);
1104 casu_substr(dateobs,offs[5],lns[5],test);
1111 utc = info[3] + (double)info[4]/60.0 + ss/3600.0;
1120 info[2] = days[info[1] - 1];
1121 }
else if (info[1] == 2) {
1123 if (info[0] % 4 == 0)
1126 info[2] = days[info[1] - 1];
1133 night = 10000*info[0] + 100*info[1] + info[2];
1164 const char *name,
float val) {
1170 if (cpl_propertylist_has(plist,name) == 0) {
1171 cpl_propertylist_update_float(plist,name,val);
1173 }
else if (cpl_propertylist_get_type(plist,name) == CPL_TYPE_FLOAT) {
1174 cpl_propertylist_update_float(plist,name,val);
1180 comment = cpl_strdup(cpl_propertylist_get_comment(plist,name));
1181 cpl_propertylist_erase(plist,name);
1182 cpl_propertylist_update_float(plist,name,val);
1183 cpl_propertylist_set_comment(plist,name,comment);
1213 const char *name,
double val) {
1219 if (cpl_propertylist_has(plist,name) == 0) {
1220 cpl_propertylist_update_double(plist,name,val);
1222 }
else if (cpl_propertylist_get_type(plist,name) == CPL_TYPE_DOUBLE) {
1223 cpl_propertylist_update_double(plist,name,val);
1230 comment = cpl_strdup(cpl_propertylist_get_comment(plist,name));
1231 cpl_propertylist_erase(plist,name);
1232 cpl_propertylist_update_double(plist,name,val);
1233 cpl_propertylist_set_comment(plist,name,comment);
1265static void casu_substr(
char *in,
int ist,
int len,
char *out) {
1272 (void)strncpy(out,c,len);
1299static int casu_mjdcompare(
const cpl_frame *f1,
const cpl_frame *f2) {
1301 cpl_propertylist *p;
1303 p = cpl_propertylist_load(cpl_frame_get_filename(f1),0);
1304 if (cpl_propertylist_has(p,
"MJD-OBS") == 0) {
1305 cpl_propertylist_delete(p);
1306 p = cpl_propertylist_load(cpl_frame_get_filename(f1),1);
1308 mjd1 = cpl_propertylist_get_double(p,
"MJD-OBS");
1309 cpl_propertylist_delete(p);
1310 p = cpl_propertylist_load(cpl_frame_get_filename(f2),0);
1311 if (cpl_propertylist_has(p,
"MJD-OBS") == 0) {
1312 cpl_propertylist_delete(p);
1313 p = cpl_propertylist_load(cpl_frame_get_filename(f1),1);
1315 mjd2 = cpl_propertylist_get_double(p,
"MJD-OBS");
1316 cpl_propertylist_delete(p);
1319 else if (mjd1 > mjd2)
cpl_image * casu_fits_get_image(casu_fits *p)
void casu_fits_delete(casu_fits *p)
char * casu_fits_get_fullname(casu_fits *p)
char * casu_fits_get_filename(casu_fits *p)
void casu_prov(cpl_propertylist *p, casu_fits **inlist, int n, int isextn)
Write provenance keywords.
int casu_findcol(cpl_propertylist *p, const char *col)
Find from catalogue header which are x,y columns.
int casu_is_dummy(cpl_propertylist *p)
See if the header of an image or table says its a dummy.
int casu_compare_tags(const cpl_frame *frame1, const cpl_frame *frame2)
Compare input tags.
void casu_merge_propertylists(cpl_propertylist *p1, cpl_propertylist *p2)
Merge two propertylists.
void casu_propertylist_update_double(cpl_propertylist *plist, const char *name, double val)
long casu_getnpts(cpl_image *in)
Get the number of pixels in a 2d image.
int casu_catpars(cpl_frame *indx, char **catpath, char **catname)
Find the name of the standard catalogue and its location.
void casu_overexp(casu_fits **fitslist, int *n, int ndit, float lthr, float hthr, int ditch, float *minv, float *maxv, float *avev)
Remove over- or under-exposed images from a list.
void casu_propertylist_update_float(cpl_propertylist *plist, const char *name, float val)
int casu_compare_dims(cpl_image *im1, cpl_image *im2)
Compare dimensions of two 2d images.
void casu_dummy_property(cpl_propertylist *p)
Set dummy property keyword.
cpl_frame * casu_frameset_subgroup_1(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Extract a frame of a given label from a frameset.
cpl_image * casu_dummy_image(casu_fits *model)
Create a dummy image of zeros based on a model.
void casu_timestamp(char *out, int n)
Create a timestamp string.
int casu_fndmatch(float x, float y, float *xlist, float *ylist, int nlist, float err)
Match the x,y coordinates of an object to one from a list.
int casu_night_from_dateobs(char *dateobs)
Use the dateobs value to work out the night of an observation.
cpl_table * casu_dummy_catalogue(int type)
Create a dummy catalogue of a given type with no rows.
void casu_rename_property(cpl_propertylist *p, const char *oldname, char *newname)
Rename a property in a given propertylist.
void casu_sort(float **a, int n, int m)
Sort a 2d array by the first column and co-sort the rest.
int casu_gaincor_calc(cpl_frame *frame, int *n, float **cors, int *status)
Work out gain corrections.
cpl_frameset * casu_frameset_subgroup(cpl_frameset *frameset, cpl_size *labels, cpl_size nlab, const char *tag)
Extract a frameset from another frameset.
void imcore_tabinit(ap_t *ap, int *xcol, int *ycol, int cattype, cpl_table **tab)
Initialise catalogues.