00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <math.h>
00013 #include <HMIparam.h>
00014 #include <mkl.h>
00015
00016 char *module_name = "correction_velocities";
00017 #define kRecSetIn "begin" //beginning time for which an output is wanted. MANDATORY PARAMETER.
00018 #define kRecSetIn2 "end" //end time for which an output is wanted. MANDATORY PARAMETER.
00019 #define kTypeSetIn "levin" //series name of the input data
00020 #define kTypeSetOut "levout" //series name of the output data
00021 #define kForced "forced" //forced computation even if tuning changes middway
00022 #define kForced2 "mindata" //forced computation even if the minimum number of hmi.V_45s_nrt data is not available
00023
00024
00025 #define QUAL_ISSTARGET (0x4000) //the ISS loop was OPEN for one or several filtergrams used to produce the observable
00026 #define QUAL_ECLIPSE (0x100) //at least one lev1 record was taken during an eclipse
00027
00028
00029 #define LIGHT_SIDE 2 //SIDE CAMERA
00030 #define LIGHT_FRONT 3 //FRONT CAMERA
00031 #define DARK_SIDE 0 //SIDE CAMERA
00032 #define DARK_FRONT 1 //FRONT CAMERA
00033
00034
00035 ModuleArgs_t module_args[] =
00036 {
00037 {ARG_STRING, kRecSetIn, "", "beginning time for which an output is wanted"},
00038 {ARG_STRING, kRecSetIn2, "", "end time for which an output is wanted"},
00039 {ARG_STRING, kTypeSetIn, "", "series name of input data"},
00040 {ARG_STRING, kTypeSetOut,"", "series name of output data"},
00041 {ARG_INT, kForced, "0", "force computation even if tuning changes midway.Default =0. Set to 1 to force."},
00042 {ARG_INT, kForced2,"0", "force computation even if minimum number of input data is not available.Default =0. Set to 1 to force."},
00043 {ARG_END}
00044 };
00045
00046
00047
00048
00049
00050
00051
00052
00053 void lininterp1f(double *yinterp, double *xv, double *yv, double *x, double ydefault, int m, int minterp)
00054 {
00055 int i, j;
00056 int nrowsinterp, nrowsdata;
00057 nrowsinterp = minterp;
00058 nrowsdata = m;
00059 for (i=0; i<nrowsinterp; i++)
00060 {
00061 if((x[i] < xv[0]) || (x[i] > xv[nrowsdata-1])) yinterp[i] = ydefault;
00062 else
00063 {
00064 for(j=1; j<nrowsdata; j++)
00065 {
00066 if(x[i]<=xv[j])
00067 {
00068 yinterp[i] = (x[i]-xv[j-1]) / (xv[j]-xv[j-1]) * (yv[j]-yv[j-1]) + yv[j-1];
00069 break;
00070 }
00071 }
00072 }
00073 }
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 int DoIt(void) {
00086
00087 #define MaxNString 256
00088 #define mindata 1600
00089
00090 int errbufstat = setvbuf(stderr, NULL, _IONBF, BUFSIZ);
00091 int outbufstat = setvbuf(stdout, NULL, _IONBF, BUFSIZ);
00092
00093 char *inRecQuery = cmdparams_get_str(&cmdparams, kRecSetIn, NULL);
00094 char *inRecQuery2 = cmdparams_get_str(&cmdparams, kRecSetIn2, NULL);
00095 char *inLev = cmdparams_get_str(&cmdparams, kTypeSetIn, NULL);
00096 char *outLev = cmdparams_get_str(&cmdparams, kTypeSetOut, NULL);
00097 int forced = cmdparams_get_int(&cmdparams, kForced, NULL);
00098 int forced2 = cmdparams_get_int(&cmdparams, kForced2,NULL);
00099
00100 char HMISeriesLev1[MaxNString];
00101 char uplo[] = "U";
00102 char *RAWMEDNS = "RAWMEDN";
00103 char *OBSVRS = "OBS_VR";
00104 char *QUALITYS = "QUALITY";
00105 char *DATAUSEDS= "DATAUSED";
00106 char *CALFSNS = "CAL_FSN";
00107 char *DATES = "DATE";
00108 char *TRECS = "T_REC";
00109 char *TSTARTS = "T_START";
00110 char *TSTOPS = "T_STOP";
00111 char *COEFF0S = "COEFF0";
00112 char *COEFF1S = "COEFF1";
00113 char *COEFF2S = "COEFF2";
00114 char *COEFF3S = "COEFF3";
00115 char *NDATAS = "NDATA";
00116 char *CROTA2S = "CROTA2";
00117 char *MISSVALS = "MISSVALS";
00118
00119 double *RAWMEDN=NULL;
00120 double *OBSVR=NULL;
00121 double *A=NULL,*coeffd=NULL;
00122 double temp=0.0;
00123 float *CROTA2=NULL;
00124 int *MISSVAL=NULL;
00125
00126 int *QUALITY=NULL;
00127 int *CALFSN=NULL;
00128 int ncoeff=4;
00129 int nRecs1=0;
00130 int ngood,info;
00131 int ione = 1,i,j;
00132 int malign=32,nsample=0;
00133 int error=0,status=0;
00134
00135 DRMS_RecordSet_t *recLev1 = NULL;
00136
00137 TIME TREC;
00138
00139
00140 strcpy(HMISeriesLev1,inLev);
00141 strcat(HMISeriesLev1,"[");
00142 strcat(HMISeriesLev1,inRecQuery);
00143 strcat(HMISeriesLev1,"-");
00144 strcat(HMISeriesLev1,inRecQuery2);
00145 strcat(HMISeriesLev1,"]");
00146 printf("LEVEL 1 SERIES QUERY = %s\n",HMISeriesLev1);
00147
00148 recLev1 = drms_open_records(drms_env,HMISeriesLev1,&status);
00149 if (status != DRMS_SUCCESS || recLev1 == NULL || recLev1->n == 0)
00150 {
00151 printf("could not open lev1 records\n");
00152 return 1;
00153 }
00154 nRecs1=recLev1->n;
00155
00156 RAWMEDN = (double *)malloc(nRecs1*sizeof(double));
00157 if(RAWMEDN == NULL)
00158 {
00159 printf("Error: memory could not be allocated to RAWMEDN\n");
00160 return 1;
00161 }
00162 OBSVR = (double *)malloc(nRecs1*sizeof(double));
00163 if(OBSVR == NULL)
00164 {
00165 printf("Error: memory could not be allocated to OBSVR\n");
00166 return 1;
00167 }
00168 QUALITY = (int *)malloc(nRecs1*sizeof(int));
00169 if(QUALITY == NULL)
00170 {
00171 printf("Error: memory could not be allocated to QUALITY\n");
00172 return 1;
00173 }
00174 CALFSN = (int *)malloc(nRecs1*sizeof(int));
00175 if(CALFSN == NULL)
00176 {
00177 printf("Error: memory could not be allocated to CALFSN\n");
00178 return 1;
00179 }
00180 CROTA2 = (float *)malloc(nRecs1*sizeof(float));
00181 if(CROTA2 == NULL)
00182 {
00183 printf("Error: memory could not be allocated to CROTA2\n");
00184 return 1;
00185 }
00186
00187 MISSVAL = (int *) malloc(nRecs1*sizeof(int));
00188 if(MISSVAL == NULL)
00189 {
00190 printf("Error: memory could not be allocated to MISSVAL\n");
00191 return 1;
00192 }
00193
00194 nsample=nRecs1;
00195 j=0;
00196 for(i=0;i<nRecs1;++i)
00197 {
00198 OBSVR[j] = drms_getkey_double(recLev1->records[i],OBSVRS, &status);
00199 RAWMEDN[j]= drms_getkey_double(recLev1->records[i],RAWMEDNS,&status);
00200 QUALITY[j]= drms_getkey_int(recLev1->records[i],QUALITYS,&status);
00201
00202 temp = RAWMEDN[j];
00203 RAWMEDN[j]= RAWMEDN[j]-OBSVR[j];
00204 OBSVR[j] = temp/6500.;
00205 CALFSN[j] = drms_getkey_int(recLev1->records[i],CALFSNS,&status);
00206 CROTA2[j] = drms_getkey_float(recLev1->records[i],CROTA2S,&status);
00207 MISSVAL[j]= drms_getkey_int(recLev1->records[i],MISSVALS,&status);
00208
00209 if(isnan(RAWMEDN[j]) || isnan(OBSVR[j]) || (QUALITY[j] & QUAL_ISSTARGET) == QUAL_ISSTARGET || fabs(RAWMEDN[j]-OBSVR[j]) > 1000. || (QUALITY[j] & QUAL_ECLIPSE) == QUAL_ECLIPSE || fabs(CROTA2[j]-180.) > 5.0 || MISSVAL[j] > 10000)
00210 {
00211 j-=1;
00212 nsample-=1;
00213 }
00214
00215
00216
00217 if(j > 0)
00218 {
00219 if(forced == 0)
00220 {
00221
00222 if(CALFSN[j] != CALFSN[j-1])
00223 {
00224 printf("Error: the look-up tables used to calculate the observables changed during the time range\n");
00225 return 1;
00226 }
00227
00228 }
00229 if(forced == 1)
00230 {
00231 if(CALFSN[j] != CALFSN[j-1])
00232 {
00233 printf("Error: the look-up tables used to calculate the observables changed during the time range\n");
00234 j-=1;
00235 nsample-=1;
00236 }
00237 }
00238 }
00239
00240 j++;
00241 }
00242
00243
00244 printf("NUMBER OF DATA REJECTED: %d %d\n",nRecs1-nsample,nsample);
00245
00246
00247 if(nsample < mindata)
00248 {
00249 printf("NUMBER OF AVAILABLE DATA %d IS BELOW MINIMUM NUMBER %d\n",nsample,mindata);
00250 printf("IF THE CODE IS NOT FORCED, IT WILL NOT PRODUCE ANY OUTPUT RECORD\n");
00251 if(forced2 == 0) return 1;
00252 }
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 A = (double *)(MKL_malloc(ncoeff*ncoeff*sizeof(double),malign));
00264 coeffd= (double *)(MKL_malloc(ncoeff*sizeof(double),malign));
00265
00266
00267 double sum =0.0;
00268 double sumx =0.0;
00269 double sumx2=0.0;
00270 double sumx3=0.0;
00271 double sumx4=0.0;
00272 double sumx5=0.0;
00273 double sumx6=0.0;
00274
00275 for(i=0;i<nsample;i++)
00276 {
00277 sum +=1;
00278 sumx +=OBSVR[i];
00279 sumx2+=OBSVR[i]*OBSVR[i];
00280 sumx3+=OBSVR[i]*OBSVR[i]*OBSVR[i];
00281 sumx4+=OBSVR[i]*OBSVR[i]*OBSVR[i]*OBSVR[i];
00282 sumx5+=OBSVR[i]*OBSVR[i]*OBSVR[i]*OBSVR[i]*OBSVR[i];
00283 sumx6+=OBSVR[i]*OBSVR[i]*OBSVR[i]*OBSVR[i]*OBSVR[i]*OBSVR[i];
00284 }
00285
00286
00287 A[0] = sum;
00288 A[0+1] = sumx;
00289 A[0+2] = sumx2;
00290 A[0+3] = sumx3;
00291 A[1*4+0] = sumx;
00292 A[1*4+1] = sumx2;
00293 A[1*4+2] = sumx3;
00294 A[1*4+3] = sumx4;
00295 A[2*4+0] = sumx2;
00296 A[2*4+1] = sumx3;
00297 A[2*4+2] = sumx4;
00298 A[2*4+3] = sumx5;
00299 A[3*4+0] = sumx3;
00300 A[3*4+1] = sumx4;
00301 A[3*4+2] = sumx5;
00302 A[3*4+3] = sumx6;
00303
00304
00305 sum =0.0;
00306 sumx =0.0;
00307 sumx2=0.0;
00308 sumx3=0.0;
00309
00310 for(i=0;i<nsample;i++)
00311 {
00312 sum +=RAWMEDN[i];
00313 sumx +=RAWMEDN[i]*OBSVR[i];
00314 sumx2+=RAWMEDN[i]*OBSVR[i]*OBSVR[i];
00315 sumx3+=RAWMEDN[i]*OBSVR[i]*OBSVR[i]*OBSVR[i];
00316 }
00317
00318 coeffd[0]=sum;
00319 coeffd[1]=sumx;
00320 coeffd[2]=sumx2;
00321 coeffd[3]=sumx3;
00322
00323
00324 printf("A before Cholesky decomposition\n");
00325 for(i=0;i<ncoeff;i++) printf("[%f,%f,%f,%f]\n",A[i*ncoeff+0],A[i*ncoeff+1],A[i*ncoeff+2],A[i*ncoeff+3]);
00326
00327 dpotrf(uplo,&ncoeff,A,&ncoeff,&info);
00328
00329 printf("A after Cholesky decomposition\n");
00330 for(i=0;i<ncoeff;i++) printf("[%f,%f,%f,%f]\n",A[i*ncoeff+0],A[i*ncoeff+1],A[i*ncoeff+2],A[i*ncoeff+3]);
00331
00332 dpotrs(uplo,&ncoeff,&ione,A,&ncoeff,coeffd,&ncoeff,&info);
00333
00334 coeffd[1]=coeffd[1]/6500;
00335 coeffd[2]=coeffd[2]/6500.0/6500.0;
00336 coeffd[3]=coeffd[3]/6500/6500./6500.;
00337
00338 for(i=0;i<ncoeff;i++) printf("COEFFICIENT: %+20.12e\n",coeffd[i]);
00339
00340
00341 DRMS_RecordSet_t *dataout = NULL;
00342 DRMS_Record_t *recout = NULL;
00343
00344 dataout = drms_create_records(drms_env,1,outLev,DRMS_PERMANENT,&status);
00345 if (status != DRMS_SUCCESS)
00346 {
00347 printf("Could not create a record for the polynomial coefficients\n");
00348 exit(EXIT_FAILURE);
00349 }
00350 if (status == DRMS_SUCCESS)
00351 {
00352 printf("Writing a record on the DRMS for the polynomial coefficients\n");
00353 recout = dataout->records[0];
00354
00355
00356 TREC=(sscan_time(inRecQuery)+sscan_time(inRecQuery2))/2.0;
00357 status = drms_setkey_time(recout,TRECS,TREC);
00358 status = drms_setkey_time(recout,TSTARTS,sscan_time(inRecQuery));
00359 status = drms_setkey_time(recout,TSTOPS,sscan_time(inRecQuery2));
00360 char DATEOBS[256];
00361 sprint_time(DATEOBS,CURRENT_SYSTEM_TIME,"UTC",1);
00362 status = drms_setkey_string(recout,DATES,DATEOBS);
00363 status = drms_setkey_string(recout,DATAUSEDS,HMISeriesLev1);
00364 status = drms_setkey_double(recout,COEFF0S,coeffd[0]);
00365 status = drms_setkey_double(recout,COEFF1S,coeffd[1]);
00366 status = drms_setkey_double(recout,COEFF2S,coeffd[2]);
00367 status = drms_setkey_double(recout,COEFF3S,coeffd[3]);
00368 status = drms_setkey_int(recout,NDATAS,nsample);
00369 status = drms_setkey_int(recout,CALFSNS,CALFSN[0]);
00370
00371
00372 drms_close_records(dataout, DRMS_INSERT_RECORD);
00373 }
00374
00375 MKL_free(coeffd);
00376 MKL_free(A);
00377
00378 free(RAWMEDN);
00379 free(OBSVR);
00380 free(QUALITY);
00381 free(CALFSN);
00382 free(CROTA2);
00383 free(MISSVAL);
00384
00385 return error;
00386
00387 }