00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <math.h>
00013 #include <complex.h>
00014 #include <omp.h>
00015 #include "interpol_code.h"
00016 #include "HMIparam.h"
00017 #include "fstats.h"
00018 #include "/home/jsoc/cvs/Development/JSOC/proj/libs/astro/astro.h"
00019 #include <fresize.h>
00020 #include "/home/jsoc/cvs/Development/JSOC/proj/lev0/apps/imgdecode.h"
00021 #include "/home/jsoc/cvs/Development/JSOC/proj/lev0/apps/lev0lev1.h"
00022 #include "/home/jsoc/cvs/Development/JSOC/proj/lev0/apps/limb_fit.h"
00023
00024
00025
00026 #undef I //I is the complex number (0,1) in complex.h. We un-define it to avoid confusion with the loop iterative variable i
00027
00028 char *module_name= "undistort_lev1";
00029
00030 #define kRecSetIn "begin" //beginning time for which an output is wanted. MANDATORY PARAMETER.
00031 #define kRecSetIn2 "end" //end time for which an output is wanted. MANDATORY PARAMETER.
00032 #define SeriesIn "in" //series name for the input (i.e. hmi.lev1) filtergrams
00033 #define SeriesOut "out" //series name for the output filtergrams
00034
00035 #define minval(x,y) (((x) < (y)) ? (x) : (y))
00036 #define maxval(x,y) (((x) < (y)) ? (y) : (x))
00037
00038
00039 #define LIGHT_SIDE 2 //SIDE CAMERA
00040 #define LIGHT_FRONT 3 //FRONT CAMERA
00041 #define DARK_SIDE 0 //SIDE CAMERA
00042 #define DARK_FRONT 1 //FRONT CAMERA
00043
00044
00045 ModuleArgs_t module_args[] =
00046 {
00047 {ARG_STRING, kRecSetIn, "" , "beginning time for which an output is wanted"},
00048 {ARG_STRING, kRecSetIn2, "" , "end time for which an output is wanted"},
00049 {ARG_STRING, SeriesIn, "hmi.lev1", "Name of the input (i.e. hmi.lev1) series"},
00050 {ARG_STRING, SeriesOut, "" , "Name of the output series"},
00051 {ARG_STRING, "dpath", "/home/jsoc/cvs/Development/JSOC/proj/lev1.5_hmi/apps/", "directory where the source code is located"},
00052
00053 {ARG_END}
00054 };
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 int MaskCreation(unsigned char *Mask, int nx, int ny, DRMS_Array_t *BadPixels, int HIMGCFID, float *image, DRMS_Array_t *CosmicRays, int nbadperm)
00069 {
00070
00071 int status=2;
00072
00073 if(nx != 4096 || ny != 4096) return status;
00074
00075 char *filename="/home/production/img_cnfg_ids";
00076 const int minid=80;
00077 const int maxid=124;
00078 const int n_tables=1;
00079 const int maxtab=256;
00080
00081 int *badpixellist=BadPixels->data;
00082 int nBadPixels=BadPixels->axis[0];
00083 int *cosmicraylist=NULL;
00084 int ncosmic=0;
00085
00086 if(CosmicRays != NULL)
00087 {
00088 cosmicraylist=CosmicRays->data;
00089 ncosmic=CosmicRays->axis[0];
00090 }
00091 else ncosmic = -1;
00092
00093 int x_orig,y_orig,x_dir,y_dir;
00094 int skip, take, skip_x, skip_y;
00095 int datum, nsx, nss, nlin,nq,nqq,nsy;
00096 int idn=-1;
00097
00098 int *id=NULL, *tab=NULL, *nrows=NULL, *ncols=NULL, *rowstr=NULL, *colstr=NULL, *config=NULL;
00099 id =(int *)(malloc(maxtab*sizeof(int)));
00100 if(id == NULL)
00101 {
00102 printf("Error: memory could not be allocated to id\n");
00103 return 1;
00104
00105 }
00106 tab =(int *)(malloc(maxtab*sizeof(int)));
00107 if(tab == NULL)
00108 {
00109 printf("Error: memory could not be allocated to tab\n");
00110 return 1;
00111
00112 }
00113 nrows =(int *)(malloc(maxtab*sizeof(int)));
00114 if(nrows == NULL)
00115 {
00116 printf("Error: memory could not be allocated to nrows\n");
00117 return 1;
00118
00119 }
00120 ncols =(int *)(malloc(maxtab*sizeof(int)));
00121 if(ncols == NULL)
00122 {
00123 printf("Error: memory could not be allocated to ncols\n");
00124 return 1;
00125
00126 }
00127 rowstr=(int *)(malloc(maxtab*sizeof(int)));
00128 if(rowstr == NULL)
00129 {
00130 printf("Error: memory could not be allocated to rowstr\n");
00131 return 1;
00132
00133 }
00134 colstr=(int *)(malloc(maxtab*sizeof(int)));
00135 if(colstr == NULL)
00136 {
00137 printf("Error: memory could not be allocated to colstr\n");
00138 return 1;
00139
00140 }
00141 config=(int *)(malloc(maxtab*sizeof(int)));
00142 if(config == NULL)
00143 {
00144 printf("Error: memory could not be allocated to config\n");
00145 return 1;
00146
00147 }
00148
00149
00150 int skipt[4*2048];
00151 int taket[4*2048];
00152
00153 int **kx=NULL;
00154 int *kkx=NULL;
00155
00156 kx=(int **)(malloc(9*sizeof(int *)));
00157 if(kx == NULL)
00158 {
00159 printf("Error: memory could not be allocated to kx\n");
00160 return 1;
00161
00162 }
00163
00164
00165 int kx0[11]={4,0,0,1,0,1,1,0,1,1,1};
00166 kx[0]=kx0;
00167 int kx1[7]={2,1,0,1,1,1,2};
00168 kx[2]=kx1;
00169 int kx2[7]={2,0,1,0,0,1,2} ;
00170 kx[4]=kx2;
00171 int kx3[7]={2,0,0,1,0,2,1};
00172 kx[1]=kx3;
00173 int kx4[7]={2,1,1,0,1,2,1};
00174 kx[3]=kx4;
00175 int kx5[7]={1,0,0,2,2};
00176 kx[5]=kx5;
00177 int kx6[5]={1,1,0,2,2};
00178 kx[6]=kx6;
00179 int kx7[5]={1,1,1,2,2};
00180 kx[7]=kx7;
00181 int kx8[5]={1,0,1,2,2};
00182 kx[8]=kx8;
00183
00184 char **filename_table=NULL;
00185 filename_table=(char **)(malloc(n_tables*sizeof(char *)));
00186 if(filename_table == NULL)
00187 {
00188 printf("Error: memory could not be allocated to filename_table\n");
00189 return 1;
00190
00191 }
00192
00193 char *filename_croptable="/home/cvsuser/cvsroot/EGSE/tables/crop/crop6";
00194 filename_table[0]=filename_croptable;
00195
00196 int i,j,k;
00197 int ix, jx;
00198 int n_config;
00199 char string[256];
00200 char strng[5];
00201
00202
00203 FILE *config_file=NULL;
00204 FILE *crop_table=NULL;
00205
00206
00207 config_file=fopen(filename, "r");
00208
00209 if (config_file != NULL){
00210 fgets(string, 256, config_file);
00211 fgets(string, 256, config_file);
00212 fgets(string, 256, config_file);
00213
00214
00215 n_config=0;
00216 fscanf(config_file, "%d", &datum);
00217 id[n_config]=datum;
00218
00219
00220 do {
00221
00222 fscanf(config_file, "%s", string);
00223
00224 config[n_config]=0;
00225 if (string[0] == '4')config[n_config]=0;
00226 if (string[0] == '2') if (string[7] == 'E')config[n_config]=1;
00227 if (string[0] == '2') if (string[7] == 'F')config[n_config]=2;
00228 if (string[0] == '2') if (string[7] == 'G')config[n_config]=3;
00229 if (string[0] == '2') if (string[7] == 'H')config[n_config]=4;
00230 if (string[0] == '1') if (string[7] == 'E')config[n_config]=5;
00231 if (string[0] == '1') if (string[7] == 'F')config[n_config]=6;
00232 if (string[0] == '1') if (string[7] == 'G')config[n_config]=7;
00233 if (string[0] == '1') if (string[7] == 'H')config[n_config]=8;
00234
00235
00236 fscanf(config_file, "%s", string);
00237
00238 fscanf(config_file, "%s", string);
00239
00240 fscanf(config_file, "%s", string);
00241
00242 if (string[0] == 'N') tab[n_config]=-1;
00243 if (string[0] == 'c') tab[n_config]=0;
00244
00245 fscanf(config_file, "%s", string);
00246
00247 fscanf(config_file, "%d", &datum);
00248
00249 fscanf(config_file, "%d", &datum);
00250
00251 fscanf(config_file, "%s", string);
00252
00253 fscanf(config_file, "%d", &datum);
00254 nrows[n_config]=datum;
00255
00256 fscanf(config_file, "%d", &datum);
00257 ncols[n_config]=datum;
00258
00259 fscanf(config_file, "%d", &datum);
00260 rowstr[n_config]=datum;
00261
00262 fscanf(config_file, "%d", &datum);
00263 colstr[n_config]=datum;
00264
00265 fscanf(config_file, "%d", &datum);
00266
00267 fscanf(config_file, "%d", &datum);
00268
00269
00270
00271 ++n_config;
00272 fscanf(config_file, "%d", &datum);
00273 id[n_config]=datum;
00274
00275
00276
00277
00278 }
00279 while (!feof(config_file) && n_config < maxtab);
00280
00281 fclose(config_file);
00282 }
00283
00284
00285
00286
00287 for (i=0; i<n_config; ++i) if (id[i] == HIMGCFID) idn=i;
00288
00289 skip_x=ncols[idn]/2;
00290 skip_y=nrows[idn]/2;
00291
00292
00293
00294 kkx=kx[config[idn]];
00295 nq=kkx[0];
00296 nlin=kkx[2*nq+3-2]*ny/2;
00297 nss=kkx[2*nq+3-1]*nx/2;
00298
00299
00300
00301 if (idn == -1)
00302 {printf("Error: invalid HIMGCFID\n"); status=2;}
00303 else
00304 {
00305 if (tab[idn] != -1)
00306 {
00307 filename_croptable=filename_table[tab[idn]];
00308 crop_table=fopen(filename_croptable, "r");
00309
00310
00311 fscanf(crop_table, "%d", &datum);
00312
00313 fscanf(crop_table, "%d", &datum);
00314 fscanf(crop_table, "%d", &nqq);
00315
00316 for (j=0; j<nqq; ++j)
00317 {
00318 fscanf(crop_table, "%d", &skip);
00319 fscanf(crop_table, "%d", &take);
00320 skipt[j]=skip;
00321 taket[j]=take;
00322 }
00323 fclose(crop_table);
00324 }
00325 else
00326 {
00327 printf("no crop table\n");
00328
00329 for (k=0; k<nq; ++k)
00330 for (j=0; j<nlin; ++j)
00331 {
00332 skipt[k*nss+j]=0;
00333 taket[k*nss+j]=nss;
00334 }
00335 }
00336
00337 nsx=nss-skip_x;
00338 nsy=nlin-skip_y;
00339
00340
00341
00342 for (k=0; k<nq; ++k)
00343 {
00344 x_orig=kkx[k*2+1]*nx - kkx[k*2+1];
00345 y_orig=kkx[k*2+2]*ny - kkx[k*2+2];
00346 x_dir=-(kkx[k*2+1])*2+1;
00347 y_dir=-(kkx[k*2+2])*2+1;
00348
00349
00350 for (j=0; j<skip_y; ++j) for (i=0; i<nss; ++i) Mask[(y_orig+y_dir*j)*nx+x_orig+x_dir*i]=2;
00351 for (j=0; j<nlin; ++j) for (i=0; i<skip_x; ++i) Mask[(y_orig+y_dir*j)*nx+x_orig+x_dir*i]=2;
00352
00353
00354 for (j=0; j<nsy; ++j)
00355 {
00356 jx=j+skip_y;
00357
00358 for (i=0; i<minval(skipt[k*nss+j],nss); ++i){ix=i+skip_x; Mask[(y_orig+y_dir*jx)*nx+x_orig+x_dir*ix]=2;}
00359 for (i=skipt[k*nss+j]; i<minval(skipt[k*nss+j]+taket[k*nss+j],nss); ++i){ix=i+skip_x; Mask[(y_orig+y_dir*jx)*nx+x_orig+x_dir*ix]=0;}
00360 for (i=(skipt[k*nss+j]+taket[k*nss+j]); i<nss; ++i){ix=i+skip_x; Mask[(y_orig+y_dir*jx)*nx+x_orig+x_dir*ix]=2;}
00361 }
00362 }
00363
00364
00365 for(k=0;k<nx*ny;++k)
00366 {
00367 if(Mask[k] == 0)
00368 {
00369 if(isnan(image[k])) Mask[k] = 1;
00370 }
00371 }
00372
00373
00374 if(ncosmic != -1 && nbadperm != -1) nBadPixels = nbadperm;
00375 if(nBadPixels > 0)
00376 {
00377 for (k=0;k<nBadPixels;++k)
00378 {
00379 if(Mask[badpixellist[k]] == 0) Mask[badpixellist[k]] = 1;
00380 }
00381 }
00382
00383
00384
00385 if(ncosmic > 0)
00386 {
00387 for (k=0;k<ncosmic;++k)
00388 {
00389 if(Mask[cosmicraylist[k]] == 0) Mask[cosmicraylist[k]] = 1;
00390 }
00391 }
00392
00393 status=0;
00394
00395 }
00396
00397 free(id);
00398 free(nrows);
00399 free(ncols);
00400 free(rowstr);
00401 free(colstr);
00402 free(config);
00403 free(kx);
00404 free(filename_table);
00405 id=NULL;
00406 nrows=NULL;
00407 ncols=NULL;
00408 rowstr=NULL;
00409 colstr=NULL;
00410 config=NULL;
00411 kx=NULL;
00412 filename_table=NULL;
00413
00414 return status;
00415 }
00416
00417
00418
00419 int heightformation(int FID, double OBSVR, float *CDELT1, float *RSUN, float *CRPIX1, float *CRPIX2, float CROTA2)
00420 {
00421 int wl=0;
00422 int status=0;
00423 float correction=0.0,correction2=0.0;
00424
00425 wl = (FID/10)%20;
00426
00427 if( (wl >= 0) && (wl < 20) )
00428 {
00429 correction = 0.445*exp(-(wl-10.-(float)OBSVR/(0.690/6173.*3.e8/20.)-0.25)*(wl-10.-(float)OBSVR/(0.690/6173.*3.e8/20.)-0.25)/7.1);
00430 correction2 = 0.39*(-2.0*(wl-10.- (float)OBSVR/(0.690/6173.*3.e8/20.)-0.35)/6.15)*exp(-(wl-10.-(float)OBSVR/(0.690/6173.*3.e8/20.)-0.35)*(wl-10.-(float)OBSVR/(0.690/6173.*3.e8/20.)-0.35)/6.15);
00431
00432 *CDELT1 = *CDELT1*(*RSUN)/((*RSUN)-correction);
00433 *RSUN = *RSUN-correction;
00434 *CRPIX1 = *CRPIX1-cos(M_PI-CROTA2*M_PI/180.)*correction2;
00435 *CRPIX2 = *CRPIX2-sin(M_PI-CROTA2*M_PI/180.)*correction2;
00436 }
00437 else status=1;
00438
00439 return status;
00440
00441 }
00442
00443 int DoIt(void)
00444 {
00445
00446 #define MaxNString 512 //maximum length of strings in character number
00447
00448
00449
00450
00451 char *inRecQuery = cmdparams_get_str(&cmdparams, kRecSetIn, NULL);
00452 char *inRecQuery2 = cmdparams_get_str(&cmdparams, kRecSetIn2, NULL);
00453 char *inLev1Series = cmdparams_get_str(&cmdparams,SeriesIn, NULL);
00454 char *outLev1Series = cmdparams_get_str(&cmdparams,SeriesOut, NULL);
00455 char *dpath = cmdparams_get_str(&cmdparams,"dpath", NULL);
00456
00457
00458 char *DISTCOEFFILEF=NULL;
00459 char *DISTCOEFFILES=NULL;
00460 char *ROTCOEFFILE=NULL;
00461 char *DISTCOEFPATH=NULL;
00462 char *ROTCOEFPATH =NULL;
00463 struct init_files initfiles;
00464 double minimum,maximum,median,mean,sigma,skewness,kurtosis;
00465
00466 char dpath2[MaxNString];
00467 strcpy(dpath2,dpath);
00468 DISTCOEFFILEF=strdup(strcat(dpath2,"/../libs/lev15/distmodel_front_o6_100624.txt"));
00469 strcpy(dpath2,dpath);
00470 DISTCOEFFILES=strdup(strcat(dpath2,"/../libs/lev15/distmodel_side_o6_100624.txt"));
00471 strcpy(dpath2,dpath);
00472 ROTCOEFFILE =strdup(strcat(dpath2,"/../libs/lev15/rotcoef_file.txt"));
00473 strcpy(dpath2,dpath);
00474 DISTCOEFPATH =strdup(strcat(dpath2,"/../libs/lev15/"));
00475 strcpy(dpath2,dpath);
00476 ROTCOEFPATH =strdup(strcat(dpath2,"/../libs/lev15/"));
00477
00478 initfiles.dist_file_front=DISTCOEFFILEF;
00479 initfiles.dist_file_side =DISTCOEFFILES;
00480 initfiles.diffrot_coef =ROTCOEFFILE;
00481
00482 char HMISeriesLev1[MaxNString];
00483 char CosmicRaySeries[MaxNString]= "hmi.cosmic_rays";
00484 char HMISeriesTemp[MaxNString];
00485 char FSNtemps[]="00000000000000";
00486 char *COUNTS = "COUNT";
00487 char *FSNS = "FSN";
00488 char *HCAMIDS = "HCAMID";
00489 char *RSUNS = "R_SUN";
00490 char *CRPIX1S = "CRPIX1";
00491 char *CRPIX2S = "CRPIX2";
00492 char *CROTA2S = "CROTA2";
00493 char *CRLTOBSS = "CRLT_OBS";
00494 char *DSUNOBSS = "DSUN_OBS";
00495 char *HCFTIDS = "HCFTID";
00496 char *TOBSS = "T_OBS";
00497 char *HIMGCFIDS = "HIMGCFID";
00498 char *NBADPERMS = "NBADPERM";
00499 char *ierror = NULL;
00500 char DATEOBS[MaxNString];
00501
00502 DRMS_RecordSet_t *recLev1 = NULL;
00503 DRMS_RecordSet_t *rectemp = NULL;
00504 DRMS_RecordSet_t *recLev1Out= NULL;
00505
00506 int i,nRecs1,status,FSN,HCAMID,Nelem=0,CFINDEX=0,kk;
00507 int COSMICCOUNT=0;
00508 int axisout[2];
00509 axisout[0]=4096;
00510 axisout[1]=axisout[0];
00511 int axisin[2];
00512 axisin[0]=4096;
00513 axisin[1]=axisin[0];
00514 int nthreads=0;
00515 int HIMGCFID=0;
00516 int NBADPERM=0;
00517 int ngood=0;
00518 int missvals=0;
00519 int FID=0;
00520
00521 DRMS_Array_t *Segment = NULL;
00522 DRMS_Array_t *Ierror = NULL;
00523 DRMS_Array_t *arrLev1Out = NULL;
00524 DRMS_Array_t *BadPixels = NULL;
00525 DRMS_Array_t *CosmicRays = NULL;
00526
00527 DRMS_Type_t type1d = DRMS_TYPE_FLOAT;
00528 DRMS_Type_t typeEr = DRMS_TYPE_CHAR;
00529 DRMS_Segment_t *segin = NULL;
00530 DRMS_Segment_t *segout = NULL;
00531 float *image =NULL;
00532 float RSUN=0.0,X0=0.0,Y0=0.0,CRLTOBS=0.0,CROTA2=0.0;
00533 float cdelt1;
00534 double solar_radius = 696000000.0;
00535 double DSUNOBS=0.0;
00536 double OBSVR=0.0;
00537 unsigned char *Mask =NULL;
00538 struct initial const_param;
00539 struct keyword *KeyInterp=NULL;
00540 struct keyword KeyInterpOut;
00541 TIME internTOBS=0.0;
00542 TIME TimeBegin,TimeEnd;
00543 char timeBegin[MaxNString] ="2000.12.25_00:00:00";
00544 char timeEnd[MaxNString] ="2000.12.25_00:00:00";
00545 double RSUN_LF=0.0, X0_LF=0.0, Y0_LF=0.0;
00546 float RSUN_LF2=0.0, X0_LF2=0.0, Y0_LF2=0.0;
00547
00548
00549
00550
00551 nthreads=omp_get_max_threads();
00552 printf("NUMBER OF THREADS USED BY OPEN MP= %d\n",nthreads);
00553
00554
00555
00556 printf("BEGINNING TIME: %s\n",inRecQuery);
00557 printf("ENDING TIME: %s\n",inRecQuery2);
00558
00559 TimeBegin=sscan_time(inRecQuery);
00560 TimeEnd =sscan_time(inRecQuery2);
00561
00562 if(TimeBegin > TimeEnd)
00563 {
00564 printf("Error: the ending time must be later than the beginning time!\n");
00565 return 1;
00566 }
00567
00568
00569
00570
00571 strcpy(dpath2,dpath);
00572 strcat(dpath2,"/../../../");
00573 status = initialize_interpol(&const_param,&initfiles,4096,4096,dpath2);
00574 if(status != 0)
00575 {
00576 printf("Error: could not initialize the gapfilling and /or undistortion routines\n");
00577 return 1;
00578 }
00579
00580
00581 Nelem = 4096*4096;
00582
00583 KeyInterp = (struct keyword *)malloc(sizeof(struct keyword));
00584 if(KeyInterp == NULL)
00585 {
00586 printf("Error: memory could not be allocated to KeyInterp\n");
00587 return 1;
00588 }
00589
00590
00591
00592 strcpy(HMISeriesLev1,inLev1Series);
00593
00594
00595
00596
00597 sprint_time(timeBegin,TimeBegin,"TAI",0);
00598 sprint_time(timeEnd,TimeEnd,"TAI",0);
00599 strcat(HMISeriesLev1,"[");
00600 strcat(HMISeriesLev1,timeBegin);
00601 strcat(HMISeriesLev1,"-");
00602 strcat(HMISeriesLev1,timeEnd);
00603 strcat(HMISeriesLev1,"]");
00604 printf("LEVEL 1 SERIES QUERY = %s\n",HMISeriesLev1);
00605 recLev1 = drms_open_records(drms_env,HMISeriesLev1,&status);
00606 if (status != DRMS_SUCCESS || recLev1 == NULL || recLev1->n == 0){printf("Could not open input series\n"); return 1;}
00607 nRecs1 = recLev1->n;
00608
00609 Ierror = drms_array_create(typeEr,2,axisout,NULL,&status);
00610 if(status != DRMS_SUCCESS){printf("Could not create Ierror array\n"); return 1;}
00611
00612
00613
00614 printf("START LOOP OVER ALL lev1 FILTERGRAMS\n");
00615 for(i=0;i<nRecs1;i++)
00616 {
00617
00618 printf("filtergram number %d out of %d \n",i+1,nRecs1);
00619 recLev1Out = drms_create_records(drms_env,1,outLev1Series,DRMS_PERMANENT,&status);
00620 if(status != DRMS_SUCCESS){printf("Could not open output series\n"); return 1;}
00621
00622 FSN = drms_getkey_int(recLev1->records[i],FSNS,&status);
00623 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00624 HCAMID = drms_getkey_int(recLev1->records[i],HCAMIDS,&status);
00625 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00626 FID = drms_getkey_int(recLev1->records[i],"FID",&status);
00627 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00628 OBSVR = drms_getkey_double(recLev1->records[i],"OBS_VR",&status);
00629 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00630 RSUN = (float)drms_getkey_double(recLev1->records[i],RSUNS,&status);
00631 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00632 X0 = (float)drms_getkey_double(recLev1->records[i],CRPIX1S,&status);
00633 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00634 X0 = X0-1.0;
00635 Y0 = (float)drms_getkey_double(recLev1->records[i],CRPIX2S,&status);
00636 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00637 Y0 = Y0-1.0;
00638 CRLTOBS= (float)drms_getkey_double(recLev1->records[i],CRLTOBSS,&status);
00639 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00640 DSUNOBS= drms_getkey_double(recLev1->records[i],DSUNOBSS,&status);
00641 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00642 CFINDEX= drms_getkey_int(recLev1->records[i],HCFTIDS,&status);
00643 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00644 internTOBS= drms_getkey_time(recLev1->records[i],TOBSS,&status);
00645 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00646 HIMGCFID= drms_getkey_int(recLev1->records[i],HIMGCFIDS,&status);
00647 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00648 NBADPERM= drms_getkey_int(recLev1->records[i],NBADPERMS,&status);
00649 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00650 CROTA2 = (float)drms_getkey_double(recLev1->records[i],CROTA2S,&status);
00651 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00652
00653 CROTA2=-CROTA2;
00654 if(status != DRMS_SUCCESS){printf("Could not read a keyword of input record\n"); return 1;}
00655
00656 if(isnan(X0) || isnan(Y0) || isnan(RSUN) || isnan(CRLTOBS) || isnan(DSUNOBS) || isnan(CROTA2) || isnan(OBSVR) )
00657 {
00658 printf("Error: lev1 record FSN=%d has missing or bad keywords\n",FSN);
00659 drms_copykeys(recLev1Out->records[0],recLev1->records[i],0,kDRMS_KeyClass_Explicit);
00660
00661 status=drms_close_records(recLev1Out,DRMS_INSERT_RECORD);
00662 recLev1Out=NULL;
00663 }
00664 else
00665 {
00666 printf("segment needs to be read for FSN %d %d\n",FSN,HCAMID);
00667 segin = drms_segment_lookupnum(recLev1->records[i],0);
00668 Segment = drms_segment_read(segin,type1d, &status);
00669 if (status != DRMS_SUCCESS || Segment == NULL)
00670 {
00671 printf("Error: could not read the segment of level 1 record\n");
00672 return 1;
00673 }
00674
00675
00676 printf("read bad pixel list\n");
00677 segin = drms_segment_lookup(recLev1->records[i],"bad_pixel_list");
00678 BadPixels = drms_segment_read(segin,segin->info->type,&status);
00679 if(status != DRMS_SUCCESS || BadPixels == NULL)
00680 {
00681 printf("Error: cannot read the list of bad pixels of level 1 filtergram\n");
00682 return 1;
00683 }
00684
00685
00686 strcpy(HMISeriesTemp,CosmicRaySeries);
00687 strcat(HMISeriesTemp,"[][");
00688 sprintf(FSNtemps,"%d",FSN);
00689 strcat(HMISeriesTemp,FSNtemps);
00690 strcat(HMISeriesTemp,"]");
00691 rectemp=NULL;
00692 printf("query for cosmic ray hits: %s\n",HMISeriesTemp);
00693
00694 rectemp=drms_open_records(drms_env,HMISeriesTemp,&status);
00695 if(status != DRMS_SUCCESS || rectemp->n == 0)
00696 {
00697 printf("Warning: no cosmic-ray hits record for for FSN %d\n",FSN);
00698 CosmicRays = NULL;
00699 rectemp = NULL;
00700 }
00701 else
00702 {
00703 segin = drms_segment_lookupnum(rectemp->records[0],0);
00704
00705 COSMICCOUNT= drms_getkey_int(rectemp->records[0],COUNTS,&status);
00706 printf("COSMICCOUNT= %d\n",COSMICCOUNT);
00707
00708 CosmicRays = drms_segment_read(segin,segin->info->type,&status);
00709 if(status != DRMS_SUCCESS || CosmicRays == NULL)
00710 {
00711 printf("Error: the list of cosmic-ray hits could not be read for FSN %d\n",FSN);
00712 return 1;
00713 }
00714 }
00715
00716 image = Segment->data;
00717
00718 printf("creating mask\n");
00719 Mask = (unsigned char *)malloc(Nelem*sizeof(unsigned char));
00720 if(Mask == NULL)
00721 {
00722 printf("Error: cannot allocate memory for Mask\n");
00723 return 1;
00724 }
00725 status = MaskCreation(Mask,axisin[0],axisin[1],BadPixels,HIMGCFID,image,CosmicRays,NBADPERM);
00726 if(status != 0)
00727 {
00728 printf("Error: unable to create a mask for the gap filling function\n");
00729 return 1;
00730 }
00731
00732 ierror = Ierror->data;
00733
00734 printf("STARTING GAPFILL\n");
00735 status =do_gapfill(image,Mask,&const_param,ierror,axisin[0],axisin[1]);
00736 if(status != 0)
00737 {
00738 printf("Error: gapfilling code did not work on the level 1 filtergram FSN = %d\n",FSN);
00739 return 1;
00740 }
00741
00742
00743
00744 if(HCAMID == LIGHT_FRONT) KeyInterp[0].camera=0;
00745 else KeyInterp[0].camera=1;
00746 KeyInterp[0].rsun=RSUN;
00747 printf("actual solar radius of image in = %f\n",RSUN);
00748 KeyInterp[0].xx0=X0;
00749 KeyInterp[0].yy0=Y0;
00750 KeyInterp[0].dist=(float)(DSUNOBS/(double)AstroUnit);
00751 KeyInterp[0].b0=CRLTOBS/180.*M_PI;
00752 KeyInterp[0].p0=CROTA2/180.*M_PI;
00753 KeyInterp[0].time=internTOBS;
00754 KeyInterp[0].focus=CFINDEX;
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776 KeyInterpOut.rsun=KeyInterp[0].rsun;
00777 KeyInterpOut.xx0 =KeyInterp[0].xx0;
00778 KeyInterpOut.yy0 =KeyInterp[0].yy0;
00779 KeyInterpOut.dist =KeyInterp[0].dist;
00780 KeyInterpOut.b0 =KeyInterp[0].b0;
00781 KeyInterpOut.p0 =KeyInterp[0].p0;
00782
00783 KeyInterpOut.time =KeyInterp[0].time;
00784 KeyInterpOut.focus=KeyInterp[0].focus;
00785
00786 arrLev1Out = drms_array_create(type1d,2,axisout,NULL,&status);
00787 if(status != DRMS_SUCCESS || arrLev1Out == NULL)
00788 {
00789 printf("Error: cannot create a DRMS array for an output filtergram\n");
00790 return 1;
00791 }
00792
00793 printf("STARTING UNDISTORTION\n");
00794 printf("input parameters: %f %f %f %f %f %f %d\n",KeyInterp[0].rsun,KeyInterp[0].xx0,KeyInterp[0].yy0,KeyInterp[0].dist,KeyInterp[0].b0,KeyInterp[0].p0,KeyInterp[0].focus);
00795 status=do_interpolate(&image,&ierror,arrLev1Out->data,KeyInterp,&KeyInterpOut,&const_param,1,axisin[0],axisin[1],-1.0,dpath2);
00796 if(status != 0){printf("Error: un-distortion failed\n"); return 1;}
00797
00798
00799 printf("RUNNING STATISTICS FUNCTION\n");
00800 image=(float *)arrLev1Out->data;
00801 status=fstats(axisout[0]*axisout[1],image,&minimum,&maximum,&median,&mean,&sigma,&skewness,&kurtosis,&ngood);
00802 if(status != 0)
00803 {
00804 printf("Error: the statistics function did not run properly \n");
00805 return 1;
00806 }
00807 missvals=0;
00808 segout = drms_segment_lookupnum(recLev1Out->records[0],0);
00809 arrLev1Out->bzero=segout->bzero;
00810 arrLev1Out->bscale=segout->bscale;
00811 arrLev1Out->israw=0;
00812 for(kk=0;kk<axisout[0]*axisout[1];kk++) if (image[kk] > (2147483647.*arrLev1Out->bscale+arrLev1Out->bzero) || image[kk] < (-2147483648.*arrLev1Out->bscale+arrLev1Out->bzero))
00813 {
00814 missvals+=1;
00815 ngood -= 1;
00816 }
00817
00818
00819 printf("RUNNING LIMB FINDER\n");
00820 float *replaceNaN;
00821 replaceNaN=arrLev1Out->data;
00822
00823 status = limb_fit(recLev1->records[i],replaceNaN,&RSUN_LF,&X0_LF,&Y0_LF,4096,4096,0);
00824 RSUN_LF2=(float)RSUN_LF;
00825 X0_LF2=(float)X0_LF;
00826 Y0_LF2=(float)Y0_LF;
00827 cdelt1=1.0/RSUN_LF*asin(solar_radius/((double)KeyInterpOut.dist*(double)AstroUnit))*180.*60.*60./M_PI;
00828
00829 status = heightformation(FID,OBSVR,&cdelt1,&RSUN_LF2,&X0_LF2,&Y0_LF2,CROTA2);
00830 if(status != 0){printf("Error: formation height correction failed\n"); return 1;}
00831
00832
00833 printf("WRITING KEYWORDS \n");
00834 status = drms_copykeys(recLev1Out->records[0],recLev1->records[i],0,kDRMS_KeyClass_Explicit);
00835 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00836
00837 status = drms_setkey_float(recLev1Out->records[0],"CRLT_OBS",KeyInterpOut.b0*180./M_PI);
00838 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00839 status = drms_setkey_float(recLev1Out->records[0],"CROTA2",-KeyInterpOut.p0*180./M_PI);
00840 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00841 status = drms_setkey_float(recLev1Out->records[0],"CDELT1",cdelt1);
00842 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00843 status = drms_setkey_float(recLev1Out->records[0],"CDELT2",cdelt1);
00844 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00845 status = drms_setkey_float(recLev1Out->records[0],"CRPIX1",X0_LF2+1.);
00846 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00847 status = drms_setkey_float(recLev1Out->records[0],"CRPIX2",Y0_LF2+1.);
00848 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00849 status = drms_setkey_double(recLev1Out->records[0],"DSUN_OBS",(double)KeyInterpOut.dist*(double)AstroUnit);
00850 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00851 status = drms_setkey_float(recLev1Out->records[0],"R_SUN",RSUN_LF2);
00852 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00853 status = drms_setkey_float(recLev1Out->records[0],"X0_LF",X0_LF);
00854 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00855 status = drms_setkey_float(recLev1Out->records[0],"Y0_LF",Y0_LF);
00856 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00857 status = drms_setkey_float(recLev1Out->records[0],"RSUN_LF",RSUN_LF);
00858 sprint_time(DATEOBS,CURRENT_SYSTEM_TIME,"UTC",1);
00859 status = drms_setkey_string(recLev1Out->records[0],"DATE",DATEOBS);
00860 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00861 status= drms_setkey_float(recLev1Out->records[0],"DATAMIN" ,(float)minimum);
00862 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00863 status= drms_setkey_float(recLev1Out->records[0],"DATAMAX" ,(float)maximum);
00864 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00865 status= drms_setkey_float(recLev1Out->records[0],"DATAMEDN",(float)median);
00866 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00867 status= drms_setkey_float(recLev1Out->records[0],"DATAMEAN",(float)mean);
00868 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00869 status= drms_setkey_float(recLev1Out->records[0],"DATARMS" ,(float)sigma);
00870 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00871 status= drms_setkey_float(recLev1Out->records[0],"DATASKEW",(float)skewness);
00872 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00873 status= drms_setkey_float(recLev1Out->records[0],"DATAKURT",(float)kurtosis);
00874 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00875 status= drms_setkey_int(recLev1Out->records[0],"TOTVALS" ,ngood+missvals);
00876 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00877 status= drms_setkey_int(recLev1Out->records[0],"DATAVALS",ngood);
00878 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00879 status= drms_setkey_int(recLev1Out->records[0],"MISSVALS",missvals);
00880 if(status != DRMS_SUCCESS){printf("Error: could not write a keyword\n"); return 1;}
00881
00882
00883 printf("WRITING DATA SEGMENT\n");
00884 status=drms_segment_write(segout,arrLev1Out,0);
00885 if(status != DRMS_SUCCESS){printf("Error: a call to drms_segment_write failed\n"); return 1;}
00886
00887 segout = drms_segment_lookupnum(recLev1Out->records[0],1);
00888 status = drms_segment_write(segout,BadPixels,0);
00889 if(status != DRMS_SUCCESS){printf("Error: a call to drms_segment_write failed\n"); return 1;}
00890
00891
00892
00893 printf("FREEING ARRAYS\n");
00894 status=drms_close_records(recLev1Out,DRMS_INSERT_RECORD);
00895 recLev1Out=NULL;
00896 drms_free_array(arrLev1Out);
00897 arrLev1Out=NULL;
00898 drms_free_array(Segment);
00899 Segment=NULL;
00900 if(BadPixels != NULL) drms_free_array(BadPixels);
00901 BadPixels=NULL;
00902 if(CosmicRays != NULL) drms_free_array(CosmicRays);
00903 CosmicRays=NULL;
00904 if(rectemp != NULL) drms_close_records(rectemp,DRMS_FREE_RECORD);
00905 rectemp=NULL;
00906 if(Mask != NULL) free(Mask);
00907 Mask=NULL;
00908 }
00909 }
00910
00911 free(KeyInterp);
00912 KeyInterp=NULL;
00913 status=drms_close_records(recLev1,DRMS_FREE_RECORD);
00914 drms_free_array(Ierror);
00915 Ierror=NULL;
00916
00917 return status;
00918
00919 }