00001
00002
00003
00004
00005
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 #include <stdio.h>
00134 #include <stdlib.h>
00135 #include <string.h>
00136 #include <math.h>
00137 #include <complex.h>
00138 #include <omp.h>
00139 #include "interpol_code.h"
00140 #include "polcal.h"
00141 #include "HMIparam.h"
00142 #include "fstats.h"
00143
00144 #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
00145
00146 char *module_name = "HMI_IQUV_averaging_modL";
00147
00148 #define kRecSetIn "begin" //beginning time for which an output is wanted. MANDATORY PARAMETER.
00149 #define kRecSetIn2 "end" //end time for which an output is wanted. MANDATORY PARAMETER.
00150
00151 #define WaveLengthIn "wavelength" //filtergram name Ii starting the framelist (i ranges from 0 to 5). MANDATORY PARAMETER.
00152 #define CamIDIn "camid" //front camera (camid=1) or side camera (camid=0)?
00153
00154
00155
00156 #define DataCadenceIn "cadence" //cadence (45, 90, 120, 135, or 150 seconds)
00157 #define NpolIn "npol" //number of polarizations in observable framelist
00158 #define FramelistSizeIn "size" //size of the framelist
00159 #define SeriesIn "lev1" //series name for the lev1 filtergrams
00160 #define QuickLookIn "quicklook" //quicklook data (yes=1,no=0)? 0 BY DEFAULT
00161 #define Average "average" //average over 12 or 96 minutes? (12 by default)
00162 #define RotationalFlat "rotational" //force the use of rotational flat fields?
00163 #define Linearity "linearity" //force the correction for non-linearity of cameras
00164
00165 #define minval(x,y) (((x) < (y)) ? (x) : (y))
00166 #define maxval(x,y) (((x) < (y)) ? (y) : (x))
00167
00168
00169 #define LIGHT_SIDE 2 //SIDE CAMERA
00170 #define LIGHT_FRONT 3 //FRONT CAMERA
00171 #define DARK_SIDE 0 //SIDE CAMERA
00172 #define DARK_FRONT 1 //FRONT CAMERA
00173
00174 #define Q_ACS_ECLP 0x2000 //eclipse keyword for the lev1 data
00175 #define Q_ACS_ISSLOOP 0x20000 //ISS loop OPEN for lev1
00176 #define Q_ACS_NOLIMB 0x10 //limbfinder error for lev1
00177 #define Q_MISSING_SEGMENT 0x80000000 //missing image segment for lev1 record
00178 #define Q_ACS_LUNARTRANSIT 0x800000 //lunar transit
00179 #define Q_ACS_THERMALRECOVERY 0x400000//therma recovery after eclipses
00180 #define Q_CAMERA_ANOMALY 0x00000080 //camera issue with HMI (e.g. DATAMIN=0): resulting images might be usable, but most likely bad
00181
00182 #define CALVER_DEFAULT 0 // both the default and missing value of CALVER64
00183
00184 #define CALVER_LINEARITY 0x2000 //bitmask for CALVER64 to indicate the use of non-linearity correction //VALUE USED AFTER JANUARY 15, 2014
00185 #define CALVER_ROTATIONAL 0x10000 //bitmask for CALVER64 to indicate the use of rotational flat fields
00186 #define CALVER_MODL 0x20000 //bitmask for CALVER64 to indicate the use of a mod L observables sequence
00187 #define CALVER_NOMODL 0xFFFFFFFFFFF1FFFF //to reset the mask
00188
00189
00190
00191
00192 #define QUAL_NODATA (0x80000000) //not all the IQUV images at all the wavelengths were produced (SOME OR ALL DATA SEGMENTS ARE MISSING)
00193 #define QUAL_TARGETFILTERGRAMMISSING (0x40000000) //no target filtergram was found near target time (the target filtergram is the filtergram used to identify the framelist): could be due to missing filtergrams or because no observable sequence was run at that time
00194 #define QUAL_NOINTERPOLATEDKEYWORDS (0x20000000) //could not interpolate some keywords at target time (CROTA2, DSUN_OBS, and CRLT_OBS are required by do_interpolate()), because some level 1 records are missing or corrupted
00195 #define QUAL_NOFRAMELISTINFO (0x10000000) //could not figure out which observables framelist was used, or the framelist run for the required dates is not an observables framelist
00196 #define QUAL_WRONGCADENCE (0x8000000) //the cadence corresponding to the framelist run at required times does not match the expected value provided by user (could be an error from user, or an issue with the framelist)
00197 #define QUAL_WRONGFRAMELISTSIZE (0x4000000) //the current framelist size does not match the value from the command line
00198 #define QUAL_WRONGNPOL (0x2000000) //the current framelist npol does not match the value from the command line
00199 #define QUAL_WRONGPOLTYPE (0x1000000) //the current framelist does not allow for the production of I,Q,U, and V data
00200 #define QUAL_WRONGTARGET (0x800000) //the target filtergram does not belong to the current framelist (there is something wrong either with the framelist or the target filtergram)
00201 #define QUAL_ERRORFRAMELIST (0x400000) //the filtergrams are not where they should be in the framelist
00202 #define QUAL_WRONGWAVELENGTHNUM (0x200000) //the number of wavelengths in the lev1d records is not correct (issue with the framelist, or too many lev 1 records were missing or corrupted)
00203 #define QUAL_NOLOOKUPRECORD (0x100000) //could not find a record for the look-up tables for the MDI-like algorithm (the MDI-like algorithm cannot be used)
00204 #define QUAL_NOLOOKUPKEYWORD (0x80000) //could not read the keywords of the look-up tables for the MDI-like algorithm (the MDI-like algorithm cannot be used)
00205 #define QUAL_NOTENOUGHINTERPOLANTS (0x40000) //not enough interpolation points for the temporal interpolation at a given wavelength and polarization (too many lev 1 records were missing or corrupted)
00206 #define QUAL_INTERPOLATIONFAILED (0x20000) //the temporal interpolation routine failed
00207
00208
00209 #define QUAL_LOWINTERPNUM (0x10000) //the number of averaging points is lower than TempIntNum, AND/OR 2 interpolation points were separated by more than the cadence
00210 #define QUAL_LOWKEYWORDNUM (0x8000) //some keywords (especially CROTA2, DSUN_OBS, and CRLT_OBS) could not be interpolated properly at target time, but a closest-neighbour approximation was used
00211 #define QUAL_ISSTARGET (0x4000) //the ISS loop was OPEN for one or several filtergrams used to produce the observable
00212 #define QUAL_NOTEMP (0x2000) //cannot read the temperatures needed for polarization calibration (default temperature used)
00213 #define QUAL_NOGAPFILL (0x1000) //the code could not properly gap-fill all the lev 1 filtergrams
00214 #define QUAL_LIMBFITISSUE (0x800) //some lev1 records were discarded because R_SUN, and/or CRPIX1/CRPIX2 were missing or too different from the median value of the other lev 1 records (too much jitter for instance)
00215 #define QUAL_NOCOSMICRAY (0x400) //some cosmic-ray hit lists could not be read for the level 1 filtergrams
00216 #define QUAL_ECLIPSE (0x200) //at least one lev1 record was taken during an eclipse
00217 #define QUAL_LARGEFTSID (0x100) //HFTSACID of target filtergram > 4000, which adds noise to observables
00218 #define QUAL_POORQUALITY (0x20) //poor quality: careful when using these observables due to eclipse, or lunar transit, or thermal recovery, or open ISS, or other issues...
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231 ModuleArgs_t module_args[] =
00232 {
00233 {ARG_STRING, kRecSetIn, "" , "beginning time for which an output is wanted"},
00234 {ARG_STRING, kRecSetIn2, "" , "end time for which an output is wanted"},
00235 {ARG_INT , WaveLengthIn,"3" , "Index of the wavelength starting the framelist. FROM 0 TO 5"},
00236 {ARG_INT , CamIDIn , "0" , "Front (1) or side (0) camera?"},
00237 {ARG_DOUBLE, DataCadenceIn,"135.0" ,"Cadence: 45, 90, 120, 135, or 150 seconds"},
00238 {ARG_INT, NpolIn,"4", "Number of polarizations in framelist"},
00239 {ARG_INT, FramelistSizeIn,"36", "size of framelist"},
00240 {ARG_STRING, SeriesIn, "hmi_test.lev1_dcon", "Name of the lev1 series"},
00241 {ARG_INT , QuickLookIn, "0" , "Quicklook data? No=0; Yes=1"},
00242 {ARG_INT , Average, "12" , "Average over 12 or 96 minutes? (12 by default)"},
00243 {ARG_INT , RotationalFlat, "0", "Use rotational flat fields? yes=1, no=0 (default)"},
00244 {ARG_STRING, "dpath", "/home/jsoc/cvs/Development/JSOC/proj/lev1.5_hmi/apps/", "directory where the source code is located"},
00245 {ARG_INT , Linearity, "0", "Correct for non-linearity of cameras? yes=1, no=0 (default)"},
00246 {ARG_END}
00247 };
00248
00249
00250
00251
00252
00253
00254
00255
00256 int needtochangeFID(int HFLID)
00257 {
00258 int need;
00259
00260 switch(HFLID)
00261 {
00262 case 58312: need=1;
00263 break;
00264 case 1022: need=1;
00265 break;
00266 default: need=0;
00267 }
00268
00269 return need;
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279 int signj(int v)
00280 {
00281 return v > 0 ? 1 : (v < 0 ? -1 : 0);
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 int WhichWavelength(int FID)
00297 {
00298 int result;
00299 int temp;
00300 if(FID < 100000) temp=(FID/10) % 20;
00301 else
00302 {
00303 temp = ((FID-100000)/10) % 20;
00304 }
00305 switch (temp)
00306 {
00307 case 19: result=9;
00308 break;
00309 case 17: result=7;
00310 break;
00311 case 15: result=0;
00312 break;
00313 case 14: result=0;
00314 break;
00315 case 13: result=1;
00316 break;
00317 case 12: result=1;
00318 break;
00319 case 11: result=2;
00320 break;
00321 case 10: result=2;
00322 break;
00323 case 9: result=3;
00324 break;
00325 case 8: result=3;
00326 break;
00327 case 7: result=4;
00328 break;
00329 case 6: result=4;
00330 break;
00331 case 5: result=5;
00332 break;
00333 case 3: result=6;
00334 break;
00335 case 1: result=8;
00336 break;
00337 default: result=-101;
00338 }
00339
00340 return result;
00341
00342 }
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371 int framelistInfo(int HFLID,int HPLTID,int HWLTID,int WavelengthID,int *PHWPLPOS,int *WavelengthIndex,int *WavelengthLocation, int *PPolarizationType,int CamIdIn,int *Pcombine,int *Pnpol,int MaxNumFiltergrams,TIME *PDataCadence,int *CameraValues,int *FID,char *dpath)
00372 {
00373 int framelistSize=0,HFLIDread,FIDread,i,j,compteur;
00374 int PLINDEX,WLINDEX;
00375 FILE *sequencefile;
00376
00377
00378
00379
00380
00381
00382
00383 char *filename=NULL;
00384 char *filename2=NULL;
00385 char *filename3=NULL;
00386
00387 char dpath2[256];
00388 strcpy(dpath2,dpath);
00389 filename =strdup(strcat(dpath2,"/../Sequences3.txt"));
00390 strcpy(dpath2,dpath);
00391 filename2=strdup(strcat(dpath2,"/../../tables/hmi_mech/std_flight.w"));
00392 strcpy(dpath2,dpath);
00393 filename3=strdup(strcat(dpath2,"/../../tables/hmi_mech/std_flight.p"));
00394
00395
00396 char line[256];
00397 int PL_Index[MaxNumFiltergrams],WL_Index[MaxNumFiltergrams];
00398 int WT1P,WT2P,WT3P,WT4P,PS1P,PS2P,PS3P;
00399 int found=0;
00400
00401 int combinef,combines,npolf,npols,framelistSizef,framelistSizes,PolarizationTypef,PolarizationTypes,CAMERA;
00402 float DataCadencef,DataCadences;
00403
00404
00405
00406
00407
00408 printf("HFLID OF FRAMELIST = %d\n",HFLID);
00409 sequencefile = fopen(filename,"r");
00410 if(sequencefile == NULL)
00411 {
00412 printf("The file %s does not exist or cannot be read\n",filename);
00413 free(filename);
00414 free(filename2);
00415 free(filename3);
00416 filename=NULL;
00417 filename2=NULL;
00418 filename3=NULL;
00419 return 1;
00420 }
00421
00422 i=0;
00423 compteur=0;
00424 while (fgets(line,256,sequencefile) != NULL)
00425 {
00426 sscanf(line,"%d %d %d %f %f %d %d %d %d %d %d %d %d",&HFLIDread,&PolarizationTypef,&PolarizationTypes,&DataCadencef,&DataCadences,&npolf,&npols,&combinef,&combines,&framelistSizef,&framelistSizes,&j,&CAMERA);
00427 if(HFLIDread == HFLID)
00428 {
00429
00430 if(CamIdIn == LIGHT_FRONT)
00431 {
00432 *Pcombine=combinef;
00433 *Pnpol=npolf;
00434 *PDataCadence=DataCadencef;
00435 *PPolarizationType=PolarizationTypef;
00436 framelistSize=framelistSizef;
00437 if(combinef == 0)
00438 {
00439 if(CAMERA == 3)
00440 {
00441 FID[i]=j;
00442 WavelengthLocation[i]=compteur;
00443 CameraValues[i]=LIGHT_FRONT;
00444 i+=1;
00445 }
00446 }
00447 else
00448 {
00449 FID[i]=j;
00450 WavelengthLocation[i]=compteur;
00451 if(CAMERA == 3) CameraValues[i]=LIGHT_FRONT;
00452 else CameraValues[i]=LIGHT_SIDE;
00453 i+=1;
00454 }
00455 }
00456 if(CamIdIn == LIGHT_SIDE)
00457 {
00458 *Pcombine=combines;
00459 *Pnpol=npols;
00460 *PDataCadence=DataCadences;
00461 *PPolarizationType=PolarizationTypes;
00462 framelistSize=framelistSizes;
00463 if(combines == 0)
00464 {
00465 if(CAMERA == 2)
00466 {
00467 FID[i]=j;
00468 WavelengthLocation[i]=compteur;
00469 CameraValues[i]=LIGHT_SIDE;
00470 i+=1;
00471 }
00472 }
00473 else
00474 {
00475 FID[i]=j;
00476 WavelengthLocation[i]=compteur;
00477 if(CAMERA == 3) CameraValues[i]=LIGHT_FRONT;
00478 else CameraValues[i]=LIGHT_SIDE;
00479 i+=1;
00480 }
00481
00482 }
00483 found=1;
00484 compteur+=1;
00485 }
00486 }
00487 fclose(sequencefile);
00488
00489
00490
00491
00492 int baseindexW,baseindexP;
00493
00494
00495 if((framelistSize == 12) || (framelistSize == 24) || (framelistSize == 36) || (framelistSize == 48) || (framelistSize == 16) || (framelistSize == 32) || (framelistSize == 20) || (framelistSize == 40))
00496 {
00497 switch(WavelengthID)
00498 {
00499 case 9: baseindexW=HWLTID-19;
00500 break;
00501 case 7: baseindexW=HWLTID-17;
00502 break;
00503 case 0: baseindexW=HWLTID-15;
00504 break;
00505 case 1: baseindexW=HWLTID-13;
00506 break;
00507 case 2: baseindexW=HWLTID-11;
00508 break;
00509 case 3: baseindexW=HWLTID-9;
00510 break;
00511 case 4: baseindexW=HWLTID-7;
00512 break;
00513 case 5: baseindexW=HWLTID-5;
00514 break;
00515 case 6: baseindexW=HWLTID-3;
00516 break;
00517 case 8: baseindexW=HWLTID-1;
00518 break;
00519 }
00520 }
00521 if( (framelistSize == 10) )
00522 {
00523 switch(WavelengthID)
00524 {
00525 case 0: baseindexW=HWLTID-14;
00526 break;
00527 case 1: baseindexW=HWLTID-12;
00528 break;
00529 case 2: baseindexW=HWLTID-10;
00530 break;
00531 case 3: baseindexW=HWLTID-8;
00532 break;
00533 case 4: baseindexW=HWLTID-6;
00534 break;
00535 }
00536 }
00537
00538 baseindexP=(HPLTID/10)*10;
00539
00540 for(i=0;i<framelistSize;++i)
00541 {
00542 WLINDEX=(FID[i]/10) % 20;
00543 WL_Index[i]=WLINDEX+baseindexW;
00544 PLINDEX=FID[i]%10;
00545 PL_Index[i]=PLINDEX+baseindexP;
00546 switch(WLINDEX)
00547 {
00548 case 19: WavelengthIndex[i]=9;
00549 break;
00550 case 17: WavelengthIndex[i]=7;
00551 break;
00552 case 15: WavelengthIndex[i]=0;
00553 break;
00554 case 14: WavelengthIndex[i]=0;
00555 break;
00556 case 13: WavelengthIndex[i]=1;
00557 break;
00558 case 12: WavelengthIndex[i]=1;
00559 break;
00560 case 11: WavelengthIndex[i]=2;
00561 break;
00562 case 10: WavelengthIndex[i]=2;
00563 break;
00564 case 9: WavelengthIndex[i]=3;
00565 break;
00566 case 8: WavelengthIndex[i]=3;
00567 break;
00568 case 7: WavelengthIndex[i]=4;
00569 break;
00570 case 6: WavelengthIndex[i]=4;
00571 break;
00572 case 5: WavelengthIndex[i]=5;
00573 break;
00574 case 3: WavelengthIndex[i]=6;
00575 break;
00576 case 1: WavelengthIndex[i]=8;
00577 break;
00578 default: WavelengthIndex[i]=-101;
00579 }
00580 if(WavelengthIndex[i] == -101)
00581 {
00582 printf("Error: WavelengthIndex[i]=-101 \n");
00583 free(filename);
00584 free(filename2);
00585 free(filename3);
00586 filename=NULL;
00587 filename2=NULL;
00588 filename3=NULL;
00589 return 1;
00590 }
00591 }
00592
00593
00594
00595
00596
00597
00598
00599 sequencefile = fopen(filename2,"r");
00600 if(sequencefile == NULL)
00601 {
00602 printf("The file %s does not exist or cannot be read\n",filename2);
00603 free(filename);
00604 free(filename2);
00605 free(filename3);
00606 filename=NULL;
00607 filename2=NULL;
00608 filename3=NULL;
00609 return 1;
00610 }
00611
00612 for(j=0;j<6;++j) fgets(line,256,sequencefile);
00613 while (fgets(line,256,sequencefile) != NULL)
00614 {
00615 if(line[0] != '#')
00616 {
00617 sscanf(line,"%d %d %d %d %d",&j,&WT1P,&WT2P,&WT3P,&WT4P);
00618 for(i=0;i<framelistSize;++i)
00619 {
00620 if(j == WL_Index[i])
00621 {
00622 PHWPLPOS[i*7 ]=WT1P;
00623 PHWPLPOS[i*7+1]=WT2P;
00624 PHWPLPOS[i*7+2]=WT3P;
00625 PHWPLPOS[i*7+3]=WT4P;
00626 }
00627 }
00628 }
00629 }
00630 fclose(sequencefile);
00631
00632 sequencefile = fopen(filename3,"r");
00633 if(sequencefile == NULL)
00634 {
00635 printf("The file %s does not exist or cannot be read\n",filename3);
00636 free(filename);
00637 free(filename2);
00638 free(filename3);
00639 filename=NULL;
00640 filename2=NULL;
00641 filename3=NULL;
00642 return 1;
00643 }
00644
00645 for(j=0;j<6;++j) fgets(line,256,sequencefile);
00646 while (fgets(line,256,sequencefile) != NULL)
00647 {
00648 if(line[0] != '#')
00649 {
00650 sscanf(line,"%d %d %d %d",&j,&PS1P,&PS2P,&PS3P);
00651 for(i=0;i<framelistSize;++i)
00652 {
00653 if(j == PL_Index[i])
00654 {
00655 PHWPLPOS[i*7+4]=PS1P;
00656 PHWPLPOS[i*7+5]=PS2P;
00657 PHWPLPOS[i*7+6]=PS3P;
00658 }
00659 }
00660 }
00661 }
00662 fclose(sequencefile);
00663
00664
00665 if(found == 0)
00666 {
00667 framelistSize=0;
00668 }
00669
00670 free(filename);
00671 free(filename2);
00672 free(filename3);
00673 filename=NULL;
00674 filename2=NULL;
00675 filename3=NULL;
00676
00677 return framelistSize;
00678 }
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691 int MaskCreation(unsigned char *Mask, int nx, int ny, DRMS_Array_t *BadPixels, int HIMGCFID, float *image, DRMS_Array_t *CosmicRays, int nbadperm)
00692 {
00693
00694 int status=2;
00695
00696 if(nx != 4096 || ny != 4096) return status;
00697
00698 char *filename="/home/production/img_cnfg_ids";
00699 const int minid=80;
00700 const int maxid=124;
00701 const int n_tables=1;
00702 const int maxtab=256;
00703
00704 int *badpixellist=BadPixels->data;
00705 int nBadPixels=BadPixels->axis[0];
00706 int *cosmicraylist=NULL;
00707 int ncosmic=0;
00708 if(CosmicRays != NULL)
00709 {
00710 cosmicraylist=CosmicRays->data;
00711 ncosmic=CosmicRays->axis[0];
00712 }
00713 else ncosmic = -1;
00714
00715 int x_orig,y_orig,x_dir,y_dir;
00716 int skip, take, skip_x, skip_y;
00717 int datum, nsx, nss, nlin,nq,nqq,nsy;
00718 int idn=-1;
00719
00720 int *id=NULL, *tab=NULL, *nrows=NULL, *ncols=NULL, *rowstr=NULL, *colstr=NULL, *config=NULL;
00721 id =(int *)(malloc(maxtab*sizeof(int)));
00722 if(id == NULL)
00723 {
00724 printf("Error: memory could not be allocated to id\n");
00725 return 1;
00726 }
00727 tab =(int *)(malloc(maxtab*sizeof(int)));
00728 if(tab == NULL)
00729 {
00730 printf("Error: memory could not be allocated to tab\n");
00731 return 1;
00732 }
00733 nrows =(int *)(malloc(maxtab*sizeof(int)));
00734 if(nrows == NULL)
00735 {
00736 printf("Error: memory could not be allocated to nrows\n");
00737 return 1;
00738 }
00739 ncols =(int *)(malloc(maxtab*sizeof(int)));
00740 if(ncols == NULL)
00741 {
00742 printf("Error: memory could not be allocated to ncols\n");
00743 return 1;
00744 }
00745 rowstr=(int *)(malloc(maxtab*sizeof(int)));
00746 if(rowstr == NULL)
00747 {
00748 printf("Error: memory could not be allocated to rowstr\n");
00749 return 1;
00750 }
00751 colstr=(int *)(malloc(maxtab*sizeof(int)));
00752 if(colstr == NULL)
00753 {
00754 printf("Error: memory could not be allocated to colstr\n");
00755 return 1;
00756 }
00757 config=(int *)(malloc(maxtab*sizeof(int)));
00758 if(config == NULL)
00759 {
00760 printf("Error: memory could not be allocated to config\n");
00761 return 1;
00762 }
00763
00764 int skipt[4*2048];
00765 int taket[4*2048];
00766
00767 int **kx=NULL;
00768 int *kkx=NULL;
00769
00770 kx=(int **)(malloc(9*sizeof(int *)));
00771 if(kx == NULL)
00772 {
00773 printf("Error: memory could not be allocated to kx\n");
00774 return 1;
00775 }
00776
00777
00778 int kx0[11]={4,0,0,1,0,1,1,0,1,1,1};
00779 kx[0]=kx0;
00780 int kx1[7]={2,1,0,1,1,1,2};
00781 kx[2]=kx1;
00782 int kx2[7]={2,0,1,0,0,1,2} ;
00783 kx[4]=kx2;
00784 int kx3[7]={2,0,0,1,0,2,1};
00785 kx[1]=kx3;
00786 int kx4[7]={2,1,1,0,1,2,1};
00787 kx[3]=kx4;
00788 int kx5[7]={1,0,0,2,2};
00789 kx[5]=kx5;
00790 int kx6[5]={1,1,0,2,2};
00791 kx[6]=kx6;
00792 int kx7[5]={1,1,1,2,2};
00793 kx[7]=kx7;
00794 int kx8[5]={1,0,1,2,2};
00795 kx[8]=kx8;
00796
00797 char **filename_table=NULL;
00798 filename_table=(char **)(malloc(n_tables*sizeof(char *)));
00799 if(filename_table == NULL)
00800 {
00801 printf("Error: memory could not be allocated to filename_table\n");
00802 return 1;
00803 }
00804
00805 char *filename_croptable="/home/cvsuser/cvsroot/EGSE/tables/crop/crop6";
00806 filename_table[0]=filename_croptable;
00807
00808 int i,j,k;
00809 int ix, jx;
00810 int n_config;
00811 char string[256];
00812 char strng[5];
00813
00814
00815 FILE *config_file=NULL;
00816 FILE *crop_table=NULL;
00817
00818
00819 config_file=fopen(filename, "r");
00820
00821 if (config_file != NULL){
00822 fgets(string, 256, config_file);
00823 fgets(string, 256, config_file);
00824 fgets(string, 256, config_file);
00825
00826
00827 n_config=0;
00828 fscanf(config_file, "%d", &datum);
00829 id[n_config]=datum;
00830
00831 do {
00832
00833 fscanf(config_file, "%s", string);
00834
00835 config[n_config]=0;
00836 if (string[0] == '4')config[n_config]=0;
00837 if (string[0] == '2') if (string[7] == 'E')config[n_config]=1;
00838 if (string[0] == '2') if (string[7] == 'F')config[n_config]=2;
00839 if (string[0] == '2') if (string[7] == 'G')config[n_config]=3;
00840 if (string[0] == '2') if (string[7] == 'H')config[n_config]=4;
00841 if (string[0] == '1') if (string[7] == 'E')config[n_config]=5;
00842 if (string[0] == '1') if (string[7] == 'F')config[n_config]=6;
00843 if (string[0] == '1') if (string[7] == 'G')config[n_config]=7;
00844 if (string[0] == '1') if (string[7] == 'H')config[n_config]=8;
00845
00846
00847 fscanf(config_file, "%s", string);
00848
00849 fscanf(config_file, "%s", string);
00850
00851 fscanf(config_file, "%s", string);
00852
00853 if (string[0] == 'N') tab[n_config]=-1;
00854 if (string[0] == 'c') tab[n_config]=0;
00855
00856 fscanf(config_file, "%s", string);
00857
00858 fscanf(config_file, "%d", &datum);
00859
00860 fscanf(config_file, "%d", &datum);
00861
00862 fscanf(config_file, "%s", string);
00863
00864 fscanf(config_file, "%d", &datum);
00865 nrows[n_config]=datum;
00866
00867 fscanf(config_file, "%d", &datum);
00868 ncols[n_config]=datum;
00869
00870 fscanf(config_file, "%d", &datum);
00871 rowstr[n_config]=datum;
00872
00873 fscanf(config_file, "%d", &datum);
00874 colstr[n_config]=datum;
00875
00876 fscanf(config_file, "%d", &datum);
00877
00878 fscanf(config_file, "%d", &datum);
00879
00880
00881
00882 ++n_config;
00883 fscanf(config_file, "%d", &datum);
00884 id[n_config]=datum;
00885
00886
00887
00888
00889 }
00890 while (!feof(config_file) && n_config < maxtab);
00891
00892 fclose(config_file);
00893 }
00894
00895
00896
00897 for (i=0; i<n_config; ++i) if (id[i] == HIMGCFID) idn=i;
00898
00899 skip_x=ncols[idn]/2;
00900 skip_y=nrows[idn]/2;
00901
00902
00903
00904 kkx=kx[config[idn]];
00905 nq=kkx[0];
00906 nlin=kkx[2*nq+3-2]*ny/2;
00907 nss=kkx[2*nq+3-1]*nx/2;
00908
00909
00910
00911 if (idn == -1)
00912 {printf("Error: invalid HIMGCFID\n"); status=2;}
00913 else
00914 {
00915 if (tab[idn] != -1)
00916 {
00917 filename_croptable=filename_table[tab[idn]];
00918 crop_table=fopen(filename_croptable, "r");
00919
00920
00921 fscanf(crop_table, "%d", &datum);
00922
00923 fscanf(crop_table, "%d", &datum);
00924 fscanf(crop_table, "%d", &nqq);
00925
00926 for (j=0; j<nqq; ++j)
00927 {
00928 fscanf(crop_table, "%d", &skip);
00929 fscanf(crop_table, "%d", &take);
00930 skipt[j]=skip;
00931 taket[j]=take;
00932 }
00933
00934 fclose(crop_table);
00935 }
00936 else
00937 {
00938 printf("no crop table\n");
00939
00940 for (k=0; k<nq; ++k)
00941 for (j=0; j<nlin; ++j)
00942 {
00943 skipt[k*nss+j]=0;
00944 taket[k*nss+j]=nss;
00945 }
00946 }
00947
00948 nsx=nss-skip_x;
00949 nsy=nlin-skip_y;
00950
00951
00952
00953 for (k=0; k<nq; ++k)
00954 {
00955 x_orig=kkx[k*2+1]*nx - kkx[k*2+1];
00956 y_orig=kkx[k*2+2]*ny - kkx[k*2+2];
00957 x_dir=-(kkx[k*2+1])*2+1;
00958 y_dir=-(kkx[k*2+2])*2+1;
00959
00960
00961 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;
00962 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;
00963
00964
00965 for (j=0; j<nsy; ++j)
00966 {
00967 jx=j+skip_y;
00968
00969 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;}
00970 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;}
00971 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;}
00972 }
00973 }
00974
00975
00976
00977 for(k=0;k<nx*ny;++k)
00978 {
00979 if(Mask[k] == 0)
00980 {
00981 if(isnan(image[k])) Mask[k] = 1;
00982 }
00983 }
00984
00985
00986
00987 if(ncosmic != -1 && nbadperm != -1) nBadPixels = nbadperm;
00988 if(nBadPixels > 0)
00989 {
00990 for (k=0;k<nBadPixels;++k)
00991 {
00992 if(Mask[badpixellist[k]] == 0) Mask[badpixellist[k]] = 1;
00993 }
00994 }
00995
00996
00997 if(ncosmic > 0)
00998 {
00999 for (k=0;k<ncosmic;++k)
01000 {
01001 if(Mask[cosmicraylist[k]] == 0) Mask[cosmicraylist[k]] = 1;
01002 }
01003 }
01004
01005 status=0;
01006
01007 }
01008
01009 free(id);
01010 free(nrows);
01011 free(ncols);
01012 free(rowstr);
01013 free(colstr);
01014 free(config);
01015 free(kx);
01016 free(filename_table);
01017 id=NULL;
01018 nrows=NULL;
01019 ncols=NULL;
01020 rowstr=NULL;
01021 colstr=NULL;
01022 config=NULL;
01023 kx=NULL;
01024 filename_table=NULL;
01025
01026 return status;
01027 }
01028
01029
01030
01031 char *iquv_version()
01032 {
01033 return strdup("$Id: HMI_IQUV_averaging_dcon.c,v 1.3 2016/10/03 17:43:52 arta Exp $");
01034 }
01035
01036
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053 int DoIt(void)
01054 {
01055
01056 #define MaxNString 512 //maximum length of strings in character number
01057 int errbufstat =setvbuf(stderr, NULL, _IONBF, BUFSIZ);
01058 int outbufstat =setvbuf(stdout, NULL, _IONBF, BUFSIZ);
01059
01060
01061
01062
01063 char *inRecQuery = cmdparams_get_str(&cmdparams, kRecSetIn, NULL);
01064 char *inRecQuery2 = cmdparams_get_str(&cmdparams, kRecSetIn2, NULL);
01065 int WavelengthID = cmdparams_get_int(&cmdparams,WaveLengthIn , NULL);
01066 int CamId = cmdparams_get_int(&cmdparams,CamIDIn, NULL);
01067 TIME DataCadence = cmdparams_get_double(&cmdparams,DataCadenceIn,NULL);
01068 int Npolin = cmdparams_get_int(&cmdparams,NpolIn, NULL);
01069 int Framelistsizein = cmdparams_get_int(&cmdparams,FramelistSizeIn,NULL);
01070 char *inLev1Series = cmdparams_get_str(&cmdparams,SeriesIn, NULL);
01071 int QuickLook = cmdparams_get_int(&cmdparams,QuickLookIn, NULL);
01072 int Averaging = cmdparams_get_int(&cmdparams,Average, NULL);
01073 int inRotationalFlat = cmdparams_get_int(&cmdparams,RotationalFlat, NULL);
01074 char *dpath = cmdparams_get_str(&cmdparams,"dpath", NULL);
01075 int inLinearity = cmdparams_get_int(&cmdparams,Linearity, NULL);
01076
01077
01078 char *CODEVERSION =NULL;
01079 CODEVERSION=iquv_version();
01080 char *CODEVERSION1=NULL;
01081 CODEVERSION1=interpol_version();
01082 char *CODEVERSION2=NULL;
01083 CODEVERSION2=interpol_version();
01084 char *CODEVERSION3=NULL;
01085 CODEVERSION3=polcal_version();
01086
01087
01088 char *DISTCOEFPATH=NULL;
01089 char *ROTCOEFPATH =NULL;
01090
01091
01092 char HISTORY[MaxNString];
01093
01094 char COMMENT[MaxNString];
01095 strcpy(COMMENT,"De-rotation: ON; Un-distortion: ON; Re-centering: ON; Re-sizing: OFF; correction for cosmic-ray hits; RSUNerr=5.0 pixels; correction front/side intensity implemented for mod L; dpath=");
01096 strcat(COMMENT,dpath);
01097 if(inLinearity == 1) strcat(COMMENT,"; linearity=1 with coefficients updated on 2014/01/15");
01098 if(inRotationalFlat == 1) strcat(COMMENT,"; rotational=1");
01099 strcat(COMMENT,"; propagate eclipse bit from level 1");
01100
01101 struct init_files initfiles;
01102
01103
01104
01105
01106
01107
01108
01109 char *DISTCOEFFILEF=NULL;
01110 char *DISTCOEFFILES=NULL;
01111 char *ROTCOEFFILE=NULL;
01112
01113 char dpath2[MaxNString];
01114 strcpy(dpath2,dpath);
01115 DISTCOEFFILEF=strdup(strcat(dpath2,"/../libs/lev15/distmodel_front_o6_100624.txt"));
01116 strcpy(dpath2,dpath);
01117 DISTCOEFFILES=strdup(strcat(dpath2,"/../libs/lev15/distmodel_side_o6_100624.txt"));
01118 strcpy(dpath2,dpath);
01119 ROTCOEFFILE =strdup(strcat(dpath2,"/../libs/lev15/rotcoef_file.txt"));
01120 strcpy(dpath2,dpath);
01121 DISTCOEFPATH =strdup(strcat(dpath2,"/../libs/lev15/"));
01122 strcpy(dpath2,dpath);
01123 ROTCOEFPATH =strdup(strcat(dpath2,"/../libs/lev15/"));
01124
01125
01126
01127 initfiles.dist_file_front=DISTCOEFFILEF;
01128 initfiles.dist_file_side=DISTCOEFFILES;
01129 initfiles.diffrot_coef=ROTCOEFFILE;
01130
01131
01132 if(CamId == 0) CamId = LIGHT_SIDE;
01133 else CamId = LIGHT_FRONT;
01134
01135 if(QuickLook != 0 && QuickLook != 1)
01136 {
01137 printf("The parameter quicklook must be either 0 or 1\n");
01138 return 1;
01139
01140 }
01141
01142
01143 printf("COMMAND LINE PARAMETERS= %s %s %d %d %f %d %d %d %d %s %d\n",inRecQuery,inRecQuery2,WavelengthID,CamId,DataCadence,Npolin,QuickLook,Averaging,inRotationalFlat,dpath,inLinearity);
01144
01145
01146
01147
01148 TIME AverageTime;
01149 if(Averaging == 12) AverageTime=720.;
01150 else AverageTime=5760.;
01151
01152
01153 int NumWavelengths=10;
01154 int MaxNumFiltergrams=72;
01155 int TempIntNum;
01156 TIME TimeCaution = DataCadence;
01157 const int nRecmax = 23040;
01158 char HMISeriesLev1[MaxNString];
01159 char HMISeriesLev1p[MaxNString];
01160 char HMISeriesLev10[MaxNString];
01161
01162 TIME CadenceRead;
01163 int nWavelengths;
01164
01165
01166
01167 TempIntNum=ceil((AverageTime*1.5+90.)/DataCadence);
01168
01169 if(TempIntNum % 2 != 0) TempIntNum+=1;
01170 printf("TEMPINTNUM = %d\n",TempIntNum);
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182 DRMS_RecordSet_t *recLev1 = NULL;
01183 DRMS_RecordSet_t *recLev1p = NULL;
01184 DRMS_RecordSet_t *rectemp = NULL;
01185 DRMS_RecordSet_t *recflat = NULL;
01186 DRMS_RecordSet_t *recflatrot= NULL;
01187
01188 char CosmicRaySeries[MaxNString]= "hmi.cosmic_rays";
01189 char HMISeries[MaxNString];
01190 char timeBegin[MaxNString] ="2000.12.25_00:00:00";
01191 char timeEnd[MaxNString] ="2000.12.25_00:00:00";
01192 char timeBegin2[MaxNString]="2000.12.25_00:00:00";
01193 char timeEnd2[MaxNString] ="2000.12.25_00:00:00";
01194 char **IMGTYPE=NULL;
01195 char HMISeriesTemp[MaxNString];
01196 char HMISeriesTemperature[MaxNString]= "hmi.temperature_summary_300s";
01197 char DATEOBS[MaxNString];
01198 char jsocverss[MaxNString];
01199 char **HWLTNSET=NULL;
01200 char TargetISS[]="CLOSED";
01201 char FSNtemps[]="00000000000000";
01202 char **source;
01203 char recnums[MaxNString];
01204 char HMIRotationalFlats[MaxNString]= "hmi.flatfield_update";
01205 char HMIFlatField0[MaxNString];
01206 char *HMIFlatField;
01207
01208 TIME MaxSearchDistanceL,MaxSearchDistanceR;
01209
01210
01211
01212 TIME TREC_EPOCH = sscan_time("1993.01.01_00:00:00_TAI");
01213 TIME TREC_EPOCH0= sscan_time("1993.01.01_00:00:00_TAI");
01214
01215 TIME TREC_STEP = AverageTime;
01216 TIME temptime=0.0, temptime2=0.0;
01217 TIME TimeBegin,TimeEnd,TimeBegin2,TimeEnd2,TargetTime,PreviousTargetTime;
01218 TIME *internTOBS=NULL ;
01219 TIME tobs;
01220
01221 int combine;
01222 int ThresholdPol=TempIntNum-2;
01223 int npol=Npolin;
01224 int npolout=4;
01225 int fsn;
01226 int method=1;
01227 int *ps1=NULL,*ps2=NULL,*ps3=NULL;
01228 int axisin[2] ;
01229 int axisout[2];
01230 int nTime=0;
01231 int PHWPLPOS[MaxNumFiltergrams*7];
01232 int WavelengthIndex[MaxNumFiltergrams], WavelengthLocation[MaxNumFiltergrams], OrganizeFramelist=0, OrganizeFramelist2=0, *FramelistArray=NULL, *SegmentStatus=NULL;
01233 int FiltergramLocation, Needed;
01234 int TargetWavelength=0;
01235 int *IndexFiltergram=NULL;
01236 int nIndexFiltergram;
01237 int TargetHFLID=0;
01238 int TargetHWLPOS[4];
01239 int TargetHPLPOS[3];
01240 int TargetCFINDEX;
01241 int nRecs1;
01242 int ActualTempIntNum;
01243 int framelistSize=Framelistsizein;
01244 int PolarizationType;
01245 int i,temp,temp2,k,ii,iii,it,it2,timeindex,j;
01246 int status = DRMS_SUCCESS, status2 = DRMS_SUCCESS, statusA[54], TotalStatus;
01247 int *HWL1POS=NULL;
01248 int *HWL2POS=NULL;
01249 int *HWL3POS=NULL;
01250 int *HWL4POS=NULL;
01251 int *HPL1POS=NULL;
01252 int *HPL2POS=NULL;
01253 int *HPL3POS=NULL;
01254 int *FID=NULL;
01255 int *HFLID=NULL;
01256 int *CFINDEX=NULL;
01257 int *FSN=NULL;
01258 int *HIMGCFID=NULL;
01259 int *HWLTID=NULL;
01260 int *HPLTID=NULL;
01261 int WavelengthID2;
01262 int *KeywordMissing=NULL;
01263 int *SegmentRead=NULL;
01264
01265
01266
01267 int *Badkeyword=NULL;
01268 int *HCAMID=NULL;
01269 int ngood;
01270 int camera;
01271 int *CARROTint=NULL;
01272 int *CARROT=NULL;
01273 int *NBADPERM=NULL;
01274 int fidfilt;
01275 int TargetHWLTID;
01276 int TargetHPLTID;
01277 int CreateEmptyRecord=0;
01278 int *QUALITY=NULL;
01279 int FIDValues[MaxNumFiltergrams];
01280 int CameraValues[MaxNumFiltergrams];
01281 int *CAMAVG=NULL;
01282 int wl=0;
01283 int row,column;
01284 int *QUALITYin=NULL;
01285 int *QUALITYlev1=NULL;
01286 int *QUALITYLEV1=NULL;
01287 int COSMICCOUNT=0;
01288 int initialrun=0;
01289 int *totalTempIntNum;
01290 int *CAMERA=NULL;
01291
01292 long long *CALVER32=NULL;
01293
01294 float *image=NULL;
01295 float **images=NULL;
01296 float **imagesi=NULL;
01297 float **imagesout=NULL;
01298 float *RSUN=NULL;
01299 float *CROTA2=NULL;
01300 float *CRLTOBS=NULL;
01301 double *DSUNOBS=NULL;
01302 float *X0=NULL;
01303 float *Y0=NULL;
01304 float *X0AVG=NULL,*Y0AVG=NULL,*RSUNAVG=NULL;
01305 float *X0ARR=NULL,*Y0ARR=NULL,*RSUNARR=NULL;
01306 float *X0RMS=NULL,*Y0RMS=NULL,*RSUNRMS=NULL;
01307 float TSEL;
01308 float TFRONT;
01309 float *CRLNOBSint=NULL,*CRLTOBSint=NULL,*CROTA2int=NULL,*RSUNint=NULL,ctime1,ctime2;
01310 double *OBSVRint=NULL,*OBSVWint=NULL,*OBSVNint=NULL,*DSUNOBSint=NULL;
01311 float X0AVGT,Y0AVGT;
01312 float cdelt1;
01313 double *OBSVR=NULL;
01314 double *OBSVW=NULL;
01315 double *OBSVN=NULL;
01316 float *CRLNOBS=NULL;
01317
01318 float *CDELT1=NULL;
01319 float RSUNerr=5.0;
01320 float diffXfs= 6.14124;
01321 float diffYfs=-4.28992;
01322 float correction,correction2;
01323 float distance;
01324 float X0LF=0.0,Y0LF=0.0;
01325 float *pztflat;
01326 float *rotflat;
01327 float *EXPTIME=NULL;
01328 float tempvalue=0.0;
01329 float *DATAMEAN=NULL;
01330
01331
01332 char *FSNS = "FSN";
01333 char *TOBSS = "T_OBS";
01334 char *HWL1POSS = "HWL1POS";
01335 char *HWL2POSS = "HWL2POS";
01336 char *HWL3POSS = "HWL3POS";
01337 char *HWL4POSS = "HWL4POS";
01338 char *HPL1POSS = "HPL1POS";
01339 char *HPL2POSS = "HPL2POS";
01340 char *HPL3POSS = "HPL3POS";
01341 char *FIDS = "FID";
01342 char *HFLIDS = "HFLID";
01343 char *HIMGCFIDS = "HIMGCFID";
01344 char *IMGTYPES = "IMG_TYPE";
01345 char *RSUNS = "R_SUN";
01346 char *CROTA2S = "CROTA2";
01347 char *CRLTOBSS = "CRLT_OBS";
01348 char *DSUNOBSS = "DSUN_OBS";
01349 char *CRPIX1S = "CRPIX1";
01350 char *CRPIX2S = "CRPIX2";
01351 char *HCAMIDS = "HCAMID";
01352 char *HCFTIDS = "HCFTID";
01353 char *CDELT1S = "CDELT1";
01354 char *CDELT2S = "CDELT2";
01355 char *CSYSER1S = "CSYSER1";
01356 char *CSYSER2S = "CSYSER2";
01357 char *WCSNAMES = "WCSNAME";
01358 char *OBSVRS = "OBS_VR";
01359 char *OBSVWS = "OBS_VW";
01360 char *OBSVNS = "OBS_VN";
01361 char *CRLNOBSS = "CRLN_OBS";
01362 char *CARROTS = "CAR_ROT";
01363
01364 char *RSUNOBSS = "RSUN_OBS";
01365 char *HWLTIDS = "HWLTID";
01366 char *HPLTIDS = "HPLTID";
01367 char *WavelengthIDS = "WAVELNID";
01368 char *HWLTNSETS = "HWLTNSET";
01369 char *NBADPERMS = "NBADPERM";
01370 char *X0LFS = "X0_LF";
01371 char *Y0LFS = "Y0_LF";
01372 char *COUNTS = "COUNT";
01373 char *FLATREC = "FLAT_REC";
01374 char *CALVER32S = "CALVER32";
01375 char *CALVER64S = "CALVER64";
01376
01377
01378 char *TRECS = "T_REC";
01379 char *TRECEPOCHS = "T_REC_epoch";
01380 char *TRECSTEPS = "T_REC_step";
01381 char *CADENCES = "CADENCE";
01382 char *DATES = "DATE";
01383 char *DATEOBSS = "DATE__OBS";
01384 char *INSTRUMES = "INSTRUME";
01385 char *CAMERAS = "CAMERA";
01386 char *QUALITYS = "QUALITY";
01387 char *HISTORYS = "HISTORY";
01388 char *COMMENTS = "COMMENT";
01389 char *BLDVERSS = "BLD_VERS";
01390 char **TOTVALSS = NULL;
01391 char **DATAVALSS = NULL;
01392 char **MISSVALSS = NULL;
01393 char **DATAMINS = NULL;
01394 char **DATAMAXS = NULL;
01395 char **DATAMEDNS = NULL;
01396 char **DATAMEANS = NULL;
01397 char **DATARMSS = NULL;
01398 char **DATASKEWS = NULL;
01399 char **DATAKURTS = NULL;
01400 char **DATAMINS2 = NULL;
01401 char **DATAMAXS2 = NULL;
01402 char **DATAMEDNS2 = NULL;
01403 char **DATAMEANS2 = NULL;
01404 char **DATARMSS2 = NULL;
01405 char **DATASKEWS2 = NULL;
01406 char **DATAKURTS2 = NULL;
01407 char *TSELS = "TSEL";
01408 char *TFRONTS = "TFRONT";
01409 char *TINTNUMS = "TINTNUM";
01410 char *SINTNUMS = "SINTNUM";
01411 char *DISTCOEFS = "DISTCOEF";
01412 char *ROTCOEFS = "ROTCOEF";
01413 char *ODICOEFFS = "ODICOEFF";
01414 char *OROCOEFFS = "OROCOEFF";
01415 char *POLCALMS = "POLCALM";
01416 char *CODEVER0S = "CODEVER0";
01417 char *CODEVER1S = "CODEVER1";
01418 char *CODEVER2S = "CODEVER2";
01419 char *CODEVER3S = "CODEVER3";
01420 char *TS08 = "T08_PSASM_MEAN";
01421 char *TS01 = "T01_FWMR1_MEAN";
01422 char *TS02 = "T02_FWMR2_MEAN";
01423 char query[MaxNString];
01424 char *ierror = NULL;
01425 char **ierrors = NULL;
01426 char *SOURCES = "SOURCE";
01427 char *QUALLEV1S = "QUALLEV1";
01428 char *ROTFLAT = "ROT_FLAT";
01429 char QueryFlatField[MaxNString];
01430 strcpy(QueryFlatField,"");
01431
01432 DRMS_Array_t **Segments=NULL;
01433 DRMS_Array_t **Ierror=NULL;
01434 DRMS_Array_t *arrin[TempIntNum];
01435 DRMS_Array_t *arrerrors[TempIntNum];
01436 DRMS_Array_t *BadPixels= NULL;
01437 DRMS_Array_t *CosmicRays= NULL;
01438 DRMS_Array_t **arrLev1d= NULL;
01439 DRMS_Array_t **arrLev1p= NULL;
01440 DRMS_Array_t *flatfield=NULL;
01441 DRMS_Array_t *flatfieldrot=NULL;
01442 DRMS_Array_t *rotationalflats=NULL;
01443
01444 DRMS_Segment_t *segin = NULL;
01445 DRMS_Segment_t *segout = NULL;
01446
01447 struct initial const_param;
01448
01449 unsigned char *Mask =NULL;
01450
01451 DRMS_Type_t type1d = DRMS_TYPE_FLOAT;
01452 DRMS_Type_t type1p = DRMS_TYPE_FLOAT;
01453 DRMS_Type_t typet = DRMS_TYPE_TIME;
01454 DRMS_Type_t typeEr = DRMS_TYPE_CHAR;
01455
01456 struct keyword *KeyInterp=NULL;
01457 struct keyword KeyInterpOut;
01458 struct polcal_struct pars;
01459
01460 double minimum,maximum,median,mean,sigma,skewness,kurtosis;
01461 double *keyF=NULL;
01462 double TSTARTFLAT=0.0, TSTOPFLAT=0.0;
01463
01464
01465
01466
01467
01468
01469
01470
01471
01472
01473
01474 double nonlins[]={0.0,0.025409177,-4.0088672e-06,1.0615198e-10};
01475
01476 double nonlinf[]={0.0,0.020677687,-3.1873243e-06,8.7536678e-11};
01477
01478
01479 char Lev1pSegName[40][5]={"I0","Q0","U0","V0","I1","Q1","U1","V1","I2","Q2","U2","V2","I3","Q3","U3","V3","I4","Q4","U4","V4","I5","Q5","U5","V5","I6","Q6","U6","V6","I7","Q7","U7","V7","I8","Q8","U8","V8","I9","Q9","U9","V9"};
01480
01481
01482 DRMS_Record_t *rec = NULL;
01483
01484 float totalfront=0.0;
01485 float totalside=0.0;
01486 int countfront=0;
01487 int countside=0;
01488 float iratio=0.0;
01489
01490
01491
01492
01493 int nthreads;
01494
01495
01496 nthreads=omp_get_max_threads();
01497 printf("NUMBER OF THREADS USED BY OPENMP= %d\n",nthreads);
01498
01499
01500
01501
01502
01503
01504
01505
01506
01507
01508
01509
01510
01511 if(WavelengthID > NumWavelengths-1 || WavelengthID < 0)
01512 {
01513 printf("The parameter WaveLengthIn is not in the range 0-9\n");
01514 return 1;
01515 }
01516
01517
01518
01519 if(DataCadence != 45.0 && DataCadence != 90.0 && DataCadence != 120.0 && DataCadence != 135.0 && DataCadence != 150.0)
01520 {
01521 printf("The parameter cadence should be 45, 90, 120, 135, or 150 seconds\n");
01522 return 1;
01523 }
01524
01525
01526
01527
01528
01529
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539
01540
01541 printf("BEGINNING TIME: %s\n",inRecQuery);
01542 printf("ENDING TIME: %s\n",inRecQuery2);
01543 TimeBegin=sscan_time(inRecQuery);
01544 TimeEnd =sscan_time(inRecQuery2);
01545 printf("TimeBegin= %f\n",TimeBegin);
01546 printf("TimeEnd= %f\n",TimeEnd);
01547
01548
01549 temptime = (TIME)floor((TimeBegin-TREC_EPOCH+TREC_STEP/2.0)/TREC_STEP)*TREC_STEP+TREC_EPOCH;
01550 if(temptime < TimeBegin) temptime += TREC_STEP;
01551
01552 nTime =floor((TimeEnd-temptime)/AverageTime+1.0);
01553 printf("nTime = %d\n",nTime);
01554
01555 if(TimeBegin > TimeEnd)
01556 {
01557 printf("Error: the ending time must be later than the beginning time!\n");
01558 return 1;
01559 }
01560
01561
01562
01563
01564 strcpy(dpath2,dpath);
01565 strcat(dpath2,"/../../../");
01566 status = initialize_interpol(&const_param,&initfiles,4096,4096,dpath2);
01567 if(status != 0)
01568 {
01569 printf("Error: could not initialize the gapfilling, derotation, and temporal interpolation routines\n");
01570 return 1;
01571 }
01572
01573
01574 status = init_polcal(&pars,method);
01575 if(status != 0)
01576 {
01577 printf("Error: could not initialize the polarization calibration routine\n");
01578 return 1;
01579 }
01580
01581
01582
01583
01584 strcpy(HMISeriesLev1,inLev1Series);
01585
01586 strcpy(HMISeriesLev10,HMISeriesLev1);
01587
01588
01589
01590
01591 if(QuickLook == 1)
01592 {
01593 if(AverageTime == 720.0 && (DataCadence == 90.0 || DataCadence == 135.0)) strcpy(HMISeriesLev1p,"hmi_test.S_720s_dcon_nrt");
01594 else
01595 {
01596 if(AverageTime == 720.0 && (DataCadence == 120.0 || DataCadence == 150.0)) strcpy(HMISeriesLev1p,"hmi_test.S2_720s_dcon_nrt");
01597 else
01598 {
01599 printf("No output series exists for your command-line parameters %f %f %s\n",AverageTime,DataCadence,HMISeriesLev1p);
01600 return 1;
01601 }
01602 }
01603
01604
01605 }
01606 else
01607 {
01608 if(AverageTime == 720.0 && DataCadence == 90.0) strcpy(HMISeriesLev1p,"hmi_test.S_720s_dcon");
01609 if(AverageTime == 720.0 && DataCadence == 135.0) strcpy(HMISeriesLev1p,"hmi_test.S_720s_dcon");
01610 if(AverageTime == 720.0 && DataCadence == 120.0) strcpy(HMISeriesLev1p,"hmi_test.S2_720s_dcon");
01611 if(AverageTime == 720.0 && DataCadence == 150.0) strcpy(HMISeriesLev1p,"hmi_test.S2_720s_dcon");
01612
01613 if(AverageTime == 5760.0)strcpy(HMISeriesLev1p,"hmi_test.S_5760s_dcon");
01614 if(AverageTime != 5760.0 && AverageTime != 720.0)
01615 {
01616 printf("No output series exists for your command-line parameters\n");
01617 return 1;
01618 }
01619 }
01620
01621
01622
01623
01624
01625
01626
01627 TimeBegin2=TimeBegin-(TIME)TempIntNum*DataCadence/2.-TimeCaution;
01628 TimeEnd2 =TimeEnd +(TIME)TempIntNum*DataCadence/2.+TimeCaution;
01629 sprint_time(timeBegin2,TimeBegin2,"TAI",0);
01630 sprint_time(timeEnd2,TimeEnd2,"TAI",0);
01631 strcat(HMISeriesLev1,"[");
01632 strcat(HMISeriesLev1,timeBegin2);
01633 strcat(HMISeriesLev1,"-");
01634 strcat(HMISeriesLev1,timeEnd2);
01635 strcat(HMISeriesLev1,"]");
01636
01637
01638
01639
01640 printf("LEVEL 1 SERIES QUERY = %s\n",HMISeriesLev1);
01641 recLev1 = drms_open_records(drms_env,HMISeriesLev1,&status);
01642 if (status == DRMS_SUCCESS && recLev1 != NULL && recLev1->n > 0)
01643 {
01644 nRecs1 = recLev1->n;
01645
01646 if(nRecs1 >= nRecmax)
01647 {
01648 printf("Too many records requested\n");
01649 return 1;
01650 }
01651
01652 printf("Number of level 1 records opened: %d\n",nRecs1);
01653
01654
01655 FSN = (int *)malloc(nRecs1*sizeof(int));
01656 if(FSN == NULL)
01657 {
01658 printf("Error: memory could not be allocated to FSN\n");
01659 return 1;
01660 }
01661 internTOBS = (TIME *)malloc(nRecs1*sizeof(TIME));
01662 if(internTOBS == NULL)
01663 {
01664 printf("Error: memory could not be allocated to internTOBS\n");
01665 return 1;
01666 }
01667 HWL1POS = (int *)malloc(nRecs1*sizeof(int));
01668 if(HWL1POS == NULL)
01669 {
01670 printf("Error: memory could not be allocated to HWL1POS\n");
01671 return 1;
01672 }
01673 HWL2POS = (int *)malloc(nRecs1*sizeof(int));
01674 if(HWL2POS == NULL)
01675 {
01676 printf("Error: memory could not be allocated to HWL2POS\n");
01677 return 1;
01678 }
01679 HWL3POS = (int *)malloc(nRecs1*sizeof(int));
01680 if(HWL3POS == NULL)
01681 {
01682 printf("Error: memory could not be allocated to HWL3POS\n");
01683 return 1;
01684 }
01685 HWL4POS = (int *)malloc(nRecs1*sizeof(int));
01686 if(HWL4POS == NULL)
01687 {
01688 printf("Error: memory could not be allocated to HWL4POS\n");
01689 return 1;
01690 }
01691 HPL1POS = (int *)malloc(nRecs1*sizeof(int));
01692 if(HPL1POS == NULL)
01693 {
01694 printf("Error: memory could not be allocated to HPL1POS\n");
01695 return 1;
01696 }
01697 HPL2POS = (int *)malloc(nRecs1*sizeof(int));
01698 if(HPL2POS == NULL)
01699 {
01700 printf("Error: memory could not be allocated to HPL2POS\n");
01701 return 1;
01702 }
01703 HPL3POS = (int *)malloc(nRecs1*sizeof(int));
01704 if(HPL3POS == NULL)
01705 {
01706 printf("Error: memory could not be allocated to HPL3POS\n");
01707 return 1;
01708 }
01709 FID = (int *)malloc(nRecs1*sizeof(int));
01710 if(FID == NULL)
01711 {
01712 printf("Error: memory could not be allocated to FID\n");
01713 return 1;
01714 }
01715 HFLID = (int *)malloc(nRecs1*sizeof(int));
01716 if(HFLID == NULL)
01717 {
01718 printf("Error: memory could not be allocated to HFLID\n");
01719 return 1;
01720 }
01721 HCAMID = (int *)malloc(nRecs1*sizeof(int));
01722 if(HCAMID == NULL)
01723 {
01724 printf("Error: memory could not be allocated to HCAMID\n");
01725 return 1;
01726 }
01727 RSUN = (float *)malloc(nRecs1*sizeof(float));
01728 if(RSUN == NULL)
01729 {
01730 printf("Error: memory could not be allocated to RSUN\n");
01731 return 1;
01732 }
01733 CROTA2 = (float *)malloc(nRecs1*sizeof(float));
01734 if(CROTA2 == NULL)
01735 {
01736 printf("Error: memory could not be allocated to CROTA2\n");
01737 return 1;
01738 }
01739 CRLTOBS = (float *)malloc(nRecs1*sizeof(float));
01740 if(CRLTOBS == NULL)
01741 {
01742 printf("Error: memory could not be allocated to CRLTOBS\n");
01743 return 1;
01744 }
01745 DSUNOBS = (double *)malloc(nRecs1*sizeof(double));
01746 if(DSUNOBS == NULL)
01747 {
01748 printf("Error: memory could not be allocated to DSUNOBS\n");
01749 return 1;
01750 }
01751 X0 = (float *)malloc(nRecs1*sizeof(float));
01752 if(X0 == NULL)
01753 {
01754 printf("Error: memory could not be allocated to X0\n");
01755 return 1;
01756 }
01757 Y0 = (float *)malloc(nRecs1*sizeof(float));
01758 if(Y0 == NULL)
01759 {
01760 printf("Error: memory could not be allocated to Y0\n");
01761 return 1;
01762 }
01763 SegmentRead= (int *)malloc(nRecs1*sizeof(int));
01764 if(SegmentRead == NULL)
01765 {
01766 printf("Error: memory could not be allocated to SegmentRead\n");
01767 return 1;
01768 }
01769 KeywordMissing= (int *)malloc(nRecs1*sizeof(int));
01770 if(KeywordMissing == NULL)
01771 {
01772 printf("Error: memory could not be allocated to KeywordMissing\n");
01773 return 1;
01774 }
01775 Segments = (DRMS_Array_t **)malloc(nRecs1*sizeof(DRMS_Array_t *));
01776 if(Segments == NULL)
01777 {
01778 printf("Error: memory could not be allocated to Segments\n");
01779 return 1;
01780 }
01781 for(i=0;i<nRecs1;i++) Segments[i]=NULL;
01782 Ierror = (DRMS_Array_t **)malloc(nRecs1*sizeof(DRMS_Array_t *));
01783 if(Ierror == NULL)
01784 {
01785 printf("Error: memory could not be allocated to Ierror\n");
01786 return 1;
01787 }
01788 for(i=0;i<nRecs1;i++) Ierror[i]=NULL;
01789 IndexFiltergram = (int *)malloc(nRecs1*sizeof(int));
01790 if(IndexFiltergram == NULL)
01791 {
01792 printf("Error: memory could not be allocated to IndexFiltergram\n");
01793 return 1;
01794 }
01795 CFINDEX = (int *)malloc(nRecs1*sizeof(int));
01796 if(CFINDEX == NULL)
01797 {
01798 printf("Error: memory could not be allocated to CFINDEX\n");
01799 return 1;
01800 }
01801 HIMGCFID = (int *)malloc(nRecs1*sizeof(int));
01802 if(HIMGCFID == NULL)
01803 {
01804 printf("Error: memory could not be allocated to HIMGCFID\n");
01805 return 1;
01806 }
01807 IMGTYPE = (char **)malloc(nRecs1*sizeof(char *));
01808 if(IMGTYPE == NULL)
01809 {
01810 printf("Error: memory could not be allocated to IMGTYPE\n");
01811 return 1;
01812 }
01813 for(i=0;i<nRecs1;i++) IMGTYPE[i]=NULL;
01814 CDELT1 = (float *)malloc(nRecs1*sizeof(float));
01815 if(CDELT1 == NULL)
01816 {
01817 printf("Error: memory could not be allocated to CDELT1\n");
01818 return 1;
01819 }
01820 OBSVR = (double *)malloc(nRecs1*sizeof(double));
01821 if(OBSVR == NULL)
01822 {
01823 printf("Error: memory could not be allocated to OBSVR\n");
01824 return 1;
01825 }
01826 OBSVW = (double *)malloc(nRecs1*sizeof(double));
01827 if(OBSVW == NULL)
01828 {
01829 printf("Error: memory could not be allocated to OBSVW\n");
01830 return 1;
01831 }
01832 OBSVN = (double *)malloc(nRecs1*sizeof(double));
01833 if(OBSVN == NULL)
01834 {
01835 printf("Error: memory could not be allocated to OBSVN\n");
01836 return 1;
01837 }
01838 CRLNOBS = (float *)malloc(nRecs1*sizeof(float));
01839 if(CRLNOBS == NULL)
01840 {
01841 printf("Error: memory could not be allocated to CRLNOBS\n");
01842 return 1;
01843 }
01844
01845
01846
01847
01848
01849
01850 CARROT = (int *)malloc(nRecs1*sizeof(int));
01851 if(CARROT == NULL)
01852 {
01853 printf("Error: memory could not be allocated to CARROT\n");
01854 return 1;
01855 }
01856 HWLTID = (int *)malloc(nRecs1*sizeof(int));
01857 if(HWLTID == NULL)
01858 {
01859 printf("Error: memory could not be allocated to HWLTID\n");
01860 return 1;
01861 }
01862 HPLTID = (int *)malloc(nRecs1*sizeof(int));
01863 if(HPLTID == NULL)
01864 {
01865 printf("Error: memory could not be allocated to HPLTID\n");
01866 return 1;
01867 }
01868 HWLTNSET = (char **)malloc(nRecs1*sizeof(char *));
01869 if(HWLTNSET == NULL)
01870 {
01871 printf("Error: memory could not be allocated to HWLTNSET\n");
01872 return 1;
01873 }
01874 NBADPERM = (int *)malloc(nRecs1*sizeof(int));
01875 if(NBADPERM == NULL)
01876 {
01877 printf("Error: memory could not be allocated to NBADPERM\n");
01878 return 1;
01879 }
01880 QUALITYin = (int *)malloc(nRecs1*sizeof(int));
01881 if(QUALITYin == NULL)
01882 {
01883 printf("Error: memory could not be allocated to QUALITYin\n");
01884 return 1;
01885 }
01886 QUALITYlev1 = (int *)malloc(nRecs1*sizeof(int));
01887 if(QUALITYlev1 == NULL)
01888 {
01889 printf("Error: memory could not be allocated to QUALITYlev1\n");
01890 return 1;
01891 }
01892 for(i=0;i<nRecs1;++i) QUALITYlev1[i]=0;
01893 EXPTIME = (float *)malloc(nRecs1*sizeof(float));
01894 if(EXPTIME == NULL)
01895 {
01896 printf("Error: memory could not be allocated to EXPTIME\n");
01897 return 1;
01898 }
01899 CALVER32 = (long long *)malloc(nRecs1*sizeof(long long));
01900 if(CALVER32 == NULL)
01901 {
01902 printf("Error: memory could not be allocated to CALVER32\n");
01903 return 1;
01904 }
01905 CAMERA = (int *)malloc(nRecs1*sizeof(int));
01906 if(CAMERA == NULL)
01907 {
01908 printf("Error: memory could not be allocated to CAMERA\n");
01909 return 1;
01910 }
01911 DATAMEAN = (float *)malloc(nRecs1*sizeof(float));
01912 if(DATAMEAN == NULL)
01913 {
01914 printf("Error: memory could not be allocated to DATAMEAN\n");
01915 return 1;
01916 }
01917
01918
01919
01920
01921
01922 k=0;
01923 for(i=0;i<nRecs1;++i)
01924 {
01925 FSN[i] = drms_getkey_int(recLev1->records[i] ,FSNS ,&statusA[0]);
01926 internTOBS[i] = drms_getkey_time(recLev1->records[i],TOBSS ,&statusA[1]);
01927 HWL1POS[i] = drms_getkey_int(recLev1->records[i] ,HWL1POSS ,&statusA[2]);
01928 HWL2POS[i] = drms_getkey_int(recLev1->records[i] ,HWL2POSS ,&statusA[3]);
01929 HWL3POS[i] = drms_getkey_int(recLev1->records[i] ,HWL3POSS ,&statusA[4]);
01930 HWL4POS[i] = drms_getkey_int(recLev1->records[i] ,HWL4POSS ,&statusA[5]);
01931 HPL1POS[i] = drms_getkey_int(recLev1->records[i] ,HPL1POSS ,&statusA[6]);
01932 HPL2POS[i] = drms_getkey_int(recLev1->records[i] ,HPL2POSS ,&statusA[7]);
01933 HPL3POS[i] = drms_getkey_int(recLev1->records[i] ,HPL3POSS ,&statusA[8]);
01934 FID[i] = drms_getkey_int(recLev1->records[i] ,FIDS ,&statusA[9]);
01935 HFLID[i] = drms_getkey_int(recLev1->records[i] ,"HFTSACID" ,&statusA[10]);
01936
01937
01938
01939
01940 if(CamId == LIGHT_SIDE)
01941 {
01942 iii=needtochangeFID(HFLID[i]);
01943 if(iii == 1)
01944 {
01945 ii = drms_getkey_int(recLev1->records[i],"HFLPSITN",&status);
01946 if( (ii%72)/24 == 1)
01947 {
01948 FID[i]=FID[i]+100000;
01949 }
01950 }
01951
01952 }
01953
01954 HCAMID[i] = drms_getkey_int(recLev1->records[i] ,HCAMIDS ,&statusA[11]);
01955 if(HCAMID[i] != LIGHT_SIDE && HCAMID[i] != LIGHT_FRONT) statusA[11]=1;
01956 CFINDEX[i] = drms_getkey_int(recLev1->records[i] ,HCFTIDS ,&statusA[12]);
01957 IMGTYPE[i] = (char *)malloc(6*sizeof(char *));
01958 if(IMGTYPE[i] == NULL)
01959 {
01960 printf("Error: memory could not be allocated to IMGTYPE[%d]\n",i);
01961 return 1;
01962 }
01963 IMGTYPE[i] = drms_getkey_string(recLev1->records[i],IMGTYPES ,&statusA[13]);
01964 X0[i] = (float)drms_getkey_double(recLev1->records[i],CRPIX1S, &statusA[14]);
01965 if(statusA[14] == DRMS_SUCCESS && !isnan(X0[i])) X0[i]=X0[i]-1.0;
01966 else statusA[14] = 1;
01967 Y0[i] = (float)drms_getkey_double(recLev1->records[i],CRPIX2S,&statusA[15]);
01968 if(statusA[15] == DRMS_SUCCESS && !isnan(Y0[i])) Y0[i]=Y0[i]-1.0;
01969 else statusA[15] = 1;
01970
01971
01972 X0LF = (float)drms_getkey_double(recLev1->records[i],X0LFS, &status);
01973 Y0LF = (float)drms_getkey_double(recLev1->records[i],Y0LFS, &status2);
01974 if(status != DRMS_SUCCESS || status2 != DRMS_SUCCESS || isnan(X0LF) || isnan(Y0LF))
01975 {
01976 statusA[14]=1;
01977 statusA[15]=1;
01978 X0[i]=sqrt(-1);
01979 Y0[i]=sqrt(-1);
01980 KeywordMissing[i]=1;
01981 }
01982
01983
01984 RSUN[i] = (float)drms_getkey_double(recLev1->records[i],RSUNS ,&statusA[16]);
01985 if(isnan(RSUN[i])) statusA[16]=1;
01986 CROTA2[i] = (float)drms_getkey_double(recLev1->records[i],CROTA2S ,&statusA[17]);
01987
01988
01989 if(statusA[17] == DRMS_SUCCESS && !isnan(CROTA2[i]))
01990 {
01991 if(CROTA2[i] > 362. || CROTA2[i] < -362.) statusA[17] = 1;
01992 }
01993
01994 if(statusA[17] == DRMS_SUCCESS && !isnan(CROTA2[i])) CROTA2[i]=-CROTA2[i];
01995 else statusA[17] = 1;
01996 CRLTOBS[i] = (float)drms_getkey_double(recLev1->records[i],CRLTOBSS,&statusA[18]);
01997 if(isnan(CRLTOBS[i])) statusA[18] = 1;
01998 DSUNOBS[i] = drms_getkey_double(recLev1->records[i],DSUNOBSS,&statusA[19]);
01999 if(isnan(DSUNOBS[i])) statusA[19] = 1;
02000 HIMGCFID[i] = drms_getkey_int(recLev1->records[i] ,HIMGCFIDS ,&statusA[20]);
02001 if(isnan(HIMGCFID[i])) statusA[20] = 1;
02002 CDELT1[i] = (float)drms_getkey_double(recLev1->records[i] ,CDELT1S,&statusA[21]);
02003 if(isnan(CDELT1[i])) statusA[21] = 1;
02004 OBSVR[i] = drms_getkey_double(recLev1->records[i] ,OBSVRS ,&statusA[22]);
02005 if(isnan(OBSVR[i])) statusA[22] = 1;
02006 OBSVW[i] = drms_getkey_double(recLev1->records[i] ,OBSVWS ,&statusA[23]);
02007 if(isnan(OBSVW[i])) statusA[23] = 1;
02008 OBSVN[i] = drms_getkey_double(recLev1->records[i] ,OBSVNS ,&statusA[24]);
02009 if(isnan(OBSVN[i])) statusA[24] = 1;
02010 CARROT[i] = drms_getkey_int(recLev1->records[i] ,CARROTS ,&statusA[25]);
02011 SegmentRead[i]= 0;
02012 KeywordMissing[i]=0;
02013 CRLNOBS[i] = (float)drms_getkey_double(recLev1->records[i] ,CRLNOBSS,&statusA[26]);
02014 if(isnan(CRLNOBS[i])) statusA[26] = 1;
02015
02016
02017
02018 statusA[27]=0;
02019
02020
02021 HWLTID[i] = drms_getkey_int(recLev1->records[i] ,HWLTIDS ,&statusA[28]);
02022 HPLTID[i] = drms_getkey_int(recLev1->records[i] ,HPLTIDS ,&statusA[29]);
02023 HWLTNSET[i] = (char *)malloc(7*sizeof(char *));
02024 if(HWLTNSET[i] == NULL)
02025 {
02026 printf("Error: memory could not be allocated to HWLTNSET[%d]\n",i);
02027 return 1;
02028 }
02029 HWLTNSET[i] = drms_getkey_string(recLev1->records[i] ,HWLTNSETS ,&statusA[30]);
02030 EXPTIME[i] = (float)drms_getkey_double(recLev1->records[i],"EXPTIME",&statusA[31]);
02031
02032 NBADPERM[i] = drms_getkey_int(recLev1->records[i] ,NBADPERMS ,&statusA[32]);
02033 if(statusA[32] != DRMS_SUCCESS) NBADPERM[i]=-1;
02034 QUALITYin[i] = drms_getkey_int(recLev1->records[i] ,QUALITYS ,&statusA[33]);
02035 if(statusA[33] != DRMS_SUCCESS) KeywordMissing[i]=1;
02036
02037 if( (QUALITYin[i] & Q_MISSING_SEGMENT) == Q_MISSING_SEGMENT)
02038 {
02039 statusA[33]=1;
02040 SegmentRead[i]= -1;
02041 KeywordMissing[i]=1;
02042 }
02043
02044 CALVER32[i] = (long long)drms_getkey_int(recLev1->records[i] ,CALVER32S ,&statusA[34]);
02045 if(statusA[34] != DRMS_SUCCESS)
02046 {
02047 CALVER32[i]=CALVER_DEFAULT;
02048 KeywordMissing[i]=1;
02049 }
02050 if( CALVER32[i] != CALVER32[0] )
02051 {
02052 printf("Error: CALVER32[%d] is different from CALVER32[0]\n",i);
02053 return 1;
02054 }
02055 CAMERA[i] = drms_getkey_int(recLev1->records[i],CAMERAS,&statusA[35]);
02056 if(CAMERA[i] == -2147483648 || statusA[35] != DRMS_SUCCESS) KeywordMissing[i]=1;
02057
02058 DATAMEAN[i] = (float)drms_getkey_double(recLev1->records[i],"DATAMEAN",&statusA[36]);
02059 if(statusA[36] != DRMS_SUCCESS) KeywordMissing[i]=1;
02060
02061
02062
02063
02064
02065
02066
02067
02068
02069
02070
02071
02072
02073
02074
02075
02076 TotalStatus=0;
02077 for(ii=0;ii<=13;++ii) TotalStatus+=statusA[ii];
02078 for(ii=16;ii<=31;++ii) TotalStatus+=statusA[ii];
02079 if(TotalStatus != 0 || !strcmp(IMGTYPE[i],"DARK") )
02080 {
02081 printf("Error: the level 1 filtergram index %d is missing at least one keyword, or is a dark frame\n",i);
02082 for(iii=0;iii<=36;++iii) printf(" %d ",statusA[iii]);
02083 printf("\n");
02084
02085 FID[i] = MISSINGKEYWORDINT;
02086 FSN[i] = MISSINGKEYWORDINT;
02087 internTOBS[i] = MISSINGKEYWORD;
02088 HWL1POS[i] = MISSINGKEYWORDINT;
02089 HWL2POS[i] = MISSINGKEYWORDINT;
02090 HWL3POS[i] = MISSINGKEYWORDINT;
02091 HWL4POS[i] = MISSINGKEYWORDINT;
02092 HPL1POS[i] = MISSINGKEYWORDINT;
02093 HPL2POS[i] = MISSINGKEYWORDINT;
02094 HPL3POS[i] = MISSINGKEYWORDINT;
02095 X0[i] = MISSINGKEYWORD;
02096 Y0[i] = MISSINGKEYWORD;
02097 RSUN[i] = MISSINGKEYWORD;
02098 CROTA2[i] = MISSINGKEYWORD;
02099 CRLTOBS[i] = MISSINGKEYWORD;
02100 DSUNOBS[i] = MISSINGKEYWORD;
02101 HIMGCFID[i] = MISSINGKEYWORD;
02102 CDELT1[i] = MISSINGKEYWORD;
02103 OBSVR[i] = MISSINGKEYWORD;
02104 OBSVW[i] = MISSINGKEYWORD;
02105 OBSVN[i] = MISSINGKEYWORD;
02106 CARROT[i] = MISSINGKEYWORDINT;
02107 HCAMID[i] = MISSINGKEYWORDINT;
02108 CRLNOBS[i] = MISSINGKEYWORD;
02109
02110 HWLTID[i] = MISSINGKEYWORD;
02111 HPLTID[i] = MISSINGKEYWORD;
02112 strcpy(HWLTNSET[i],"NONE");
02113 EXPTIME[i] = MISSINGKEYWORD;
02114 DATAMEAN[i] = MISSINGKEYWORD;
02115
02116 KeywordMissing[i]=1;
02117 }
02118 else
02119 {
02120 if(WhichWavelength(FID[i]) == WavelengthID && HCAMID[i] == CamId)
02121 {
02122 IndexFiltergram[k]=i;
02123 ++k;
02124 }
02125 }
02126 }
02127
02128 nIndexFiltergram=k;
02129 if(nIndexFiltergram == 0)
02130 {
02131 printf("Error: no filtergram was found with the wavelength %d in the requested level 1 records %s\n",WavelengthID,HMISeriesLev1);
02132
02133 }
02134
02135 }
02136 else
02137 {
02138
02139
02140 printf("Error: no level 1 records in the time interval requested %s\n",HMISeriesLev1);
02141 return 1;
02142 }
02143
02144
02145
02146 for(i=0;i<nRecs1;++i)
02147 {
02148 if( (KeywordMissing[i] !=1) && (HCAMID[i] == LIGHT_FRONT) && (WhichWavelength(FID[i]) == 5) )
02149 {totalfront += DATAMEAN[i];
02150 countfront += 1;
02151 }
02152 if( (KeywordMissing[i] !=1) && (HCAMID[i] == LIGHT_SIDE) && (WhichWavelength(FID[i]) == 5) )
02153 {totalside += DATAMEAN[i];
02154 countside += 1;
02155 }
02156 }
02157 if(countfront != 0 && countside !=0)
02158 {
02159 totalfront = totalfront/(float)countfront;
02160 totalside = totalside/(float)countside;
02161 iratio = totalfront/totalside;
02162 printf("INTENSITY RATIO FRONT/SIDE CAMERAS = %f BASED ON %d FRONT and %d SIDE IMAGES \n",iratio,countfront,countside);
02163 }
02164 else
02165 {
02166 printf("WARNING: THERE WAS NOT ENOUGH LEV1 AT WAVELENGTH INDEX 5 TO COMPUTE AN INTENSITY RATIO BETWEEN FRONT AND SIDE CAMERAS: THE CODE WILL USE THE VALUE 1.05 IF CAMERAS HAVE TO BE COMBINED \n");
02167 iratio = 1.05;
02168 }
02169
02170
02171
02172
02173 printf("CREATE %d LEVEL 1p RECORDS\n",nTime);
02174 recLev1p = drms_create_records(drms_env,nTime,HMISeriesLev1p,DRMS_PERMANENT,&status);
02175 if (status != DRMS_SUCCESS || recLev1p == NULL || recLev1p->n == 0)
02176 {
02177 printf("Could not create records for the level 1p series\n");
02178 return 1;
02179 }
02180 TREC_EPOCH=drms_getkey_time(recLev1p->records[0],TRECEPOCHS,&status);
02181 if(status != DRMS_SUCCESS)
02182 {
02183 printf("Error: unable to read the %s keyword\n",TRECEPOCHS);
02184 return 1;
02185 }
02186 if(TREC_EPOCH != TREC_EPOCH0)
02187 {
02188 printf("Error: TREC_EPOCH should be equal to TREC_EPOCH0 and not %f\n",TREC_EPOCH);
02189 return 1;
02190 }
02191 TREC_STEP= drms_getkey_time(recLev1p->records[0],TRECSTEPS,&status);
02192 if(status != DRMS_SUCCESS)
02193 {
02194 printf("Error: cannot read the keyword %s\n",TRECSTEPS);
02195 return 1;
02196 }
02197 if(TREC_STEP != AverageTime)
02198 {
02199 printf("Error: the AverageTime is not equal to the T_REC_step keyword of the level 1p data\n");
02200 return 1;
02201 }
02202
02203 axisin[0] = 4096;
02204 axisin[1] = 4096;
02205 axisout[0]= axisin[0];
02206 axisout[1]= axisin[1];
02207
02208
02209
02210 Mask = (unsigned char *)malloc(axisin[0]*axisin[1]*sizeof(unsigned char));
02211 if(Mask == NULL)
02212 {
02213 printf("Error: cannot allocate memory for Mask\n");
02214 return 1;
02215 }
02216
02217 MaxSearchDistanceL=(TIME)(TempIntNum/2-1)*DataCadence+TimeCaution;
02218 MaxSearchDistanceR=(TIME)(TempIntNum/2) *DataCadence+TimeCaution;
02219 FramelistArray = (int *)malloc(TempIntNum*sizeof(int));
02220 if(FramelistArray == NULL)
02221 {
02222 printf("Error: memory could not be allocated to FramelistArray\n");
02223 return 1;
02224 }
02225
02226
02227 nWavelengths = Framelistsizein/Npolin;
02228 printf("number of wavelengths = %d\n",nWavelengths);
02229 if(nWavelengths != 5 && nWavelengths != 6 && nWavelengths != 8 && nWavelengths != 10)
02230 {
02231 printf("Error: the number of wavelengths should be 5, 6, 8, or 10 but is %d\n",nWavelengths);
02232 return 1;
02233 }
02234
02235 int PolarizationArray[3][Npolin];
02236 int WavelengthArray[4];
02237 int IndexTargetFiltergramId;
02238
02239
02240
02241 QUALITY =(int *)malloc(nTime*sizeof(int));
02242 if(QUALITY == NULL)
02243 {
02244 printf("Error: memory could not be allocated to QUALITY\n");
02245 return 1;
02246 }
02247 for(i=0;i<nTime;i++) QUALITY[i]=0;
02248 QUALITYLEV1 =(int *)malloc(nTime*sizeof(int));
02249 if(QUALITYLEV1 == NULL)
02250 {
02251 printf("Error: memory could not be allocated to QUALITYLEV1\n");
02252 return 1;
02253 }
02254 for(i=0;i<nTime;i++) QUALITYLEV1[i]=0;
02255 X0AVG =(float *)malloc(nTime*sizeof(float));
02256 if(X0AVG == NULL)
02257 {
02258 printf("Error: memory could not be allocated to X0AVG\n");
02259 return 1;
02260 }
02261 Y0AVG =(float *)malloc(nTime*sizeof(float));
02262 if(Y0AVG == NULL)
02263 {
02264 printf("Error: memory could not be allocated to Y0AVG\n");
02265 return 1;
02266 }
02267 CAMAVG =(int *)malloc(nTime*sizeof(int));
02268 if(CAMAVG == NULL)
02269 {
02270 printf("Error: memory could not be allocated to CAMAVG\n");
02271 return 1;
02272 }
02273 X0RMS =(float *)malloc(nTime*sizeof(float));
02274 if(X0RMS == NULL)
02275 {
02276 printf("Error: memory could not be allocated to X0RMS\n");
02277 return 1;
02278 }
02279 Y0RMS =(float *)malloc(nTime*sizeof(float));
02280 if(Y0RMS == NULL)
02281 {
02282 printf("Error: memory could not be allocated to Y0RMS\n");
02283 return 1;
02284 }
02285 RSUNAVG =(float *)malloc(nTime*sizeof(float));
02286 if(RSUNAVG == NULL)
02287 {
02288 printf("Error: memory could not be allocated to RSUNAVG\n");
02289 return 1;
02290 }
02291 RSUNRMS =(float *)malloc(nTime*sizeof(float));
02292 if(RSUNRMS == NULL)
02293 {
02294 printf("Error: memory could not be allocated to RSUNRMS\n");
02295 return 1;
02296 }
02297 OBSVRint =(double *)malloc(nTime*sizeof(double));
02298 if(OBSVRint == NULL)
02299 {
02300 printf("Error: memory could not be allocated to OBSVRint\n");
02301 return 1;
02302 }
02303 OBSVWint =(double *)malloc(nTime*sizeof(double));
02304 if(OBSVWint == NULL)
02305 {
02306 printf("Error: memory could not be allocated to OBSVWint\n");
02307 return 1;
02308 }
02309 OBSVNint =(double *)malloc(nTime*sizeof(double));
02310 if(OBSVNint == NULL)
02311 {
02312 printf("Error: memory could not be allocated to OBSVNint\n");
02313 return 1;
02314 }
02315 CRLNOBSint =(float *)malloc(nTime*sizeof(float));
02316 if(CRLNOBSint == NULL)
02317 {
02318 printf("Error: memory could not be allocated to CRLNOBSint\n");
02319 return 1;
02320 }
02321
02322
02323
02324
02325
02326
02327 CRLTOBSint =(float *)malloc(nTime*sizeof(float));
02328 if(CRLTOBSint == NULL)
02329 {
02330 printf("Error: memory could not be allocated to CRLTOBSint\n");
02331 return 1;
02332 }
02333 DSUNOBSint =(double *)malloc(nTime*sizeof(double));
02334 if(DSUNOBSint == NULL)
02335 {
02336 printf("Error: memory could not be allocated to DSUNOBSint\n");
02337 return 1;
02338 }
02339 CROTA2int =(float *)malloc(nTime*sizeof(float));
02340 if(CROTA2int == NULL)
02341 {
02342 printf("Error: memory could not be allocated to CROTA2int\n");
02343 return 1;
02344 }
02345 RSUNint =(float *)malloc(nTime*sizeof(float));
02346 if(RSUNint == NULL)
02347 {
02348 printf("Error: memory could not be allocated to RSUNint\n");
02349 return 1;
02350 }
02351 CARROTint =(int *)malloc(nTime*sizeof(int));
02352 if(CARROTint == NULL)
02353 {
02354 printf("Error: memory could not be allocated to CARROTint\n");
02355 return 1;
02356 }
02357 source =(char **)malloc(nTime*sizeof(char *));
02358 if(source == NULL)
02359 {
02360 printf("Error: memory could not be allocated to source\n");
02361 return 1;
02362 }
02363 for(i=0;i<nTime;++i)
02364 {
02365 source[i] = (char *)malloc(64000*sizeof(char));
02366 if(source[i] == NULL)
02367 {
02368 printf("Error: memory could not be allocated to source[%d]\n",i);
02369 return 1;
02370 }
02371 strcpy(source[i],HMISeriesLev10);
02372 strcat(source[i],"[:");
02373 }
02374 totalTempIntNum =(int *)malloc(nTime*sizeof(int));
02375 if(totalTempIntNum == NULL)
02376 {
02377 printf("Error: memory could not be allocated to totalTempIntNum\n");
02378 return 1;
02379 }
02380 for(i=0;i<nTime;++i) totalTempIntNum[i]=0;
02381
02382
02383 TOTVALSS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02384 if(TOTVALSS == NULL)
02385 {
02386 printf("Error: memory could not be allocated to TOTVALSS\n");
02387 return 1;
02388 }
02389 DATAVALSS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02390 if(DATAVALSS == NULL)
02391 {
02392 printf("Error: memory could not be allocated to DATAVALSS\n");
02393 return 1;
02394 }
02395 MISSVALSS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02396 if(MISSVALSS == NULL)
02397 {
02398 printf("Error: memory could not be allocated to MISSVALSS\n");
02399 return 1;
02400 }
02401 DATAMINS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02402 if(DATAMINS == NULL)
02403 {
02404 printf("Error: memory could not be allocated to DATAMINS\n");
02405 return 1;
02406 }
02407 DATAMAXS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02408 if(DATAMAXS == NULL)
02409 {
02410 printf("Error: memory could not be allocated to DATAMAXS\n");
02411 return 1;
02412 }
02413 DATAMEDNS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02414 if(DATAMEDNS == NULL)
02415 {
02416 printf("Error: memory could not be allocated to DATAMEDNS\n");
02417 return 1;
02418 }
02419 DATAMEANS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02420 if(DATAMEANS == NULL)
02421 {
02422 printf("Error: memory could not be allocated to DATAMEANS\n");
02423 return 1;
02424 }
02425 DATARMSS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02426 if(DATARMSS == NULL)
02427 {
02428 printf("Error: memory could not be allocated to DATARMSS\n");
02429 return 1;
02430 }
02431 DATASKEWS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02432 if(DATASKEWS == NULL)
02433 {
02434 printf("Error: memory could not be allocated to DATASKEWS\n");
02435 return 1;
02436 }
02437 DATAKURTS =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02438 if(DATAKURTS == NULL)
02439 {
02440 printf("Error: memory could not be allocated to DATAKURTS\n");
02441 return 1;
02442 }
02443 DATAMINS2 =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02444 if(DATAMINS2 == NULL)
02445 {
02446 printf("Error: memory could not be allocated to DATAMINS2\n");
02447 return 1;
02448 }
02449 DATAMAXS2 =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02450 if(DATAMAXS2 == NULL)
02451 {
02452 printf("Error: memory could not be allocated to DATAMAXS2\n");
02453 return 1;
02454 }
02455 DATAMEDNS2 =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02456 if(DATAMEDNS2 == NULL)
02457 {
02458 printf("Error: memory could not be allocated to DATAMEDNS2\n");
02459 return 1;
02460 }
02461 DATAMEANS2 =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02462 if(DATAMEANS2 == NULL)
02463 {
02464 printf("Error: memory could not be allocated to DATAMEANS2\n");
02465 return 1;
02466 }
02467 DATARMSS2 =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02468 if(DATARMSS2 == NULL)
02469 {
02470 printf("Error: memory could not be allocated to DATARMSS2\n");
02471 return 1;
02472 }
02473 DATASKEWS2 =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02474 if(DATASKEWS2 == NULL)
02475 {
02476 printf("Error: memory could not be allocated to DATASKEWS2\n");
02477 return 1;
02478 }
02479 DATAKURTS2 =(char **)malloc(nWavelengths*npolout*sizeof(char *));
02480 if(DATAKURTS2 == NULL)
02481 {
02482 printf("Error: memory could not be allocated to DATAKURTS2\n");
02483 return 1;
02484 }
02485
02486 for(i=0;i<nWavelengths*npolout;i++)
02487 {
02488 TOTVALSS[i] = (char *)malloc(13*sizeof(char));
02489 if(TOTVALSS[i] == NULL)
02490 {
02491 printf("Error: memory could not be allocated to TOTVALSS[%d]\n",i);
02492 return 1;
02493 }
02494 strcpy(TOTVALSS[i],"TOTVALS[");
02495 sprintf(query,"%d",i);
02496 strcat(TOTVALSS[i],query);
02497 strcat(TOTVALSS[i],"]");
02498
02499 MISSVALSS[i] = (char *)malloc(13*sizeof(char));
02500 if(MISSVALSS[i] == NULL)
02501 {
02502 printf("Error: memory could not be allocated to MISSVALSS[%d]\n",i);
02503 return 1;
02504 }
02505 strcpy(MISSVALSS[i],"MISSVALS[");
02506 sprintf(query,"%d",i);
02507 strcat(MISSVALSS[i],query);
02508 strcat(MISSVALSS[i],"]");
02509
02510 DATAVALSS[i] = (char *)malloc(13*sizeof(char));
02511 if(DATAVALSS[i] == NULL)
02512 {
02513 printf("Error: memory could not be allocated to DATAVALSS[%d]\n",i);
02514 return 1;
02515 }
02516 strcpy(DATAVALSS[i],"DATAVALS[");
02517 sprintf(query,"%d",i);
02518 strcat(DATAVALSS[i],query);
02519 strcat(DATAVALSS[i],"]");
02520
02521 DATAMEANS[i] = (char *)malloc(13*sizeof(char));
02522 if(DATAMEANS[i] == NULL)
02523 {
02524 printf("Error: memory could not be allocated to DATAMEANS[%d]\n",i);
02525 return 1;
02526 }
02527 strcpy(DATAMEANS[i],"DATAMEA2[");
02528 sprintf(query,"%d",i);
02529 strcat(DATAMEANS[i],query);
02530 strcat(DATAMEANS[i],"]");
02531
02532 DATAMINS[i] = (char *)malloc(13*sizeof(char));
02533 if(DATAMINS[i] == NULL)
02534 {
02535 printf("Error: memory could not be allocated to DATAMINS[%d]\n",i);
02536 return 1;
02537 }
02538 strcpy(DATAMINS[i],"DATAMIN2[");
02539 sprintf(query,"%d",i);
02540 strcat(DATAMINS[i],query);
02541 strcat(DATAMINS[i],"]");
02542
02543 DATAMAXS[i] = (char *)malloc(13*sizeof(char));
02544 if(DATAMAXS[i] == NULL)
02545 {
02546 printf("Error: memory could not be allocated to DATAMAXS[%d]\n",i);
02547 return 1;
02548 }
02549 strcpy(DATAMAXS[i],"DATAMAX2[");
02550 sprintf(query,"%d",i);
02551 strcat(DATAMAXS[i],query);
02552 strcat(DATAMAXS[i],"]");
02553
02554
02555 DATAMEDNS[i] = (char *)malloc(13*sizeof(char));
02556 if(DATAMEDNS[i] == NULL)
02557 {
02558 printf("Error: memory could not be allocated to DATAMEDNS[%d]\n",i);
02559 return 1;
02560 }
02561 strcpy(DATAMEDNS[i],"DATAMED2[");
02562 sprintf(query,"%d",i);
02563 strcat(DATAMEDNS[i],query);
02564 strcat(DATAMEDNS[i],"]");
02565
02566 DATARMSS[i] = (char *)malloc(13*sizeof(char));
02567 if(DATARMSS[i] == NULL)
02568 {
02569 printf("Error: memory could not be allocated to DATARMSS[%d]\n",i);
02570 return 1;
02571 }
02572 strcpy(DATARMSS[i],"DATARMS2[");
02573 sprintf(query,"%d",i);
02574 strcat(DATARMSS[i],query);
02575 strcat(DATARMSS[i],"]");
02576
02577 DATASKEWS[i] = (char *)malloc(13*sizeof(char));
02578 if(DATASKEWS[i] == NULL)
02579 {
02580 printf("Error: memory could not be allocated to DATASKEWS[%d]\n",i);
02581 return 1;
02582 }
02583 strcpy(DATASKEWS[i],"DATASKE2[");
02584 sprintf(query,"%d",i);
02585 strcat(DATASKEWS[i],query);
02586 strcat(DATASKEWS[i],"]");
02587
02588 DATAKURTS[i] = (char *)malloc(13*sizeof(char));
02589 if(DATAKURTS[i] == NULL)
02590 {
02591 printf("Error: memory could not be allocated to DATAKURTS[%d]\n",i);
02592 return 1;
02593 }
02594 strcpy(DATAKURTS[i],"DATAKUR2[");
02595 sprintf(query,"%d",i);
02596 strcat(DATAKURTS[i],query);
02597 strcat(DATAKURTS[i],"]");
02598
02599
02600 DATAMINS2[i] = (char *)malloc(13*sizeof(char));
02601 if(DATAMINS2[i] == NULL)
02602 {
02603 printf("Error: memory could not be allocated to DATAMINS2[%d]\n",i);
02604 return 1;
02605 }
02606 strcpy(DATAMINS2[i],"DATAMIN[");
02607 sprintf(query,"%d",i);
02608 strcat(DATAMINS2[i],query);
02609 strcat(DATAMINS2[i],"]");
02610
02611 DATAMAXS2[i] = (char *)malloc(13*sizeof(char));
02612 if(DATAMAXS2[i] == NULL)
02613 {
02614 printf("Error: memory could not be allocated to DATAMAXS2[%d]\n",i);
02615 return 1;
02616 }
02617 strcpy(DATAMAXS2[i],"DATAMAX[");
02618 sprintf(query,"%d",i);
02619 strcat(DATAMAXS2[i],query);
02620 strcat(DATAMAXS2[i],"]");
02621
02622 DATAMEANS2[i] = (char *)malloc(13*sizeof(char));
02623 if(DATAMEANS2[i] == NULL)
02624 {
02625 printf("Error: memory could not be allocated to DATAMEANS2[%d]\n",i);
02626 return 1;
02627 }
02628 strcpy(DATAMEANS2[i],"DATAMEAN[");
02629 sprintf(query,"%d",i);
02630 strcat(DATAMEANS2[i],query);
02631 strcat(DATAMEANS2[i],"]");
02632
02633 DATAMEDNS2[i] = (char *)malloc(13*sizeof(char));
02634 if(DATAMEDNS2[i] == NULL)
02635 {
02636 printf("Error: memory could not be allocated to DATAMEDNS2[%d]\n",i);
02637 return 1;
02638 }
02639 strcpy(DATAMEDNS2[i],"DATAMEDN[");
02640 sprintf(query,"%d",i);
02641 strcat(DATAMEDNS2[i],query);
02642 strcat(DATAMEDNS2[i],"]");
02643
02644 DATARMSS2[i] = (char *)malloc(13*sizeof(char));
02645 if(DATARMSS2[i] == NULL)
02646 {
02647 printf("Error: memory could not be allocated to DATARMSS2[%d]\n",i);
02648 return 1;
02649 }
02650 strcpy(DATARMSS2[i],"DATARMS[");
02651 sprintf(query,"%d",i);
02652 strcat(DATARMSS2[i],query);
02653 strcat(DATARMSS2[i],"]");
02654
02655 DATASKEWS2[i] = (char *)malloc(13*sizeof(char));
02656 if(DATASKEWS2[i] == NULL)
02657 {
02658 printf("Error: memory could not be allocated to DATASKEWS2[%d]\n",i);
02659 return 1;
02660 }
02661 strcpy(DATASKEWS2[i],"DATASKEW[");
02662 sprintf(query,"%d",i);
02663 strcat(DATASKEWS2[i],query);
02664 strcat(DATASKEWS2[i],"]");
02665
02666 DATAKURTS2[i] = (char *)malloc(13*sizeof(char));
02667 if(DATAKURTS2[i] == NULL)
02668 {
02669 printf("Error: memory could not be allocated to DATAKURTS2[%d]\n",i);
02670 return 1;
02671 }
02672 strcpy(DATAKURTS2[i],"DATAKURT[");
02673 sprintf(query,"%d",i);
02674 strcat(DATAKURTS2[i],query);
02675 strcat(DATAKURTS2[i],"]");
02676
02677 }
02678
02679
02680 imagesi = (float **)malloc(TempIntNum*sizeof(float *));
02681 if(imagesi == NULL)
02682 {
02683 printf("Error: memory could not be allocated to imagesi\n");
02684 return 1;
02685 }
02686
02687 ierrors = (char **)malloc(TempIntNum*sizeof(char *));
02688 if(ierrors == NULL)
02689 {
02690 printf("Error: memory could not be allocated to ierrors\n");
02691 return 1;
02692 }
02693
02694 KeyInterp = (struct keyword *)malloc(TempIntNum*sizeof(struct keyword));
02695 if(KeyInterp == NULL)
02696 {
02697 printf("Error: memory could not be allocated to KeyInterp\n");
02698 return 1;
02699 }
02700
02701
02702 images = (float **)malloc(Npolin*sizeof(float *));
02703 if(images == NULL)
02704 {
02705 printf("Error: memory could not be allocated to images\n");
02706 return 1;
02707 }
02708
02709 imagesout = (float **)malloc(npolout*sizeof(float *));
02710 if(imagesout == NULL)
02711 {
02712 printf("Error: memory could not be allocated to imagesout\n");
02713 return 1;
02714 }
02715
02716 ps1 = (int *)malloc(Npolin*sizeof(int *));
02717 if(ps1 == NULL)
02718 {
02719 printf("Error: memory could not be allocated to ps1\n");
02720 return 1;
02721 }
02722 ps2 = (int *)malloc(Npolin*sizeof(int *));
02723 if(ps2 == NULL)
02724 {
02725 printf("Error: memory could not be allocated to ps2\n");
02726 return 1;
02727 }
02728 ps3 = (int *)malloc(Npolin*sizeof(int *));
02729 if(ps3 == NULL)
02730 {
02731 printf("Error: memory could not be allocated to ps3\n");
02732 return 1;
02733 }
02734
02735
02736
02737
02738
02739
02740
02741
02742
02743
02744 initialrun=1;
02745
02746 for(it=0;it<nWavelengths;++it)
02747 {
02748 printf("----------------------------------------------------------------------------------------\n");
02749 printf("CURRENT WAVELENGTH/FILTER NUMBER = %d\n",it);
02750 printf("----------------------------------------------------------------------------------------\n");
02751
02752
02753
02754
02755
02756
02757
02758
02759
02760 temptime = (TIME)floor((TimeBegin-TREC_EPOCH+TREC_STEP/2.0)/TREC_STEP)*TREC_STEP+TREC_EPOCH;
02761 if(temptime < TimeBegin) temptime += TREC_STEP;
02762 TargetTime = temptime;
02763 timeindex = 0;
02764 PreviousTargetTime=TargetTime;
02765
02766 while(TargetTime <= TimeEnd)
02767 {
02768
02769
02770
02771 if(inRotationalFlat == 1)
02772 {
02773
02774
02775 if(floor(TargetTime/86400.0) != floor(PreviousTargetTime/86400.0) )
02776 {
02777 printf("Error: the new target time is for a different day than the previous target time: you are not allowed to change day when applying a rotational flat field\n");
02778 return 1;
02779 }
02780
02781
02782 if(initialrun != 1)
02783 {
02784 if(TargetTime < TSTARTFLAT || TargetTime > TSTOPFLAT)
02785 {
02786 printf("Error: the target time is not within the time range for which the rotation flat field used is valid\n");
02787 return 1;
02788 }
02789 }
02790
02791 }
02792
02793
02794 sprint_time(timeBegin2,TargetTime,"TAI",0);
02795 printf("TARGET TIME= %s %f\n",timeBegin2,TargetTime);
02796
02797 if(nIndexFiltergram == 0)
02798 {
02799 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_TARGETFILTERGRAMMISSING;
02800 CreateEmptyRecord = 1;
02801 goto NextTargetTime;
02802 }
02803
02804
02805
02806 arrLev1d = (DRMS_Array_t **)malloc(Npolin*sizeof(DRMS_Array_t *));
02807 if(arrLev1d == NULL)
02808 {
02809 printf("Error: memory could not be allocated to arrLev1d\n");
02810 return 1;
02811 }
02812
02813 for(k=0;k<Npolin;++k)
02814 {
02815 arrLev1d[k] = drms_array_create(type1d,2,axisout,NULL,&status);
02816 if(status != DRMS_SUCCESS || arrLev1d[k] == NULL)
02817 {
02818 printf("Error: cannot create a DRMS array for a level 1d filtergram with index %d at target time %s\n",k,timeBegin2);
02819 return 1;
02820 }
02821 }
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831
02832
02833 for(it2=0;it2<Npolin;++it2)
02834 {
02835
02836
02837 printf("CURRENT POLARIZATION INDEX = %d\n",it2);
02838 printf("--------------------------------\n");
02839 printf("QUALITY KEYWORD VALUE: %d\n",QUALITY[timeindex]);
02840
02841 if(recLev1->n == 0)
02842 {
02843 CreateEmptyRecord=1;
02844 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_TARGETFILTERGRAMMISSING;
02845 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NODATA;
02846 goto NextTargetTime;
02847 }
02848
02849
02850
02851
02852 temptime = 100000000.0;
02853 i=0;
02854 while(fabs(internTOBS[IndexFiltergram[i]]-TargetTime) <= temptime)
02855 {
02856
02857 temptime=fabs(internTOBS[IndexFiltergram[i]]-TargetTime);
02858 if(i <= nIndexFiltergram-2) ++i;
02859 else break;
02860 }
02861 if(temptime > DataCadence/2.0 )
02862 {
02863 printf("Error: could not find a filtergram with wavelength %d at time %s\n",WavelengthID,timeBegin2);
02864 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_TARGETFILTERGRAMMISSING;
02865 CreateEmptyRecord=1; goto NextTargetTime;
02866 }
02867 TargetWavelength= i-1;
02868 temp = IndexFiltergram[TargetWavelength];
02869
02870
02871 if(TargetTime > 1145850460.469931 && TargetTime < 1146422026.006866)
02872 {
02873 printf("Warning: target time is within 6 days of the accidental reboot of HMI on April 24, 2013\n");
02874 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
02875 }
02876
02877
02878 if((QUALITYin[temp] & Q_ACS_ISSLOOP) == Q_ACS_ISSLOOP)
02879 {
02880 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_ISSTARGET ;
02881 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
02882 }
02883 if((QUALITYin[temp] & Q_ACS_ECLP) == Q_ACS_ECLP)
02884 {
02885 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_ECLIPSE;
02886 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
02887 }
02888 if((QUALITYin[temp] & Q_ACS_NOLIMB) == Q_ACS_NOLIMB) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LIMBFITISSUE;
02889 if((QUALITYin[temp] & Q_ACS_LUNARTRANSIT) == Q_ACS_LUNARTRANSIT) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
02890 if((QUALITYin[temp] & Q_ACS_THERMALRECOVERY) == Q_ACS_THERMALRECOVERY) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
02891 if((QUALITYin[temp] & Q_CAMERA_ANOMALY) == Q_CAMERA_ANOMALY) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
02892 if(isnan(X0[temp]) || isnan(Y0[temp]))
02893 {
02894 printf("Error: target filtergram FSN[%d] does not have valid X0_LF and/or Y0_LF keywords\n",FSN[temp]);
02895 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LIMBFITISSUE;
02896 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_TARGETFILTERGRAMMISSING;
02897 CreateEmptyRecord=1; goto NextTargetTime;
02898 }
02899
02900 TargetHFLID = HFLID[temp];
02901 if(TargetHFLID >= 4000) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LARGEFTSID;
02902 TargetHWLPOS[0] = HWL1POS[temp];
02903 TargetHWLPOS[1] = HWL2POS[temp];
02904 TargetHWLPOS[2] = HWL3POS[temp];
02905 TargetHWLPOS[3] = HWL4POS[temp];
02906 TargetHPLPOS[0] = HPL1POS[temp];
02907 TargetHPLPOS[1] = HPL2POS[temp];
02908 TargetHPLPOS[2] = HPL3POS[temp];
02909 TargetHWLTID = HWLTID[temp];
02910 TargetHPLTID = HPLTID[temp];
02911 TargetCFINDEX = CFINDEX[temp];
02912 strcpy(TargetISS,HWLTNSET[temp]);
02913 if(!strcmp(TargetISS,"OPEN")) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_ISSTARGET;
02914
02915
02916
02917
02918
02919
02920 if(inRotationalFlat == 1)
02921 {
02922
02923 HMIFlatField = (char *)malloc(MaxNString*sizeof(char *));
02924 if(HMIFlatField == NULL)
02925 {
02926 printf("Error: memory could not be allocated to HMIFlatField\n");
02927 return 1;
02928 }
02929
02930 HMIFlatField = drms_getkey_string(recLev1->records[temp],FLATREC,&status);
02931 if (status != DRMS_SUCCESS)
02932 {
02933 printf("Error: could not read the FLAT_REC keyword for the target filtergram FSN= %d",FSN[temp]);
02934 return 1;
02935 }
02936 else
02937 {
02938 printf("PZT FLAT FIELD USED ON TARGET FILTERGRAM= %s\n",HMIFlatField);
02939 if(initialrun == 1)
02940 {
02941 strcpy(HMIFlatField0,HMIFlatField);
02942
02943
02944 recflat = drms_open_records(drms_env,HMIFlatField,&status);
02945 if (status != DRMS_SUCCESS || recflat == NULL || recflat->n == 0)
02946 {
02947 printf("Error: record missing or corrupt for the flat field query %s\n",HMIFlatField);
02948 return 1;
02949 }
02950 segin = drms_segment_lookup(recflat->records[0],"flatfield");
02951 flatfield = drms_segment_read(segin,type1d,&status);
02952 if (status != DRMS_SUCCESS || flatfield == NULL)
02953 {
02954 printf("Error: could not read the data segment for the flat field query %s\n",HMIFlatField);
02955 return 1;
02956 }
02957 pztflat = flatfield->data;
02958 status=drms_close_records(recflat,DRMS_FREE_RECORD);
02959
02960
02961 char keylist[]="T_START,T_STOP";
02962 int unique = 0;
02963 rotationalflats = drms_record_getvector(drms_env,HMIRotationalFlats,keylist,DRMS_TYPE_DOUBLE,unique,&status);
02964 if(status != DRMS_SUCCESS)
02965 {
02966 printf("Error: cannot read a list of keywords in the rotation flat-field series\n");
02967 return 1;
02968 }
02969 printf("DIMENSIONS OF ROTATIONAL FLAT-FIELD SERIES= %d %d\n",rotationalflats->axis[0],rotationalflats->axis[1]);
02970 int n0,n1;
02971 n1 =rotationalflats->axis[1];
02972 n0 =rotationalflats->axis[0];
02973 keyF=rotationalflats->data;
02974
02975 i=0;
02976 while(TargetTime > keyF[i] && TargetTime > keyF[n1+i])
02977 {
02978 i++;
02979 }
02980
02981 if(TargetTime >= keyF[i] && TargetTime <= keyF[n1+i])
02982 {
02983
02984 TSTARTFLAT=keyF[i];
02985 TSTOPFLAT =keyF[n1+i];
02986 sprint_time(query,keyF[i],"TAI",1);
02987 strcpy(QueryFlatField,HMIRotationalFlats);
02988 strcat(QueryFlatField,"[");
02989 if(CamId == LIGHT_SIDE) strcat(QueryFlatField,"1");
02990 if(CamId == LIGHT_FRONT) strcat(QueryFlatField,"2");
02991 strcat(QueryFlatField,"][");
02992 strcat(QueryFlatField,query);
02993 strcat(QueryFlatField,"]");
02994 printf("QUERY FOR ROTATIONAL FLAT FIELD= %s\n",QueryFlatField);
02995
02996
02997 recflatrot = drms_open_records(drms_env,QueryFlatField,&status);
02998 if (status != DRMS_SUCCESS || recflatrot == NULL || recflatrot->n == 0)
02999 {
03000 printf("Error: record missing or corrupt for the rotational flat field query %s\n",QueryFlatField);
03001 return 1;
03002 }
03003 segin = drms_segment_lookup(recflatrot->records[0],"flatfield");
03004 flatfieldrot = drms_segment_read(segin,type1d,&status);
03005 if (status != DRMS_SUCCESS || flatfieldrot == NULL)
03006 {
03007 printf("Error: could not read the data segment for the rotational flat field query %s\n",QueryFlatField);
03008 return 1;
03009 }
03010 rotflat = flatfieldrot->data;
03011
03012 }
03013 else
03014 {
03015 printf("Error: no rotational flat field record exists for the target time %s\n",timeBegin2);
03016 return 1;
03017 }
03018
03019 drms_free_array(rotationalflats);
03020 rotationalflats=NULL;
03021 }
03022 else if(strcmp(HMIFlatField,HMIFlatField0) != 0)
03023 {
03024 printf("Warning: the hmi.flatfield record used to produce the level 1 records changed during the run of the observables code\n");
03025 printf("The new hmi,flatfield record used is: %s\n",HMIFlatField);
03026
03027 recflat = drms_open_records(drms_env,HMIFlatField,&status);
03028 if (status != DRMS_SUCCESS || recflat == NULL || recflat->n == 0)
03029 {
03030 printf("Error: record missing or corrupt for the flat field query %s\n",HMIFlatField);
03031 return 1;
03032 }
03033 drms_free_array(flatfield);
03034 strcpy(HMIFlatField0,HMIFlatField);
03035 segin = drms_segment_lookup(recflat->records[0],"flatfield");
03036 flatfield = drms_segment_read(segin,type1d,&status);
03037 if (status != DRMS_SUCCESS || flatfield == NULL)
03038 {
03039 printf("Error: could not read the data segment for the flat field query %s\n",HMIFlatField);
03040 return 1;
03041 }
03042 pztflat = flatfield->data;
03043 status=drms_close_records(recflat,DRMS_FREE_RECORD);
03044 }
03045
03046 }
03047 free(HMIFlatField);
03048 }
03049
03050
03051
03052
03053
03054
03055 framelistSize = framelistInfo(TargetHFLID,TargetHPLTID,TargetHWLTID,WavelengthID,PHWPLPOS,WavelengthIndex,WavelengthLocation,&PolarizationType,CamId,&combine,&npol,MaxNumFiltergrams,&CadenceRead,CameraValues,FIDValues,dpath);
03056 if(framelistSize == 1) return 1;
03057
03058 if(framelistSize != Framelistsizein)
03059 {
03060 printf("Error: the current framelist does not match what is expected from the command line, at target time %s\n",timeBegin2);
03061 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_WRONGFRAMELISTSIZE;
03062 CreateEmptyRecord=1; goto NextTargetTime;
03063 }
03064 if(CadenceRead != DataCadence)
03065 {
03066 printf("Error: the cadence from the current framelist is %f and does not match the cadence entered on the command line %f, at target time %s\n",CadenceRead,DataCadence,timeBegin2);
03067 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_WRONGCADENCE;
03068 CreateEmptyRecord=1; goto NextTargetTime;
03069 }
03070 if(PolarizationType != 1)
03071 {
03072 printf("Error: this program produces only I,Q,U, and V data and does not work on LCP+RCP data \n");
03073 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_WRONGPOLTYPE;
03074 CreateEmptyRecord=1; goto NextTargetTime;
03075 }
03076 if(npol != Npolin)
03077 {
03078 printf("Error: npol does not match the value entered on the command line, at target time %s\n",timeBegin2);
03079 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_WRONGNPOL;
03080 CreateEmptyRecord=1; goto NextTargetTime;
03081 }
03082
03083 k=0;
03084 for(i=0;i<Framelistsizein;++i) if(WavelengthIndex[i] == it)
03085 {
03086
03087 PolarizationArray[0][k]=PHWPLPOS[i*7+4];
03088 PolarizationArray[1][k]=PHWPLPOS[i*7+5];
03089 PolarizationArray[2][k]=PHWPLPOS[i*7+6];
03090
03091 WavelengthArray[0] =PHWPLPOS[i*7+0];
03092 WavelengthArray[1] =PHWPLPOS[i*7+1];
03093 WavelengthArray[2] =PHWPLPOS[i*7+2];
03094 WavelengthArray[3] =PHWPLPOS[i*7+3];
03095 k+=1;
03096 }
03097 if(k != Npolin)
03098 {
03099 printf("Error: k is different from NpolIn %d %d\n",k,Npolin);
03100 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_WRONGNPOL;
03101 CreateEmptyRecord=1; goto NextTargetTime;
03102 }
03103
03104
03105
03106
03107
03108
03109
03110
03111
03112 ii=-1;
03113 for(i=0;i<framelistSize;++i) if(PHWPLPOS[i*7+0] == TargetHWLPOS[0] && PHWPLPOS[i*7+1] == TargetHWLPOS[1] && PHWPLPOS[i*7+2] == TargetHWLPOS[2] && PHWPLPOS[i*7+3] == TargetHWLPOS[3] && PHWPLPOS[i*7+4] == TargetHPLPOS[0] && PHWPLPOS[i*7+5] == TargetHPLPOS[1] && PHWPLPOS[i*7+6] == TargetHPLPOS[2]) ii=i;
03114 if(ii == -1)
03115 {
03116 printf("Error: the target filtergram %d %d %d %d does not match any frame of the corresponding framelist\n",WavelengthID,TargetHPLPOS[0],TargetHPLPOS[1],TargetHPLPOS[2]);
03117 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_WRONGTARGET;
03118 CreateEmptyRecord=1; goto NextTargetTime;
03119 }
03120 IndexTargetFiltergramId=WavelengthLocation[ii];
03121
03122 for(i=0;i<framelistSize;++i) if(WavelengthIndex[i] == it && PHWPLPOS[i*7+4] == PolarizationArray[0][it2] && PHWPLPOS[i*7+5] == PolarizationArray[1][it2] && PHWPLPOS[i*7+6] == PolarizationArray[2][it2])
03123 {
03124 OrganizeFramelist = WavelengthLocation[i]-IndexTargetFiltergramId;
03125 OrganizeFramelist2 = signj(OrganizeFramelist);
03126 break;
03127 }
03128 if(WavelengthIndex[i] == WavelengthID)
03129 {
03130 if(PHWPLPOS[i*7+4] == TargetHPLPOS[0] && PHWPLPOS[i*7+5] == TargetHPLPOS[1] && PHWPLPOS[i*7+6] == TargetHPLPOS[2])
03131 {
03132 OrganizeFramelist = 0;
03133 OrganizeFramelist2 = 0;
03134 }
03135 else if(abs(WavelengthLocation[i]-IndexTargetFiltergramId) <= 3)
03136 {
03137 OrganizeFramelist = 0;
03138 OrganizeFramelist2 = 0;
03139 }
03140 }
03141 printf("GROUPING OF FILTERGRAMS: %d %d\n",OrganizeFramelist,OrganizeFramelist2);
03142
03143
03144
03145
03146
03147
03148
03149 printf("CURRENT TARGET FILTERGRAM INFORMATION. Index = %d; FSN = %d; %d %d %d %d %d %d %d \n",temp,FSN[temp],HWL1POS[temp],HWL2POS[temp],HWL3POS[temp],HWL4POS[temp],HPL1POS[temp],HPL2POS[temp],HPL3POS[temp]);
03150
03151
03152 ii=-1;
03153 for(i=0;i<framelistSize;++i) if(WavelengthIndex[i] == it && PHWPLPOS[i*7+4] == PolarizationArray[0][it2] && PHWPLPOS[i*7+5] == PolarizationArray[1][it2] && PHWPLPOS[i*7+6] == PolarizationArray[2][it2] ) ii=i;
03154 if(ii == -1)
03155 {
03156 printf("Error: the filtergram %d %d %d %d does not match any frame of the corresponding framelist\n",WavelengthID,PolarizationArray[0][it2],PolarizationArray[1][it2],PolarizationArray[2][it2]);
03157 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_ERRORFRAMELIST;
03158 CreateEmptyRecord=1; goto NextTargetTime;
03159 }
03160
03161
03162 camera=CameraValues[ii];
03163 fidfilt=FIDValues[ii];
03164
03165
03166 FramelistArray[0] =WavelengthLocation[ii]-IndexTargetFiltergramId+temp;
03167 FiltergramLocation=FramelistArray[0];
03168
03169
03170 if(KeywordMissing[FiltergramLocation] == 1)
03171 {
03172 FramelistArray[0]=-1;
03173 printf("Error: the filtergram index %d has missing keywords and the code cannot identify it",FiltergramLocation);
03174 }
03175 else
03176 {
03177 if(HWL1POS[FiltergramLocation] != WavelengthArray[0] || HWL2POS[FiltergramLocation] != WavelengthArray[1] || HWL3POS[FiltergramLocation] != WavelengthArray[2] || HWL4POS[FiltergramLocation] != WavelengthArray[3] || HPL1POS[FiltergramLocation] != PolarizationArray[0][it2] || HPL2POS[FiltergramLocation] != PolarizationArray[1][it2] || HPL3POS[FiltergramLocation] != PolarizationArray[2][it2] || CFINDEX[FiltergramLocation] != TargetCFINDEX || HCAMID[FiltergramLocation] != camera)
03178 {
03179 printf("Warning: filtergram FSN= %d near the target location is not what it should be. Looking for the correct filtergram\n",FSN[FiltergramLocation]);
03180 FiltergramLocation=temp;
03181 for(ii=1;ii<framelistSize;++ii)
03182 {
03183 if(OrganizeFramelist > 0)
03184 {
03185 k=FiltergramLocation+ii;
03186 if(k < nRecs1 && KeywordMissing[k] != 1)
03187 {
03188 if(HWL1POS[k] == WavelengthArray[0] && HWL2POS[k] == WavelengthArray[1] && HWL3POS[k] == WavelengthArray[2] && HWL4POS[k] == WavelengthArray[3] && HPL1POS[k] == PolarizationArray[0][it2] && HPL2POS[k] == PolarizationArray[1][it2] && HPL3POS[k] == PolarizationArray[2][it2] && CFINDEX[k] == TargetCFINDEX && HCAMID[k] == camera) break;
03189 }
03190 }
03191 else
03192 {
03193 k=FiltergramLocation-ii;
03194 if(k > 0 && KeywordMissing[k] != 1)
03195 {
03196 if(HWL1POS[k] == WavelengthArray[0] && HWL2POS[k] == WavelengthArray[1] && HWL3POS[k] == WavelengthArray[2] && HWL4POS[k] == WavelengthArray[3] && HPL1POS[k] == PolarizationArray[0][it2] && HPL2POS[k] == PolarizationArray[1][it2] && HPL3POS[k] == PolarizationArray[2][it2] && CFINDEX[k] == TargetCFINDEX && HCAMID[k] == camera) break;
03197 }
03198 }
03199 }
03200 if(ii == framelistSize)
03201 {
03202 FramelistArray[0]=-1;
03203
03204 ii=-1;
03205 for(i=0;i<framelistSize;++i) if(WavelengthIndex[i] == it && PHWPLPOS[i*7+4] == PolarizationArray[0][it2] && PHWPLPOS[i*7+5] == PolarizationArray[1][it2] && PHWPLPOS[i*7+6] == PolarizationArray[2][it2] ) ii=i;
03206 FiltergramLocation=WavelengthLocation[ii]-IndexTargetFiltergramId+temp;
03207 printf("Error: the filtergram FSN = %d is not what it should be\n",FSN[FiltergramLocation]);
03208 }
03209 else
03210 {
03211 FramelistArray[0]=k;
03212 FiltergramLocation=k;
03213 }
03214 }
03215 }
03216
03217
03218
03219
03220
03221 if(OrganizeFramelist2 <= 0)
03222 {
03223
03224 k=FiltergramLocation-1;
03225 for(ii=1;ii<=TempIntNum/2-1;++ii)
03226 {
03227 while(k > 0)
03228 {
03229 if(KeywordMissing[k] != 1)
03230 {
03231 if(HWL1POS[k] != WavelengthArray[0] || HWL2POS[k] != WavelengthArray[1] || HWL3POS[k] != WavelengthArray[2] || HWL4POS[k] != WavelengthArray[3] || HPL1POS[k] != PolarizationArray[0][it2] || HPL2POS[k] != PolarizationArray[1][it2] || HPL3POS[k] != PolarizationArray[2][it2] || HCAMID[k] != camera || CFINDEX[k] != TargetCFINDEX)
03232 {
03233 if ((internTOBS[k]-TargetTime) >= -MaxSearchDistanceL) --k;
03234 else break;
03235 }
03236 else break;
03237 }
03238 else --k;
03239 }
03240 if(k < 0)
03241 {
03242 FramelistArray[ii]=-1;
03243 continue;
03244 }
03245 if(KeywordMissing[k] != 1)
03246 {
03247 if(HWL1POS[k] == WavelengthArray[0] && HWL2POS[k] == WavelengthArray[1] && HWL3POS[k] == WavelengthArray[2] && HWL4POS[k] == WavelengthArray[3] && HPL1POS[k] == PolarizationArray[0][it2] && HPL2POS[k] == PolarizationArray[1][it2] && HPL3POS[k] == PolarizationArray[2][it2] && HCAMID[k] == camera && CFINDEX[k] == TargetCFINDEX && (internTOBS[k]-TargetTime) >= -MaxSearchDistanceL)
03248 {
03249 FramelistArray[ii]=k;
03250 }
03251 else FramelistArray[ii]=-1;
03252 }
03253 else FramelistArray[ii]=-1;
03254 --k;
03255 }
03256 k=FiltergramLocation+1;
03257 for(ii=TempIntNum/2;ii<TempIntNum;++ii)
03258 {
03259 while(k < nRecs1-1)
03260 {
03261 if(KeywordMissing[k] != 1)
03262 {
03263 if(HWL1POS[k] != WavelengthArray[0] || HWL2POS[k] != WavelengthArray[1] || HWL3POS[k] != WavelengthArray[2] || HWL4POS[k] != WavelengthArray[3] || HPL1POS[k] != PolarizationArray[0][it2] || HPL2POS[k] != PolarizationArray[1][it2] || HPL3POS[k] != PolarizationArray[2][it2] || HCAMID[k] != camera || CFINDEX[k] != TargetCFINDEX)
03264 {
03265 if ((internTOBS[k]-TargetTime) <= MaxSearchDistanceR) ++k;
03266 else break;
03267 }
03268 else break;
03269 }
03270 else ++k;
03271 }
03272 if(k > nRecs1-1)
03273 {
03274 FramelistArray[ii]=-1;
03275 continue;
03276 }
03277 if(KeywordMissing[k] != 1)
03278 {
03279 if(HWL1POS[k] == WavelengthArray[0] && HWL2POS[k] == WavelengthArray[1] && HWL3POS[k] == WavelengthArray[2] && HWL4POS[k] == WavelengthArray[3] && HPL1POS[k] == PolarizationArray[0][it2] && HPL2POS[k] == PolarizationArray[1][it2] && HPL3POS[k] == PolarizationArray[2][it2] && HCAMID[k] == camera && CFINDEX[k] == TargetCFINDEX && (internTOBS[k]-TargetTime) <= MaxSearchDistanceR)
03280 {
03281 FramelistArray[ii]=k;
03282 }
03283 else FramelistArray[ii]=-1;
03284 }
03285 else FramelistArray[ii]=-1;
03286 ++k;
03287 }
03288 }
03289 else
03290 {
03291
03292 k=FiltergramLocation-1;
03293 for(ii=1;ii<=TempIntNum/2;++ii)
03294 {
03295 while(k > 0)
03296 {
03297 if(KeywordMissing[k] != 1)
03298 {
03299 if(HWL1POS[k] != WavelengthArray[0] || HWL2POS[k] != WavelengthArray[1] || HWL3POS[k] != WavelengthArray[2] || HWL4POS[k] != WavelengthArray[3] || HPL1POS[k] != PolarizationArray[0][it2] || HPL2POS[k] != PolarizationArray[1][it2] || HPL3POS[k] != PolarizationArray[2][it2] || HCAMID[k] != camera || CFINDEX[k] != TargetCFINDEX)
03300 {
03301 if ((internTOBS[k]-TargetTime) >= -MaxSearchDistanceR) --k;
03302 else break;
03303 }
03304 else break;
03305 }
03306 else --k;
03307 }
03308 if(k < 0)
03309 {
03310 FramelistArray[ii]=-1;
03311 continue;
03312 }
03313 if(KeywordMissing[k] != 1)
03314 {
03315 if(HWL1POS[k] == WavelengthArray[0] && HWL2POS[k] == WavelengthArray[1] && HWL3POS[k] == WavelengthArray[2] && HWL4POS[k] == WavelengthArray[3] && HPL1POS[k] == PolarizationArray[0][it2] && HPL2POS[k] == PolarizationArray[1][it2] && HPL3POS[k] == PolarizationArray[2][it2] && HCAMID[k] == camera && CFINDEX[k] == TargetCFINDEX && (internTOBS[k]-TargetTime) >= -MaxSearchDistanceR)
03316 {
03317 FramelistArray[ii]=k;
03318 }
03319 else FramelistArray[ii]=-1;
03320 }
03321 else FramelistArray[ii]=-1;
03322 --k;
03323 }
03324 k=FiltergramLocation+1;
03325 for(ii=TempIntNum/2+1;ii<TempIntNum;++ii)
03326 {
03327 while(k < nRecs1-1)
03328 {
03329 if(KeywordMissing[k] != 1)
03330 {
03331 if(HWL1POS[k] != WavelengthArray[0] || HWL2POS[k] != WavelengthArray[1] || HWL3POS[k] != WavelengthArray[2] || HWL4POS[k] != WavelengthArray[3] || HPL1POS[k] != PolarizationArray[0][it2] || HPL2POS[k] != PolarizationArray[1][it2] || HPL3POS[k] != PolarizationArray[2][it2] || HCAMID[k] != camera || CFINDEX[k] != TargetCFINDEX)
03332 {
03333 if ((internTOBS[k]-TargetTime) <= MaxSearchDistanceL) ++k;
03334 else break;
03335 }
03336 else break;
03337 }
03338 else ++k;
03339 }
03340 if(k > nRecs1-1)
03341 {
03342 FramelistArray[ii]=-1;
03343 continue;
03344 }
03345 if(KeywordMissing[k] != 1)
03346 {
03347 if(HWL1POS[k] == WavelengthArray[0] && HWL2POS[k] == WavelengthArray[1] && HWL3POS[k] == WavelengthArray[2] && HWL4POS[k] == WavelengthArray[3] && HPL1POS[k] == PolarizationArray[0][it2] && HPL2POS[k] == PolarizationArray[1][it2] && HPL3POS[k] == PolarizationArray[2][it2] && HCAMID[k] == camera && CFINDEX[k] == TargetCFINDEX && (internTOBS[k]-TargetTime) <= MaxSearchDistanceL)
03348 {
03349 FramelistArray[ii]=k;
03350 }
03351 else FramelistArray[ii]=-1;
03352 }
03353 else FramelistArray[ii]=-1;
03354 ++k;
03355 }
03356 }
03357
03358 printf("FRAMELIST = %d",FramelistArray[0]);
03359 for(ii=1;ii<TempIntNum;++ii)printf(" %d ",FramelistArray[ii]);
03360 printf("\n");
03361
03362
03363
03364
03365
03366
03367 for(ii=0;ii<nRecs1;++ii)
03368 {
03369 if(SegmentRead[ii] == 1)
03370 {
03371 Needed=0;
03372 for(i=0;i<TempIntNum;++i) if (FramelistArray[i] == ii) Needed=1;
03373 if(Needed == 0)
03374 {
03375 drms_free_array(Segments[ii]);
03376 drms_free_array(Ierror[ii]);
03377 Segments[ii]=NULL;
03378 Ierror[ii]=NULL;
03379 SegmentRead[ii] = 0;
03380 }
03381
03382 }
03383
03384 }
03385
03386
03387 if(it2 == 0 && it == 0)
03388 {
03389
03390
03391
03392
03393
03394 i=IndexFiltergram[TargetWavelength];
03395 j=i;
03396
03397 if(internTOBS[IndexFiltergram[TargetWavelength]] < TargetTime && TargetWavelength < nIndexFiltergram-1)
03398 {
03399 j=TargetWavelength;
03400 while(internTOBS[IndexFiltergram[j]] < TargetTime && j < nIndexFiltergram-1 && fabs(internTOBS[IndexFiltergram[j]]-internTOBS[i]) <= 2.1*DataCadence) j++;
03401 if(fabs(internTOBS[IndexFiltergram[j]]-internTOBS[i]) > 2.1*DataCadence) j=i;
03402 else j=IndexFiltergram[j];
03403 }
03404 if(internTOBS[IndexFiltergram[TargetWavelength]] >= TargetTime && TargetWavelength > 0)
03405 {
03406 j=TargetWavelength;
03407 while(internTOBS[IndexFiltergram[j]] >= TargetTime && j > 0 && fabs(internTOBS[IndexFiltergram[j]]-internTOBS[i]) <= 2.1*DataCadence) --j;
03408 if(fabs(internTOBS[IndexFiltergram[j]]-internTOBS[i]) > 2.1*DataCadence) j=i;
03409 else
03410 {
03411 i=IndexFiltergram[j];
03412 j=IndexFiltergram[TargetWavelength];
03413 }
03414 }
03415
03416 if(i == j)
03417 {
03418 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LOWKEYWORDNUM;
03419 if(TargetWavelength>=1 && TargetWavelength<nIndexFiltergram-1)
03420 {
03421 i=IndexFiltergram[TargetWavelength-1];
03422 j=IndexFiltergram[TargetWavelength+1];
03423 }
03424 if(TargetWavelength>=1 && TargetWavelength == nIndexFiltergram-1 )
03425 {
03426 i=IndexFiltergram[TargetWavelength-1];
03427 j=IndexFiltergram[TargetWavelength];
03428 }
03429 if(TargetWavelength<nIndexFiltergram-1 && TargetWavelength == 0)
03430 {
03431 i=IndexFiltergram[TargetWavelength];
03432 j=IndexFiltergram[TargetWavelength+1];
03433 }
03434 }
03435
03436
03437
03438 if(KeywordMissing[j] == 1 || KeywordMissing[i] == 1)
03439 {
03440 printf("Error: some keywords are missing/corrupted to interpolate OBS_VR, OBS_VW, OBS_VN, CRLN_OBS, CROTA2, and CAR_ROT at target time %s\n",timeBegin2);
03441 if(SegmentRead[temp])
03442 {
03443 drms_free_array(Segments[temp]);
03444 Segments[temp]=NULL;
03445 if(Ierror[temp] != NULL) drms_free_array(Ierror[temp]);
03446 Ierror[temp]=NULL;
03447 SegmentRead[temp]=0;
03448 }
03449 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOINTERPOLATEDKEYWORDS;
03450 CreateEmptyRecord=1; goto NextTargetTime;
03451 }
03452
03453
03454 if(i != j)
03455 {
03456 DSUNOBSint[timeindex]=(DSUNOBS[j]-DSUNOBS[i])/(internTOBS[j]-internTOBS[i])*(TargetTime-internTOBS[i])+DSUNOBS[i];
03457 DSUNOBSint[timeindex]=DSUNOBSint[timeindex]/(double)AstroUnit;
03458 }
03459 else
03460 {
03461 DSUNOBSint[timeindex]=DSUNOBS[i];
03462 DSUNOBSint[timeindex]=DSUNOBSint[timeindex]/(double)AstroUnit;
03463 }
03464
03465
03466 tobs = TargetTime+(DSUNOBSint[timeindex]-1.0)/2.00398880422056639358e-03;
03467
03468
03469
03470 if(i != j)
03471 {
03472 printf("FSNs used for interpolation of OBS_VR, OBS_VW, OBS_VN, CRLN_OBS, CROTA2, and CAR_ROT: %d %d\n",FSN[j],FSN[i]);
03473
03474 DSUNOBSint[timeindex]=(DSUNOBS[j]-DSUNOBS[i])/(internTOBS[j]-internTOBS[i])*(tobs-internTOBS[i])+DSUNOBS[i];
03475 DSUNOBSint[timeindex]=DSUNOBSint[timeindex]/(double)AstroUnit;
03476 CRLTOBSint[timeindex]=(CRLTOBS[j]-CRLTOBS[i])/(internTOBS[j]-internTOBS[i])*(tobs-internTOBS[i])+CRLTOBS[i];
03477 CROTA2int[timeindex] =(CROTA2[j]-CROTA2[i])/(internTOBS[j]-internTOBS[i])*(tobs-internTOBS[i])+CROTA2[i];
03478 OBSVRint[timeindex] =(OBSVR[j]-OBSVR[i])/(internTOBS[j]-internTOBS[i])*(tobs-internTOBS[i])+OBSVR[i];
03479 OBSVWint[timeindex] =(OBSVW[j]-OBSVW[i])/(internTOBS[j]-internTOBS[i])*(tobs-internTOBS[i])+OBSVW[i];
03480 OBSVNint[timeindex] =(OBSVN[j]-OBSVN[i])/(internTOBS[j]-internTOBS[i])*(tobs-internTOBS[i])+OBSVN[i];
03481 ctime1 =-CRLNOBS[i];
03482 ctime2 =360.0*(float)(CARROT[j]-CARROT[i])-CRLNOBS[j];
03483 CRLNOBSint[timeindex]=(ctime2-ctime1)/(internTOBS[j]-internTOBS[i])*(tobs-internTOBS[i])+ctime1;
03484 if(CARROT[j] > CARROT[i])
03485 {
03486 if(CRLNOBSint[timeindex] > 0.0)
03487 {
03488 CRLNOBSint[timeindex] = 360.0 - CRLNOBSint[timeindex];
03489 CARROTint[timeindex] = CARROT[j];
03490 }
03491 else
03492 {
03493 CRLNOBSint[timeindex] = -CRLNOBSint[timeindex];
03494 CARROTint[timeindex] = CARROT[i];
03495 }
03496 }
03497 else
03498 {
03499 CRLNOBSint[timeindex] = -CRLNOBSint[timeindex];
03500 CARROTint[timeindex] = CARROT[i];
03501 }
03502
03503 }
03504 else
03505 {
03506 printf("FSN used for interpolation of OBS_VR, OBS_VW, OBS_VN, CRLN_OBS, CROTA2, and CAR_ROT: %d\n",FSN[i]);
03507
03508 DSUNOBSint[timeindex]=DSUNOBS[i];
03509 DSUNOBSint[timeindex]=DSUNOBSint[timeindex]/(double)AstroUnit;
03510 CRLTOBSint[timeindex]=CRLTOBS[i];
03511 CROTA2int[timeindex] =CROTA2[i];
03512 OBSVRint[timeindex] =OBSVR[i];
03513 OBSVWint[timeindex] =OBSVW[i];
03514 OBSVNint[timeindex] =OBSVN[i];
03515 CRLNOBSint[timeindex]=CRLNOBS[i];
03516 CARROTint[timeindex] =CARROT[i];
03517
03518 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LOWKEYWORDNUM;
03519 }
03520
03521
03522
03523
03524
03525
03526 X0ARR =(float *)malloc(TempIntNum*sizeof(float));
03527 Y0ARR =(float *)malloc(TempIntNum*sizeof(float));
03528 RSUNARR =(float *)malloc(TempIntNum*sizeof(float));
03529
03530 for(i=0;i<TempIntNum;++i)
03531 {
03532 temp=FramelistArray[i];
03533 if(temp != -1)
03534 {
03535 X0ARR[i]=X0[temp];
03536 Y0ARR[i]=Y0[temp];
03537 RSUNARR[i]=RSUN[temp];
03538 CAMAVG[timeindex]=HCAMID[temp];
03539 }
03540 else
03541 {
03542 X0ARR[i] =MISSINGRESULT;
03543 Y0ARR[i] =MISSINGRESULT;
03544 RSUNARR[i]=MISSINGRESULT;
03545 }
03546
03547 }
03548
03549
03550 status=fstats(TempIntNum,X0ARR,&minimum,&maximum,&median,&mean,&sigma,&skewness,&kurtosis,&ngood);
03551 X0AVG[timeindex]=median;
03552 X0RMS[timeindex]=sigma;
03553 status=fstats(TempIntNum,Y0ARR,&minimum,&maximum,&median,&mean,&sigma,&skewness,&kurtosis,&ngood);
03554 Y0AVG[timeindex]=median;
03555 Y0RMS[timeindex]=sigma;
03556 status=fstats(TempIntNum,RSUNARR,&minimum,&maximum,&median,&mean,&sigma,&skewness,&kurtosis,&ngood);
03557 RSUNAVG[timeindex]=median;
03558 RSUNRMS[timeindex]=sigma;
03559
03560 free(X0ARR);
03561 free(Y0ARR);
03562 free(RSUNARR);
03563 X0ARR=NULL;
03564 Y0ARR=NULL;
03565 RSUNARR=NULL;
03566
03567 }
03568
03569
03570
03571
03572
03573
03574 ActualTempIntNum=TempIntNum;
03575
03576
03577
03578
03579 for(i=0;i<TempIntNum;++i)
03580 {
03581 temp=FramelistArray[i];
03582 if(temp != -1)
03583 {
03584 if(SegmentRead[temp] != -1)
03585 {
03586
03587 if(SegmentRead[temp] == 0)
03588 {
03589 printf("segment needs to be read for FSN %d \n",FSN[temp]);
03590 segin = drms_segment_lookupnum(recLev1->records[temp], 0);
03591 Segments[temp] = drms_segment_read(segin,type1d, &status);
03592 if (status != DRMS_SUCCESS || Segments[temp] == NULL)
03593 {
03594 printf("Error: could not read the segment of level 1 record index %d at target time %s\n",temp,timeBegin2);
03595 return 1;
03596
03597
03598
03599
03600
03601
03602 }
03603 else
03604 {
03605 Ierror[temp] = drms_array_create(typeEr,2,axisout,NULL,&status);
03606 if(status != DRMS_SUCCESS || Ierror[temp] == NULL)
03607 {
03608 printf("Error: could not create an array for Ierror at target time %s\n",timeBegin2);
03609 drms_free_array(Segments[temp]);
03610 Segments[temp]=NULL;
03611 Ierror[temp]=NULL;
03612 SegmentRead[temp]=-1;
03613 ActualTempIntNum-=1;
03614 arrin[i] = NULL;
03615 arrerrors[i] = NULL;
03616 }
03617 else
03618 {
03619 arrin[i] = Segments[temp];
03620 arrerrors[i] = Ierror[temp];
03621 if( arrin[i]->axis[0] != axisin[0] || arrin[i]->axis[1] != axisin[1])
03622 {
03623 printf("Error: level 1 record index %d at target time %s has a segment with dimensions %d x %d instead of %d x %d\n",temp,timeBegin2,arrin[i]->axis[0],arrin[i]->axis[1],axisin[0],axisin[1]);
03624 drms_free_array(Segments[temp]);
03625 drms_free_array(Ierror[temp]);
03626 ActualTempIntNum-=1;
03627 arrin[i] = NULL;
03628 arrerrors[i] = NULL;
03629 Segments[temp]=NULL;
03630 Ierror[temp]=NULL;
03631 SegmentRead[temp]=-1;
03632 }
03633 else
03634 {
03635 SegmentRead[temp]=1;
03636
03637
03638
03639 segin = drms_segment_lookup(recLev1->records[temp],"bad_pixel_list");
03640 BadPixels = NULL;
03641 BadPixels = drms_segment_read(segin,segin->info->type,&status);
03642 if(status != DRMS_SUCCESS || BadPixels == NULL)
03643 {
03644 printf("Error: cannot read the list of bad pixels of level 1 filtergram FSN= %d\n",FSN[temp]);
03645 return 1;
03646
03647
03648
03649
03650
03651
03652
03653
03654 }
03655 else
03656 {
03657
03658 strcpy(HMISeriesTemp,CosmicRaySeries);
03659 strcat(HMISeriesTemp,"[][");
03660 sprintf(FSNtemps,"%d",FSN[temp]);
03661 strcat(HMISeriesTemp,FSNtemps);
03662 strcat(HMISeriesTemp,"]");
03663 rectemp=NULL;
03664
03665 rectemp=drms_open_records(drms_env,HMISeriesTemp,&statusA[0]);
03666 if(statusA[0] == DRMS_SUCCESS && rectemp != NULL && rectemp->n != 0)
03667 {
03668 segin = drms_segment_lookupnum(rectemp->records[0],0);
03669 CosmicRays = NULL;
03670
03671 COSMICCOUNT=drms_getkey_int(rectemp->records[0],COUNTS,&status);
03672 if(status != DRMS_SUCCESS || COSMICCOUNT == -1)
03673 {
03674 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOCOSMICRAY;
03675 QUALITYlev1[temp] = QUALITYlev1[temp] | QUAL_NOCOSMICRAY;
03676 }
03677 else
03678 {
03679 CosmicRays = drms_segment_read(segin,segin->info->type,&status);
03680 if(status != DRMS_SUCCESS || CosmicRays == NULL)
03681 {
03682 printf("Error: the list of cosmic-ray hits could not be read for FSN %d\n",FSN[temp]);
03683 return 1;
03684
03685
03686 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOCOSMICRAY;
03687 QUALITYlev1[temp] = QUALITYlev1[temp] | QUAL_NOCOSMICRAY;
03688 CosmicRays = NULL;
03689
03690 }
03691 }
03692
03693
03694 }
03695 else
03696 {
03697 printf("Unable to open the series %s for FSN %d\n",HMISeriesTemp,FSN[temp]);
03698
03699
03700
03701 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOCOSMICRAY;
03702 QUALITYlev1[temp] = QUALITYlev1[temp] | QUAL_NOCOSMICRAY;
03703 CosmicRays = NULL;
03704
03705 }
03706
03707 image = Segments[temp]->data;
03708
03709
03710
03711
03712
03713 if(inRotationalFlat == 1)
03714 {
03715
03716 HMIFlatField = (char *)malloc(MaxNString*sizeof(char *));
03717 if(HMIFlatField == NULL)
03718 {
03719 printf("Error: memory could not be allocated to HMIFlatField\n");
03720 return 1;
03721 }
03722 HMIFlatField = drms_getkey_string(recLev1->records[temp],FLATREC,&status);
03723 if (status != DRMS_SUCCESS)
03724 {
03725 printf("Error: could not read the FLAT_REC keyword for the target filtergram FSN= %d",FSN[temp]);
03726 return 1;
03727 }
03728
03729
03730 if(strcmp(HMIFlatField,HMIFlatField0) != 0)
03731 {
03732 printf("Warning: the hmi.flatfield record used to produce the level 1 records changed during the run of the observables code\n");
03733
03734 recflat = drms_open_records(drms_env,HMIFlatField,&status);
03735 if (status != DRMS_SUCCESS || recflat == NULL || recflat->n == 0)
03736 {
03737 printf("Error: record missing or corrupt for the flat field query %s\n",HMIFlatField);
03738 return 1;
03739 }
03740 drms_free_array(flatfield);
03741 strcpy(HMIFlatField0,HMIFlatField);
03742 segin = drms_segment_lookup(recflat->records[0],"flatfield");
03743 flatfield = drms_segment_read(segin,type1d,&status);
03744 if (status != DRMS_SUCCESS || flatfield == NULL)
03745 {
03746 printf("Error: could not read the data segment for the flat field query %s\n",HMIFlatField);
03747 return 1;
03748 }
03749 pztflat = flatfield->data;
03750 status=drms_close_records(recflat,DRMS_FREE_RECORD);
03751 }
03752
03753 if(inLinearity == 1)
03754 {
03755 printf("applying rotational flat field and correcting for non-linearity of camera on record FSN=%d\n",FSN[temp]);
03756 for(iii=0;iii<axisin[0]*axisin[1];++iii)
03757 {
03758
03759 image[iii]=(image[iii]*pztflat[iii])/rotflat[iii];
03760
03761 tempvalue = image[iii]*EXPTIME[temp];
03762 if(HCAMID[temp] == LIGHT_FRONT)tempvalue = (nonlinf[0]+nonlinf[1]*tempvalue+nonlinf[2]*tempvalue*tempvalue+nonlinf[3]*tempvalue*tempvalue*tempvalue)+tempvalue;
03763 else tempvalue = (nonlins[0]+nonlins[1]*tempvalue+nonlins[2]*tempvalue*tempvalue+nonlins[3]*tempvalue*tempvalue*tempvalue)+tempvalue;
03764 image[iii] = tempvalue/EXPTIME[temp];
03765 }
03766 }
03767 else
03768 {
03769 printf("applying rotational flat field on record FSN=%d\n",FSN[temp]);
03770 for(iii=0;iii<axisin[0]*axisin[1];++iii)
03771 {
03772
03773 image[iii]=(image[iii]*pztflat[iii])/rotflat[iii];
03774 }
03775 }
03776
03777 free(HMIFlatField);
03778
03779 }
03780 else
03781 {
03782 if(inLinearity == 1)
03783 {
03784 printf("correcting for non-linearity of camera on record FSN=%d\n",FSN[temp]);
03785 for(iii=0;iii<axisin[0]*axisin[1];++iii)
03786 {
03787
03788 tempvalue = image[iii]*EXPTIME[temp];
03789 if(HCAMID[temp] == LIGHT_FRONT) tempvalue = (nonlinf[0]+nonlinf[1]*tempvalue+nonlinf[2]*tempvalue*tempvalue+nonlinf[3]*tempvalue*tempvalue*tempvalue)+tempvalue;
03790 else tempvalue = (nonlins[0]+nonlins[1]*tempvalue+nonlins[2]*tempvalue*tempvalue+nonlins[3]*tempvalue*tempvalue*tempvalue)+tempvalue;
03791 image[iii] = tempvalue/EXPTIME[temp];
03792 }
03793 }
03794
03795 }
03796
03797
03798
03799
03800
03801 status = MaskCreation(Mask,axisin[0],axisin[1],BadPixels,HIMGCFID[temp],image,CosmicRays,NBADPERM[temp]);
03802 if(status != 0)
03803 {
03804 printf("Error: unable to create a mask for the gap filling function\n");
03805 return 1;
03806 }
03807 if(BadPixels != NULL)
03808 {
03809 drms_free_array(BadPixels);
03810 BadPixels=NULL;
03811 }
03812 if(CosmicRays != NULL)
03813 {
03814 drms_free_array(CosmicRays);
03815 CosmicRays=NULL;
03816 }
03817 if(rectemp != NULL)
03818 {
03819 drms_close_records(rectemp,DRMS_FREE_RECORD);
03820 rectemp=NULL;
03821 }
03822
03823 image = arrin[i]->data;
03824 ierror = arrerrors[i]->data;
03825
03826
03827 status =do_gapfill(image,Mask,&const_param,ierror,axisin[0],axisin[1]);
03828
03829
03830 if(status != 0)
03831 {
03832 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOGAPFILL;
03833 QUALITYlev1[temp] = QUALITYlev1[temp] | QUAL_NOGAPFILL;
03834 printf("Error: gapfilling code did not work on a level 1 filtergram at target time %s\n",timeBegin2);
03835 }
03836 }
03837 }
03838 }
03839 }
03840 }
03841 else
03842 {
03843 arrin[i] = Segments[temp];
03844 arrerrors[i] = Ierror[temp];
03845 }
03846
03847 }
03848 else
03849 {
03850 printf("Error: at least one of the keyword needed by the temporal interpolation function is missing, at target time %s\n",timeBegin2);
03851 ActualTempIntNum-=1;
03852 arrin[i] = NULL;
03853 arrerrors[i] = NULL;
03854 }
03855 }
03856 else
03857 {
03858 ActualTempIntNum-=1;
03859 arrin[i] = NULL;
03860 arrerrors[i] = NULL;
03861 }
03862 }
03863
03864
03865
03866
03867 printf("NUMBER OF LEVEL 1 FILTERGRAMS AVAILABLE FOR THE TEMPORAL AVERAGING: %d\n",ActualTempIntNum);
03868
03869 if(ActualTempIntNum >= ThresholdPol)
03870 {
03871
03872
03873
03874 ii=0;
03875 for(i=0;i<TempIntNum;++i) if (arrin[i] != NULL && arrerrors[i] != NULL)
03876 {
03877 temp=FramelistArray[i];
03878 if(fabs(RSUN[temp]-RSUNAVG[timeindex]) > 1.82*RSUNerr || isnan(RSUN[temp]))
03879 {
03880 printf("Warning: image %d passed to do_interpolate has a RSUN value %f too different from the median value and will be rejected\n",i,RSUN[temp]);
03881 ActualTempIntNum-=1;
03882 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LIMBFITISSUE;
03883 continue;
03884 }
03885 if(combine == 0)
03886 {
03887 if(fabs(X0[temp]-X0AVG[timeindex]) > RSUNerr || isnan(X0[temp]))
03888 {
03889 printf("Warning: image %d passed to do_interpolate has a X0 value %f too different from the median value and will be rejected\n",i,X0[temp]);
03890 ActualTempIntNum-=1;
03891 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LIMBFITISSUE;
03892 continue;
03893 }
03894 if(fabs(Y0[temp]-Y0AVG[timeindex]) > RSUNerr || isnan(Y0[temp]))
03895 {
03896 printf("Warning: image %d passed to do_interpolate has a Y0 value %f too different from the median value and will be rejected\n",i,Y0[temp]);
03897 ActualTempIntNum-=1;
03898 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LIMBFITISSUE;
03899 continue;
03900 }
03901 }
03902 else
03903 {
03904 if(CAMAVG[timeindex] == LIGHT_FRONT && HCAMID[temp] == LIGHT_FRONT)
03905 {
03906 X0AVGT=X0AVG[timeindex];
03907 Y0AVGT=Y0AVG[timeindex];
03908 }
03909 if(CAMAVG[timeindex] == LIGHT_SIDE && HCAMID[temp] == LIGHT_FRONT)
03910 {
03911 X0AVGT=X0AVG[timeindex]+diffXfs;
03912 Y0AVGT=Y0AVG[timeindex]+diffYfs;
03913 }
03914 if(CAMAVG[timeindex] == LIGHT_FRONT && HCAMID[temp] == LIGHT_SIDE)
03915 {
03916 X0AVGT=X0AVG[timeindex]-diffXfs;
03917 Y0AVGT=Y0AVG[timeindex]-diffYfs;
03918 }
03919 if(CAMAVG[timeindex] == LIGHT_SIDE && HCAMID[temp] == LIGHT_SIDE)
03920 {
03921 X0AVGT=X0AVG[timeindex];
03922 Y0AVGT=Y0AVG[timeindex];
03923 }
03924
03925 if(fabs(X0[temp]-X0AVGT) > RSUNerr || isnan(X0[temp]))
03926 {
03927 printf("Warning: image %d passed to do_interpolate has a X0 value %f too different from the median value and will be rejected\n",i,X0[temp]);
03928 ActualTempIntNum-=1;
03929 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LIMBFITISSUE;
03930 continue;
03931 }
03932 if(fabs(Y0[temp]-Y0AVGT) > RSUNerr || isnan(Y0[temp]))
03933 {
03934 printf("Warning: image %d passed to do_interpolate has a Y0 value %f too different from the median value and will be rejected\n",i,Y0[temp]);
03935 ActualTempIntNum-=1;
03936 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LIMBFITISSUE;
03937 continue;
03938 }
03939
03940 }
03941 imagesi[ii]=arrin[i]->data;
03942 image=arrin[i]->data;
03943 if( (combine == 1) && (HCAMID[temp] == LIGHT_FRONT)){
03944
03945 for(iii=0;iii<axisin[0]*axisin[1];++iii) image[iii]= image[iii]/iratio;
03946 printf("MOD L intensity correction applied to FSN=%d \n",FSN[temp]);
03947 }
03948
03949 ierrors[ii]=arrerrors[i]->data;
03950 printf("FSN filtergram used: %d %d %f %f %f %f %f %f %f\n",FSN[temp],HCAMID[temp],RSUN[temp],X0[temp],Y0[temp],DSUNOBS[temp]/AstroUnit,CRLTOBS[temp],CROTA2[temp],internTOBS[temp]);
03951 if(HCAMID[temp] == LIGHT_FRONT) KeyInterp[ii].camera=0;
03952 else KeyInterp[ii].camera=1;
03953 if(!strcmp(HWLTNSET[temp],"OPEN")) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_ISSTARGET;
03954 if((QUALITYin[temp] & Q_ACS_ECLP) == Q_ACS_ECLP) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_ECLIPSE;
03955 if((QUALITYin[temp] & Q_ACS_LUNARTRANSIT) == Q_ACS_LUNARTRANSIT) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
03956 if((QUALITYin[temp] & Q_ACS_THERMALRECOVERY) == Q_ACS_THERMALRECOVERY) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
03957 if((QUALITYin[temp] & Q_CAMERA_ANOMALY) == Q_CAMERA_ANOMALY) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_POORQUALITY;
03958 KeyInterp[ii].rsun=RSUNAVG[timeindex];
03959 KeyInterp[ii].xx0=X0[temp];
03960 KeyInterp[ii].yy0=Y0[temp];
03961 KeyInterp[ii].dist=(float)(DSUNOBS[temp]/(double)AstroUnit);
03962 KeyInterp[ii].b0=CRLTOBS[temp]/180.*M_PI;
03963 KeyInterp[ii].p0=CROTA2[temp]/180.*M_PI;
03964 KeyInterp[ii].time=internTOBS[temp];
03965 KeyInterp[ii].focus=CFINDEX[temp];
03966
03967 rec=recLev1->records[temp];
03968 sprintf(recnums,"%ld",rec->recnum);
03969 if(it != 0 || it2 != 0 || ii != 0) strcat(source[timeindex],",#");
03970 else strcat(source[timeindex],"#");
03971 strcat(source[timeindex],recnums);
03972
03973 QUALITYLEV1[timeindex] = QUALITYLEV1[timeindex] | QUALITYin[temp];
03974 QUALITY[timeindex] = QUALITY[timeindex] | QUALITYlev1[temp];
03975
03976 ii+=1;
03977 }
03978
03979
03980 RSUNint[timeindex]=RSUNAVG[timeindex];
03981 KeyInterpOut.rsun=RSUNint[timeindex];
03982 KeyInterpOut.xx0=X0AVG[timeindex];
03983 KeyInterpOut.yy0=Y0AVG[timeindex];
03984 KeyInterpOut.dist=(float)DSUNOBSint[timeindex];
03985 KeyInterpOut.b0=CRLTOBSint[timeindex]/180.*M_PI;
03986 KeyInterpOut.p0=CROTA2int[timeindex]/180.*M_PI;
03987 tobs = TargetTime+(DSUNOBSint[timeindex]-1.0)/2.00398880422056639358e-03;
03988 KeyInterpOut.time=tobs;
03989 KeyInterpOut.focus=TargetCFINDEX;
03990
03991
03992 printf("Calling temporal averaging, de-rotation, and un-distortion code\n");
03993
03994 totalTempIntNum[timeindex] += ActualTempIntNum;
03995 if(ActualTempIntNum != TempIntNum) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LOWINTERPNUM;
03996 else
03997 {
03998 minimum=KeyInterp[0].time;
03999 maximum=KeyInterp[0].time;
04000 for(ii=1;ii<TempIntNum;++ii)
04001 {
04002 if(KeyInterp[ii].time < minimum) minimum=KeyInterp[ii].time;
04003 if(KeyInterp[ii].time > maximum) maximum=KeyInterp[ii].time;
04004 }
04005 if((maximum-minimum) > (DataCadence*(double)(TempIntNum-1)+DataCadence/(double)framelistSize) ) QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LOWINTERPNUM;
04006 }
04007
04008 if(ActualTempIntNum >= ThresholdPol)
04009 {
04010 printf("KEYWORDS OUT: %f %f %f %f %f %f %f %d\n",KeyInterpOut.rsun,KeyInterpOut.xx0,KeyInterpOut.yy0,KeyInterpOut.dist,KeyInterpOut.b0,KeyInterpOut.p0,KeyInterpOut.time,KeyInterpOut.focus);
04011
04012 status=do_interpolate(imagesi,ierrors,arrLev1d[it2]->data,KeyInterp,&KeyInterpOut,&const_param,ActualTempIntNum,axisin[0],axisin[1],AverageTime,dpath2);
04013
04014
04015
04016
04017
04018 printf("End temporal interpolation\n");
04019 }
04020 else
04021 {
04022 printf("Error: ActualTempIntNum < ThresholdPol\n");
04023 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOTENOUGHINTERPOLANTS;
04024 status = 1;
04025 }
04026
04027 if (status != 0)
04028 {
04029 printf("Error: temporal interpolation failed at target time %s\n",timeBegin2);
04030 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_INTERPOLATIONFAILED;
04031 CreateEmptyRecord=1; goto NextTargetTime;
04032 }
04033
04034 }
04035 else
04036 {
04037 printf("Error: not enough valid level 1 filtergrams to produce a level 1d filtergram at target time %s\n",timeBegin2);
04038 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOTENOUGHINTERPOLANTS;
04039 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_LOWINTERPNUM;
04040 CreateEmptyRecord=1; goto NextTargetTime;
04041
04042 }
04043
04044 }
04045
04046
04047
04048
04049
04050
04051
04052
04053
04054
04055
04056
04057
04058 printf("Creating level 1p arrays\n");
04059 arrLev1p = (DRMS_Array_t **)malloc(npolout*sizeof(DRMS_Array_t *));
04060 if(arrLev1p == NULL)
04061 {
04062 printf("Error: memory could not be allocated to arrLev1p\n");
04063
04064 return 1;
04065 }
04066
04067 for(i=0;i<npolout;++i)
04068 {
04069 arrLev1p[i] = drms_array_create(type1p,2,axisout,NULL,&status);
04070 if(status != DRMS_SUCCESS || arrLev1p[i] == NULL)
04071 {
04072 printf("Error: cannot create an array for a level 1p data at target time %s\n",timeBegin2);
04073 return 1;
04074 }
04075 }
04076
04077
04078
04079 printf("Peopling the images with level 1d arrays\n");
04080 for(it2=0;it2<npol;++it2)
04081 {
04082 images[it2]=arrLev1d[it2]->data;
04083 ps1[it2]=PolarizationArray[0][it2];
04084 ps2[it2]=PolarizationArray[1][it2];
04085 ps3[it2]=PolarizationArray[2][it2];
04086 printf("ps1 = %d, ps2 = %d, ps3 = %d\n",ps1[it2],ps2[it2],ps3[it2]);
04087 }
04088
04089 printf("Setting keywords for level 1p\n");
04090 KeyInterpOut.rsun=RSUNint[timeindex];
04091 KeyInterpOut.xx0=X0AVG[timeindex];
04092 KeyInterpOut.yy0=Y0AVG[timeindex];
04093 KeyInterpOut.dist=(float)DSUNOBSint[timeindex];
04094 KeyInterpOut.b0=CRLTOBSint[timeindex]/180.*M_PI;
04095 KeyInterpOut.p0=CROTA2int[timeindex]/180.*M_PI;
04096 tobs=TargetTime+(DSUNOBSint[timeindex]-1.0)/2.00398880422056639358e-03;
04097 KeyInterpOut.time=tobs;
04098
04099
04100 if(QuickLook == 1)
04101 {
04102 TSEL=20.;
04103 TFRONT=20.;
04104 }
04105 else
04106 {
04107
04108 strcpy(HMISeriesTemp,HMISeriesTemperature);
04109 strcat(HMISeriesTemp,"[");
04110 strcat(HMISeriesTemp,timeBegin2);
04111 strcat(HMISeriesTemp,"]");
04112 rectemp=NULL;
04113 rectemp = drms_open_records(drms_env,HMISeriesTemp,&status);
04114 printf("TEMPERATURE QUERY = %s\n",HMISeriesTemp);
04115 if(statusA[0] == DRMS_SUCCESS && rectemp != NULL && rectemp->n != 0) TSEL=drms_getkey_float(rectemp->records[0],TS08,&status);
04116 else status = 1;
04117 if(status != DRMS_SUCCESS || isnan(TSEL))
04118 {
04119 printf("Error: the temperature keyword %s could not be read\n",TS08);
04120 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOTEMP;
04121 TSEL=20.;
04122 }
04123 statusA[1]=1;
04124 if(statusA[0] == DRMS_SUCCESS && rectemp != NULL && rectemp->n != 0) TFRONT=(drms_getkey_float(rectemp->records[0],TS01,&statusA[0])+drms_getkey_float(rectemp->records[0],TS02,&statusA[1]))/2.0;
04125 if(statusA[0] != DRMS_SUCCESS || statusA[1] != DRMS_SUCCESS || isnan(TFRONT))
04126 {
04127 printf("Error: temperature keyword %s and/or %s could not be read\n",TS01,TS02);
04128 QUALITY[timeindex] = QUALITY[timeindex] | QUAL_NOTEMP;
04129 TFRONT=20.;
04130 }
04131 printf("TEMPERATURES = %f %f\n",TSEL,TFRONT);
04132 if(rectemp != NULL)
04133 {
04134 drms_close_records(rectemp,DRMS_FREE_RECORD);
04135 rectemp=NULL;
04136 }
04137 }
04138
04139
04140 statusA[8] = drms_setkey_int(recLev1p->records[timeindex],QUALITYS,QUALITY[timeindex]);
04141 statusA[44]= drms_setkey_int(recLev1p->records[timeindex],QUALLEV1S,QUALITYLEV1[timeindex]);
04142 statusA[23]= drms_setkey_int(recLev1p->records[timeindex],TINTNUMS,totalTempIntNum[timeindex]);
04143 if(statusA[8] != 0 || statusA[44] != 0 || statusA[23] != 0) printf("WARNING: could not set some of the keywords modified by the temporal interpolation subroutine for the level 1p data at target time %s\n",timeBegin2);
04144
04145 if(it == 0)
04146 {
04147
04148
04149 printf("SET KEYWORDS FOR LEVEL 1p AT TARGET TIME: %f %s\n",TargetTime,timeBegin2);
04150
04151
04152
04153
04154
04155 if(combine == 1)camera=3;
04156 if(combine == 0 && CamId == LIGHT_SIDE) camera=1;
04157 if(combine == 0 && CamId == LIGHT_FRONT) camera=2;
04158
04159
04160 cdelt1=1.0/RSUNint[timeindex]*asin(solar_radius/(DSUNOBSint[timeindex]*(double)AstroUnit))*180.*60.*60./M_PI;
04161 statusA[0] = drms_setkey_time(recLev1p->records[timeindex],TRECS,TargetTime);
04162 statusA[1] = drms_setkey_time(recLev1p->records[timeindex],TOBSS,tobs);
04163 statusA[2] = drms_setkey_int(recLev1p->records[timeindex],CAMERAS,camera);
04164 statusA[3] = drms_setkey_string(recLev1p->records[timeindex],HISTORYS,HISTORY);
04165 statusA[4] = drms_setkey_string(recLev1p->records[timeindex],COMMENTS,COMMENT);
04166 statusA[5] = drms_setkey_float(recLev1p->records[timeindex],CADENCES,DataCadence);
04167 statusA[6] = drms_setkey_int(recLev1p->records[timeindex],HFLIDS,TargetHFLID);
04168 statusA[7] = drms_setkey_int(recLev1p->records[timeindex],HCFTIDS,TargetCFINDEX);
04169
04170 statusA[9] = drms_setkey_double(recLev1p->records[timeindex],DSUNOBSS,DSUNOBSint[timeindex]*(double)AstroUnit);
04171 statusA[10]= drms_setkey_float(recLev1p->records[timeindex],CRLTOBSS,KeyInterpOut.b0*180./M_PI);
04172 statusA[11]= drms_setkey_float(recLev1p->records[timeindex],CROTA2S,-KeyInterpOut.p0*180./M_PI);
04173 statusA[12]= drms_setkey_float(recLev1p->records[timeindex],CDELT1S,cdelt1);
04174 statusA[13]= drms_setkey_float(recLev1p->records[timeindex],CDELT2S,cdelt1);
04175 statusA[14]= drms_setkey_float(recLev1p->records[timeindex],CRPIX1S,KeyInterpOut.xx0+1.);
04176 statusA[15]= drms_setkey_float(recLev1p->records[timeindex],CRPIX2S,KeyInterpOut.yy0+1.);
04177 sprintf(jsocverss,"%d",jsoc_vers_num);
04178 statusA[16]= drms_setkey_string(recLev1p->records[timeindex],BLDVERSS,jsocverss);
04179 statusA[17]= drms_setkey_double(recLev1p->records[timeindex],OBSVRS,OBSVRint[timeindex]);
04180 statusA[18]= drms_setkey_double(recLev1p->records[timeindex],OBSVWS,OBSVWint[timeindex]);
04181 statusA[19]= drms_setkey_double(recLev1p->records[timeindex],OBSVNS,OBSVNint[timeindex]);
04182
04183 statusA[20]=0;
04184 statusA[21]= drms_setkey_float(recLev1p->records[timeindex],CRLNOBSS,CRLNOBSint[timeindex]);
04185 statusA[22]= drms_setkey_int(recLev1p->records[timeindex],CARROTS,CARROTint[timeindex]);
04186 statusA[24]= drms_setkey_int(recLev1p->records[timeindex],SINTNUMS,const_param.order_int);
04187 statusA[25]= drms_setkey_string(recLev1p->records[timeindex],CODEVER0S,CODEVERSION);
04188 statusA[26]= drms_setkey_string(recLev1p->records[timeindex],DISTCOEFS,DISTCOEFPATH);
04189 statusA[27]= drms_setkey_string(recLev1p->records[timeindex],ROTCOEFS,ROTCOEFPATH);
04190 statusA[28]= drms_setkey_int(recLev1p->records[timeindex],ODICOEFFS,const_param.order_dist_coef);
04191 statusA[29]= drms_setkey_int(recLev1p->records[timeindex],OROCOEFFS,2*const_param.order2_rot_coef);
04192 statusA[30]= drms_setkey_string(recLev1p->records[timeindex],CODEVER1S,CODEVERSION1);
04193 statusA[31]= drms_setkey_string(recLev1p->records[timeindex],CODEVER2S,CODEVERSION2);
04194 statusA[32]= drms_setkey_float(recLev1p->records[timeindex] ,TFRONTS,TFRONT);
04195 statusA[33]= drms_setkey_float(recLev1p->records[timeindex] ,TSELS,TSEL);
04196 statusA[34]= drms_setkey_int(recLev1p->records[timeindex] ,POLCALMS,method);
04197 statusA[35]= drms_setkey_string(recLev1p->records[timeindex],CODEVER3S,CODEVERSION3);
04198 statusA[36]= drms_setkey_float(recLev1p->records[timeindex],RSUNOBSS,asin(solar_radius/(KeyInterpOut.dist*AstroUnit))*180.*60.*60./M_PI);
04199 sprint_time(DATEOBS,tobs-DataCadence/2.0,"UTC",1);
04200 statusA[37]= drms_setkey_string(recLev1p->records[timeindex],DATEOBSS,DATEOBS);
04201 sprint_time(DATEOBS,CURRENT_SYSTEM_TIME,"UTC",1);
04202 statusA[38]= drms_setkey_string(recLev1p->records[timeindex],DATES,DATEOBS);
04203 statusA[39]= drms_setkey_int(recLev1p->records[timeindex],HWLTIDS,TargetHWLTID);
04204 statusA[40]= drms_setkey_int(recLev1p->records[timeindex],HPLTIDS,TargetHPLTID);
04205 statusA[41]= drms_setkey_int(recLev1p->records[timeindex],WavelengthIDS,WavelengthID);
04206 if(camera == 2) strcpy(DATEOBS,"HMI_FRONT2");
04207 if(camera == 1) strcpy(DATEOBS,"HMI_SIDE1");
04208 if(camera == 3) strcpy(DATEOBS,"HMI_COMBINED");
04209 statusA[42]= drms_setkey_string(recLev1p->records[timeindex],INSTRUMES,DATEOBS);
04210 statusA[43]= drms_setkey_int(recLev1p->records[timeindex],HCAMIDS,CamId);
04211
04212 if(inLinearity == 1) CALVER32[0] = CALVER32[0] | CALVER_LINEARITY;
04213 if(inRotationalFlat == 1) CALVER32[0] = CALVER32[0] | CALVER_ROTATIONAL;
04214 if(TargetHFLID == 58312 || TargetHFLID == 1022) {CALVER32[0] = CALVER32[0] | CALVER_MODL;} else CALVER32[0] = CALVER32[0] & CALVER_NOMODL;
04215 statusA[45]= drms_setkey_longlong(recLev1p->records[timeindex],CALVER64S,CALVER32[0]);
04216
04217 TotalStatus=0;
04218 for(i=0;i<46;++i)
04219 if(TotalStatus != 0)
04220 {
04221 printf("WARNING: could not set some of the keywords modified by the temporal interpolation subroutine for the level 1p data at target time %s\n",timeBegin2);
04222 }
04223 }
04224
04225
04226 if(it != nWavelengths-1) statusA[44]= drms_setkey_string(recLev1p->records[timeindex],SOURCES,source[timeindex]);
04227 else statusA[44]= drms_setkey_string(recLev1p->records[timeindex],SOURCES,strcat(source[timeindex],"]"));
04228
04229 printf("Peopling imagesout arrays\n");
04230 for(i=0;i<npolout;++i) imagesout[i]=arrLev1p[i]->data;
04231
04232
04233
04234
04235 printf("Producing level 1p data, npol= %d; polarization type= %d; npolout= %d; %d %d %d \n",npol,PolarizationType,npolout,axisout[0],axisout[1],axisout[1]);
04236 polcal(&pars,npol,PolarizationType,images,imagesout,ps1,ps2,ps3,TSEL,TFRONT,axisout[0],axisout[1],axisout[1]);
04237
04238
04239
04240
04241
04242 for(i=0;i<npolout;++i)
04243 {
04244 printf("writing segment %d out of %d\n",i,npolout);
04245 segout = drms_segment_lookup(recLev1p->records[timeindex],Lev1pSegName[it*npolout+i]);
04246 arrLev1p[i]->bzero=segout->bzero;
04247 arrLev1p[i]->bscale=segout->bscale;
04248 arrLev1p[i]->israw=0;
04249 status=drms_segment_write(segout,arrLev1p[i],0);
04250 if(status != DRMS_SUCCESS)
04251 {
04252 printf("Error: a call to drms_segment_write failed\n");
04253 return 1;
04254 }
04255
04256
04257
04258 status=fstats(axisout[0]*axisout[1],imagesout[i],&minimum,&maximum,&median,&mean,&sigma,&skewness,&kurtosis,&ngood);
04259 if(status != 0)
04260 {
04261 printf("Error: the statistics function did not run properly at target time %s\n",timeBegin2);
04262 }
04263 statusA[0]= drms_setkey_float(recLev1p->records[timeindex],DATAMINS[it*npolout+i],(float)minimum);
04264 statusA[1]= drms_setkey_float(recLev1p->records[timeindex],DATAMAXS[it*npolout+i],(float)maximum);
04265 statusA[2]= drms_setkey_float(recLev1p->records[timeindex],DATAMEDNS[it*npolout+i],(float)median);
04266 statusA[3]= drms_setkey_float(recLev1p->records[timeindex],DATAMEANS[it*npolout+i],(float)mean);
04267 statusA[4]= drms_setkey_float(recLev1p->records[timeindex],DATARMSS[it*npolout+i],(float)sigma);
04268 statusA[5]= drms_setkey_float(recLev1p->records[timeindex],DATASKEWS[it*npolout+i],(float)skewness);
04269 statusA[6]= drms_setkey_float(recLev1p->records[timeindex],DATAKURTS[it*npolout+i],(float)kurtosis);
04270 statusA[7]= drms_setkey_int(recLev1p->records[timeindex],TOTVALSS[it*npolout+i],axisout[0]*axisout[1]);
04271 statusA[8]= drms_setkey_int(recLev1p->records[timeindex],DATAVALSS[it*npolout+i],ngood);
04272 statusA[9]= drms_setkey_int(recLev1p->records[timeindex],MISSVALSS[it*npolout+i],axisout[0]*axisout[1]-ngood);
04273
04274 image=arrLev1p[i]->data;
04275 for(ii=0;ii<axisout[0]*axisout[1];++ii)
04276 {
04277 row =ii / axisout[0];
04278 column=ii % axisout[0];
04279 distance = sqrt(((float)row-Y0AVG[timeindex])*((float)row-Y0AVG[timeindex])+((float)column-X0AVG[timeindex])*((float)column-X0AVG[timeindex]));
04280 if(distance > 0.99*RSUNint[timeindex]) image[ii]=NAN;
04281 }
04282
04283 status=fstats(axisout[0]*axisout[1],imagesout[i],&minimum,&maximum,&median,&mean,&sigma,&skewness,&kurtosis,&ngood);
04284 if(status != 0)
04285 {
04286 printf("Error: the statistics function did not run properly at target time %s\n",timeBegin2);
04287 }
04288 statusA[0]= drms_setkey_float(recLev1p->records[timeindex],DATAMINS2[it*npolout+i],(float)minimum);
04289 statusA[1]= drms_setkey_float(recLev1p->records[timeindex],DATAMAXS2[it*npolout+i],(float)maximum);
04290 statusA[2]= drms_setkey_float(recLev1p->records[timeindex],DATAMEDNS2[it*npolout+i],(float)median);
04291 statusA[3]= drms_setkey_float(recLev1p->records[timeindex],DATAMEANS2[it*npolout+i],(float)mean);
04292 statusA[4]= drms_setkey_float(recLev1p->records[timeindex],DATARMSS2[it*npolout+i],(float)sigma);
04293 statusA[5]= drms_setkey_float(recLev1p->records[timeindex],DATASKEWS2[it*npolout+i],(float)skewness);
04294 statusA[6]= drms_setkey_float(recLev1p->records[timeindex],DATAKURTS2[it*npolout+i],(float)kurtosis);
04295
04296 TotalStatus=0;
04297 for(ii=0;ii<10;++ii) TotalStatus+=statusA[ii];
04298 if(TotalStatus != 0)
04299 {
04300 for(ii=0;ii<10;++ii) printf(" %d ",statusA[ii]);
04301 printf("\n");
04302 printf("WARNING: could not set some of the keywords modified by the temporal interpolation subroutine for the level 1p data at target time %s\n",timeBegin2);
04303 }
04304
04305 }
04306
04307 NextTargetTime:
04308
04309
04310
04311
04312
04313
04314
04315
04316
04317 printf("FREEING RECORD\n");
04318 if(nIndexFiltergram != 0)
04319 {
04320 printf("free arrLev1d\n");
04321 for(i=0;i<Npolin;++i)
04322 {
04323 drms_free_array(arrLev1d[i]);
04324 arrLev1d[i]=NULL;
04325 }
04326 free(arrLev1d);
04327 arrLev1d=NULL;
04328
04329 if(arrLev1p != NULL)
04330 {
04331 for(i=0;i<npolout;++i) if(arrLev1p[i] != NULL)
04332 {
04333 drms_free_array(arrLev1p[i]);
04334 arrLev1p[i]=NULL;
04335 }
04336 }
04337 }
04338
04339 if(CreateEmptyRecord)
04340 {
04341 printf("Warning: creating/updating empty lev1p record\n");
04342 QUALITY[timeindex]= QUALITY[timeindex] | QUAL_NODATA;
04343 if(CamId == LIGHT_SIDE) camera=1;
04344 if(CamId == LIGHT_FRONT) camera=2;
04345 statusA[0] = drms_setkey_time(recLev1p->records[timeindex],TRECS,TargetTime);
04346
04347 statusA[2] = drms_setkey_int(recLev1p->records[timeindex],CAMERAS,camera);
04348 statusA[3] = drms_setkey_int(recLev1p->records[timeindex],QUALITYS,QUALITY[timeindex]);
04349 sprint_time(DATEOBS,CURRENT_SYSTEM_TIME,"UTC",1);
04350 statusA[4]= drms_setkey_string(recLev1p->records[timeindex],DATES,DATEOBS);
04351
04352 CreateEmptyRecord=0;
04353 }
04354
04355
04356 printf("TIME= %f\n",TargetTime);
04357 PreviousTargetTime=TargetTime;
04358 TargetTime+=AverageTime;
04359 printf("TIME= %f\n",TargetTime);
04360 printf("END TIME= %f\n",TimeEnd);
04361 timeindex+=1;
04362 initialrun=0;
04363 }
04364
04365 }
04366
04367
04368 printf("END LOOP OVER WAVELENGTHS\n");
04369
04370 printf("INSERT LEVEL 1p RECORDS IN DRMS\n");
04371 if(recLev1p != NULL && recLev1p->n != 0)
04372 {
04373 status=drms_close_records(recLev1p,DRMS_INSERT_RECORD);
04374 recLev1p=NULL;
04375 }
04376
04377 if(nIndexFiltergram != 0)
04378 {
04379
04380 free(FramelistArray);
04381 FramelistArray=NULL;
04382
04383 if(recLev1->n > 0)
04384 {
04385 status=drms_close_records(recLev1,DRMS_FREE_RECORD);
04386 recLev1=NULL;
04387 free(internTOBS);
04388 free(HWL1POS);
04389 free(HWL2POS);
04390 free(HWL3POS);
04391 free(HWL4POS);
04392 free(HPL1POS);
04393 free(HPL2POS);
04394 free(HPL3POS);
04395 free(HWLTID);
04396 free(HPLTID);
04397 free(FID);
04398 free(HFLID);
04399 free(HCAMID);
04400 free(RSUN);
04401 free(CROTA2);
04402 free(CRLTOBS);
04403 free(DSUNOBS);
04404 free(X0);
04405 free(Y0);
04406 free(SegmentRead);
04407 free(KeywordMissing);
04408 free(Segments);
04409
04410 if(inRotationalFlat == 1)
04411 {
04412 drms_free_array(flatfield);
04413 drms_free_array(flatfieldrot);
04414 status=drms_close_records(recflatrot,DRMS_FREE_RECORD);
04415 }
04416 free(Badkeyword);
04417 free(Ierror);
04418 free(IndexFiltergram);
04419 free(FSN);
04420 free(CFINDEX);
04421 free(HIMGCFID);
04422 for(i=0;i<nRecs1;++i) free(IMGTYPE[i]);
04423 free(IMGTYPE);
04424 free(CDELT1);
04425 for(i=0;i<nRecs1;++i) free(HWLTNSET[i]);
04426 free(HWLTNSET);
04427 free(NBADPERM);
04428 free(QUALITYin);
04429 free(EXPTIME);
04430 free(CAMERA);
04431 free(CALVER32);
04432 }
04433
04434 for(i=0;i<nTime;++i) free(source[i]);
04435 free(source);
04436 free(totalTempIntNum);
04437 free(images);
04438 free(imagesout);
04439 free(ps1);
04440 free(ps2);
04441 free(ps3);
04442 free(imagesi);
04443 free(ierrors);
04444 images=NULL;
04445 ierrors=NULL;
04446 free(KeyInterp);
04447 KeyInterp=NULL;
04448 free(X0AVG);
04449 free(Y0AVG);
04450 free(CAMAVG);
04451 free(RSUNAVG);
04452 free(X0RMS);
04453 free(Y0RMS);
04454 free(RSUNRMS);
04455 free(OBSVRint);
04456 free(OBSVWint);
04457 free(OBSVNint);
04458 free(CRLNOBSint);
04459 free(CRLTOBSint);
04460
04461 free(DSUNOBSint);
04462 free(CROTA2int);
04463 free(RSUNint);
04464 free(CARROTint);
04465 free(QUALITY);
04466 free(QUALITYLEV1);
04467 free(QUALITYlev1);
04468 for(i=0;i<nWavelengths*npolout;i++)
04469 {
04470 free(DATAMINS[i]);
04471 free(DATAMAXS[i]);
04472 free(DATAMEDNS[i]);
04473 free(DATAMEANS[i]);
04474 free(DATARMSS[i]);
04475 free(DATASKEWS[i]);
04476 free(DATAMINS2[i]);
04477 free(DATAMAXS2[i]);
04478 free(DATAMEDNS2[i]);
04479 free(DATAMEANS2[i]);
04480 free(DATARMSS2[i]);
04481 free(DATASKEWS2[i]);
04482 free(TOTVALSS[i]);
04483 free(MISSVALSS[i]);
04484 free(DATAVALSS[i]);
04485 }
04486 free(DATAMINS);
04487 free(DATAMAXS);
04488 free(DATAMEDNS);
04489 free(DATAMEANS);
04490 free(DATARMSS);
04491 free(DATASKEWS);
04492 free(DATAMINS2);
04493 free(DATAMAXS2);
04494 free(DATAMEDNS2);
04495 free(DATAMEANS2);
04496 free(DATARMSS2);
04497 free(DATASKEWS2);
04498 free(TOTVALSS);
04499 free(MISSVALSS);
04500 free(DATAVALSS);
04501
04502 free_interpol(&const_param);
04503 status = free_polcal(&pars);
04504 free(Mask);
04505 }
04506
04507 free(CODEVERSION);
04508 free(CODEVERSION1);
04509 free(CODEVERSION2);
04510 free(CODEVERSION3);
04511 free(DISTCOEFFILEF);
04512 free(DISTCOEFFILES);
04513 free(ROTCOEFFILE);
04514 free(DISTCOEFPATH);
04515 free(ROTCOEFPATH);
04516
04517 printf("END PROGRAM\n");
04518
04519 status=0;
04520 return status;
04521
04522 }
04523
04524