some settings for .dat file processing

This commit is contained in:
eddyem 2016-09-30 18:57:41 +03:00
parent 8f5337f5d3
commit dec8c284bc
16 changed files with 2142 additions and 0 deletions

View File

@ -10,3 +10,9 @@ Allows to stream BTA data over TCP socket
** Stellarium_control
Allows to point telescope with help of stellarium
** fits_headers
Process FITS-files with WCS headers and calculate reference angle between plate angle (P2) and parallax angle
** wfs_read
Read WFS/DAT files got by SHA tool (Shack-Hartmann processing utilite) and get some information from it.

24
wfs_read/Makefile Normal file
View File

@ -0,0 +1,24 @@
PROGRAM = readwfs
LDFLAGS =
SRCS = $(wildcard *.c)
CC = gcc
DEFINES = -D_XOPEN_SOURCE=1111
CXX = gcc
CFLAGS = -Wall -Werror -Wextra $(DEFINES) -std=gnu99
OBJS = $(SRCS:.c=.o)
all : $(PROGRAM)
$(PROGRAM) : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) $(LDFLAGS) -o $(PROGRAM)
# some addition dependencies
# %.o: %.c
# $(CC) $(LDFLAGS) $(CFLAGS) $< -o $@
$(SRCS) : %.c : %.h
touch $@
%.h: ;
clean:
/bin/rm -f *.o *~
depend:
$(CXX) -MM $(CXX.SRCS)

85
wfs_read/cmdlnopts.c Normal file
View File

