00001
00002
00003
00004
00005
00006 #include <string.h>
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <math.h>
00010 #include <unistd.h>
00011 #include <gsl/gsl_math.h>
00012 #include <gsl/gsl_deriv.h>
00013 #include <gsl/gsl_rng.h>
00014 #include <gsl/gsl_randist.h>
00015 #include <gsl/gsl_vector.h>
00016 #include <gsl/gsl_blas.h>
00017 #include <gsl/gsl_multifit_nlin.h>
00018 #include <gsl/gsl_errno.h>
00019 #include <gsl/gsl_min.h>
00020 #include <time.h>
00021
00022 #include "nrutil.h"
00023
00024 #include <jsoc_main.h>
00025
00026 #include "expmax.h"
00027 #include "expfit.h"
00028
00029 #define CODE_VERSION "V6r1"
00030 #define CODE_DATE "Fri Mar 6 22:30:42 PST 2015"
00031 #define LOGMSG1 "LIMBFITS"
00032 #if defined(AB512) && AB512
00033 #define CODE_NAME "limbfit_tas2(ab512)"
00034 #define JSD_NAME "scholl_limbfit_tas_ab512.jsd"
00035 #elif defined(AB1024) && AB1024
00036 #define CODE_NAME "limbfit_tas2(ab1024)"
00037 #define JSD_NAME "scholl_limbfit_tas_ab1024.jsd"
00038 #else
00039 #define CODE_NAME "limbfit_tas"
00040 #define JSD_NAME "scholl_limbfit_tas.jsd"
00041 #endif
00042
00043
00044
00045
00046
00047
00048 #define NUMRECPERTRANS 960 // must be equal to Unitsize in the JSD
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #define ERR_EXIT 1
00061 #define ERR_USAGE -2
00062 #define ERR_MALLOC_FAILED -11
00063 #define ERR_SPECIAL -100
00064 #define ERR_DRMS_WRITE -200
00065 #define ERR_DRMS_READ -201
00066 #define ERR_DRMS_ARRAY_CREATE -202
00067 #define ERR_DRMS_SEGMENT_WRITE -203
00068 #define ERR_DRMS_SEGMENT_LOOKUPNUM -204
00069 #define WAR_DRMS_NORECORD 201
00070 #define WAR_DRMS_FREE_ARRAY 202
00071 #define DEBUG_MSG 999
00072 #define VOID 0
00073
00074
00075 #define ERR_DRMS_WRITE_KW -300
00076 #define ERR_DRMS_READ_MISSING_DATA -301
00077 #define ERR_DRMS_READ_MISSING_KW -302
00078 #define ERR_DRMS_READ_MISSING_XYR_LF -303
00079 #define ERR_NR_STACK_TOO_SMALL -352
00080
00081
00082 #define ERR_LIMBFIT_FAILED -501
00083 #define ERR_LIMBFIT_FIT_FAILED -502 // error on exit from Marcelo's fitting routines
00084 #define ERR_LIMBFIT_FLDF_FAILED -503
00085 #define ERR_DISK_OUTSIDE_IMAGE -511
00086 #define ERR_SIZE_ANN_TOO_BIG -512
00087 #define ERR_GSL_FINMIN_SET_FAILED -541
00088 #define ERR_GSL_FINMIN_NOMIN_FAILED -542
00089 #define ERR_GSL_FINMIN_PRO_FAILED -543
00090 #define ERR_GSL_GAUSSFIT_SET_FAILED -551
00091 #define ERR_GSL_GAUSSFIT_FDFSOLVER_FAILED -552
00092
00093
00094 #define PROCSTAT_OK "OK"
00095 #define PROCSTAT_NOK "NOK"
00096 #define PROCSTAT_NO_LF_FAILED "LF_FAILED"
00097 #define PROCSTAT_NO_LF_MISSVALS "NO_LF_MISSVALS"
00098 #define PROCSTAT_NO_LF_DARKIMG "NO_LF_DARKIMG"
00099 #define PROCSTAT_NO_LF_OPENLOOP "NO_LF_OPENLOOP"
00100 #define PROCSTAT_NO_LF_DB_READ_PB "NO_LF_DB_READ_PB"
00101 #define PROCSTAT_NO_LF_XYR_LF_MISSING "NO_LF_XYR_LF_MISSING"
00102 #define PROCSTAT_NO_LF_DB_WRITE_PB "NO_LF_DB_WRITE_PB"
00103
00104
00105 #define ANNULUS_WIDTH 200 //
00106 #define MAX_SIZE_ANN_VARS 8000000 // ! must be the same value than JPT in fortran code !
00107 #define NUM_LDF 180 // n/jang=NUM_LDF+1
00108 #define NUM_RADIAL_BINS 64 // n/jprf
00109 #if defined(AB512) && AB512
00110 #define NUM_AB_BINS 512 // n/jreg
00111 #elif defined(AB1024) && AB1024
00112 #define NUM_AB_BINS 1024 // n/jreg
00113 #else
00114 #define NUM_AB_BINS 256 // n/jreg
00115 #endif
00116 #define LO_LIMIT 32.0 // ! the sum of these 2 must be equal to ANNULUS_WIDTH
00117 #define UP_LIMIT 32.0 //
00118 #define INC_X -4.0 //
00119 #define INC_Y -4.0 //
00120 #define NUM_FITPNTS 9 // 2*NUM_FITPNTS<NUM_RADIAL_BINS
00121 #define GUESS_RANGE 8 //
00122 #define NB_ITER 2 //
00123 #define BAD_PIXEL_VALUE -2147483648.0
00124
00125
00126 #define AHI 70000.0 //
00127
00128
00129 #define AHI2 30000.0
00130 #define LO_LIMIT2 24.0
00131 #define UP_LIMIT2 24.0
00132 #define NB_ITER2 1
00133
00134
00135
00136 typedef struct {
00137 float *data;
00138 int img_sz0;
00139 int img_sz1;
00140 int cc;
00141 int spe;
00142 int iter;
00143 int fldf;
00144 double ix;
00145 double iy;
00146 double ir;
00147 int src;
00148
00149 unsigned int fsn;
00150 } LIMBFIT_INPUT;
00151
00152 typedef struct {
00153 float *anls;
00154 long anls_nbpix;
00155 float *pf_anls;
00156 float *pl_anls;
00157 int is_firstobs;
00158 } LIMBFIT_IO_PUT;
00159
00160 typedef struct {
00161
00162
00163 int numext;
00164 float cenx;
00165 float ceny;
00166 double radius;
00167 double cmean;
00168 float max_limb;
00169 int quality;
00170 int error1;
00171 int error2;
00172
00173
00174 float* fits_ldfs_data;
00175 float* fits_fulldfs;
00176 float* fits_alpha_beta;
00177 float fits_as[6];
00178 float fits_es[6];
00179
00180
00181 int fits_ldfs_naxis1;
00182 int fits_ldfs_naxis2;
00183 int fits_fldfs_nrows;
00184 int fits_fldfs_tfields;
00185 int fits_ab_naxis1;
00186 int fits_ab_naxis2;
00187
00188
00189 int ann_wd;
00190 long mxszannv;
00191 int nb_ldf;
00192 int nb_rdb;
00193 int nb_abb;
00194 double up_limit;
00195 double lo_limit;
00196 double inc_x;
00197 double inc_y;
00198 int nfitpnts;
00199 int nb_iter;
00200 int cc;
00201 double ahi;
00202 int nb_fbins;
00203 int fldfr;
00204
00205
00206 char* dsin;
00207 char* comment;
00208 char* code_date;
00209 char* code_version;
00210 char* code_name;
00211 char* bld_vers;
00212 char* series_name;
00213
00214
00215 FILE *opf;
00216 char* tmp_dir;
00217 char* dsout;
00218 int debug;
00219
00220 } LIMBFIT_OUTPUT;
00221
00222
00223 void close_on_error(DRMS_Record_t *record_in,DRMS_Record_t *record_out, DRMS_Array_t *data_array);
00224 int do_one_limbfit(DRMS_Record_t *record_in,DRMS_Record_t *record_out,
00225 LIMBFIT_INPUT *input, LIMBFIT_OUTPUT *results, LIMBFIT_IO_PUT *ios, int *status);
00226 double fin_min(double A[], double m, int range, int debug, FILE *fd);
00227 int gaussfit(double y[], double t[],double sigma[], double A[], double erro[], long N, int degf, int debug, FILE *opf);
00228 void get_sdate(char *sdate);
00229 int indexx(unsigned long n, float *arr, unsigned long *indx);
00230 void lf_logmsg(char * type1, char * type2, int return_code, int status, char *message, char *code_name, FILE *opf);
00231 int limbfit(LIMBFIT_INPUT *input, LIMBFIT_OUTPUT *results, LIMBFIT_IO_PUT *ios);
00232 float median(float * tmed, int siz);
00233 int mk_fldfs(float cmx, float cmy, double radius, int naxis_row, int naxis_col,
00234 long npixels, float *data, float **save_full_ldf, int *bins1, int *bins2, FILE *opf, int debug);
00235 int process_n_records_fsn(char * open_dsname, LIMBFIT_INPUT *lfv, LIMBFIT_OUTPUT *lfr, LIMBFIT_IO_PUT *lfw, int *status);
00236 int process_all_records_smpl(char * open_dsname, LIMBFIT_INPUT *lfv, LIMBFIT_OUTPUT *lfr, LIMBFIT_IO_PUT *lfw, int *status);
00237 int write_mini_output(char * errcode, DRMS_Record_t *record_in,DRMS_Record_t *record_out,int tbf, LIMBFIT_OUTPUT *lfr);
00238 int write_lf_keywords(char * errcode, DRMS_Record_t *record_out, LIMBFIT_OUTPUT *results, int pass, int src);
00239 void sav_b0(float *pf_sb0, float *pl_sb0, float *pf_b0);
00240 void sum_b0(float *beta, float *pf_b0, float *pl_b0);
00241 int sort(unsigned long n, float *arr);
00242
00243
00244 void limb_(float *anls, int *jk, float *cmx, float *cmy, float *r, int *nitr, int *ncut,
00245 float* rprf, float* lprf, float *rsi, float *rso, float *dx, float *dy,
00246 float* alph, float* beta, int *ifail, float* b0, int *centyp, float *lahi);