00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 int get_vlist_crop (char *table_list, int *list, int *img_row_length) {
00012 DRMS_Array_t *table;
00013 HContainer_t *keylist;
00014 int status;
00015 short *tblvals;
00016
00017 int addr, addr_lo, addr_hi, row_length, ct, n, pixct;
00018
00019 keylist = NULL;
00020 if (!(table = drms_fitsrw_read (drms_env, table_list, 0, &keylist,
00021 &status))) {
00022 fprintf (stderr, "Error unable to read file %s as FITS\n", table_list);
00023 return -1;
00024 }
00025 tblvals = (short *)table->data;
00026 *img_row_length = tblvals[1];
00027 ct = tblvals[2];
00028 if (table->axis[0] < (3 * ct + 16)) {
00029
00030 fprintf (stderr,"Error: pixel list %s is incomplete\n", table_list);
00031 return -1;
00032 }
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 tblvals += 16;
00043 pixct = 0;
00044 while (ct--) {
00045 addr_lo = *tblvals++;
00046 addr_hi = *tblvals++;
00047 row_length = *tblvals++;
00048 if (addr_lo < 0) addr_lo += 65536;
00049 if (addr_hi < 0) addr_hi += 65536;
00050 if (row_length < 0) row_length += 65536;
00051 n = addr = addr_lo + 65536 * addr_hi;
00052 while (n <= addr + row_length) {
00053 list[pixct++] = n++;
00054 }
00055 }
00056 drms_free_array (table);
00057
00058 return (pixct);
00059 }
00060
00061 char *get_limbcrop_from_keylist (HContainer_t *keylist) {
00062 DRMS_Keyword_t *keyword = NULL;
00063 HIterator_t hit;
00064 unsigned long dpc;
00065 static char limbcrop[5];
00066
00067 hiter_new_sort (&hit, keylist, drms_keyword_ranksort);
00068 while (keyword = hiter_getnext (&hit)) {
00069 if (!strcmp (keyword->info->name, "DPC")) {
00070
00071 dpc = strtol (keyword->value.string_val, NULL, 16);
00072 sprintf (limbcrop, "%04x", (int)(dpc & 0xffff));
00073 hiter_free (&hit);
00074 return limbcrop;
00075 }
00076 }
00077 strcpy (limbcrop, "0000");
00078 hiter_free (&hit);
00079 return limbcrop;
00080 }
00081
00082 char *get_limbcrop_from_keyword (DRMS_Record_t *irec) {
00083 static char limbcrop[5];
00084
00085 int status;
00086
00087 char *dpcstr = drms_getkey_string (irec, "DPC", &status);
00088 unsigned long dpc = strtol (dpcstr, NULL, 16);
00089
00090 if (status) strcpy (limbcrop, "0000");
00091 else {
00092 dpc = strtol (dpcstr, NULL, 16);
00093 sprintf (limbcrop, "%04x", (int)(dpc & 0xffff));
00094 }
00095 return limbcrop;
00096 }
00097
00098 int embed_limbpixels (const char *ifile, DRMS_Record_t *orec, float *cropmin,
00099 float *cropmax) {
00100 static DRMS_Array_t *img, *limbdat;
00101 static float cropmn, cropmx;
00102 static int *vlist;
00103 static int img_vals, ntot, initial = 1;
00104 static unsigned short binparam, limbparam;
00105 static short *blank, *recon;
00106 static char lastcrop[] = {"0000"};
00107 static char filedir[] = {"/home/soi/CM/tables/bin_list"};
00108
00109 DRMS_Segment_t *iseg, *oseg;
00110 HContainer_t *keylist;
00111 int axes[2];
00112 int cols, rows, n;
00113 short *orig;
00114 char *limbcrop;
00115 char bin_list[DRMS_MAXPATHLEN];
00116
00117 int status = 0;
00118
00119 if (initial) {
00120 axes[1] = rows = axes[0] = cols = 1024;
00121 ntot = cols * rows;
00122 vlist = (int *)malloc (ntot * sizeof (int));
00123 blank = (short *)malloc (ntot * sizeof (short));
00124 recon = (short *)malloc (ntot * sizeof (short));
00125 for (n = 0; n < ntot; n++) blank[n] = -32768;
00126
00127 img = drms_array_create (DRMS_TYPE_SHORT, 2, axes, (void *)recon, &status);
00128 initial = 0;
00129 }
00130
00131 oseg = drms_segment_lookupnum (orec, 0);
00132 keylist = NULL;
00133 if (!(limbdat = drms_fitsrw_read (drms_env, ifile, 0, &keylist,
00134 &status))) {
00135 fprintf (stderr, "Error unable to read file %s as FITS \n", ifile);
00136 return status;
00137 }
00138 orig = (short *)limbdat->data;
00139 limbcrop = get_limbcrop_from_keylist (keylist);
00140 if (strcmp (limbcrop, lastcrop)) {
00141
00142 sprintf (bin_list, "%s/limb%s.fits", filedir, limbcrop);
00143 img_vals = get_vlist_crop (bin_list, vlist, &cols);
00144 sscanf (limbcrop, "%04x", &limbparam);
00145 binparam = limbparam & 0x0fff;
00146 cropmn = 0.125 * binparam;
00147 cropmx = cropmn + 0.5 * (limbparam >> 12);
00148 strcpy (lastcrop, limbcrop);
00149 }
00150
00151 recon = (short *)(img->data);
00152 memcpy (recon, blank, ntot * sizeof (short));
00153 for (n = 0; n < img_vals; n++) recon[vlist[n]] = orig[n];
00154
00155 if (drms_segment_write (oseg, img, 0)) {
00156 fprintf (stderr, "Warning: unable to write to output segment; skipped\n");
00157 return 1;
00158 }
00159
00160 *cropmin = cropmn;
00161 *cropmax = cropmx;
00162
00163 return status;
00164 }
00165