@ -0,0 +1,85 @@
/*
* cmdlnopts.c - the only function that parse cmdln args and returns glob parameters
*
* Copyright 2013 Edward V. Emelianoff <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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <assert.h>
#include <strings.h>
#include "cmdlnopts.h"
#include "usefull_macros.h"
#include "zernike.h" // for DEFAULT_CRD_STEP & DEFAULT_WF_UNIT
/*
* here are global parameters initialisation
*/
int help;
static glob_pars G;
// DEFAULTS
// default global parameters
glob_pars const Gdefault = {
.inwfs = NULL // input WFS file name
,.indat = NULL // input DAT file name
,.outname = "wavefront_coords.dat" // output file name
,.step = DEFAULT_CRD_STEP // coordinate step in wavefront map
,.wfunits = DEFAULT_WF_UNIT // units for wavefront measurement in WF map
,.wavelength = DEFAULT_WAVELENGTH // default wavelength
};
/*
* Define command line options by filling structure:
* name has_arg flag val type argptr help
*/
myoption cmdlnopts[] = {
// set 1 to param despite of its repeating number:
{"help", NO_ARGS, NULL, 'h', arg_int, APTR(&help), _("show this help")},
// simple integer parameter with obligatory arg:
{"wfs", NEED_ARG, NULL, 'w', arg_string, APTR(&G.inwfs), _("input WFS file name")},
{"dat", NEED_ARG, NULL, 'd', arg_string, APTR(&G.indat), _("input DAT file name")},
{"output", NEED_ARG, NULL, 'o', arg_string, APTR(&G.outname), _("output file name")},
{"step", NEED_ARG, NULL, 's', arg_double, APTR(&G.step), _("coordinate step in wavefront map (R=1)")},
{"wfunits", NEED_ARG, NULL, 'u', arg_string, APTR(&G.wfunits), _("units for wavefront measurement in WF map")},
{"wavelength", NEED_ARG, NULL, 'l', arg_double, APTR(&G.wavelength),_("default wavelength (in meters, microns or nanometers), 101..9999nm")},
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
*/
glob_pars *parse_args(int argc, char **argv){
void *ptr = memcpy(&G, &Gdefault, sizeof(G));
if(!ptr) ERR(_("Can't memcpy"));
// format of help: "Usage: progname [args]\n"
change_helpstring(_("Usage: %s [args]\n\n\tWhere args are:\n"));
// parse arguments
parseargs(&argc, &argv, cmdlnopts);
if(help) showhelp(-1, cmdlnopts);
/*
int i;
if(argc > 0){
G.rest_pars_num = argc;
G.rest_pars = calloc(argc, sizeof(char*));
for (i = 0; i < argc; i++)
G.rest_pars[i] = strdup(argv[i]);
}*/
return &G;
}

45
wfs_read/cmdlnopts.h Normal file
View File

@ -0,0 +1,45 @@
/*
* cmdlnopts.h - comand line options for parceargs
*
* Copyright 2013 Edward V. Emelianoff <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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __CMDLNOPTS_H__
#define __CMDLNOPTS_H__
#include "parseargs.h"
/*
* here are some typedef's for global data
*/
typedef struct{
char *inwfs; // input WFS file name
char *indat; // input DAT file name
char *outname; // output file name
double step; // coordinate step in wavefront map
char *wfunits; // units for wavefront measurement in WF map
double wavelength; // default wavelength
} glob_pars;
// default & global parameters
extern glob_pars const Gdefault;
glob_pars *parse_args(int argc, char **argv);
#endif // __CMDLNOPTS_H__

95
wfs_read/main.c Normal file
View File

@ -0,0 +1,95 @@
/*
* main.c
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <math.h>
#include "usefull_macros.h"
#include "cmdlnopts.h"
#include "readwfs.h"
#include "readdat.h"
#include "zernike.h"
glob_pars *GP = NULL;
/**
* Read and dump WFS file
*/
void proc_WFS(){
int fd = open(GP->inwfs, O_RDONLY);
if(fd < 0){
WARN(_("open %s"), GP->inwfs);
return;
}
if(!test_wfs_file(fd)){
WARNX(_("Bad WFS file %s"), GP->inwfs);
return;
}
Zhistory *hist = show_zhistry(fd);
print_table(hist, fd);
hist = show_zhistry(fd);
print_table(hist, fd);
close(fd);
}
void proc_DAT(){
int i, Zn;
if(fabs(GP->step - DEFAULT_CRD_STEP) > DBL_EPSILON){ // user change default step
if((i = z_set_step(GP->step)))
WARNX(_("Can't change step to %g, value is too %s"), GP->step, i < 0 ? "small" : "big");
return;
}
if(fabs(GP->wavelength - DEFAULT_WAVELENGTH) > DBL_EPSILON){ // user want to change wavelength
// WARNING! This option test should be before changing unit because units depends on wavelength
if(z_set_wavelength(GP->wavelength)){
WARNX(_("Bad wavelength: %g, should be from 100 to 10000um (1e-7 to 1e-5 or 0.1 to 10)"), GP->wavelength);
return;
}
}
if(strcmp(GP->wfunits, DEFAULT_WF_UNIT)){ // user ask to change default unit
if(z_set_wfunit(GP->wfunits)){
WARNX(_("Bad wavefront unit: %s. Should be one of"), GP->wfunits);
z_print_wfunits();
return;
}
}
double *zerncoeffs = read_dat_file(GP->indat, &Zn);
if(!zerncoeffs){
WARNX(_("Bad DAT file %s"), GP->indat);
return;
}
printf("Read coefficients:\n");
for(i = 0; i < Zn; ++i) printf("%4d\t%g\n", i, zerncoeffs[i]);
}
/**
* Read DAT file and build table with wavefront coordinates
*/
int main(int argc, char** argv){
initial_setup();
GP = parse_args(argc, argv);
if(!GP->inwfs && !GP->indat) ERRX(_("You should give input file name"));
if(GP->inwfs){
proc_WFS();
}
if(GP->indat){
proc_DAT();
}
return 0;
}

0
wfs_read/main.h Normal file
View File

497
wfs_read/parseargs.c Normal file
View File

@ -0,0 +1,497 @@
/*
* parseargs.c - parsing command line arguments & print help
*
* Copyright 2013 Edward V. Emelianoff <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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <stdio.h> // printf
#include <getopt.h> // getopt_long
#include <stdlib.h> // calloc, exit, strtoll
#include <assert.h> // assert
#include <string.h> // strdup, strchr, strlen
#include <strings.h>// strcasecmp
#include <limits.h> // INT_MAX & so on
#include <libintl.h>// gettext
#include <ctype.h> // isalpha
#include "parseargs.h"
#include "usefull_macros.h"
char *helpstring = "%s\n";
/**
* Change standard help header
* MAY consist ONE "%s" for progname
* @param str (i) - new format
*/
void change_helpstring(char *s){
int pcount = 0, scount = 0;
char *str = s;
// check `helpstring` and set it to default in case of error
for(; pcount < 2; str += 2){
if(!(str = strchr(str, '%'))) break;
if(str[1] != '%') pcount++; // increment '%' counter if it isn't "%%"
else{
str += 2; // pass next '%'
continue;
}
if(str[1] == 's') scount++; // increment "%s" counter
};
if(pcount > 1 || pcount != scount){ // amount of pcount and/or scount wrong
/// "îÅÐÒÁ×ÉÌØÎÙÊ ÆÏÒÍÁÔ ÓÔÒÏËÉ ÐÏÍÏÝÉ"
ERRX(_("Wrong helpstring!"));
}
helpstring = s;
}
/**
* Carefull atoll/atoi
* @param num (o) - returning value (or NULL if you wish only check number) - allocated by user
* @param str (i) - string with number must not be NULL
* @param t (i) - T_INT for integer or T_LLONG for long long (if argtype would be wided, may add more)
* @return TRUE if conversion sone without errors, FALSE otherwise
*/
static bool myatoll(void *num, char *str, argtype t){
long long tmp, *llptr;
int *iptr;
char *endptr;
assert(str);
assert(num);
tmp = strtoll(str, &endptr, 0);
if(endptr == str || *str == '\0' || *endptr != '\0')
return FALSE;
switch(t){
case arg_longlong:
llptr = (long long*) num;
*llptr = tmp;
break;
case arg_int:
default:
if(tmp < INT_MIN || tmp > INT_MAX){
/// "ãÅÌÏÅ ×ÎÅ ÄÏÐÕÓÔÉÍÏÇÏ ÄÉÁÐÁÚÏÎÁ"
WARNX(_("Integer out of range"));
return FALSE;
}
iptr = (int*)num;
*iptr = (int)tmp;
}
return TRUE;
}
// the same as myatoll but for double
// There's no NAN & INF checking here (what if they would be needed?)
static bool myatod(void *num, const char *str, argtype t){
double tmp, *dptr;
float *fptr;
char *endptr;
assert(str);
tmp = strtod(str, &endptr);
if(endptr == str || *str == '\0' || *endptr != '\0')
return FALSE;
switch(t){
case arg_double:
dptr = (double *) num;
*dptr = tmp;
break;
case arg_float:
default:
fptr = (float *) num;
*fptr = (float)tmp;
break;
}
return TRUE;
}
/**
* Get index of current option in array options
* @param opt (i) - returning val of getopt_long
* @param options (i) - array of options
* @return index in array
*/
static int get_optind(int opt, myoption *options){
int oind;
myoption *opts = options;
assert(opts);
for(oind = 0; opts->name && opts->val != opt; oind++, opts++);
if(!opts->name || opts->val != opt) // no such parameter
showhelp(-1, options);
return oind;
}
/**
* reallocate new value in array of multiple repeating arguments
* @arg paptr - address of pointer to array (**void)
* @arg type - its type (for realloc)
* @return pointer to new (next) value
*/
void *get_aptr(void *paptr, argtype type){
int i = 1;
void **aptr = *((void***)paptr);
if(aptr){ // there's something in array
void **p = aptr;
while(*p++) ++i;
}
size_t sz = 0;
switch(type){
default:
case arg_none:
/// "îÅ ÍÏÇÕ ÉÓÐÏÌØÚÏ×ÁÔØ ÎÅÓËÏÌØËÏ ÐÁÒÁÍÅÔÒÏ× ÂÅÚ ÁÒÇÕÍÅÎÔÏ×!"
ERRX("Can't use multiple args with arg_none!");
break;
case arg_int:
sz = sizeof(int);
break;
case arg_longlong:
sz = sizeof(long long);
break;
case arg_double:
sz = sizeof(double);
break;
case arg_float:
sz = sizeof(float);
break;
case arg_string:
sz = 0;
break;
/* case arg_function:
sz = sizeof(argfn *);
break;*/
}
aptr = realloc(aptr, (i + 1) * sizeof(void*));
*((void***)paptr) = aptr;
aptr[i] = NULL;
if(sz){
aptr[i - 1] = malloc(sz);
}else
aptr[i - 1] = &aptr[i - 1];
return aptr[i - 1];
}
/**
* Parse command line arguments
* ! If arg is string, then value will be strdup'ed!
*
* @param argc (io) - address of argc of main(), return value of argc stay after `getopt`
* @param argv (io) - address of argv of main(), return pointer to argv stay after `getopt`
* BE CAREFUL! if you wanna use full argc & argv, save their original values before
* calling this function
* @param options (i) - array of `myoption` for arguments parcing
*
* @exit: in case of error this function show help & make `exit(-1)`
*/
void parseargs(int *argc, char ***argv, myoption *options){
char *short_options, *soptr;
struct option *long_options, *loptr;
size_t optsize, i;
myoption *opts = options;
// check whether there is at least one options
assert(opts);
assert(opts[0].name);
// first we count how much values are in opts
for(optsize = 0; opts->name; optsize++, opts++);
// now we can allocate memory
short_options = calloc(optsize * 3 + 1, 1); // multiply by three for '::' in case of args in opts
long_options = calloc(optsize + 1, sizeof(struct option));
opts = options; loptr = long_options; soptr = short_options;
// in debug mode check the parameters are not repeated
#ifdef EBUG
char **longlist = MALLOC(char*, optsize);
char *shortlist = MALLOC(char, optsize);
#endif
// fill short/long parameters and make a simple checking
for(i = 0; i < optsize; i++, loptr++, opts++){
// check
assert(opts->name); // check name
#ifdef EBUG
longlist[i] = strdup(opts->name);
#endif
if(opts->has_arg){
assert(opts->type != arg_none); // check error with arg type
assert(opts->argptr); // check pointer
}
if(opts->type != arg_none) // if there is a flag without arg, check its pointer
assert(opts->argptr);
// fill long_options
// don't do memcmp: what if there would be different alignment?
loptr->name = opts->name;
loptr->has_arg = (opts->has_arg < MULT_PAR) ? opts->has_arg : 1;
loptr->flag = opts->flag;
loptr->val = opts->val;
// fill short options if they are:
if(!opts->flag && opts->val){
#ifdef EBUG
shortlist[i] = (char) opts->val;
#endif
*soptr++ = opts->val;
if(loptr->has_arg) // add ':' if option has required argument
*soptr++ = ':';
if(loptr->has_arg == 2) // add '::' if option has optional argument
*soptr++ = ':';
}
}
// sort all lists & check for repeating
#ifdef EBUG
int cmpstringp(const void *p1, const void *p2){
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
int cmpcharp(const void *p1, const void *p2){
return (int)(*(char * const)p1 - *(char *const)p2);
}
qsort(longlist, optsize, sizeof(char *), cmpstringp);
qsort(shortlist,optsize, sizeof(char), cmpcharp);
char *prevl = longlist[0], prevshrt = shortlist[0];
for(i = 1; i < optsize; ++i){
if(longlist[i]){
if(prevl){
if(strcmp(prevl, longlist[i]) == 0) ERRX("double long arguments: --%s", prevl);
}
prevl = longlist[i];
}
if(shortlist[i]){
if(prevshrt){
if(prevshrt == shortlist[i]) ERRX("double short arguments: -%c", prevshrt);
}
prevshrt = shortlist[i];
}
}
#endif
// now we have both long_options & short_options and can parse `getopt_long`
while(1){
int opt;
int oindex = 0, optind = 0; // oindex - number of option in argv, optind - number in options[]
if((opt = getopt_long(*argc, *argv, short_options, long_options, &oindex)) == -1) break;
if(opt == '?'){
opt = optopt;
optind = get_optind(opt, options);
if(options[optind].has_arg == NEED_ARG || options[optind].has_arg == MULT_PAR)
showhelp(optind, options); // need argument
}
else{
if(opt == 0 || oindex > 0) optind = oindex;
else optind = get_optind(opt, options);
}
opts = &options[optind];
if(opt == 0 && opts->has_arg == NO_ARGS) continue; // only long option changing integer flag
// now check option
if(opts->has_arg == NEED_ARG || opts->has_arg == MULT_PAR)
if(!optarg) showhelp(optind, options); // need argument
void *aptr;
if(opts->has_arg == MULT_PAR){
aptr = get_aptr(opts->argptr, opts->type);
}else
aptr = opts->argptr;
bool result = TRUE;
// even if there is no argument, but argptr != NULL, think that optarg = "1"
if(!optarg) optarg = "1";
switch(opts->type){
default:
case arg_none:
if(opts->argptr) *((int*)aptr) += 1; // increment value
break;
case arg_int:
result = myatoll(aptr, optarg, arg_int);
break;
case arg_longlong:
result = myatoll(aptr, optarg, arg_longlong);
break;
case arg_double:
result = myatod(aptr, optarg, arg_double);
break;
case arg_float:
result = myatod(aptr, optarg, arg_float);
break;
case arg_string:
result = (*((void**)aptr) = (void*)strdup(optarg));
break;
case arg_function:
result = ((argfn)aptr)(optarg);
break;
}
if(!result){
showhelp(optind, options);
}
}
*argc -= optind;
*argv += optind;
}
/**
* compare function for qsort
* first - sort by short options; second - sort arguments without sort opts (by long options)
*/
static int argsort(const void *a1, const void *a2){
const myoption *o1 = (myoption*)a1, *o2 = (myoption*)a2;
const char *l1 = o1->name, *l2 = o2->name;
int s1 = o1->val, s2 = o2->val;
int *f1 = o1->flag, *f2 = o2->flag;
// check if both options has short arg
if(f1 == NULL && f2 == NULL){ // both have short arg
return (s1 - s2);
}else if(f1 != NULL && f2 != NULL){ // both don't have short arg - sort by long
return strcmp(l1, l2);
}else{ // only one have short arg -- return it
if(f2) return -1; // a1 have short - it is 'lesser'
else return 1;
}
}
/**
* Show help information based on myoption->help values
* @param oindex (i) - if non-negative, show only help by myoption[oindex].help
* @param options (i) - array of `myoption`
*
* @exit: run `exit(-1)` !!!
*/
void showhelp(int oindex, myoption *options){
int max_opt_len = 0; // max len of options substring - for right indentation
const int bufsz = 255;
char buf[bufsz+1];
myoption *opts = options;
assert(opts);
assert(opts[0].name); // check whether there is at least one options
if(oindex > -1){ // print only one message
opts = &options[oindex];
printf(" ");
if(!opts->flag && isalpha(opts->val)) printf("-%c, ", opts->val);
printf("--%s", opts->name);
if(opts->has_arg == 1) printf("=arg");
else if(opts->has_arg == 2) printf("[=arg]");
printf(" %s\n", _(opts->help));
exit(-1);
}
// header, by default is just "progname\n"
printf("\n");
if(strstr(helpstring, "%s")) // print progname
printf(helpstring, __progname);
else // only text
printf("%s", helpstring);
printf("\n");
// count max_opt_len
do{
int L = strlen(opts->name);
if(max_opt_len < L) max_opt_len = L;
}while((++opts)->name);
max_opt_len += 14; // format: '-S , --long[=arg]' - get addition 13 symbols
opts = options;
// count amount of options
int N; for(N = 0; opts->name; ++N, ++opts);
if(N == 0) exit(-2);
// Now print all help (sorted)
opts = options;
qsort(opts, N, sizeof(myoption), argsort);
do{
int p = sprintf(buf, " "); // a little indent
if(!opts->flag && opts->val) // .val is short argument
p += snprintf(buf+p, bufsz-p, "-%c, ", opts->val);
p += snprintf(buf+p, bufsz-p, "--%s", opts->name);
if(opts->has_arg == 1) // required argument
p += snprintf(buf+p, bufsz-p, "=arg");
else if(opts->has_arg == 2) // optional argument
p += snprintf(buf+p, bufsz-p, "[=arg]");
assert(p < max_opt_len); // there would be magic if p >= max_opt_len
printf("%-*s%s\n", max_opt_len+1, buf, _(opts->help)); // write options & at least 2 spaces after
++opts;
}while(--N);
printf("\n\n");
exit(-1);
}
/**
* get suboptions from parameter string
* @param str - parameter string
* @param opt - pointer to suboptions structure
* @return TRUE if all OK
*/
bool get_suboption(char *str, mysuboption *opt){
int findsubopt(char *par, mysuboption *so){
int idx = 0;
if(!par) return -1;
while(so[idx].name){
if(strcasecmp(par, so[idx].name) == 0) return idx;
++idx;
}
return -1; // badarg
}
bool opt_setarg(mysuboption *so, int idx, char *val){
mysuboption *soptr = &so[idx];
bool result = FALSE;
void *aptr = soptr->argptr;
switch(soptr->type){
default:
case arg_none:
if(soptr->argptr) *((int*)aptr) += 1; // increment value
result = TRUE;
break;
case arg_int:
result = myatoll(aptr, val, arg_int);
break;
case arg_longlong:
result = myatoll(aptr, val, arg_longlong);
break;
case arg_double:
result = myatod(aptr, val, arg_double);
break;
case arg_float:
result = myatod(aptr, val, arg_float);
break;
case arg_string:
result = (*((void**)aptr) = (void*)strdup(val));
break;
case arg_function:
result = ((argfn)aptr)(val);
break;
}
return result;
}
char *tok;
bool ret = FALSE;
char *tmpbuf;
tok = strtok_r(str, ":,", &tmpbuf);
do{
char *val = strchr(tok, '=');
int noarg = 0;
if(val == NULL){ // no args
val = "1";
noarg = 1;
}else{
*val++ = '\0';
if(!*val || *val == ':' || *val == ','){ // no argument - delimeter after =
val = "1"; noarg = 1;
}
}
int idx = findsubopt(tok, opt);
if(idx < 0){
/// "îÅÐÒÁ×ÉÌØÎÙÊ ÐÁÒÁÍÅÔÒ: %s"
WARNX(_("Wrong parameter: %s"), tok);
goto returning;
}
if(noarg && opt[idx].has_arg == NEED_ARG){
/// "%s: ÎÅÏÂÈÏÄÉÍ ÁÒÇÕÍÅÎÔ!"
WARNX(_("%s: argument needed!"), tok);
goto returning;
}
if(!opt_setarg(opt, idx, val)){
/// "îÅÐÒÁ×ÉÌØÎÙÊ ÁÒÇÕÍÅÎÔ \"%s\" ÐÁÒÁÍÅÔÒÁ \"%s\""
WARNX(_("Wrong argument \"%s\" of parameter \"%s\""), val, tok);
goto returning;
}
}while((tok = strtok_r(NULL, ":,", &tmpbuf)));
ret = TRUE;
returning:
return ret;
}

124
wfs_read/parseargs.h Normal file
View File

@ -0,0 +1,124 @@
/*
* parseargs.h - headers for parsing command line arguments
*
* Copyright 2013 Edward V. Emelianoff <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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __PARSEARGS_H__
#define __PARSEARGS_H__
#include <stdbool.h>// bool
#include <stdlib.h>
#ifndef TRUE
#define TRUE true
#endif
#ifndef FALSE
#define FALSE false
#endif
// macro for argptr
#define APTR(x) ((void*)x)
// if argptr is a function:
typedef bool(*argfn)(void *arg);
/*
* type of getopt's argument
* WARNING!
* My function change value of flags by pointer, so if you want to use another type
* make a latter conversion, example:
* char charg;
* int iarg;
* myoption opts[] = {
* {"value", 1, NULL, 'v', arg_int, &iarg, "char val"}, ..., end_option};
* ..(parse args)..
* charg = (char) iarg;
*/
typedef enum {
arg_none = 0, // no arg
arg_int, // integer
arg_longlong, // long long
arg_double, // double
arg_float, // float
arg_string, // char *
arg_function // parse_args will run function `bool (*fn)(char *optarg, int N)`
} argtype;
/*
* Structure for getopt_long & help
* BE CAREFUL: .argptr is pointer to data or pointer to function,
* conversion depends on .type
*
* ATTENTION: string `help` prints through macro PRNT(), bu default it is gettext,
* but you can redefine it before `#include "parseargs.h"`
*
* if arg is string, then value wil be strdup'ed like that:
* char *str;
* myoption opts[] = {{"string", 1, NULL, 's', arg_string, &str, "string val"}, ..., end_option};
* *(opts[1].str) = strdup(optarg);
* in other cases argptr should be address of some variable (or pointer to allocated memory)
*
* NON-NULL argptr should be written inside macro APTR(argptr) or directly: (void*)argptr
*
* !!!LAST VALUE OF ARRAY SHOULD BE `end_option` or ZEROS !!!
*
*/
typedef enum{
NO_ARGS = 0, // first three are the same as in getopt_long
NEED_ARG = 1,
OPT_ARG = 2,
MULT_PAR
} hasarg;
typedef struct{
// these are from struct option:
const char *name; // long option's name
hasarg has_arg; // 0 - no args, 1 - nesessary arg, 2 - optionally arg, 4 - need arg & key can repeat (args are stored in null-terminated array)
int *flag; // NULL to return val, pointer to int - to set its value of val (function returns 0)
int val; // short opt name (if flag == NULL) or flag's value
// and these are mine:
argtype type; // type of argument
void *argptr; // pointer to variable to assign optarg value or function `bool (*fn)(char *optarg, int N)`
const char *help; // help string which would be shown in function `showhelp` or NULL
} myoption;
/*
* Suboptions structure, almost the same like myoption
* used in parse_subopts()
*/
typedef struct{
const char *name;
hasarg has_arg;
argtype type;
void *argptr;
} mysuboption;
// last string of array (all zeros)
#define end_option {0,0,0,0,0,0,0}
#define end_suboption {0,0,0,0}
extern const char *__progname;
void showhelp(int oindex, myoption *options);
void parseargs(int *argc, char ***argv, myoption *options);
void change_helpstring(char *s);
bool get_suboption(char *str, mysuboption *opt);
#endif // __PARSEARGS_H__

146
wfs_read/readdat.c Normal file
View File

@ -0,0 +1,146 @@
/*
* readdat.c
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <ctype.h> // isspace
#include <strings.h> // strncasecmp
#include "usefull_macros.h"
#include "readdat.h"
/**
* Get next text line ending with '\n' or '\n\r'
* @param start (i) - text in current line
* @param end (i) - end of text
* @return pointer to next line first character (spaces are omit) or NULL
*/
char *nextline(char *start, char* end){
if(!start || !end) return NULL;
while(start < end) // get next newline symbol
if(*start++ == '\n') break;
while(start < end){ // now skip all spaces, '\r' etc
if(isspace(*start)) ++start;
else break;
}
if(start == end) return NULL;
return start;
}
/**
* Read next double value from .dat file
* @param begin (i) - beginning of data portion
* @param end (i) - data end
* @param num (o) - number read
* @return pointer to next data item if line isn't end after current
* or pointer to next '\n' symbol (or `end`) if this value was last in string;
* NULL if there was an error during strtod
*/
char *read_double(char *begin, char *end, double *num){
char *nextchar;
if(!num || !begin|| !end || begin >= end) return NULL;
*num = strtod(begin, &nextchar);
if(!nextchar) // end of data
return end;
if(begin == nextchar || !isspace(*nextchar)){
char buf[10];
snprintf(buf, 10, "%s", begin);
WARNX(_("Not a number %s..."), buf);
return NULL;
}
while(nextchar < end){
char c = *nextchar;
if(isspace(c) && c != '\n') ++nextchar;
else break;
}
return nextchar;
}
/**
* Find in .dat file neader column number named `val`
* @param val (i) - name of column to search
* @param dat (i) - header start
* @param end (i) - data end
* @return number of column found or -1
*/
int get_hdrval(char *val, char *dat, char *end){
size_t l = strlen(val);
int i = 0;
while(dat < end){
char c = 0;
while(dat < end){ // skip spaces
c = *dat;
if(isspace(c) && c != '\n') ++dat;
else break;
}
if(c == '\n' || dat == end) return -1; // end of line or end of data reached
if(!strncasecmp(dat, val, l)) return i;
while(dat < end){ // skip non-spaces
if(!isspace(*dat)) ++dat;
else break;
}
++i;
}
return -1;
}
/**
* Read .dat file and get Zernike coefficients from it
* @param fname (i) - .dat file name
* @param sz (o) - size of coefficients' array
* @return dynamically allocated array or NULL in case of error
*/
double *read_dat_file(char *fname, int *sz){
if(!fname) return NULL;
mmapbuf *dbuf = My_mmap(fname);
if(!dbuf) return NULL;
char *dat = dbuf->data, *eptr = dat + dbuf->len;
if(strncasecmp(dat, "time", 4)){
WARNX(_("Bad header"));
return NULL;
}
int rd = 0, skipfst = get_hdrval("piston", dat, eptr), L = Z_REALLOC_STEP;
if(skipfst < 0){
WARNX(_("Dat file don't have OSA Zernike coefficients"));
return NULL;
}
dat = nextline(dat, eptr);
double *zern = MALLOC(double, Z_REALLOC_STEP);
while(dat < eptr){
double d;
char *next = read_double(dat, eptr, &d);
if(!next) break;
dat = next;
if(skipfst > 0){ // skip this value
--skipfst;
continue;
}
if(++rd > L){
L += Z_REALLOC_STEP;
double *z_realloc = realloc(zern, L * sizeof(double));
if(!z_realloc){
WARN(_("Reallocation of memory failed"));
break;
}
zern = z_realloc;
}
zern[rd - 1] = d;
if(*next == '\n') break;
}
if(sz) *sz = rd;
return zern;
}

30
wfs_read/readdat.h Normal file
View File

@ -0,0 +1,30 @@
/*
* readdat.h
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __READDAT_H__
#define __READDAT_H__
// allocate memory with quantum of this
#define Z_REALLOC_STEP (10)
double *read_dat_file(char *fname, int *sz);
#endif // __READDAT_H__

257
wfs_read/readwfs.c Normal file
View File

@ -0,0 +1,257 @@
/*
* readwfs.c
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <time.h>
#include "usefull_macros.h"
#include "readwfs.h"
WFS_header hdr;
void hexdump(int fd, int N);
void signals(int sig){
exit(sig);
}
void gettime(struct tm *tm, SYSTEMTIME *st){
if(!tm || !st) return;
tm->tm_sec = st->wSecond;
tm->tm_min = st->wMinute;
tm->tm_hour = st->wHour;
tm->tm_mday = st->wDay;
tm->tm_mon = st->wMonth - 1;
tm->tm_year = st->wYear - 1900;
tm->tm_isdst = -1;
}
/**
* read from file fd maxlen bytes into optr with size optrlen
* return 0 if suxeed
*/
int get_struct(int fd, void *optr, int optrlen, int maxlen){
if(maxlen < optrlen) return 1;
if(optrlen != read(fd, optr, optrlen)) return 2;
if(optrlen != maxlen){
//printf("seek to %d", maxlen-optrlen);
lseek(fd, maxlen-optrlen, SEEK_CUR);
}
return 0;
}
void show_sparam(int fd){
Sparam *par = &hdr.sparam;
if(get_struct(fd, (void*)par, sizeof(Sparam), hdr.Sparam_sz)) ERRX("read sparam");
//if(get_struct(fd, (void*)par, sizeof(Sparam), sizeof(Sparam))) ERRX("read sparam");
printf("\nSPARAM:\nInput pupil: %gmm, wavelength: %gnm\n", par->SystemInputPupilM*1e3,
par->WLength*1e9);
printf("Focus len: %gmm, refraction idx: %g\n", par->SysFocusLength*1e3,
par->SysRefraction);
printf("Pix2WF: %g, pixel size: %gmkm, sensor size(W:H): %d:%d\n", par->Pix2WF, par->Pix2Meter*1e6,
par->W, par->H);
printf("Distance between lenses: %g, %spre-estimate\n", par->LensD, par->bPreEstimate ? "" : "not ");
printf("Pupil shift: %gmm, output pupil: %dm\n", par->dPupilShift*1e3,
par->SystemOutputPupilP);
printf("Xdir: %g, Ydir: %g\n", par->XDir, par->YDir);
printf("Version: %d\n", par->Version);
printf("Using %d polynomials\n", par->NumberOfPolynomials);
printf("Lenslet geometry: %s\n", par->iLensletGeometry == 8 ? "square":
(par->iLensletGeometry == 6 ? "hexagonal" : "rombus"));
if(par->Afocal) printf("Afocal system\n");
printf("Lenslet F: %gmm\n", par->LensletFocusLength);
printf("Scale factor: %g, Well depth: %de\n\n", par->ScaleFactor, par->WellDepth);
}
void show_mparam(int fd){
Mparam *par = &hdr.mparam;
if(get_struct(fd, (void*)par, sizeof(Mparam), hdr.Mparam_sz)) ERRX("read mparam");
//if(get_struct(fd, (void*)par, sizeof(Mparam), sizeof(Mparam))) ERRX("read mparam");
struct tm tm;
gettime(&tm, &par->DateTime);
time_t t = mktime(&tm);
printf("\nMPARAM:\nMeasurements date/time: %s\n", ctime(&t));
int i;
printf("#\tMeasurement id\n");
for(i = 0; i < 1024; ++i) if(par->MeasurementID[i]) printf("%d\t0x%02X\n", i, par->MeasurementID[i]);
}
/**
"* Test header of WFS file
*/
int test_wfs_file(int fd){
if(fd < 1) return 0;
int L = 3*sizeof(uint32_t);
if(read(fd, &hdr, L) != L){
ERR("read");
}
if(sizeof(Zhistory) > hdr.Zhistory_sz){
ERRX("Zhistory size is %d instead of %d\n", hdr.Zhistory_sz, sizeof(Zhistory));
}
printf("Zhistory: should be: %ld, in file: %d\n", sizeof(Zhistory), hdr.Zhistory_sz);
if(sizeof(Hhistory) > hdr.Hhistory_sz){
ERRX("Hhistory size is %d instead of %d\n", hdr.Hhistory_sz, sizeof(Hhistory));
}
printf("Hhistory: should be: %ld, in file: %d\n", sizeof(Hhistory), hdr.Hhistory_sz);
if(sizeof(Sparam) > hdr.Sparam_sz){
ERRX("Sparam size is %d instead of %d\n", hdr.Sparam_sz, sizeof(Sparam));
}
printf("Sparam: should be: %ld, in file: %d\n", sizeof(Sparam), hdr.Sparam_sz);
show_sparam(fd);
if(sizeof(uint32_t) != read(fd, &hdr.Mparam_sz, sizeof(uint32_t))){
ERR("read");
}
printf("Mparam: should be: %ld, in file: %d\n", sizeof(Mparam), hdr.Mparam_sz);
if(sizeof(Mparam) > hdr.Mparam_sz){
ERRX("Mparam size is %d instead of %d\n", hdr.Mparam_sz, sizeof(Mparam));
}
show_mparam(fd);
if(sizeof(uint32_t) != read(fd, &hdr.History_len, sizeof(uint32_t))){
ERR("read");
}
printf("\n\n%d records in history\n\n", hdr.History_len);
return 1;
}
void show_hhistry(Hhistory *h){
printf("\nHHISTORY:\nN spots: %d\n", h->NSpots);
printf("Unitary circle center: (%d, %d), radius: %d\n", h->C.ux, h->C.uy, h->C.ur);
printf("Measurement area center: (%d, %d), ", h->C.ax, h->C.ay);
if(h->C.ar == 0) printf("rectangle\n");
else{
if(h->C.ar < 0) printf("radius: %d (area outside this radius)\n", -h->C.ar);
else printf("radius: %d\n", h->C.ar);
}
printf("Pointers: ");
int i; for(i = 0; i < 6; ++i) printf("%d, ", h->pointers[i]);
printf("\nPdiff: ");
for(i = 1; i < 6; ++i) printf("%d, ", h->pointers[i] - h->pointers[i-1]);
printf("\n");
}
static off_t zhstart;
Zhistory *show_zhistry(int fd){
Zhistory *hist = MALLOC(Zhistory, 1);
zhstart = lseek(fd, 0, SEEK_CUR);
int L = (void*)&hist->h - (void*)hist;
if(get_struct(fd, (void*)hist, L, L)) ERRX("read Zhistory header");
if(get_struct(fd, (void*)&hist->h, sizeof(Hhistory), hdr.Hhistory_sz)) ERRX("read Hhistory");
L = (void*)&hist->zhend - (void*)&hist->sx;
int szdiff = (hdr.Zhistory_sz - sizeof(Zhistory)) - (hdr.Hhistory_sz - sizeof(Hhistory));
// 2 - dirty hack
if(get_struct(fd, (void*)&hist->sx, L, L+szdiff+2)) ERRX("read remain of Zhistory");
//printf("diff: %d\n", szdiff);
printf("\nZHISTORY:\nCurrent number of poly: %d\n", hist->CurrentNumberOfPolynomials);
if(hist->CurrentNumberOfPolynomials < 37){
int i;
printf("#\t polynomial value\n");
for(i = 0; i < 37; ++i) printf("%d\t%g\n", i, hist->loPolynomials[i]);
}
printf("Sphere: %g, Cylinder: %g, Axis: %g\n", hist->Sphere, hist->Cylinder, hist->Axis);
printf("Circle diameter: %gmm\n", hist->ADiameter * 1e3);
printf("Time from measurements start: %dms\n", hist->Time);
printf("Polynomial set: %s\n", hist->PolynomialSet == 0 ? "Fringe" :
(hist->PolynomialSet == 1 ? "Born & Wolf" :
(hist->PolynomialSet == 2 ? "OSA" : "Ring")));
show_hhistry(&hist->h);
printf("Beam center: (%g, %g)mm, radius: %gmm\n",
hist->sx*1e3, hist->sy*1e3, hist->sr*1e3);
printf("Unitary circle diameter: %gmm\n", hist->UDiameter*1e3);
if(hist->bBad) red("Bad image - not use it!\n");
if(hist->bZRW) printf("Reconstructed by zonal method\n");
printf("Chi^2 = %g\n", hist->chi2);
printf("Time from start %Lgmks\n", hist->time_mcsec);
printf("\n\nRESERVED:\nReserved3_1: %g, %g, %g\n", hist->Reserved3_1[0], hist->Reserved3_1[1], hist->Reserved3_1[2]);
printf("Reserved3_2: %g, %g, %g\n", hist->Reserved3_2[0], hist->Reserved3_2[1], hist->Reserved3_2[2]);
printf("pfReserved3: %d, iReserved4: %d\n", hist->pfReserved3, hist->iReserved4);
printf("pdReserved6: %d, pReserved7: %d\n", hist->pdReserved6, hist->pReserved7);
return hist;
}
float *getflt(int fd, int sz){
int L = sizeof(float)*sz;
float *flt = MALLOC(float, sz);
if(L != read(fd, flt, L)) ERR("read");
return flt;
}
uint8_t *get_uint8(int fd, int sz){
uint8_t *u = MALLOC(uint8_t, sz);
if(sz != read(fd, u, sz)) ERR("read");
return u;
}
void hexdump(int fd, int N){
uint8_t *hx = MALLOC(uint8_t, N);
int i, L = read(fd, hx, N);
for(i = 0; i < L; ++i){
printf("0x%02X ", hx[i]);
if(i%16 == 15) printf("\n");
}
printf("\n");
FREE(hx);
}
/**
* print table of parameters for given "history"
*/
void print_table(Zhistory *hist, int fd){
int Nspots = hist->h.NSpots, Npoly = hist->CurrentNumberOfPolynomials, i, zon = hist->bZRW;
float *XR, *YR, *XC, *YC, *W, *D, *poly = NULL, *Z=NULL, *I;
uint8_t *flag;
off_t blkstart = lseek(fd, 0, SEEK_CUR);
XR = getflt(fd, Nspots);
YR = getflt(fd, Nspots);
XC = getflt(fd, Nspots);
YC = getflt(fd, Nspots);
W = getflt(fd, Nspots);
D = getflt(fd, Nspots);
flag = MALLOC(uint8_t, Nspots);
I = getflt(fd, Nspots);
if(Npoly > 37) poly = getflt(fd, Npoly);
printf("\nTable:\n# Arrays of data for frame\n");
printf("#\tXR\tYR\tXC\tYC\tWeight\tDispersion\tFlag\tIntensity");
if(zon){
printf("\tZonal");
Z = getflt(fd, Nspots);
}
printf("\n");
for(i = 0; i < Nspots; ++i){
printf("%d\t%f\t%f\t%f\t%f\t%f\t%f\t%d\t%f", i,XR[i], YR[i], XC[i], YC[i],
W[i], D[i], flag[i], I[i]);
if(zon) printf("\t%f", Z[i]);
printf("\n");
}
if(poly){
printf("\nPolynomial coefficients\n#\tcoeff\n");
for(i = 0; i < Npoly; ++i) printf("%d\t%f\n", i, poly[i]);
}
FREE(XR); FREE(YR); FREE(XC); FREE(YC); FREE(W); FREE(D); FREE(flag);
FREE(I);
FREE(Z); FREE(poly);
//printf("next 4474:\n");
//hexdump(fd, 4474);
printf("next 1346:\n");
hexdump(fd, 1346);
off_t blkend = lseek(fd, 0, SEEK_CUR);
printf("Block length: %zd, ZH length: %zd\n", blkend - blkstart, blkend - zhstart);
/* printf("starting of next ZHistory\nzeros: \n");
hexdump(fd, 37*8);
printf("CurrentNumberOfPolynomials: ");
hexdump(fd, 4);*/
}

149
wfs_read/readwfs.h Normal file
View File

@ -0,0 +1,149 @@
/*
* wfsparams.h
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __WFSPARAMS_H__
#define __WFSPARAMS_H__
#include <stdint.h>
#pragma pack(push, 4)
typedef struct{
double SystemInputPupilM;
double WLength; // wavelength in meters
double SysFocusLength;
double SysRefraction;
double Pix2WF; // coefficient of spots' shift reduction into wavefront tilts
double Pix2Meter; // sensor's pixel size in meters
double LensD; // distance between lenses in lenslet (pixels)
int32_t W, H; // sensor's size (pixels)
int32_t bPreEstimate;
double dPupilShift;
int32_t iReserved2;
int32_t SystemOutputPupilP; // in meters
double XDir; // camera mirrored by X? (1 or -1)
uint32_t Version;
double YDir; // camera mirrored by Y? (1 or -1)
int32_t iReserved3_3[3];
double dReserved4;
int32_t iReserved4_2[2];
int32_t NumberOfPolynomials;
int32_t iLensletGeometry; // lenslet geometry: 8 - square, 6 - hexagonal, 4 - rombus
int32_t Afocal; // TRUE for afocal system
double LensletFocusLength; // in meters
int32_t iTRelay; // WTF?
double ScaleFactor;
int32_t WellDepth; // in electrons
int8_t reserved_[20]; // added for compatibility with M$W
} Sparam;
enum eDummy {eDummyNone = 0};
typedef struct _SYSTEMTIME {
uint16_t wYear;
uint16_t wMonth;
uint16_t wDayOfWeek;
uint16_t wDay;
uint16_t wHour;
uint16_t wMinute;
uint16_t wSecond;
uint16_t wMilliseconds;
} SYSTEMTIME;
typedef struct {
int8_t MeasurementID[1024];
uint8_t Dummy;
double Dummy1;
double Dummy2;
double Dummy3;
int8_t Reserved1[2048];
double Dummy4;
int8_t Dummy5;
int32_t Reserved2_2[2];
double Dummy6;
double Reserved3;
int8_t bReserved4;
double Reserved5;
SYSTEMTIME DateTime;
double Dummy7;
double Dummy8;
int8_t Dummy9;
int64_t Reserved6;
} Mparam;
typedef struct {
int32_t NSpots;
union {
struct {
int32_t ux, uy, ur; // unitary circle center & radius
int32_t ax, ay, ar; // measurement area -//- ar == 0 - rectangle, ar < 0 - inverted (outside this radius)
} C;
struct {
int32_t x, y, a, w, h; // rectangle area center, rotation angle, width and height
} R;
};
int32_t pointers[6]; //uint8_t *ucpReserved2;
uint32_t ucpReserved2;
int8_t bReserved3;
//int8_t Reserved4[256];
int8_t Reserved4[258];
} Hhistory;
typedef struct{
double loPolynomials[37]; // if amount of poly <37, they are stored here
int32_t CurrentNumberOfPolynomials;
double Sphere, Cylinder, Axis; // approximate sphere & astigmatism in dptr/degrees
double ADiameter; // measurement circle diameter in meters
int32_t Time; // time from measurement starts (ms)
double Reserved3_1[3];
uint8_t PolynomialSet; // 0 - Fringe, 1 - Born/Wolf, 2 - OSA , 4 - Ring
double Reserved3_2[3];
Hhistory h;
double sx, sy, sr; // center & beam radius calculated by S-H (meters)
double UDiameter; // diameter of unitary circle (meters)
int32_t pfReserved3; // float*
int32_t iReserved4;
uint8_t bReserved5_6[6];
int32_t pdReserved6; // double*
int8_t bBad; // == true if current image bad
int8_t bZRW; // == true if wavefront was reconstructed by zonal method
int32_t pReserved7; // float*
double chi2;
long double time_mcsec; // time from beginning of measurement, mks
int8_t reserved_[74]; // added for compatibility with M$W
uint8_t zhend[0];
} Zhistory;
typedef struct{
uint32_t Zhistory_sz;
uint32_t Hhistory_sz;
uint32_t Sparam_sz;
Sparam sparam;
uint32_t Mparam_sz;
Mparam mparam;
int32_t History_len;
} WFS_header;
#pragma pack(pop)
int test_wfs_file(int fd);
void print_table(Zhistory *hist, int fd);
Zhistory *show_zhistry(int fd);
#endif // __WFSPARAMS_H__

371
wfs_read/usefull_macros.c Normal file
View File

@ -0,0 +1,371 @@
/*
* usefull_macros.h - a set of usefull functions: memory, color etc
*
* Copyright 2013 Edward V. Emelianoff <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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "usefull_macros.h"
/**
* function for different purposes that need to know time intervals
* @return double value: time in seconds
*/
double dtime(){
double t;
struct timeval tv;
gettimeofday(&tv, NULL);
t = tv.tv_sec + ((double)tv.tv_usec)/1e6;
return t;
}
/******************************************************************************\
* Coloured terminal
\******************************************************************************/
int globErr = 0; // errno for WARN/ERR
// pointers to coloured output printf
int (*red)(const char *fmt, ...);
int (*green)(const char *fmt, ...);
int (*_WARN)(const char *fmt, ...);
/*
* format red / green messages
* name: r_pr_, g_pr_
* @param fmt ... - printf-like format
* @return number of printed symbols
*/
int r_pr_(const char *fmt, ...){
va_list ar; int i;
printf(RED);
va_start(ar, fmt);
i = vprintf(fmt, ar);
va_end(ar);
printf(OLDCOLOR);
return i;
}
int g_pr_(const char *fmt, ...){
va_list ar; int i;
printf(GREEN);
va_start(ar, fmt);
i = vprintf(fmt, ar);
va_end(ar);
printf(OLDCOLOR);
return i;
}
/*
* print red error/warning messages (if output is a tty)
* @param fmt ... - printf-like format
* @return number of printed symbols
*/
int r_WARN(const char *fmt, ...){
va_list ar; int i = 1;
fprintf(stderr, RED);
va_start(ar, fmt);
if(globErr){
errno = globErr;
vwarn(fmt, ar);
errno = 0;
globErr = 0;
}else
i = vfprintf(stderr, fmt, ar);
va_end(ar);
i++;
fprintf(stderr, OLDCOLOR "\n");
return i;
}
static const char stars[] = "****************************************";
/*
* notty variants of coloured printf
* name: s_WARN, r_pr_notty
* @param fmt ... - printf-like format
* @return number of printed symbols
*/
int s_WARN(const char *fmt, ...){
va_list ar; int i;
i = fprintf(stderr, "\n%s\n", stars);
va_start(ar, fmt);
if(globErr){
errno = globErr;
vwarn(fmt, ar);
errno = 0;
globErr = 0;
}else
i = +vfprintf(stderr, fmt, ar);
va_end(ar);
i += fprintf(stderr, "\n%s\n", stars);
i += fprintf(stderr, "\n");
return i;
}
int r_pr_notty(const char *fmt, ...){
va_list ar; int i;
i = printf("\n%s\n", stars);
va_start(ar, fmt);
i += vprintf(fmt, ar);
va_end(ar);
i += printf("\n%s\n", stars);
return i;
}
/**
* Run this function in the beginning of main() to setup locale & coloured output
*/
void initial_setup(){
// setup coloured output
if(isatty(STDOUT_FILENO)){ // make color output in tty
red = r_pr_; green = g_pr_;
}else{ // no colors in case of pipe
red = r_pr_notty; green = printf;
}
if(isatty(STDERR_FILENO)) _WARN = r_WARN;
else _WARN = s_WARN;
// Setup locale
setlocale(LC_ALL, "");
setlocale(LC_NUMERIC, "C");
#if defined GETTEXT_PACKAGE && defined LOCALEDIR
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
textdomain(GETTEXT_PACKAGE);
#endif
}
/******************************************************************************\
* Memory
\******************************************************************************/
/*
* safe memory allocation for macro ALLOC
* @param N - number of elements to allocate
* @param S - size of single element (typically sizeof)
* @return pointer to allocated memory area
*/
void *my_alloc(size_t N, size_t S){
void *p = calloc(N, S);
if(!p) ERR("malloc");
//assert(p);
return p;
}
/**
* Mmap file to a memory area
*
* @param filename (i) - name of file to mmap
* @return stuct with mmap'ed file or die
*/
mmapbuf *My_mmap(char *filename){
int fd;
char *ptr;
size_t Mlen;
struct stat statbuf;
/// "îÅ ÚÁÄÁÎÏ ÉÍÑ ÆÁÊÌÁ!"
if(!filename){
WARNX(_("No filename given!"));
return NULL;
}
if((fd = open(filename, O_RDONLY)) < 0){
/// "îÅ ÍÏÇÕ ÏÔËÒÙÔØ %s ÄÌÑ ÞÔÅÎÉÑ"
WARN(_("Can't open %s for reading"), filename);
return NULL;
}
if(fstat (fd, &statbuf) < 0){
/// "îÅ ÍÏÇÕ ×ÙÐÏÌÎÉÔØ stat %s"
WARN(_("Can't stat %s"), filename);
close(fd);
return NULL;
}
Mlen = statbuf.st_size;
if((ptr = mmap (0, Mlen, PROT_READ, MAP_PRIVATE, fd, 0)) == MAP_FAILED){
/// "ïÛÉÂËÁ mmap"
WARN(_("Mmap error for input"));
close(fd);
return NULL;
}
/// "îÅ ÍÏÇÕ ÚÁËÒÙÔØ mmap'ÎÕÔÙÊ ÆÁÊÌ"
if(close(fd)) WARN(_("Can't close mmap'ed file"));
mmapbuf *ret = MALLOC(mmapbuf, 1);
ret->data = ptr;
ret->len = Mlen;
return ret;
}
void My_munmap(mmapbuf *b){
if(munmap(b->data, b->len)){
/// "îÅ ÍÏÇÕ munmap"
ERR(_("Can't munmap"));
}
FREE(b);
}
/******************************************************************************\
* Terminal in no-echo mode
\******************************************************************************/
static struct termios oldt, newt; // terminal flags
static int console_changed = 0;
// run on exit:
void restore_console(){
if(console_changed)
tcsetattr(STDIN_FILENO, TCSANOW, &oldt); // return terminal to previous state
console_changed = 0;
}
// initial setup:
void setup_con(){
if(console_changed) return;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
if(tcsetattr(STDIN_FILENO, TCSANOW, &newt) < 0){
/// "îÅ ÍÏÇÕ ÎÁÓÔÒÏÉÔØ ËÏÎÓÏÌØ"
WARN(_("Can't setup console"));
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
signals(0); //quit?
}
console_changed = 1;
}
/**
* Read character from console without echo
* @return char readed
*/
int read_console(){
int rb;
struct timeval tv;
int retval;
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
tv.tv_sec = 0; tv.tv_usec = 10000;
retval = select(1, &rfds, NULL, NULL, &tv);
if(!retval) rb = 0;
else {
if(FD_ISSET(STDIN_FILENO, &rfds)) rb = getchar();
else rb = 0;
}
return rb;
}
/**
* getchar() without echo
* wait until at least one character pressed
* @return character readed
*/
int mygetchar(){ // getchar() without need of pressing ENTER
int ret;
do ret = read_console();
while(ret == 0);
return ret;
}
/******************************************************************************\
* TTY with select()
\******************************************************************************/
static struct termio oldtty, tty; // TTY flags
static int comfd = -1; // TTY fd
// run on exit:
void restore_tty(){
if(comfd == -1) return;
ioctl(comfd, TCSANOW, &oldtty ); // return TTY to previous state
close(comfd);
comfd = -1;
}
#ifndef BAUD_RATE
#define BAUD_RATE B9600
#endif
// init:
void tty_init(char *comdev){
DBG("\nOpen port...\n");
if ((comfd = open(comdev,O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0){
WARN("Can't use port %s\n",comdev);
ioctl(comfd, TCSANOW, &oldtty); // return TTY to previous state
close(comfd);
signals(0); // quit?
}
DBG(" OK\nGet current settings... ");
if(ioctl(comfd,TCGETA,&oldtty) < 0){ // Get settings
/// "îÅ ÍÏÇÕ ÐÏÌÕÞÉÔØ ÎÁÓÔÒÏÊËÉ"
WARN(_("Can't get settings"));
signals(0);
}
tty = oldtty;
tty.c_lflag = 0; // ~(ICANON | ECHO | ECHOE | ISIG)
tty.c_oflag = 0;
tty.c_cflag = BAUD_RATE|CS8|CREAD|CLOCAL; // 9.6k, 8N1, RW, ignore line ctrl
tty.c_cc[VMIN] = 0; // non-canonical mode
tty.c_cc[VTIME] = 5;
if(ioctl(comfd,TCSETA,&tty) < 0){
/// "îÅ ÍÏÇÕ ÕÓÔÁÎÏ×ÉÔØ ÎÁÓÔÒÏÊËÉ"
WARN(_("Can't set settings"));
signals(0);
}
DBG(" OK\n");
}
/**
* Read data from TTY
* @param buff (o) - buffer for data read
* @param length - buffer len
* @return amount of readed bytes
*/
size_t read_tty(uint8_t *buff, size_t length){
ssize_t L = 0;
fd_set rfds;
struct timeval tv;
int retval;
FD_ZERO(&rfds);
FD_SET(comfd, &rfds);
tv.tv_sec = 0; tv.tv_usec = 50000; // wait for 50ms
retval = select(comfd + 1, &rfds, NULL, NULL, &tv);
if (!retval) return 0;
if(FD_ISSET(comfd, &rfds)){
if((L = read(comfd, buff, length)) < 1) return 0;
}
return (size_t)L;
}
int write_tty(uint8_t *buff, size_t length){
ssize_t L = write(comfd, buff, length);
if((size_t)L != length){
/// "ïÛÉÂËÁ ÚÁÐÉÓÉ!"
WARN("Write error!");
return 1;
}
return 0;
}
/**
* Safely convert data from string to double
*
* @param num (o) - double number read from string
* @param str (i) - input string
* @return 1 if success, 0 if fails
*/
int str2double(double *num, const char *str){
double res;
char *endptr;
if(!str) return 0;
res = strtod(str, &endptr);
if(endptr == str || *str == '\0' || *endptr != '\0'){
/// "îÅÐÒÁ×ÉÌØÎÙÊ ÆÏÒÍÁÔ ÞÉÓÌÁ double!"
WARNX("Wrong double number format!");
return FALSE;
}
if(num) *num = res; // you may run it like myatod(NULL, str) to test wether str is double number
return TRUE;
}

138
wfs_read/usefull_macros.h Normal file
View File

@ -0,0 +1,138 @@
/*
* usefull_macros.h - a set of usefull macros: memory, color etc
*
* Copyright 2013 Edward V. Emelianoff <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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __USEFULL_MACROS_H__
#define __USEFULL_MACROS_H__
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <err.h>
#include <locale.h>
#if defined GETTEXT_PACKAGE && defined LOCALEDIR
/*
* GETTEXT
*/
#include <libintl.h>
#define _(String) gettext(String)
#define gettext_noop(String) String
#define N_(String) gettext_noop(String)
#else
#define _(String) (String)
#define N_(String) (String)
#endif
#include <stdlib.h>
#include <termios.h>
#include <termio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <stdint.h>
// unused arguments with -Wall -Werror
#define _U_ __attribute__((__unused__))
/*
* Coloured messages output
*/
#define RED "\033[1;31;40m"
#define GREEN "\033[1;32;40m"
#define OLDCOLOR "\033[0;0;0m"
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (1)
#endif
/*
* ERROR/WARNING messages
*/
extern int globErr;
extern void signals(int sig);
#define ERR(...) do{globErr=errno; _WARN(__VA_ARGS__); signals(9);}while(0)
#define ERRX(...) do{globErr=0; _WARN(__VA_ARGS__); signals(9);}while(0)
#define WARN(...) do{globErr=errno; _WARN(__VA_ARGS__);}while(0)
#define WARNX(...) do{globErr=0; _WARN(__VA_ARGS__);}while(0)
/*
* print function name, debug messages
* debug mode, -DEBUG
*/
#ifdef EBUG
#define FNAME() fprintf(stderr, "\n%s (%s, line %d)\n", __func__, __FILE__, __LINE__)
#define DBG(...) do{fprintf(stderr, "%s (%s, line %d): ", __func__, __FILE__, __LINE__); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n");} while(0)
#else
#define FNAME() do{}while(0)
#define DBG(...) do{}while(0)
#endif //EBUG
/*
* Memory allocation
*/
#define ALLOC(type, var, size) type * var = ((type *)my_alloc(size, sizeof(type)))
#define MALLOC(type, size) ((type *)my_alloc(size, sizeof(type)))
#define FREE(ptr) do{if(ptr){free(ptr); ptr = NULL;}}while(0)
#ifndef DBL_EPSILON
#define DBL_EPSILON (2.2204460492503131e-16)
#endif
double dtime();
// functions for color output in tty & no-color in pipes
extern int (*red)(const char *fmt, ...);
extern int (*_WARN)(const char *fmt, ...);
extern int (*green)(const char *fmt, ...);
void * my_alloc(size_t N, size_t S);
void initial_setup();
// mmap file
typedef struct{
char *data;
size_t len;
} mmapbuf;
mmapbuf *My_mmap(char *filename);
void My_munmap(mmapbuf *b);
void restore_console();
void setup_con();
int read_console();
int mygetchar();
void restore_tty();
void tty_init(char *comdev);
size_t read_tty(uint8_t *buff, size_t length);
int write_tty(uint8_t *buff, size_t length);
int str2double(double *num, const char *str);
#endif // __USEFULL_MACROS_H__

132
wfs_read/zernike.c Normal file
View File

@ -0,0 +1,132 @@
/*
* zernike.c
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <strings.h>
#include "zernike.h"
#include "usefull_macros.h"
// coordinate step on a grid
static double coord_step = DEFAULT_CRD_STEP;
// default wavelength for wavefront (650nm) in meters
static double wavelength = DEFAULT_WAVELENGTH;
// default coefficient to transform vawefront from wavelengths into user value
static double wf_coeff = 1.;
/**
* Set default coordinate grid step on an unity circle
* @param step - new step
* @return 0 if all OK, -1 or 1 if `step` bad
*/
int z_set_step(double step){
if(step < DBL_EPSILON) return -1;
if(step > 1.) return 1;
coord_step = step;
return 0;
}
double z_get_step(){
return coord_step;
}
/**
* Set value of default wavelength
* @param w - new wavelength (from 100nm to 10um) in meters, microns or nanometers
* @return 0 if all OK
*/
int z_set_wavelength(double w){
if(w > 1e-7 && w < 1e-5) // meters
wavelength = w;
else if(w > 0.1 && w < 10.) // micron
wavelength = w * 1e-6;
else if(w > 100. && w < 10000.) // nanometer
wavelength = w * 1e-9;
else return 1;
return 0;
}
double z_get_wavelength(){
return wavelength;
}
// thanks to http://stackoverflow.com/a/3875555/1965803
typedef struct{
double wf_coeff;
const char * const *units;
} wf_units;
wf_units wfunits[] = {
{ 1. , (const char * const []){"meter", "m", NULL}},
{1e-3 , (const char * const []){"millimeter", "mm", NULL}},
{1e-6 , (const char * const []){"micrometer", "um", "u", NULL}},
{1e-9 , (const char * const []){"nanometer", "nm", "n", NULL}},
{-1. , (const char * const []){"wavelength", "wave", "lambda", "w", "l", NULL}},
{0. , (const char * const []){NULL}}
};
/**
* Set coefficient `wf_coeff` to user defined unit
*/
int z_set_wfunit(char *U){
wf_units *u = wfunits;
while(u->units[0]){
const char * const*unit = u->units;
while(*unit){
if(strcasecmp(*unit, U) == 0){
wf_coeff = u->wf_coeff;
if(wf_coeff < 0.){ // wavelengths
wf_coeff = 1.; // in wavelengths
}else{ // meters etc
wf_coeff = wavelength / wf_coeff;
}
printf("wf_coeff = %g\n", wf_coeff);
return 0;
}
++unit;
}
++u;
}
return 1;
}
double z_get_wfcoeff(){
return wf_coeff;
}
/**
* Print all wf_units available
*/
void z_print_wfunits(){
wf_units *u = wfunits;
printf(_("Unit (meters)\tAvailable values\n"));
do{
const char * const*unit = u->units;
double val = u->wf_coeff;
if(val > 0.)
printf("%-8g\t", val);
else
printf("(wavelength)\t");
do{
printf("%s ", *unit);
}while(*(++unit));
printf("\n");
}while((++u)->units[0]);
printf("\n");
}

43
wfs_read/zernike.h Normal file
View File

@ -0,0 +1,43 @@
/*
* zernike.h
*
* Copyright 2016 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.com>
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#pragma once
#ifndef __ZERNIKE_H__
#define __ZERNIKE_H__
// default step in coordinate grid
#define DEFAULT_CRD_STEP (0.05)
// default wavefront unit: lambda
#define DEFAULT_WF_UNIT "lambda"
// default wavelength
#define DEFAULT_WAVELENGTH (0.65e-6)
int z_set_step(double step);
double z_get_step();
int z_set_wavelength(double w);
double z_get_wavelength();
int z_set_wfunit(char *u);
double z_get_wfcoeff();
void z_print_wfunits();
#endif // __ZERNIKE_H__