00001
00002
00003
00004
00005 #include <stdlib.h>
00006 #include <stdio.h>
00007 #include <math.h>
00008 #include <jsoc_main.h>
00009 #include <string.h>
00010 #include <time.h>
00011
00012
00013
00014 char *module_name = "write_offpoint";
00015
00016
00017 ModuleArgs_t module_args[] =
00018 {
00019 {ARG_STRING, "instrument"},
00020 {ARG_STRING, "series_offpoint"},
00021 {ARG_STRING,"file_offpoint"},
00022 {ARG_INT, "camera", "0"},
00023 {ARG_STRING, "wave_str", "dd"},
00024 {ARG_INT, "focus", "0"},
00025 {ARG_TIME, "t_obs"},
00026 {ARG_INTS, "fsn_list_offpoint", "-1,-1"},
00027 {ARG_INTS, "fsn_list_pzt", "-1,-1"},
00028 {ARG_INT, "pztflag", "1"},
00029 {ARG_INT, "nx", "4096"},
00030 {ARG_INT, "ny", "4096"},
00031 {ARG_END}
00032 };
00033
00034
00035
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 int DoIt(void)
00049 {
00050
00051 int status=DRMS_SUCCESS;
00052 int i,j,k;
00053 const char *input_offpoint;
00054 input_offpoint = cmdparams_get_str(&cmdparams, "file_offpoint", &status);
00055
00056 const char *instrument;
00057 instrument=cmdparams_get_str(&cmdparams, "instrument", &status);
00058
00059 if (strcmp(instrument,"HMI") != 0 && strcmp(instrument,"AIA") != 0){printf("Instrument invalid\n"); exit(EXIT_FAILURE);}
00060
00061 int inst_hmi=!strcmp(instrument,"HMI");
00062 int inst_aia=!strcmp(instrument,"AIA");
00063
00064
00065 const char *wavelength;
00066 int vvd=0, vvv=-1;
00067 short aia_cam[23]={1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,4};
00068
00069 if (inst_aia)
00070 {
00071 wavelength=cmdparams_get_str(&cmdparams, "wave_str", &status);
00072
00073 char **wavestrs=(char **)(malloc(23*sizeof(char *)));
00074
00075
00076 wavestrs[0]="131_THIN";
00077 wavestrs[1]="131_THICK";
00078 wavestrs[2]="131_OPEN";
00079 wavestrs[3]="335_THIN";
00080 {char *wv="335_THICK"; wavestrs[4]=wv;}
00081 {char *wv="335_OPEN"; wavestrs[5]=wv;}
00082 {char *wv="193_THIN"; wavestrs[6]=wv;}
00083 {char *wv="193_THICK"; wavestrs[7]=wv;}
00084 {char *wv="193_OPEN"; wavestrs[8]=wv;}
00085 {char *wv="211_THIN"; wavestrs[9]=wv;}
00086 {char *wv="211_THICK"; wavestrs[10]=wv;}
00087 {char *wv="211_OPEN"; wavestrs[11]=wv;}
00088 {char *wv="171_THIN"; wavestrs[12]=wv;}
00089 {char *wv="171_THICK"; wavestrs[13]=wv;}
00090 {char *wv="1600"; wavestrs[14]=wv;}
00091 {char *wv="1700"; wavestrs[15]=wv;}
00092 {char *wv="4500"; wavestrs[16]=wv;}
00093 {char *wv="94_THIN"; wavestrs[17]=wv;}
00094 {char *wv="94_THICK"; wavestrs[18]=wv;}
00095 {char *wv="94_OPEN"; wavestrs[19]=wv;}
00096 {char *wv="304_THIN"; wavestrs[20]=wv;}
00097 {char *wv="304_THICK"; wavestrs[21]=wv;}
00098 {char *wv="304_OPEN"; wavestrs[22]=wv;}
00099
00100
00101
00102
00103
00104 for (i=0; i<23; ++i){vvd += !strcmp(wavestrs[i], wavelength); if (!strcmp(wavestrs[i], wavelength)) vvv=i;}
00105 if (vvd == 0){printf("nonexisting wavelength id\n"); exit(EXIT_FAILURE);} else {printf("number %d\n", vvv);}
00106 }
00107
00108 int foc_id;
00109 if (inst_hmi)foc_id=cmdparams_get_int(&cmdparams, "focus", &status);
00110
00111 int pztflag;
00112 if (inst_hmi)pztflag=cmdparams_get_int(&cmdparams, "pztflag", &status);
00113
00114 int cam_id;
00115 cam_id=cmdparams_get_int(&cmdparams, "camera", &status);
00116
00117 char *camera_string;
00118
00119 if (inst_hmi)
00120 {
00121 if (cam_id == 1) camera_string="HMI_SIDE1";
00122 if (cam_id == 2) camera_string="HMI_FRONT2";
00123 if (cam_id < 1 || cam_id > 2){printf("wrong camera id for HMI\n"); exit(EXIT_FAILURE);}
00124 }
00125
00126 if (inst_aia)
00127 {
00128 if (aia_cam[vvv] == 1) camera_string="AIA_ATA1";
00129 if (aia_cam[vvv] == 2) camera_string="AIA_ATA2";
00130 if (aia_cam[vvv] == 3) camera_string="AIA_ATA3";
00131 if (aia_cam[vvv] == 4) camera_string="AIA_ATA4";
00132 }
00133
00134
00135
00136 TIME t_offpoint=cmdparams_get_time(&cmdparams, "t_obs", &status);
00137
00138
00139 int *fsn_list_offset;
00140 fsn_list_offset=(int *)(malloc(sizeof(int)*1024));
00141 int noffset=cmdparams_get_intarr(&cmdparams, "fsn_list_offpoint", &fsn_list_offset, &status);
00142
00143 int *fsn_list_pzt;
00144 fsn_list_pzt=(int *)(malloc(sizeof(int)*1024));
00145 int npzt=cmdparams_get_intarr(&cmdparams, "fsn_list_pzt", &fsn_list_pzt, &status);
00146
00147
00148 int fsn_first=fsn_list_offset[0];
00149 int fsn_last=fsn_list_offset[noffset-1];
00150
00151
00152 char fsn_string[13*1024]={""};
00153 if (noffset > 1024){ printf("too many offpoint frames\n"); exit(EXIT_FAILURE);}
00154
00155
00156 for (k=0; k<noffset; ++k)
00157 {
00158 char ffnumb[12]={""};
00159 sprintf(ffnumb, "%d", fsn_list_offset[k]);
00160 strcat(fsn_string, ffnumb);
00161 if (k<(noffset-1)) strcat(fsn_string, ",");
00162 }
00163
00164 if (fsn_list_pzt[0] != -1)
00165 {
00166 strcat(fsn_string, ";PZT_FSN:");
00167 for (k=0; k<npzt; ++k)
00168 {
00169 char ffnumb[12]={""};
00170 sprintf(ffnumb, "%d", fsn_list_pzt[k]);
00171 strcat(fsn_string, ffnumb);
00172 if (k<(npzt-1)) strcat(fsn_string, ",");
00173 }
00174 }
00175
00176
00177
00178 const char *series_name_offpoint;
00179 series_name_offpoint=cmdparams_get_str(&cmdparams, "series_offpoint", &status);
00180
00181
00182
00183 int stat = DRMS_SUCCESS;
00184 int error = 0;
00185
00186
00187 DRMS_RecordSet_t *dataout, *dataout_off, *dataout_dark, *dataout_bad;
00188
00189 DRMS_Record_t *recout = NULL;
00190 DRMS_Segment_t *segout = NULL;
00191 DRMS_Array_t *arrout, *arrout_off, *arrout_dark, *arrout_bad; ;
00192 long long recnum_off, recnum_dark, recnum_bad;
00193 DRMS_Type_t type = DRMS_TYPE_FLOAT;
00194 DRMS_Type_t type_time = DRMS_TYPE_TIME;
00195 size_t bytes_read;
00196 FILE *fgram;
00197 float *gout1;
00198 int *gout_bad;
00199
00200 int nx=cmdparams_get_int(&cmdparams, "nx", &status);
00201 int ny=cmdparams_get_int(&cmdparams, "ny", &status);
00202
00203 int axisin[2];
00204 int axisout[2]={nx,ny};
00205
00206
00207
00208
00209 const char *primekey1 = "CAMERA";
00210 const char *primekey2 = "T_OBS";
00211 const char *primekey3 = "HMI_SEQ_ID_FOCUS";
00212
00213
00214 const char *key1 = "FSN_FIRST";
00215 const char *key2 = "FSN_LAST";
00216 const char *key3 = "FSN_INPUT";
00217
00218 const char *key6="INSTRUME";
00219
00220 const char *key1aia="WAVE_STR";
00221
00222
00223 printf("START!\n");
00224
00225
00226
00227
00228
00229
00230
00231
00232 drms_series_exists(drms_env, series_name_offpoint, &status);
00233 if (status == DRMS_ERROR_UNKNOWNSERIES)
00234 {
00235 printf("Output series %s doesn't exist\n",series_name_offpoint);
00236 exit(EXIT_FAILURE);
00237 }
00238 if (status == DRMS_SUCCESS)
00239 {
00240 printf("Output series %s exists.\n",series_name_offpoint);
00241 }
00242
00243
00244
00245
00246
00247 printf("Reading OFFPOINT flatfield\n");
00248
00249 float *f_offpoint;
00250
00251 fgram=fopen(input_offpoint, "rb");
00252
00253 if (fgram==NULL){fputs("File error", stderr); printf("Could not read file\n"); exit(EXIT_FAILURE);}
00254 f_offpoint=(float *)(malloc(nx*ny*sizeof(float)));
00255 bytes_read=fread(f_offpoint,sizeof(float),nx*ny,fgram);
00256
00257 fclose(fgram);
00258 printf("done\n");
00259
00260
00261
00262 arrout_off = drms_array_create(type,2,axisout,NULL,&status);
00263 if (status !=0 || arrout_off == NULL){printf("could not create array\n"); exit(EXIT_FAILURE);}
00264
00265 gout1=arrout_off->data;
00266
00267
00268 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) gout1[j*nx+i]=f_offpoint[j*nx+i];
00269 dataout_off = drms_create_records(drms_env,1,(char *)series_name_offpoint,DRMS_PERMANENT,&stat);
00270
00271
00272 if (stat != DRMS_SUCCESS)
00273 {
00274 printf("Could not create a record for the series %s\n",series_name_offpoint);
00275 exit(EXIT_FAILURE);
00276 }
00277
00278 if (stat == DRMS_SUCCESS)
00279 {
00280 printf("Writing a record on the DRMS for the series %s\n",series_name_offpoint);
00281 recout= dataout_off->records[0];
00282 recnum_off=recout->recnum;
00283
00284
00285 status=0;
00286 status += drms_setkey_time(recout, primekey2, t_offpoint);
00287 if (inst_hmi) status += drms_setkey_int(recout, primekey1, cam_id);
00288 if (inst_hmi) status += drms_setkey_int(recout, primekey3, foc_id);
00289 if (inst_hmi) status += drms_setkey_int(recout, "PZTFLAG", pztflag);
00290
00291 if (inst_aia) status += drms_setkey_string(recout, key1aia, wavelength);
00292
00293 status += drms_setkey_int(recout, key1, fsn_first);
00294 status += drms_setkey_int(recout, key2, fsn_last);
00295 status += drms_setkey_string(recout, key3, fsn_string);
00296 status += drms_setkey_string(recout, key6, camera_string);
00297
00298 if (status != 0){printf("error setting keywords"); exit(EXIT_FAILURE);}
00299 drms_keyword_setdate(recout);
00300
00301
00302 segout = drms_segment_lookup(recout, "offpoint_flatfield");
00303 if (segout == NULL){printf("could not find segment\n"); exit(EXIT_FAILURE);}
00304
00305 status=drms_segment_write(segout, arrout_off, 0);
00306 if (status != 0){printf("could not write segment\n"); exit(EXIT_FAILURE);}
00307
00308 }
00309
00310 drms_close_records(dataout_off, DRMS_INSERT_RECORD);
00311 printf("done\n");
00312
00313
00314
00315
00316
00317
00318 drms_free_array(arrout_off);
00319
00320
00321
00322
00323 printf("COMPLETED!\n");
00324
00325
00326
00327
00328
00329 return 0;
00330
00331
00332
00333 }
00334
00335