image reading routines + starting version of example imstat

This commit is contained in:
eddyem
2019-02-22 23:09:00 +03:00
parent 65f2ed910a
commit 196c1d5d09
9 changed files with 432 additions and 150 deletions

View File

@@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 3.9)
project(examples)
include_directories(../)
link_libraries(usefull_macros FITSmanip cfitsio)
link_libraries(usefull_macros FITSmanip cfitsio m)
#add_executable(fitsstat fitsstat.c)
add_executable(keylist keylist.c)
add_executable(imstat imstat.c)

178
examples/imstat.c Normal file
View File

@@ -0,0 +1,178 @@
/*
* This file is part of the FITSmaniplib project.
* Copyright 2019 Edward V. Emelianov <edward.emelianoff@gmail.com>, <eddy@sao.ru>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This example allows to calculate simple statistics over fits file,
* add constant number or multiply by constant all images in file
*/
#include "common.h"
#include <float.h>
#include <math.h>
#include <string.h>
typedef struct{
char *outfile; // output file name (for single operation)
char **infiles; // input files list
int Ninfiles; // input files number
} glob_pars;
/*
* here are global parameters initialisation
*/
int help;
glob_pars G = {
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
myoption cmdlnopts[] = {
// common options
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
{"outfile", NEED_ARG, NULL, 'o', arg_string, APTR(&G.outfile), _("output file name (collect all input files)")},
end_option
};
typedef struct{
double mean;
double std;
double min;
double max;
} imgstat;
/**
* Parse command line options and return dynamically allocated structure
* to global parameters
* @param argc - copy of argc from main
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
int i;
char *helpstring = "Usage: %%s [args] input files\n"
"Get statistics and modify images from first image HDU of each input file\n"
"\tWhere args are:\n";
change_helpstring(helpstring);
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
G.Ninfiles = argc;
G.infiles = MALLOC(char*, argc);
for (i = 0; i < argc; i++)
G.infiles[i] = strdup(argv[i]);
}
return &G;
}
imgstat *get_imgstat(double *dimg, long totpix){
static imgstat st;
if(!dimg || !totpix) return &st; // return some trash if wrong data
st.min = dimg[0];
st.max = dimg[0];
double sum = dimg[0], sum2 = dimg[0];
for(long i = 1; i < totpix; ++i){
double val = dimg[i];
if(st.min > val) st.min = val;
if(st.max < val) st.max = val;
sum += val;
sum2 += val*val;
}
DBG("tot:%ld, sum=%g, sum2=%g, min=%g, max=%g", totpix, sum, sum2, st.min, st.max);
st.mean = sum / totpix;
st.std = sqrt(sum2/totpix - st.mean*st.mean);
return &st;
}
void printstat(imgstat *stat){
green("Statistics:\n");
printf("MEAN=%g\nSTD=%g\nMIN=%g\nMAX=%g\n", stat->mean, stat->std, stat->min, stat->max);
}
bool process_fitsfile(char *inname, FITS *output){
DBG("File %s", inname);
bool mod = FALSE;
FITS *f = FITS_read(inname);
if(!f){WARNX("Can't read %s", inname); return FALSE;}
// search first image HDU
f->curHDU = NULL;
for(int i = 1; i <= f->NHDUs; ++i){
green("File %s, %dth HDU, type: %d\n", inname, i, f->HDUs[i].hdutype);
if(f->HDUs[i].hdutype != IMAGE_HDU) continue;
FITSimage *img = f->HDUs[i].contents.image;
if(!img){
WARNX("empty image");
continue;
}
if(img->totpix == 0){
WARNX("totpix=0");
continue; // no data - only header
}
f->curHDU = &f->HDUs[i];
break;
}
if(!f->curHDU){
WARNX("Didn't find image HDU in %s", inname);
}else{ // OK, we have an image and can do something with it
green("\tGet image from this HDU.\n");
FITSimage *img = f->curHDU->contents.image;
double *dImg = image2double(img);
// calculate image statistics
imgstat *stat = get_imgstat(dImg, img->totpix);
printstat(stat);
//mod = TRUE;
FREE(dImg);
}
if(mod || output){ // file modified (or output file pointed), write differences
if(!output){
green("Rewrite file %s.\n", f->filename);
if(!FITS_rewrite(f)) WARNX("Can't rewrite %s", inname);
}else{ // copy first image HDU and its header to output file
green("Add image to %s.\n", output->filename);
FITSHDU *hdu = FITS_addHDU(output);
if(!hdu) return NULL;
hdu->hdutype = IMAGE_HDU;
hdu->keylist = keylist_copy(f->curHDU->keylist);
hdu->contents.image = image_copy(f->curHDU->contents.image);
}
}
FITS_free(&f);
return mod;
}
int main(int argc, char *argv[]){
FITS *ofits = NULL;
bool mod = FALSE;
initial_setup();
parse_args(argc, argv);
if(!G.Ninfiles) ERRX(_("No input filename[s] given!"));
if(G.outfile){
ofits = MALLOC(FITS, 1);
ofits->filename = G.outfile;
}
for(int i = 0; i < G.Ninfiles; ++i){
if(process_fitsfile(G.infiles[i], ofits)) mod = 1;
}
if(ofits && mod){
green("\nWrite all modified images to output file %s\n", ofits->filename);
FITS_write(ofits->filename, ofits);
}
return 0;
}

View File

@@ -55,6 +55,7 @@ myoption cmdlnopts[] = {
{"addrec", MULT_PAR, NULL, 'a', arg_string, APTR(&G.addrec), _("add record to first HDU (you can add more than one record in once, point more -a)")},
{"output", NEED_ARG, NULL, 'o', arg_string, APTR(&G.outfile), _("save result to file (else save to same file)")},
{"modify", MULT_PAR, NULL, 'm', arg_string, APTR(&G.modify), _("modify values values of given keys (each param should be \"key = new_value\")")},
{"infile", NEED_ARG, NULL, 'i', arg_string, APTR(&G.fitsname), _("input file name (you can also point it without any keys)")},
end_option
};
@@ -72,7 +73,7 @@ glob_pars *parse_args(int argc, char **argv){
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc)(G.fitsname = strdup(argv[0]));
if(argc && !G.fitsname)(G.fitsname = strdup(argv[0]));
if(argc > 1){
for (i = 1; i < argc; i++)
printf("Ignore extra argument: %s\n", argv[i]);
@@ -86,6 +87,16 @@ void ch(int s){
signal(s, ch);
}
void print_imgHDU(FITSimage *image){
printf("Image: naxis=%d, totpix=%ld, ", image->naxis, image->totpix);
printf("naxes=(");
for(int i = 0; i < image->naxis; ++i)
printf("%s%ld", i?", ":"", image->naxes[i]);
if(image->naxis) printf("), ");
else printf("none), ");
printf("bitpix=%d, dtype=%d", image->bitpix, image->dtype);
}
int main(int argc, char *argv[]){
initial_setup();
parse_args(argc, argv);
@@ -107,7 +118,7 @@ int main(int argc, char *argv[]){
printf("\tHDU #%d - ", i);
switch(f->HDUs[i].hdutype){
case IMAGE_HDU:
printf("Image");
print_imgHDU(f->HDUs[i].contents.image);
break;
case ASCII_TBL:
printf("ASCII table");
@@ -121,11 +132,12 @@ int main(int argc, char *argv[]){
printf("\n");
}
}
int differs = 0;
if(G.addrec){
char **ptr = G.addrec;
while(*ptr){
printf("record: %s\n", *ptr);
keylist_add_record(&(f->HDUs[1].keylist), *ptr++, 1);
if(keylist_add_record(&(f->HDUs[1].keylist), *ptr++, 1)) differs = 1;
}
}
if(G.modify){
@@ -140,28 +152,30 @@ int main(int argc, char *argv[]){
*val++ = 0; // now `val` is value + comment; ptr is key
if(!keylist_modify_key(f->HDUs[1].keylist, *ptr, val)){
WARNX("key %s not found", *ptr);
}
}else differs = 1;
++ptr;
}
}
// test for signals handler: ctrl+c, ctrl+z
signal(SIGINT, ch);
signal(SIGTSTP, ch);
// protect critical zone blocking all possible signals:
sigset_t mask, oldmask;
sigfillset(&mask);
sigprocmask(SIG_SETMASK, &mask, &oldmask);
if(G.outfile){ // save result to new file
FITS_write(G.outfile, f);
}else{
FITS_rewrite(f);
if(differs){ // file differs, need to save new file
// test for signals handler: ctrl+c, ctrl+z
signal(SIGINT, ch);
signal(SIGTSTP, ch);
// protect critical zone blocking all possible signals:
sigset_t mask, oldmask;
sigfillset(&mask);
sigprocmask(SIG_SETMASK, &mask, &oldmask);
if(G.outfile){ // save result to new file
FITS_write(G.outfile, f);
}else{
FITS_rewrite(f);
}
DBG("Written! Sleep for 2 seconds in ctitical section");
sleep(2);
// return ignoring
DBG("Unblock signals, sleep for 2 seconds");
sigprocmask(SIG_SETMASK, &oldmask, NULL);
sleep(2);
}
DBG("Written! Sleep for 2 seconds in ctitical section");
sleep(2);
// return ignoring
DBG("Unblock signals, sleep for 2 seconds");
sigprocmask(SIG_SETMASK, &oldmask, NULL);
sleep(2);
/*
* Do something here
*/