#define wolf #include #include #include #include #include "cgi-draw.h" #include "novas.h" #include "global.h" #include "calstars.h" #include "imatrix.h" #include "cnstl.h" /* ************ markstars *************** main routine of WOLF inputs: input_image - path for the input fits image file. output_image - filename of the output jpg processed image. detect - if 1, detect "unepxected" stars. write_on_target - if 1, type labels on the output image. In this case, output image must be specified. inverted - if 1, the image is inverted (black background and white stars). constellations - if 1, draw constellations (greek style) lines. right - specify the distance (in pixels) in which the labels should appear right to the exact pixel (this value can be positive or negative). below - specify the distance (in pixels) in which the labels should appear below the exact pixel (this value can be positive or negative. drawstars - if 1, draw star labels on the image quality - the quality of the created jpg image (from 1 to 100 when 100 is the best quality) background_image - if 1, create a fits file of the background of the image (no stars) background_elements - if 1, create a fits image of the elements of the background output: 1 if succeeded. */ int markstars(char *input_image,char *output_image,short detect,short write_on_target,short inverted,short constellations,short right,short below,short drawstars,short quality,short resize,short type_text,short write_stars_files,short draw_RA_DEC_lines,short draw_visibility,short background_image,short emissivity,double unwrap) { long x,y,star_type,is_jpeg,b,close_stars_num,star_point_index,label_index,star_points_num=0; short opacity_map_available=0; FONT font; BITMAP bitmap;//,source_bitmap; FILE *file; ImageMatrix *matrix; char filepath[200],star_name[50],buffer[50]; unsigned char aqua_style[10],unexpected_style[10],red_style[10]; /* initalize the cgi-draw library */ cd_init(); if (strstr(output_image,".jpg") || strstr(output_image,".JPG")) is_jpeg=1; else is_jpeg=0; /* load the image file */ #ifndef FITS if (is_jpeg) strcpy(filepath,"djpeg "); else strcpy(filepath,""); strcat(filepath,input_image); if (is_jpeg) file=popen(filepath,"r"); else file=fopen(filepath,"r"); if (!file) { printf("Could not open file '%s'\n",filepath); return(0); } bitmap=cd_load(file,NEW,0,0,255); if (bitmap==-1) { printf("Could not load bitmap file '%s'\n",filepath); return(0); } if (is_jpeg) pclose(file); else fclose(file); // source_bitmap=cd_duplicatebitmap(bitmap,ALL,ALL,ALL,ALL); /* duplicate the bitmap before drawing on it */ /* load the image to an ImageMatrix data structure */ matrix=new ImageMatrix; matrix->LoadImage(bitmap,inverted); if (tjd!=0) matrix->JulianDate=tjd; /* set the julian date of the image */ altaz2xy(MAX_DIST_FROM_CENTER,90,&x,&y); matrix->GetStarsPoints(star_points,stripes,sqrt(pow(x-MID_X,2)+pow(y-MID_Y,2))); { cd_freebitmap(bitmap); strcpy(filepath,"djpeg "); file=popen(strcat(filepath,output_image),"r"); if (!file) { printf("Could not open target image file '%s'\n",filepath); return(0); } bitmap=cd_load(file,NEW,0,0,255); if (bitmap==-1) { printf("Could not load target image '%s'\n",filepath); return(0); } } #else matrix=new ImageMatrix; if (matrix->LoadFits(input_image)==0) return(0); /* load the fits image */ if (unwrap>0) /* unwrap the fisheye view to panoramic view */ { bitmap=cd_newbitmap((long)(360*unwrap),long(90*unwrap)); /* create a bitmap for panoramic view */ matrix->unwrap(bitmap,unwrap); } else /* bitmap for fisheye view */ { bitmap=cd_newbitmap(matrix->width,matrix->height); matrix->GetPicture(bitmap); } altaz2xy(MAX_DIST_FROM_CENTER,90,&x,&y); if (drawstars || constellations || detect || type_text || draw_visibility || write_stars_files) star_points_num=matrix->GetStarsPoints(star_points,stripes,sqrt(pow(x-MID_X,2)+pow(y-MID_Y,2))); #endif /* load styles and fonts */ /* set the styles to be used */ cd_setstyle(aqua_style,0,0,0,0,0,0,0,0); /* no color */ cd_setstyle(unexpected_style,255,255,0,255,255,255,1,0); /* unexpected object */ cd_setstyle(red_style,255,0,0,255,255,255,1,0); /* for error messages printed on the image */ /* load the font */ file=fopen("fonts/couier14.bfnt","r"); if (!file) { printf("Could not open font file 'arial14.bfnt'\n"); /* font's file could not be open */ return(0); } font=cd_loadfont(file); fclose(file); cd_font_height[font]=14; /* create an opacity map */ if (draw_visibility) { char clear_frame[200],*p_clear_frame; opacity_map_available=1; /* defualt */ FindUnknownStars2(star_points); /* associate each star with its PSF. This step is needed by "IsClear" subroutine */ p_clear_frame=FindClosestFile(input_image,¤t_location,clear_frame); if (!p_clear_frame || matrix->exposure_time<160) { printf("\n",input_image); cd_drawtext(bitmap,font,100,100,red_style,"No database entry for this frame.\nImage was not processed.",NO,NO,NO); cd_rect(bitmap,80,100,95,115,1,red_style,red_style); opacity_map_available=0; /* do not generate the picture file */ if (matrix->IsClear(star_points,current_location.clear_threshold)) /* if the image is clear - copy it to the directory as a canonical image */ { char buffer[256]; sprintf(buffer,"cp %s ../data/clear/",input_image); /* create a canonical fits dor that sidereal time */ system(buffer); strcpy(buffer,"ls ../data/clear/*.fits > ../data/clear/dircont"); system(buffer); /* create the directory file */ } } else matrix->FindClouds3(¤t_location,star_points,p_clear_frame,bitmap,1); } /* type the data from the image to an external file */ if (type_text || write_stars_files) flf_write_file(star_points,input_image,matrix->width,matrix->height,matrix->exposure_time,write_stars_files); /* create the background image and the emissivity image */ if (background_image || emissivity) { double max_dist=AltModel270.CalcRules(0); char canonical_frame[256],*p_canonical_frame; if (background_image) matrix->ClearBackground(input_image,bitmap,max_dist); if (emissivity) { p_canonical_frame=FindClosestFile(input_image,¤t_location,canonical_frame); if (!background_image) matrix->ClearBackground(NULL,bitmap,max_dist); //p_canonical_frame=NULL; if (!p_canonical_frame) cd_drawtext(bitmap,font,10,10,red_style,"No database entry for this frame",NO,NO,NO); else if (emissivity) matrix->CompareBackground(bitmap,p_canonical_frame,max_dist); } } /* draw the constellations lines */ if (constellations) { if (all_constellations.init()!=0) { all_constellations.draw(bitmap,star_points); all_constellations.free_consts(); } } /* draw the right ascension and declination lines */ if (draw_RA_DEC_lines) Draw_RA_DEC(bitmap,¤t_location,current_date,font); /* start the calculations of the stars locations */ if (drawstars) { labels[0].x=-1; /* make sure no open loop */ label_index=0; flf_prepare_calc(); while (flf_get_star(star_name,&x,&y,&star_type)) { int min_dist_from_star=30; #ifdef FITS if (star_type==STAR_TYPE_SKY_OBJECT) min_dist_from_star=100000; /* mark sky objects anyway */ if (star_type==STAR_TYPE_BRIGHTSTAR || star_type==STAR_TYPE_PLANET) min_dist_from_star=20; if (star_type==STAR_TYPE_CONSTELLATION) min_dist_from_star=40; /* constellations can be marked even when stars are more distant */ #endif if (matrix->ClosestStar(x,y)>min_dist_from_star*dist_coefficient) continue; /* don't label if the star does not appear */ if (matrix->IsCloudArea(x,y) && (star_type!=STAR_TYPE_SKY_OBJECT)) continue; labels[label_index].x=x; labels[label_index].y=y; labels[label_index].type=star_type; star_name[0]=toupper(star_name[0]); strcpy(labels[label_index].text,star_name); labels[label_index+1].x=-1; label_index++; } } /* *** detect transients *** */ if (detect && matrix->exposure_time>30) /* if not automatic detectction of "unexpected" stars is needed then exit */ { if (matrix->AvgColor>5000) /* if the median value is too high - don't detect this image */ { printf("This image is not clear enough for automatic transient detection"); goto end; } { FindUnknownStars2(star_points); /* find non-catalogued objects */ /* find comparing to a canonical frame */ if (0) { char canonical_frame[256],*p_canonical_frame; p_canonical_frame=FindClosestFile(input_image,¤t_location,canonical_frame); if (!p_canonical_frame) cd_rect(bitmap,0,0,10,10,1,red_style,red_style); else FindUnknownStars(current_date,¤t_location,star_points,p_canonical_frame); } matrix->MarkUnknownStars(star_points,stripes,input_image,bitmap); } } end: /* resize the bitmap (if needed) */ if (resize!=100) { BITMAP temp_bitmap; temp_bitmap=bitmap; /* save the old bitmap */ bitmap=cd_resizebitmap(bitmap,(int)((cd_bitmapwidth[bitmap]*resize)/100),(int)((cd_bitmapheight[bitmap]*resize)/100)); /* resize the bitmap */ cd_freebitmap(temp_bitmap); /* the original; bitmap is not needed any more */ } /* draw the stars' labels */ if (drawstars) matrix->DrawLabels(bitmap,resize,right,below,font); /* draw the meta data (image name, time etc...) */ if (!(unwrap>0)) DrawStaticLabels(bitmap,input_image,resize,matrix->exposure_time); /* free the matrix data structure */ matrix->FreeImage(); delete matrix; if (draw_visibility==1 && opacity_map_available==0) goto end1; /* save the bitmap */ if (is_jpeg) sprintf(filepath,"./cjpeg -quality %i > ",quality); else strcpy(filepath,""); strcat(filepath,output_image); if (is_jpeg) file=popen(filepath,"w"); else file=fopen(filepath,"w"); if (!file) { printf("Could not open output file '%s'\n",filepath); return(0); } /* save the image to a ppm file */ if (cd_save(file,bitmap,ALL,ALL,ALL,ALL)==-1) { printf("Couldn't write to output file %s\n",filepath); return(0); } /* use cjpeg and convert the ppm to jpg */ if (is_jpeg) pclose(file); else fclose(file); end1: cd_freebitmap(bitmap); // cd_freebitmap(source_bitmap); return(1); } /* ********************************************************************************* */ /* ****** main ****** */ /* this function reads the command line attributes and call the "mark stars" routine */ /* last update: 9/02/2002 */ /* attributes: argv, argc - default attributes of command line "main" function */ int main(int argc, char *argv[]) { char error_msg[300],*ext_file=NULL,cjpeg_command_line[50],obs[3]; int res,loc_index=-1; short detect=0,arg_index,write_on_target=0,inverted=0,constellations=0,right=0,below=0,drawstars=0,quality=100,resize=100; short type_text=0,write_stars_files=0,draw_RA_DEC_lines=0,x_mid_pos=0,y_mid_pos=0,draw_visibility=0,background_image=0,emissivity=0; double tjd,unwrap=0.0; /* julian date */ if (argc<3) { printf("usage: wolf [-d|m|w|c|l|i|t|u|xN|yN|rN|bN|fFILENAME|h|qN|sN|g|e|v|pN] [julian_date] [observatory] input_image output_image\ntype \"wolf -h\" for help\n"); if (argc<2 || strcmp(argv[1],"-h")!=0) return(0); } #ifdef FITS MID_X=512; MID_Y=804; #else MID_X=248; MID_Y=235; #endif /* change the working directory to wolf's directory */ if (strrchr(argv[0],'/')) { char command[100]; strcpy(command,argv[0]); *(strrchr(command,'/'))=0; chdir(command); } ang_offset=0; /* by default - do not use the offset but only the fuzzy model for calculating the azimuth */ MAX_DIST_FOR_AUTO_DETECT=14; MAX_DIST_FROM_CENTER=3; arg_index=1; strcpy(cjpeg_command_line,""); while (argv[arg_index][0]=='-') /* flags */ { char num[10],*p,*q; if (argv[arg_index][1]=='f') { ext_file=&(argv[arg_index][2]); /* extrenal file (-f argument) */ arg_index++; continue; /* don't try to detect switches inside the file's name */ } if (strchr(argv[arg_index],'d')) detect=1; /* detect "unexpected" objects */ if (strchr(argv[arg_index],'w')) write_on_target=1; if (strchr(argv[arg_index],'i')) inverted=1; /* the input image is inverted */ if (strchr(argv[arg_index],'c')) constellations=1; /* draw constellations lines */ if (strchr(argv[arg_index],'m')) drawstars=1; /* draw stars' labels */ if (strchr(argv[arg_index],'l')) draw_RA_DEC_lines=1; /* draw the right ascemtion and declination lines */ if (strchr(argv[arg_index],'t')) type_text=1; /* type the text to the standard output stream */ if (strchr(argv[arg_index],'u')) write_stars_files=1; /* create a photomoetry file for each star */ if (strchr(argv[arg_index],'v')) draw_visibility=1; /* detect and draw clouded areas */ if (strchr(argv[arg_index],'g')) background_image=1; /* create a fits file of the background of the image */ if (strchr(argv[arg_index],'e')) emissivity=1; /* create a fits file of the emissivity */ if (strchr(argv[arg_index],'x')) { p=q=strchr(argv[arg_index],'x')+1; while (isdigit(*p) || (*p=='-') || (*p=='+')) {num[p-q]=*p;p++;} /* read the number */ num[p-q]='\0'; if (isdigit(num[0])) x_mid_pos=10000+atoi(num); /* assign an absolute value to the X coordinate */ else x_mid_pos=atoi(num); /* a relative value to the X coordinate */ } if (strchr(argv[arg_index],'y')) { p=q=strchr(argv[arg_index],'y')+1; while (isdigit(*p) || (*p=='-') || (*p=='+')) {num[p-q]=*p;p++;} /* read the number */ num[p-q]='\0'; if (isdigit(num[0])) y_mid_pos=10000+atoi(num); /* assign an absolute value to the middle Y coordinate */ else x_mid_pos=atoi(num); /* a relative value to Y coordinate */ } if (strchr(argv[arg_index],'r')) { p=q=strchr(argv[arg_index],'r')+1; while (isdigit(*p) || (*p=='-')) {num[p-q]=*p;p++;} num[p-q]='\0'; right=atoi(num); } if (strchr(argv[arg_index],'b')) { p=q=strchr(argv[arg_index],'b')+1; while (isdigit(*p) || (*p=='-')) {num[p-q]=*p;p++;} num[p-q]='\0'; below=atoi(num); } /* get the quality parameter value */ if (strchr(argv[arg_index],'q')) { p=q=strchr(argv[arg_index],'q')+1; while (isdigit(*p) || (*p=='-')) {num[p-q]=*p;p++;} num[p-q]='\0'; quality=atoi(num); } /* get the resize parameter value */ if (strchr(argv[arg_index],'s')) { p=q=strchr(argv[arg_index],'s')+1; while (isdigit(*p) || (*p=='-')) {num[p-q]=*p;p++;} num[p-q]='\0'; resize=atoi(num); } /* get the unwrapped */ if (strchr(argv[arg_index],'p')) { p=q=strchr(argv[arg_index],'p')+1; while (isdigit(*p) || (*p=='-')) {num[p-q]=*p;p++;} num[p-q]='\0'; unwrap=(double)atoi(num); } if (strchr(argv[arg_index],'h')) { ShowHelp(); return(0); } arg_index++; } /* get the julian date */ tjd=atof(argv[arg_index++]); if (tjd==0) arg_index--; /* the julian date will be read from the file's name */ else if (tjd<2000000 || tjd>8000000) { printf("'%f' is not a valid julian date\n",tjd); return(0); } /* get the observatory and set the right unique parameters of each supported observatory */ /* set the parameters of Wise observatory */ if (strrchr(argv[arg_index],'/')) strncpy(obs,&(strrchr(argv[arg_index],'/')[1]),2); else strncpy(obs,argv[arg_index],2); /* copy the name of the observatory from the beginning of the image name */ obs[2]='\0'; /* put the null terminator */ if (strcmp(obs,"wo")==0) loc_index=RAMON; /* wise observatory */ if (strcmp(obs,"rh")==0) loc_index=RHO; /* Rosemary Hill observatory */ if (strcmp(obs,"mk")==0) loc_index=MAUNA_KEA; /* Mauna Kea observatory */ if (strcmp(obs,"ci")==0) loc_index=CANARY_ISLAND; /* canary island observatory */ if (strcmp(obs,"kp")==0) loc_index=KITT_PEAK; /* kitt peak observatory */ if (strcmp(obs,"sd")==0) loc_index=SIDING_SPRING; /* siding spring observatory */ if (strcmp(obs,"sa")==0) loc_index=SOUTH_AFRICA; /* south africa observatory */ if (strcmp(obs,"hl")==0) loc_index=HALEAKALA; /* haleakala observatory */ if (strcmp(obs,"cp")==0) loc_index=CERRO_PACHON; /* cerro pachon observatory */ if (strcmp(obs,"sb")==0) loc_index=SAPIR_COLLEGE; /* concam1 at sde boker */ if (strcmp(obs,"ha")==0) loc_index=HANLE; /* hanle */ /* set the parameters of Wilson Mountain observatory */ if (strcmp(argv[arg_index],"mw")==0) { loc_index=WILSON; MAX_DIST_FOR_AUTO_DETECT=26; MAX_DIST_FROM_CENTER=24; } if (LoadObservatory()) loc_index=CUSTOM; /* load observatory information from an external file */ if (loc_index<0) { printf("'%s' is not a valid observatory name\n",argv[arg_index]); return(0); } if (strlen(argv[arg_index])==2) arg_index++; /* the observatory name was specified explicitly */ /* prepare for calculation */ res=flf_init(NULL,ext_file,error_msg); if (res==0) { printf("wolf can not initialize\n"); printf("error message: %s\n",error_msg); return(0); } /* setting the global adjustment attributes according to the location */ MID_X=locations[loc_index].MID_X; MID_Y=locations[loc_index].MID_Y; ang_offset=locations[loc_index].ang_offset; dist_coefficient=locations[loc_index].dist_coefficient; /* changing the MID_X and MID_Y value according to the parameters */ if (x_mid_pos>10000) MID_X=x_mid_pos-10000; /* absolute MID_X value (specified as an argument) */ else MID_X=MID_X+x_mid_pos; /* relative MID_X value (specified as an argument) */ if (y_mid_pos>10000) MID_Y=y_mid_pos-10000; /* absolute MID_Y value (specified as an argument) */ else MID_Y=MID_Y+y_mid_pos; /* relative MID_Y value (specified as an argument) */ /* start calculating the stars */ // res=flf_init(NULL,ext_file,error_msg); // if (res==0) // { printf("wolf can not initialize\n"); // printf("error message: %s\n",error_msg); // return(0); // } /* make sure both input and output files are specified // if (arg_index>argc-1) // { printf("input and output image files must be specified!\n"); // return(0); // } /* get the julian date from the image (if not specified) */ if (tjd==0) { tjd=GetFileDate(argv[arg_index]); if (tjd==0) { printf("Julian date not specified\n"); return(0); } } /* mark the stars */ current_location=locations[loc_index]; flf_set_date_location(tjd,locations[loc_index].latitude,locations[loc_index].longitude,locations[loc_index].height,locations[loc_index].temperature,locations[loc_index].pressure); markstars(argv[arg_index],argv[arg_index+1],detect,write_on_target,inverted,constellations,right,below,drawstars, quality,resize,type_text,write_stars_files,draw_RA_DEC_lines,draw_visibility,background_image,emissivity,unwrap); flf_cleanup(); return(1); }