start development of tables routines

This commit is contained in:
eddyem
2019-03-26 21:04:30 +03:00
parent a7927fc570
commit 39e1ac994f
14 changed files with 282 additions and 181 deletions

View File

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

47
examples/Readme.md Normal file
View File

@@ -0,0 +1,47 @@
Examples
========
## common.h
Common files for all
## imstat.c
Usage: imstat [args] input files
Get statistics and modify images from first image HDU of each input file
Where args are:
-a, --add=arg add some value (double, or 'mean', 'std', 'min', 'max')
-h, --help show this help
-m, --multiply=arg multiply by some value (double, operation run after adding)
-o, --outfile=arg output file name (collect all input files)
-z, --rmneg remove negative values (assign them to 0)
## keylist.c
Usage: keylist [args] infile.fits
Where args are:
-a, --addrec add record to first HDU (you can add more than one record in once, point more -a)
-c, --contents show short file contents
-h, --help show this help
-i, --infile=arg input file name (you can also point it without any keys)
-l, --list list all keywords
-m, --modify modify values of given keys (each param should be "key = new_value")
-o, --output=arg save result to file (else save to same file)
Contains example of protected file writing (blocking all possible signals).
## listtable.c
Usage: listtable [args]
Where args are:
-h, --help show this help
-i, --fitsname=arg name of input file
-l, --list list all tables in file
-o, --outfile=arg output file name

View File

@@ -1,30 +0,0 @@
/*
* 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/>.
*/
#include "common.h"
int main(){
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C");
#if defined GETTEXT_PACKAGE && defined LOCALEDIR
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
textdomain(GETTEXT_PACKAGE);
#endif
return 0;
}

View File

@@ -38,8 +38,8 @@ typedef struct{
/*
* here are global parameters initialisation
*/
int help;
glob_pars G = {
static int help;
static glob_pars G = {
.mult = 1.,
};
@@ -47,7 +47,7 @@ glob_pars G = {
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
myoption cmdlnopts[] = {
static 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)")},
@@ -71,9 +71,9 @@ typedef struct{
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
static glob_pars *parse_args(int argc, char **argv){
int i;
char *helpstring = "Usage: %%s [args] input files\n"
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);
@@ -89,7 +89,7 @@ glob_pars *parse_args(int argc, char **argv){
return &G;
}
imgstat *get_imgstat(double *dimg, long totpix){
static 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];
@@ -108,12 +108,12 @@ imgstat *get_imgstat(double *dimg, long totpix){
return &st;
}
void printstat(imgstat *stat){
static 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 addsomething(FITSimage *img, double *dimg, imgstat *stat){
static bool addsomething(FITSimage *img, double *dimg, imgstat *stat){
if(!G.add || !img || !stat) return FALSE;
// parser:
char *eptr;
@@ -136,7 +136,7 @@ bool addsomething(FITSimage *img, double *dimg, imgstat *stat){
return TRUE;
}
bool multbysomething(FITSimage *img, double *dimg){
static bool multbysomething(FITSimage *img, double *dimg){
if(!img || !dimg) return FALSE;
if(fabs(G.mult) < 2*DBL_EPSILON) return FALSE;
DBG("multiply by %g", G.mult);
@@ -146,7 +146,7 @@ bool multbysomething(FITSimage *img, double *dimg){
return TRUE;
}
bool process_fitsfile(char *inname, FITS *output){
static bool process_fitsfile(char *inname, FITS *output){
DBG("File %s", inname);
bool mod = FALSE;
FITS *f = FITS_read(inname);

View File

@@ -38,8 +38,8 @@ typedef struct{
/*
* here are global parameters initialisation
*/
int help;
glob_pars G; /* = {
static int help;
static glob_pars G; /* = {
;
};*/
@@ -47,14 +47,14 @@ glob_pars G; /* = {
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
myoption cmdlnopts[] = {
static myoption cmdlnopts[] = {
// common options
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
{"contents",NO_ARGS, NULL, 'c', arg_none, APTR(&G.contents), _("show short file contents")},
{"list", NO_ARGS, NULL, 'l', arg_none, APTR(&G.list), _("list all keywords")},
{"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\")")},
{"modify", MULT_PAR, NULL, 'm', arg_string, APTR(&G.modify), _("modify 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
};
@@ -66,9 +66,9 @@ myoption cmdlnopts[] = {
* @param argv - copy of argv from main
* @return allocated structure with global parameters
*/
glob_pars *parse_args(int argc, char **argv){
static glob_pars *parse_args(int argc, char **argv){
int i;
char *helpstring = "Usage: %%s [args] infile.fits\n\n\tWhere args are:\n";
char *helpstring = "Usage: %s [args] infile.fits\n\n\tWhere args are:\n";
change_helpstring(helpstring);
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
@@ -81,13 +81,13 @@ glob_pars *parse_args(int argc, char **argv){
return &G;
}
void ch(int s){
static void ch(int s){
signal(s, SIG_IGN);
printf("signal: %d\n", s);
signal(s, ch);
}
void print_imgHDU(FITSimage *image){
static 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)

86
examples/listtable.c Normal file
View File

@@ -0,0 +1,86 @@
/*
* 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/>.
*/
#include "common.h"
typedef struct{
char *fitsname; // input file name
char *outfile; // output file name
int list; // list tables
} glob_pars;
/*
* here are global parameters initialisation
*/
static int help;
static glob_pars G;
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
static myoption cmdlnopts[] = {
// common options
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
{"fitsname",NEED_ARG, NULL, 'i', arg_string, APTR(&G.fitsname), _("name of input file")},
{"list", NO_ARGS, NULL, 'l', arg_none, APTR(&G.list), _("list all tables in file")},
{"outfile", NEED_ARG, NULL, 'o', arg_none, APTR(&G.outfile), _("output file name")},
end_option
};
/**
* 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
*/
static glob_pars *parse_args(int argc, char **argv){
int i;
char *helpstring = "Usage: %s [args]\n\n\tWhere args are:\n";
change_helpstring(helpstring);
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
if(argc > 0){
for (i = 0; i < argc; i++)
printf("Ignore extra argument: %s\n", argv[i]);
}
return &G;
}
int main(int argc, char *argv[]){
FITS *ofits = NULL;
initial_setup();
parse_args(argc, argv);
if(!G.fitsname) ERRX(_("No input filename given!"));
DBG("Open file %s", G.fitsname);
if(G.outfile){
ofits = MALLOC(FITS, 1);
ofits->filename = G.outfile;
}
FITS *f = FITS_read(G.fitsname);
if(G.list) table_print_all(f);
if(ofits){
green("\nWrite to output file %s\n", ofits->filename);
FITS_write(ofits->filename, ofits);
}
return 0;
}