00001
00002
00003
00004 #include <jsoc_main.h>
00005 #include <cmdparams.h>
00006 #include <drms.h>
00007 #include <drms_names.h>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <HMIparam.h>
00012 #include "drms_defs.h"
00013 #include "drms_fitsrw.h"
00014 #include "fstats.h"
00015 #include <ctype.h>
00016 #include <sys/types.h>
00017 #include <sys/time.h>
00018 #include <sys/stat.h>
00019 #include <unistd.h>
00020 #include <printk.h>
00021 #include "/home/jsoc/cvs/Development/JSOC/proj/libs/astro/astro.h"
00022 #include <fresize.h>
00023 #include <gapfill.h>
00024 #include "/home/jsoc/cvs/Development/JSOC/proj/lev0/apps/imgdecode.h"
00025 #include "/home/jsoc/cvs/Development/JSOC/proj/lev0/apps/lev0lev1.h"
00026 #include "/home/jsoc/cvs/Development/JSOC/proj/lev0/apps/limb_fit.h"
00027
00028 char *module_name = "ingest_dcon_gen";
00029
00030
00031 ModuleArgs_t module_args[] =
00032 {
00033 {ARG_STRING,"fits", "", "FITS file to be ingested into the series"},
00034 {ARG_STRING,"out","", "Output series"},
00035 {ARG_STRING,"inRec", "", "input query"},
00036 {ARG_END}
00037 };
00038
00039 #include "/home/jsoc/cvs/Development/JSOC/proj/lev0/apps/limb_fit_function.c"
00040
00041
00042
00043 int heightformation(int FID, double OBSVR, float *CDELT1, float *RSUN, float *CRPIX1, float *CRPIX2, float CROTA2)
00044 {
00045 int wl=0;
00046 int status=0;
00047 float correction=0.0,correction2=0.0;
00048
00049 wl = (FID/10)%20;
00050
00051 if( (wl >= 0) && (wl < 20) )
00052 {
00053 correction = 0.445*exp(-(wl-10.-(float)OBSVR/(0.690/6173.*3.e8/20.)-0.25)*(wl-10.-(float)OBSVR/(0.690/6173.*3.e8/20.)-0.25)/7.1);
00054 correction2 = 0.39*(-2.0*(wl-10.- (float)OBSVR/(0.690/6173.*3.e8/20.)-0.35)/6.15)*exp(-(wl-10.-(float)OBSVR/(0.690/6173.*3.e8/20.)-0.35)*(wl-10.-(float)OBSVR/(0.690/6173.*3.e8/20.)-0.35)/6.15);
00055 printf("A CDELT1= %f CROTA2= %f OBS_VR= %f RSUN= %f correction= %f \n",*CDELT1,CROTA2,OBSVR,*RSUN,correction);
00056 *CDELT1 = *CDELT1*(*RSUN)/((*RSUN)-correction);
00057 printf("B CDELT1= %f CROTA2= %f OBS_VR= %f \n",*CDELT1,CROTA2,OBSVR);
00058 *RSUN = *RSUN-correction;
00059 *CRPIX1 = *CRPIX1-cos(M_PI-CROTA2*M_PI/180.)*correction2;
00060 *CRPIX2 = *CRPIX2-sin(M_PI-CROTA2*M_PI/180.)*correction2;
00061 }
00062 else status=1;
00063
00064 return status;
00065
00066 }
00067
00068 int DoIt(void)
00069 {
00070 int status = DRMS_SUCCESS;
00071 int nRecs;
00072 DRMS_Type_t type = DRMS_TYPE_FLOAT;
00073 float RSUN_LF=0.0, X0_LF=0.0, Y0_LF=0.0;
00074 double tempRSUN=0.0, tempX0=0.0, tempY0=0.0;
00075 int FID;
00076 double OBSVR;
00077 float CROTA2,CDELT1;
00078
00079 char *allvers = NULL;
00080
00081
00082 char **sets = NULL;
00083 DRMS_RecordSetType_t *settypes = NULL;
00084 char **snames = NULL;
00085 char **filts = NULL;
00086 int nsets = 0;
00087 DRMS_RecQueryInfo_t rsinfo;
00088
00089 char *inputSeries = NULL;
00090
00091 int nSegs;
00092 DRMS_Array_t *image = NULL;
00093 DRMS_Record_t *inputRec = NULL;
00094 DRMS_Record_t *outputRec = NULL;
00095 HIterator_t *lastseg = NULL;
00096 DRMS_Segment_t *inputSeg = NULL;
00097 DRMS_Segment_t *outputSeg = NULL;
00098 DRMS_Segment_t *tSeg = NULL;
00099
00100 char filename[DRMS_MAXSEGFILENAME];
00101
00102 const char *inDir = cmdparams_get_str(&cmdparams, "fits", NULL);
00103 const char *dsout = cmdparams_get_str(&cmdparams, "out", NULL);
00104 const char *inRecQuery = cmdparams_get_str(&cmdparams, "inRec", NULL);
00105
00106
00107 if (drms_record_parserecsetspec(inRecQuery, &allvers, &sets, &settypes, &snames, &filts, &nsets, &rsinfo) != DRMS_SUCCESS)
00108 {
00109 fprintf(stderr, "Invalid input record-set specification.\n");
00110 return EXIT_FAILURE;
00111 }
00112
00113 if (nsets != 1)
00114 {
00115 fprintf(stderr, "Invalid input record-set specification. This module does not support record-set subsets.\n");
00116 return EXIT_FAILURE;
00117 }
00118
00119 if (!snames[0] || strlen(snames[0]) == 0)
00120 {
00121 fprintf(stderr, "Invalid input record-set specification (invalid series name).\n");
00122 return EXIT_FAILURE;
00123 }
00124
00125 inputSeries = strdup(snames[0]);
00126
00127
00128 drms_record_freerecsetspecarr(&allvers, &sets, &settypes, &snames, &filts, nsets);
00129
00130
00131 if (!drms_series_exists(drms_env, dsout, &status))
00132 {
00133 fprintf(stderr, "Output series %s doesn't exist\n",dsout);
00134 return EXIT_FAILURE;
00135 }
00136
00137 printf("Output series %s exists.\n", dsout);
00138
00139
00140 DRMS_RecordSet_t *data = drms_open_records(drms_env, inRecQuery, &status);
00141 if (status == DRMS_SUCCESS && data != NULL && data->n > 0)
00142 {
00143 nRecs = data->n;
00144
00145 if (nRecs != 1)
00146 {
00147 fprintf(stderr, "This module operates on a single input/output record at a time. %d records were specified for input.\n", nRecs);
00148 exit(EXIT_FAILURE);
00149 }
00150
00151 inputRec = data->records[0];
00152
00153 printf("Number of input records satisfying the request = %d.\n", nRecs);
00154 }
00155 else
00156 {
00157 printf("Failure opening at least one input records.\n");
00158 return EXIT_FAILURE;
00159 }
00160
00161
00162 outputRec = drms_create_record(drms_env, dsout, DRMS_PERMANENT, &status);
00163 if (status != DRMS_SUCCESS || !outputRec)
00164 {
00165 fprintf(stderr, "Could not create output record in series %s.\n", dsout);
00166 return EXIT_FAILURE;
00167 }
00168
00169 while ((inputSeg = drms_record_nextseg(inputRec, &lastseg, 0)))
00170 {
00171 if (inputSeg->info->islink)
00172 {
00173 tSeg = drms_segment_lookup(inputRec, inputSeg->info->name);
00174
00175 if (!tSeg)
00176 {
00177
00178 continue;
00179 }
00180 }
00181 else
00182 {
00183 tSeg = inputSeg;
00184 }
00185
00186
00187
00188 if (tSeg->info->protocol == DRMS_TAS)
00189 {
00190 fprintf(stderr, "This module supports FITS segments only (the input series has a TAS segment).\n");
00191 return EXIT_FAILURE;
00192 }
00193 else
00194 {
00195 char fitsPath[PATH_MAX];
00196
00197 snprintf(fitsPath, sizeof(fitsPath), "%s", inDir);
00198
00199 if (rindex(inDir, '/'))
00200 {
00201 fitsPath[sizeof(fitsPath) - 1] = '\0';
00202 }
00203
00204 snprintf(filename, sizeof(filename), "%s/%s%s", inDir, inputSeg->info->name, drms_prot2ext(inputSeg->info->protocol));
00205 }
00206
00207
00208 image = drms_fitsrw_read(drms_env, filename, 0, NULL, &status);
00209
00210 if (status != DRMS_SUCCESS || !image)
00211 {
00212 fprintf(stderr, "Unable to read input FITS file %s.\n", filename);
00213 return EXIT_FAILURE;
00214 }
00215
00216
00217 outputSeg = drms_segment_lookup(outputRec, inputSeg->info->name);
00218
00219 if (!outputSeg)
00220 {
00221 fprintf(stderr, "Unable to find output segment %s.\n", inputSeg->info->name);
00222 return EXIT_FAILURE;
00223 }
00224
00225 image->bzero = outputSeg->bzero;
00226 image->bscale = outputSeg->bscale;
00227 image->israw = 0;
00228
00229 printf("Writing a segment to DRMS, %s.\n", outputSeg->info->name);
00230 if (drms_segment_write(outputSeg, image, 0) != DRMS_SUCCESS)
00231 {
00232 fprintf(stderr, "Unable to write output segment %s.\n", outputSeg->info->name);
00233 return EXIT_FAILURE;
00234 }
00235
00236 drms_free_array(image);
00237 }
00238
00239 if (lastseg)
00240 {
00241 hiter_destroy(&lastseg);
00242 }
00243
00244
00245
00246
00247
00248
00249
00250
00251 if (drms_copykeys(outputRec, inputRec, 0, kDRMS_KeyClass_Explicit) != DRMS_SUCCESS)
00252 {
00253 fprintf(stderr, "Could not copy keywords to output series record.\n");
00254 return EXIT_FAILURE;
00255 }
00256
00257 drms_close_record(outputRec, DRMS_INSERT_RECORD);
00258
00259 return 0;
00260 }