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_dark";
00015
00016
00017 ModuleArgs_t module_args[] =
00018 {
00019 {ARG_STRING, "instrument"},
00020 {ARG_STRING, "series_dark", ""},
00021 {ARG_STRING, "file_dark"},
00022 {ARG_INT, "camera", "0"},
00023 {ARG_STRING, "wave_str", "dd"},
00024 {ARG_TIME, "t_obs"},
00025 {ARG_INTS, "fsn_list_dark", "-1,-1"},
00026 {ARG_INT, "nx", "4096"},
00027 {ARG_INT, "ny", "4096"},
00028 {ARG_END}
00029 };
00030
00031
00032
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 int DoIt(void)
00045 {
00046
00047 int i,j,k;
00048 int status = DRMS_SUCCESS;
00049
00050 const char *input_dark;
00051 input_dark = cmdparams_get_str(&cmdparams, "file_dark", NULL);
00052
00053 const char *instrument;
00054 instrument=cmdparams_get_str(&cmdparams, "instrument", &status);
00055
00056 if (strcmp(instrument,"HMI") != 0 && strcmp(instrument,"AIA") != 0){printf("Instrument invalid\n"); exit(EXIT_FAILURE);}
00057
00058 int inst_hmi=!strcmp(instrument,"HMI");
00059 int inst_aia=!strcmp(instrument,"AIA");
00060
00061
00062
00063 const char *wavelength;
00064 int vvd=0, vvv=-1;
00065 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};
00066
00067 if (inst_aia)
00068 {
00069 wavelength=cmdparams_get_str(&cmdparams, "wave_str", &status);
00070 char **wavestrs=(char **)(malloc(23*sizeof(char *)));
00071
00072
00073 wavestrs[0]="131_THIN";
00074 wavestrs[1]="131_THICK";
00075 wavestrs[2]="131_OPEN";
00076 wavestrs[3]="335_THIN";
00077 {char *wv="335_THICK"; wavestrs[4]=wv;}
00078 {char *wv="335_OPEN"; wavestrs[5]=wv;}
00079 {char *wv="193_THIN"; wavestrs[6]=wv;}
00080 {char *wv="193_THICK"; wavestrs[7]=wv;}
00081 {char *wv="193_OPEN"; wavestrs[8]=wv;}
00082 {char *wv="211_THIN"; wavestrs[9]=wv;}
00083 {char *wv="211_THICK"; wavestrs[10]=wv;}
00084 {char *wv="211_OPEN"; wavestrs[11]=wv;}
00085 {char *wv="171_THIN"; wavestrs[12]=wv;}
00086 {char *wv="171_THICK"; wavestrs[13]=wv;}
00087 {char *wv="1600"; wavestrs[14]=wv;}
00088 {char *wv="1700"; wavestrs[15]=wv;}
00089 {char *wv="4500"; wavestrs[16]=wv;}
00090 {char *wv="94_THIN"; wavestrs[17]=wv;}
00091 {char *wv="94_THICK"; wavestrs[18]=wv;}
00092 {char *wv="94_OPEN"; wavestrs[19]=wv;}
00093 {char *wv="304_THIN"; wavestrs[20]=wv;}
00094 {char *wv="304_THICK"; wavestrs[21]=wv;}
00095 {char *wv="304_OPEN"; wavestrs[22]=wv;}
00096
00097
00098
00099
00100
00101 for (i=0; i<23; ++i){vvd += !strcmp(wavestrs[i], wavelength); if (!strcmp(wavestrs[i], wavelength)) vvv=i;}
00102 if (vvd == 0){printf("nonexisting wavelength id\n"); exit(EXIT_FAILURE);} else {printf("wavelength number %d\n", vvv);}
00103 }
00104
00105
00106
00107
00108 int cam_id=cmdparams_get_int(&cmdparams, "camera", NULL);
00109
00110 char *camera_string;
00111
00112 if (inst_hmi){
00113 if (cam_id == 1) camera_string="HMI_SIDE1";
00114 if (cam_id == 2) camera_string="HMI_FRONT2";
00115 if (cam_id < 1 || cam_id > 2){printf("wrong camera id for HMI\n"); exit(EXIT_FAILURE);}
00116 }
00117
00118 if (inst_aia){
00119 if (aia_cam[vvv] == 1) camera_string="AIA_ATA1";
00120 if (aia_cam[vvv] == 2) camera_string="AIA_ATA2";
00121 if (aia_cam[vvv] == 3) camera_string="AIA_ATA3";
00122 if (aia_cam[vvv] == 4) camera_string="AIA_ATA4";
00123
00124 }
00125 TIME t_dark=cmdparams_get_time(&cmdparams, "t_obs", NULL);
00126
00127
00128 int *fsn_list_dark;
00129 fsn_list_dark=(int *)(malloc(sizeof(int)*256));
00130 int ndark=cmdparams_get_intarr(&cmdparams, "fsn_list_dark", &fsn_list_dark, NULL);
00131
00132 if (ndark <1){ printf("wrong number of dark frames\n"); exit(EXIT_FAILURE);}
00133 if (ndark > 256){ printf("too many dark frames\n"); exit(EXIT_FAILURE);}
00134 char fsn_string_dark[13*256]={""};
00135 for (k=0; k<ndark; ++k)
00136 {
00137 char ffnumb[12]={""};
00138 sprintf(ffnumb, "%12d", fsn_list_dark[k]);
00139 strcat(fsn_string_dark, ffnumb);
00140 if (k<(ndark-1)) strcat(fsn_string_dark, ",");
00141 }
00142
00143
00144 int fsn_first_dark=fsn_list_dark[0];
00145 int fsn_last_dark=fsn_list_dark[ndark-1];
00146
00147
00148
00149
00150 const char *series_name_dark;
00151 series_name_dark=cmdparams_get_str(&cmdparams, "series_dark", NULL);
00152
00153
00154 int stat = DRMS_SUCCESS;
00155 int error = 0;
00156
00157
00158 DRMS_RecordSet_t *dataout, *dataout_dark;
00159
00160 DRMS_Record_t *recout = NULL;
00161 DRMS_Segment_t *segout = NULL;
00162 DRMS_Array_t *arrout_dark;
00163 long long recnum_dark;
00164 DRMS_Type_t type = DRMS_TYPE_FLOAT;
00165 DRMS_Type_t type_time = DRMS_TYPE_TIME;
00166 size_t bytes_read;
00167 FILE *fgram;
00168 float *gout1;
00169
00170
00171 int nx=cmdparams_get_int(&cmdparams, "nx", NULL);
00172 int ny=cmdparams_get_int(&cmdparams, "ny", NULL);
00173
00174 int axisin[2];
00175 int axisout[2]={nx,ny};
00176
00177
00178
00179
00180 const char *primekey1 = "CAMERA";
00181 const char *primekey2 = "T_OBS";
00182
00183
00184 const char *key1 = "FSN_FIRST";
00185 const char *key2 = "FSN_LAST";
00186 const char *key3 = "FSN_INPUT";
00187
00188 const char *key6="INSTRUME";
00189
00190 const char *key1aia="WAVE_STR";
00191
00192
00193 printf("START!\n");
00194
00195
00196
00197
00198
00199
00200 drms_series_exists(drms_env, series_name_dark, &status);
00201 if (status == DRMS_ERROR_UNKNOWNSERIES)
00202 {
00203 printf("Dark series %s doesn't exist\n",series_name_dark);
00204 exit(EXIT_FAILURE);
00205 }
00206 if (status == DRMS_SUCCESS)
00207 {
00208 printf("Dark series %s exists.\n",series_name_dark);
00209 }
00210
00211
00212
00213 float *f_dark;
00214 fgram=fopen(input_dark, "rb");
00215 if (fgram==NULL){fputs("File error", stderr); printf("Could not read file\n"); exit(EXIT_FAILURE);}
00216 f_dark=(float *)(malloc(nx*ny*sizeof(float)));
00217 bytes_read=fread(f_dark,sizeof(float),nx*ny,fgram);
00218 fclose(fgram);
00219
00220
00221 arrout_dark = drms_array_create(type,2,axisout,NULL,&status);
00222 if (status != 0 || arrout_dark == NULL){printf("could not create array\n"); exit(EXIT_FAILURE);}
00223
00224 gout1=arrout_dark->data;
00225
00226 dataout_dark = drms_create_records(drms_env,1,(char *)series_name_dark, DRMS_PERMANENT, &stat);
00227 if (stat !=0 || dataout_dark == NULL){printf("could not create dark\n"); exit(EXIT_FAILURE);}
00228
00229
00230 for (j=0; j<ny; ++j) for (i=0; i<nx; ++i) gout1[j*nx+i]=f_dark[j*nx+i];
00231
00232 if (stat != DRMS_SUCCESS)
00233 {
00234 printf("Could not create a record for the series %s\n", series_name_dark);
00235 exit(EXIT_FAILURE);
00236 }
00237
00238 if (stat == DRMS_SUCCESS)
00239 {
00240 printf("Writing a record on the DRMS for the series %s\n", series_name_dark);
00241
00242 recout = dataout_dark->records[0];
00243 recnum_dark=recout->recnum;
00244
00245 status=0;
00246 status += drms_setkey_time(recout, primekey2, t_dark);
00247
00248 if (inst_hmi) status += drms_setkey_int(recout, primekey1,cam_id);
00249
00250 status += drms_setkey_int(recout,key1,fsn_first_dark);
00251 status += drms_setkey_int(recout,key2,fsn_last_dark);
00252 status += drms_setkey_string(recout, key3, fsn_string_dark);
00253 status += drms_setkey_string(recout, key6, camera_string);
00254
00255
00256 if (inst_aia) status += drms_setkey_string(recout, key1aia, wavelength);
00257
00258
00259 if (status != 0){printf("error setting keywords"); exit(EXIT_FAILURE);}
00260 drms_keyword_setdate(recout);
00261
00262 segout=drms_segment_lookup(recout, "dark");
00263 if (segout == NULL){printf("could not find segment\n"); exit(EXIT_FAILURE);}
00264
00265 status=drms_segment_write(segout, arrout_dark, 0);
00266 if (status != 0){printf("could not write segment\n"); exit(EXIT_FAILURE);}
00267 }
00268
00269 drms_close_records(dataout_dark, DRMS_INSERT_RECORD);
00270 drms_free_array(arrout_dark);
00271 printf("done\n");
00272
00273
00274
00275 printf("COMPLETED!\n");
00276
00277
00278
00279
00280
00281 return 0;
00282
00283
00284
00285 }
00286
00287
00288