(file) Return to sharp.c CVS log (file) (dir) Up to [Development] / JSOC / proj / sharp / apps

   1 xudong 1.1 /*
   2 mbobra 1.29  *  sharp.c   
   3 xudong 1.1   *
   4 xudong 1.23  *	This module creates the pipeline Space Weather Active Region Patches (SHARPs).
   5              *	It is a hard-coded strip-down version of bmap.c.
   6              *	It takes the Mharp and Bharp series and create the following quantities:
   7              *
   8              *      Series 1: Sharp_CEA
   9              *	          CEA remapped magnetogram, bitmap, continuum, doppler (same size in map coordinate, need manual spec?)
  10              *	          CEA remapped vector field (Br, Bt, Bp) (same as above)
  11              *                Space weather indices based on vector cutouts (step 2)
  12              *
  13              *      Series 2: Sharp_cutout:
  14              *	          cutouts of magnetogram, bitmap, continuum, doppler (HARP defined, various sizes in CCD pixels)
  15              *	          cutouts of all vector data segments (same as above)
  16 mbobra 1.28  *           
  17 xudong 1.1   *	Author:
  18              *		Xudong Sun; Monica Bobra
  19              *
  20              *	Version:
  21 xudong 1.23  *              v0.0 Jul 02 2012
  22              *              v0.1 Jul 23 2012
  23              *              v0.2 Sep 04 2012
  24              *              v0.3 Dec 18 2012
  25              *              v0.4 Jan 02 2013
  26              *              v0.5 Jan 23 2013
  27              *              v0.6 Aug 12 2013
  28              *              v0.7 Jan 02 2014
  29              *              v0.8 Feb 12 2014
  30              *				v0.9 Mar 04 2014
  31 xudong 1.1   *
  32              *	Notes:
  33              *		v0.0
  34              *		Mharp & Bharp must be fully specified; other input are series names only
  35              *		All input records need to match, otherwise quit
  36              *		Mapping parameters depend on keywords of each record only, not necessarily consistent for now
  37              *		Cutout doesn't work for char segments yet (drms_segment_readslice bug)
  38              *		SW indices require ephemeris info which is not passed properly as of now
  39              *		v0.1
  40              *		Fixed char I/O thanks to Art
  41              *		SW indices fixed
  42              *		Added doppler and continuum
  43 xudong 1.3   *              Added other keywords: HEADER (populated by cvs build version), DATE_B
  44 xudong 1.6   *		v0.3
  45              *		Fixed memory leakage of 0.15G per rec; denoted with "Dec 18"
  46 xudong 1.7   *		v0.4
  47              *		Took out convert_inplace(). Was causing all the images to be int
  48 xudong 1.23  *              v0.5
  49              *              Corrected ephemeris keywords, added argument mInfo for setKeys()
  50 xudong 1.17  *		v0.6
  51              *		Changes in remapping of bitmap and conf_disambig, now near neighbor without anti-aliasing
  52 xudong 1.23  *              v0.7
  53              *              Added full disk as "b"
  54              *              Global flag fullDisk is set if "b" is set
  55              *              Utilize BharpRS and BharpRec all around
  56              *              Pass mharpRec to set_keys() too in case of full disk
  57              *              Fixed Bunit (removed from copy_me_keys(), added loops for Bunits in set_keys() here)
  58              *              Error for CEA still does account for disambiguation yet
  59              *		v0.8
  60              *		Added disambig to azimuth during error propagation
  61              *		Changed usage for disambig: bit 2 (radial acute) for full disk, bit 0 for patch
  62              *		Fixed disambig cutout for patch: 0 for even, 7 for odd
  63              *		v0.9
  64              *		Fixed unit
  65              *		Check whether in PATCH of FD mode, so the error propagation uses disambiguated azimuth or not
  66              *
  67              *
  68              *	Example Calls:
  69              *      [I]   B (full disk disambiguation)
  70              *      > sharp "mharp=hmi.Mharp_720s[1832][2012.07.12_15:24]" "b=hmi_test.B_720s[2012.07.12_15:24]" "dop=hmi.V_720s[2012.07.12_15:24]" "cont=hmi.Ic_720s[2012.07.12_15:24]" "sharp_cea=su_xudong.sharp_cea_720s" "sharp_cut=su_xudong.sharp_720s"
  71              *      [II]  BHARP (patch disambiguation)
  72              *      > sharp "mharp=hmi.Mharp_720s[1832][2012.07.12_15:24]" "bharp=hmi.Bharp_720s[1832][2012.07.12_15:24]" "dop=hmi.V_720s[2012.07.12_15:24]" "cont=hmi.Ic_720s[2012.07.12_15:24]" "sharp_cea=su_xudong.sharp_cea_720s" "sharp_cut=su_xudong.sharp_720s"
  73 arta   1.22  *
  74 xudong 1.1   *
  75              */
  76 xudong 1.6  
  77 xudong 1.1  #include <stdio.h>
  78             #include <stdlib.h>
  79             #include <time.h>
  80             #include <sys/time.h>
  81             #include <math.h>
  82             #include <string.h>
  83             #include "jsoc_main.h"
  84             #include "astro.h"
  85             #include "fstats.h"
  86             #include "cartography.c"
  87             #include "fresize.h"
  88             #include "finterpolate.h"
  89             #include "img2helioVector.c"
  90             #include "copy_me_keys.c"
  91             #include "errorprop.c"
  92             #include "sw_functions.c"
  93             
  94 mbobra 1.14 //#include <mkl.h> // Comment out mkl.h, which can only run on solar3
  95 xudong 1.1  #include <mkl_blas.h>
  96             #include <mkl_service.h>
  97             #include <mkl_lapack.h>
  98             #include <mkl_vml_functions.h>
  99             #include <omp.h>
 100 xudong 1.15 
 101 xudong 1.1  #define PI              (M_PI)
 102             #define RADSINDEG		(PI/180.)
 103             #define RAD2ARCSEC		(648000./M_PI)
 104             #define SECINDAY		(86400.)
 105             #define FOURK			(4096)
 106             #define FOURK2    (16777216)
 107             
 108             #define ARRLENGTH(ARR) (sizeof(ARR) / sizeof(ARR[0]))
 109 xudong 1.23 
 110 xudong 1.1  // Nyqvist rate at disk center is 0.03 degree. Oversample above 0.015 degree
 111             #define NYQVIST		(0.015)
 112 xudong 1.23 
 113 mbobra 1.16 // Some other things
 114 xudong 1.1  #ifndef MIN
 115             #define MIN(a,b) (((a)<(b)) ? (a) : (b))
 116             #endif
 117             #ifndef MAX
 118             #define MAX(a,b) (((a)>(b)) ? (a) : (b))
 119             #endif
 120             
 121             #define DIE(msg) {fflush(stdout); fprintf(stderr,"%s, status=%d\n", msg, status); return(status);}
 122             #define SHOW(msg) {printf("%s", msg); fflush(stdout);}
 123             
 124             #define kNotSpecified "Not Specified"
 125 xudong 1.6  
 126 xudong 1.1  // Macros for WCS transformations.  assume crpix1, crpix2 = CRPIX1, CRPIX2, sina,cosa = sin and cos of CROTA2 resp.
 127             // and crvalx and crvaly are CRVAL1 and CRVAL2, cdelt = CDELT1 == CDELT2, then
 128             // PIX_X and PIX_Y are CCD pixel addresses, WX and WY are arc-sec W and N on the Sun from disk center.
 129             #define PIX_X(wx,wy) ((((wx-crvalx)*cosa + (wy-crvaly)*sina)/cdelt)+crpix1)
 130             #define PIX_Y(wx,wy) ((((wy-crvaly)*cosa - (wx-crvalx)*sina)/cdelt)+crpix2)
 131             #define WX(pix_x,pix_y) (((pix_x-crpix1)*cosa - (pix_y-crpix2)*sina)*cdelt+crvalx)
 132             #define WY(pix_x,pix_y) (((pix_y-crpix2)*cosa + (pix_x-crpix1)*sina)*cdelt+crvaly)
 133             
 134             #define DISAMB_AZI		1
 135             #define XSCALE			0.03
 136             #define YSCALE			0.03
 137             #define NBIN			3
 138             #define INTERP			0
 139             #define dpath    "/home/jsoc/cvs/Development/JSOC"
 140             
 141 mbobra 1.16 
 142 xudong 1.1  /* ========================================================================================================== */
 143             
 144             // Space weather keywords
 145             struct swIndex {
 146 xudong 1.23     float mean_vf;
 147 arta   1.22     float count_mask;
 148 mbobra 1.29     float absFlux;
 149                 float mean_hf;
 150                 float mean_gamma;
 151                 float mean_derivative_btotal;
 152                 float mean_derivative_bh;
 153                 float mean_derivative_bz;
 154                 float mean_jz;
 155                 float us_i;
 156                 float mean_alpha;
 157 xudong 1.23     float mean_ih;
 158                 float total_us_ih;
 159                 float total_abs_ih;
 160                 float totaljz;
 161                 float totpot;
 162                 float meanpot;
 163                 float area_w_shear_gt_45;
 164                 float meanshear_angle;
 165                 float area_w_shear_gt_45h;
 166                 float meanshear_angleh;
 167 arta   1.22     float mean_derivative_btotal_err;
 168                 float mean_vf_err;
 169                 float mean_gamma_err;
 170                 float mean_derivative_bh_err;
 171                 float mean_derivative_bz_err;
 172                 float mean_jz_err;
 173                 float us_i_err;
 174                 float mean_alpha_err;
 175                 float mean_ih_err;
 176                 float total_us_ih_err;
 177                 float total_abs_ih_err;
 178                 float totaljz_err;
 179                 float meanpot_err;
 180                 float totpot_err;
 181                 float meanshear_angle_err;
 182 xudong 1.23     float Rparam;
 183 mbobra 1.30     float totfx;
 184                 float totfy;
 185                 float totfz;
 186                 float totbsq;
 187                 float epsx;
 188                 float epsy;
 189                 float epsz;
 190 xudong 1.15 };
 191 xudong 1.1  
 192             // Mapping method
 193             enum projection {
 194             	carree,
 195             	cassini,
 196             	mercator,
 197             	cyleqa,
 198             	sineqa,
 199             	gnomonic,
 200             	postel,
 201             	stereographic,
 202             	orthographic,
 203             	lambert
 204             };
 205             
 206 xudong 1.15 // WSC code
 207             char *wcsCode[] = {"CAR", "CAS", "MER", "CEA", "GLS", "TAN", "ARC", "STG",
 208             	"SIN", "ZEA"};
 209             
 210 mbobra 1.14 // Ephemeris information
 211 xudong 1.1  struct ephemeris {
 212             	double disk_lonc, disk_latc;
 213             	double disk_xc, disk_yc;
 214             	double rSun, asd, pa;
 215             };
 216             
 217             // Mapping information
 218             struct mapInfo {
 219             	float xc, yc;		// reference point: center
 220             	int nrow, ncol;		// size
 221             	float xscale, yscale;	// scale
 222             	int nbin;
 223             	enum projection proj;	// projection method
 224             	struct ephemeris ephem;		// ephemeris info
 225             	float *xi_out, *zeta_out;	// coordinate on full disk image to sample at
 226             };
 227             
 228             /* ========================================================================================================== */
 229             
 230             /* Get all input data series */
 231             int getInputRS(DRMS_RecordSet_t **mharpRS_ptr, DRMS_RecordSet_t **bharpRS_ptr,
 232 xudong 1.1  			   char *mharpQuery, char *bharpQuery);
 233             
 234             /* Check if Mharp and Bharp match */
 235             int compareHarp(DRMS_RecordSet_t *mharpRS, DRMS_RecordSet_t *bharpRS);
 236             
 237             /* Get other data series */
 238             int getInputRS_aux(DRMS_RecordSet_t **inRS_ptr, char *inQuery, DRMS_RecordSet_t *harpRS);
 239             
 240             /* Find record from record set with given T_rec */
 241             int getInputRec_aux(DRMS_Record_t **inRec_ptr, DRMS_RecordSet_t *inRS, TIME trec);
 242             
 243             /* Create CEA record */
 244             int createCeaRecord(DRMS_Record_t *mharpRec, DRMS_Record_t *bharpRec,
 245 xudong 1.15 					DRMS_Record_t *dopRec, DRMS_Record_t *contRec,
 246 xudong 1.1  					DRMS_Record_t *sharpRec, struct swIndex *swKeys_ptr);
 247             
 248             /* Mapping single segment, wrapper */
 249             int mapScaler(DRMS_Record_t *sharpRec, DRMS_Record_t *inRec, DRMS_Record_t *harpRec,
 250             			  struct mapInfo *mInfo, char *segName);
 251             
 252             /* Mapping vector magnetogram, wrapper */
 253             int mapVectorB(DRMS_Record_t *sharpRec, DRMS_Record_t *bharpRec, struct mapInfo *mInfo);
 254             
 255             /* Mapping errors of vector magnetogram, wraper */
 256             int mapVectorBErr(DRMS_Record_t *sharpRec, DRMS_Record_t *bharpRec, struct mapInfo *mInfo);
 257             
 258             /* Determine reference point coordinate and patch size according to input */
 259             int findPosition(DRMS_Record_t *inRec, struct mapInfo *mInfo);
 260             
 261             /* Get ephemeris information */
 262             int getEphemeris(DRMS_Record_t *inRec, struct ephemeris *ephem);
 263             
 264             /* Compute the coordinates at which the full disk image is sampled */
 265             void findCoord(struct mapInfo *mInfo);
 266             
 267 xudong 1.1  /* Mapping function */
 268 xudong 1.17 int performSampling(float *outData, float *inData, struct mapInfo *mInfo, int interpOpt);
 269 xudong 1.1  
 270             /* Performing local vector transformation */
 271             void vectorTransform(float *bx_map, float *by_map, float *bz_map, struct mapInfo *mInfo);
 272             
 273             /* Map and propogate errors */
 274             int getBErr(float *bx_err, float *by_err, float *bz_err,
 275 xudong 1.6  			DRMS_Record_t *inRec, struct mapInfo *mInfo);
 276 xudong 1.1  
 277             /* Read full disk vector magnetogram */
 278             int readVectorB(DRMS_Record_t *inRec, float *bx_img, float *by_img, float *bz_img);
 279             
 280             /* Read variances and covariances of vector magnetograms */
 281 xudong 1.15 int readVectorBErr(DRMS_Record_t *bharpRec,
 282 xudong 1.1  				   float *bT, float *bI, float *bA,
 283 xudong 1.15 				   float *errbT, float *errbI, float *errbA,
 284 xudong 1.1  				   float *errbTbI, float *errbTbA, float *errbIbA);
 285             
 286             // ===================
 287             
 288             /* Create Cutout record */
 289             int createCutRecord(DRMS_Record_t *mharpRec, DRMS_Record_t *bharpRec,
 290             					DRMS_Record_t *dopRec, DRMS_Record_t *contRec,
 291             					DRMS_Record_t *sharpRec, struct swIndex *swKeys_ptr);
 292             
 293             /* Get cutout and write segment */
 294             int writeCutout(DRMS_Record_t *outRec, DRMS_Record_t *inRec, DRMS_Record_t *harpRec, char *SegName);
 295             
 296             // ===================
 297             
 298             /* Compute space weather indices, no error checking for now */
 299             void computeSWIndex(struct swIndex *swKeys_ptr, DRMS_Record_t *inRec, struct mapInfo *mInfo);
 300             
 301             /* Set space weather indices, no error checking for now */
 302             void setSWIndex(DRMS_Record_t *outRec, struct swIndex *swKeys_ptr);
 303             
 304             /* Set all keywords, no error checking for now */
 305 xudong 1.23 // Changed Dec 30 XS
 306             void setKeys(DRMS_Record_t *outRec, DRMS_Record_t *mharpRec, DRMS_Record_t *bharpRec, struct mapInfo *mInfo);
 307 xudong 1.1  
 308             // ===================
 309             
 310             /* Nearest neighbor interpolation */
 311             float nnb (float *f, int nx, int ny, double x, double y);
 312             
 313             /* Wrapper for Jesper's rebin code */
 314             void frebin (float *image_in, float *image_out, int nx, int ny, int nbin, int gauss);
 315             
 316             /* ========================================================================================================== */
 317             
 318             /* Remap segment names */
 319             #define BR_SEG_CEA		"Br"
 320             #define BT_SEG_CEA		"Bt"
 321             #define BP_SEG_CEA		"Bp"
 322             #define BR_ERR_SEG_CEA		"Br_err"
 323             #define BT_ERR_SEG_CEA		"Bt_err"
 324             #define BP_ERR_SEG_CEA		"Bp_err"
 325             
 326             /* Cutout segment names, input identical to output */
 327             char *MharpSegs[] = {"magnetogram", "bitmap"};
 328 xudong 1.1  char *BharpSegs[] = {"inclination", "azimuth", "field", "vlos_mag", "dop_width", "eta_0",
 329             	"damping", "src_continuum", "src_grad", "alpha_mag", "chisq",
 330             	"conv_flag", // fixed
 331             	"info_map", "confid_map",
 332             	"inclination_err", "azimuth_err", "field_err", "vlos_err", "alpha_err",
 333             	"field_inclination_err", "field_az_err", "inclin_azimuth_err",
 334             	"field_alpha_err","inclination_alpha_err", "azimuth_alpha_err",
 335             	"disambig", "conf_disambig"};
 336             // For stats
 337 xudong 1.15 char *CutSegs[] = {"magnetogram", "bitmap", "Dopplergram", "continuum",
 338 xudong 1.1  	"inclination", "azimuth", "field", "vlos_mag", "dop_width", "eta_0",
 339             	"damping", "src_continuum", "src_grad", "alpha_mag", "chisq",
 340             	"conv_flag", // fixed
 341             	"info_map", "confid_map",
 342             	"inclination_err", "azimuth_err", "field_err", "vlos_err", "alpha_err",
 343             	"field_inclination_err", "field_az_err", "inclin_azimuth_err",
 344             	"field_alpha_err","inclination_alpha_err", "azimuth_alpha_err",
 345             	"disambig", "conf_disambig"};
 346 xudong 1.23 char *CEASegs[] = {"magnetogram", "bitmap", "Dopplergram", "continuum",
 347             	BR_SEG_CEA, BT_SEG_CEA, BP_SEG_CEA, BR_ERR_SEG_CEA, BT_ERR_SEG_CEA, BP_ERR_SEG_CEA, "conf_disambig"};
 348             // For Bunits, added Dec 30 XS
 349             char *CutBunits[] = {"Mx/cm^2", " ", "cm/s", "DN/s",
 350                 "degree", "degree", "Mx/cm^2", "cm/s", "mA", " ",
 351                 "length units", "DN/s", "DN/s", " ", " ",
 352                 " ",
 353                 " ", " ",
 354                 "degree", "degree", "Mx/cm^2", "cm/s", " ",
 355                 " ", " ", " ",
 356                 " ", " ", " ",
 357                 " ", " "};
 358             char *CEABunits[] = {"Mx/cm^2", " ", "cm/s", "DN/s",
 359                 "Mx/cm^2", "Mx/cm^2", "Mx/cm^2", "Mx/cm^2", "Mx/cm^2", "Mx/cm^2", " "};      // Mar 4 2014 XS
 360 xudong 1.15 
 361 xudong 1.1  /* ========================================================================================================== */
 362             
 363             char *module_name = "sharp";
 364 mbobra 1.14 int seed;
 365 xudong 1.1  
 366 xudong 1.23 int fullDisk;       // full disk mode
 367             int amb4err;      // Use azimuth disambiguation for error propagation, default is 0 for patch and 1 for FD
 368             
 369 xudong 1.1  ModuleArgs_t module_args[] =
 370             {
 371             	{ARG_STRING, "mharp", kNotSpecified, "Input Mharp series."},
 372             	{ARG_STRING, "bharp", kNotSpecified, "Input Bharp series."},
 373 xudong 1.23     {ARG_STRING, "b", kNotSpecified, "Input B series, if set, overrides bharp."},
 374 xudong 1.1  	{ARG_STRING, "dop", kNotSpecified, "Input Doppler series."},
 375             	{ARG_STRING, "cont", kNotSpecified, "Input Continuum series."},
 376             	{ARG_STRING, "sharp_cea", kNotSpecified, "Output Sharp CEA series."},
 377             	{ARG_STRING, "sharp_cut", kNotSpecified, "Output Sharp cutout series."},
 378 xudong 1.15     {ARG_INT,    "seed", "987654", "Seed for the random number generator."},
 379 xudong 1.23     {ARG_INT,   "f_amb4err", "0", "Force using disambiguation in error propagation"},     // Mar 4 2014 XS
 380 xudong 1.1  	{ARG_END}
 381             };
 382             
 383 xudong 1.15 int DoIt(void)
 384 xudong 1.1  {
 385 xudong 1.23     int errbufstat = setvbuf(stderr, NULL, _IONBF, BUFSIZ);
 386                 int outbufstat = setvbuf(stdout, NULL, _IONBF, BUFSIZ);
 387 xudong 1.15     
 388 xudong 1.1  	int status = DRMS_SUCCESS;
 389             	int nrecs, irec;
 390             	
 391 xudong 1.23 	char *mharpQuery, *bharpQuery, *bQuery;
 392 xudong 1.1  	char *dopQuery, *contQuery;
 393             	char *sharpCeaQuery, *sharpCutQuery;
 394             	
 395             	DRMS_RecordSet_t *mharpRS = NULL, *bharpRS = NULL;
 396             	DRMS_RecordSet_t *dopRS = NULL, *contRS = NULL;
 397             	
 398             	/* Get parameters */
 399                 
 400             	mharpQuery = (char *) params_get_str(&cmdparams, "mharp");
 401             	bharpQuery = (char *) params_get_str(&cmdparams, "bharp");
 402 xudong 1.23     bQuery = (char *) params_get_str(&cmdparams, "b");
 403 xudong 1.1  	dopQuery = (char *) params_get_str(&cmdparams, "dop");
 404             	contQuery = (char *) params_get_str(&cmdparams, "cont");
 405             	sharpCeaQuery = (char *) params_get_str(&cmdparams, "sharp_cea");
 406             	sharpCutQuery = (char *) params_get_str(&cmdparams, "sharp_cut");
 407 xudong 1.15     
 408                 seed = params_get_int(&cmdparams, "seed");
 409 xudong 1.23     int f_amb4err = params_get_int(&cmdparams, "f_amb4err");
 410 xudong 1.1  	
 411             	/* Get input data, check everything */
 412             	
 413 xudong 1.23     // Full disk mode if "b" is set
 414                 if (strcmp(bQuery, kNotSpecified)) {
 415                     fullDisk = 1;
 416                     bharpQuery = bQuery;
 417                     //        SHOW(bharpQuery); SHOW("\n");
 418                     SHOW("Full disk mode\n");
 419                 } else {
 420                     fullDisk = 0;
 421                     SHOW("Harp mode\n");
 422                 }
 423                 
 424                 // Mar 4 2014
 425                 if (f_amb4err == 0) {         // no forcing, 0 for patch and 1 for FD
 426                     amb4err = fullDisk ? 1 : 0;
 427                 } else {
 428                     amb4err = 1;
 429                 }
 430                 printf("amb4err=%d\n", amb4err);
 431                 
 432                 // Bharp point to B if full disk
 433                 if (getInputRS(&mharpRS, &bharpRS, mharpQuery, bharpQuery))
 434 xudong 1.23         DIE("Input harp data error.");
 435 xudong 1.1  	nrecs = mharpRS->n;
 436             	
 437 xudong 1.15 	if (getInputRS_aux(&dopRS, dopQuery, mharpRS))
 438 xudong 1.1  		DIE("Input doppler data error.");
 439             	
 440 xudong 1.15 	if (getInputRS_aux(&contRS, contQuery, mharpRS))
 441 xudong 1.1  		DIE("Input continuum data error.");
 442             	
 443             	/* Start */
 444             	
 445             	printf("==============\nStart. %d image(s) in total.\n", nrecs);
 446 xudong 1.15     
 447 xudong 1.1  	for (irec = 0; irec < nrecs; irec++) {
 448             		
 449             		/* Records in work */
 450             		
 451             		DRMS_Record_t *mharpRec = NULL, *bharpRec = NULL;
 452 xudong 1.23         
 453 xudong 1.1  		mharpRec = mharpRS->records[irec];
 454 xudong 1.23         
 455                     TIME trec = drms_getkey_time(mharpRec, "T_REC", &status);
 456                     
 457             		if (!fullDisk) {
 458                         bharpRec = bharpRS->records[irec];
 459                     } else {
 460                         if (getInputRec_aux(&bharpRec, bharpRS, trec)) {     // Bharp point to full disk B
 461                             printf("Fetching B failed, image #%d skipped.\n", irec);
 462                             continue;
 463                         }
 464                     }
 465                     
 466 xudong 1.1  		struct swIndex swKeys;
 467             		
 468             		DRMS_Record_t *dopRec = NULL, *contRec = NULL;
 469 xudong 1.23         
 470 xudong 1.1  		if (getInputRec_aux(&dopRec, dopRS, trec)) {
 471             			printf("Fetching Doppler failed, image #%d skipped.\n", irec);
 472             			continue;
 473             		}
 474             		if (getInputRec_aux(&contRec, contRS, trec)) {
 475             			printf("Fetching continuum failed, image #%d skipped.\n", irec);
 476             			continue;
 477             		}
 478 xudong 1.15         
 479 xudong 1.1  		/* Create CEA record */
 480             		
 481             		DRMS_Record_t *sharpCeaRec = drms_create_record(drms_env, sharpCeaQuery, DRMS_PERMANENT, &status);
 482             		if (status) {		// if failed
 483             			printf("Creating CEA failed, image #%d skipped.\n", irec);
 484             			continue;
 485             		}
 486             		
 487             		if (createCeaRecord(mharpRec, bharpRec, dopRec, contRec, sharpCeaRec, &swKeys)) {		// do the work
 488             			printf("Creating CEA failed, image #%d skipped.\n", irec);
 489             			drms_close_record(sharpCeaRec, DRMS_FREE_RECORD);
 490             			continue;
 491             		}		// swKeys updated here
 492             		
 493             		drms_close_record(sharpCeaRec, DRMS_INSERT_RECORD);
 494             		
 495             		/* Create Cutout record */
 496             		
 497             		DRMS_Record_t *sharpCutRec = drms_create_record(drms_env, sharpCutQuery, DRMS_PERMANENT, &status);
 498             		if (status) {		// if failed
 499             			printf("Creating cutout failed, image #%d skipped.\n", irec);
 500 xudong 1.1  			continue;
 501             		}
 502             		
 503             		if (createCutRecord(mharpRec, bharpRec, dopRec, contRec, sharpCutRec, &swKeys)) {		// do the work
 504             			printf("Creating cutout failed, image #%d skipped.\n", irec);
 505             			drms_close_record(sharpCutRec, DRMS_FREE_RECORD);
 506             			continue;
 507             		}		// swKeys used here
 508             		
 509             		drms_close_record(sharpCutRec, DRMS_INSERT_RECORD);
 510             		
 511             		/* Done */
 512             		
 513             		printf("Image #%d done.\n", irec);
 514             		
 515             	} // irec
 516 xudong 1.15     
 517 xudong 1.1  	
 518             	drms_close_records(mharpRS, DRMS_FREE_RECORD);
 519             	drms_close_records(bharpRS, DRMS_FREE_RECORD);
 520 xudong 1.6  	drms_close_records(dopRS, DRMS_FREE_RECORD);				// Dec 18 2012
 521             	drms_close_records(contRS, DRMS_FREE_RECORD);				// Dec 18 2012
 522 xudong 1.1  	
 523             	return 0;
 524 xudong 1.6  	
 525 xudong 1.1  }	// DoIt
 526             
 527             
 528             // ===================================================================
 529             // ===================================================================
 530             // ===================================================================
 531             
 532             
 533             /*
 534              * Get input data series, including mHarp and bharp
 535              * Need all records to match, otherwise quit
 536              *
 537              */
 538             
 539             int getInputRS(DRMS_RecordSet_t **mharpRS_ptr, DRMS_RecordSet_t **bharpRS_ptr,
 540             			   char *mharpQuery, char *bharpQuery)
 541             {
 542             	
 543             	int status = 0;
 544             	
 545             	*mharpRS_ptr = drms_open_records(drms_env, mharpQuery, &status);
 546 xudong 1.1      if (status || (*mharpRS_ptr)->n == 0) return 1;
 547             	
 548 xudong 1.23 	if (fullDisk) {
 549                     if (getInputRS_aux(bharpRS_ptr, bharpQuery, *mharpRS_ptr)) return 1;
 550                 } else {
 551                     *bharpRS_ptr = drms_open_records(drms_env, bharpQuery, &status);
 552                     if (status || (*bharpRS_ptr)->n == 0) return 1;
 553                     if (compareHarp((*mharpRS_ptr), (*bharpRS_ptr))) return 1;
 554                 }
 555 xudong 1.1  	
 556             	return 0;
 557             	
 558             }
 559             
 560             /*
 561              * Check if Mharp and Bharp match
 562              *
 563              */
 564             
 565             int compareHarp(DRMS_RecordSet_t *mharpRS, DRMS_RecordSet_t *bharpRS)
 566             {
 567             	
 568             	int status = 0;
 569             	int nrecs = mharpRS->n;
 570             	
 571             	DRMS_Record_t *mharpRec_t = NULL, *bharpRec_t = NULL;		// temporary recs for utility
 572             	
 573                 if (bharpRS->n != nrecs) {
 574             		return 1;		// return 1 if different
 575             	}
 576 xudong 1.1  	
 577             	for (int i = 0; i < nrecs; i++) {
 578             		mharpRec_t = mharpRS->records[i];
 579             		bharpRec_t = bharpRS->records[i];
 580 xudong 1.15 		if ((drms_getkey_int(mharpRec_t, "HARPNUM", &status) !=
 581 xudong 1.1  			 drms_getkey_int(bharpRec_t, "HARPNUM", &status)) ||
 582 xudong 1.15 			(drms_getkey_time(mharpRec_t, "T_REC", &status) !=
 583 xudong 1.1  			 drms_getkey_time(bharpRec_t, "T_REC", &status)))
 584             		{
 585             			return 1;
 586             		}
 587             	}
 588             	
 589             	return 0;
 590             	
 591             }
 592             
 593 xudong 1.15 /*
 594 xudong 1.1   * Get other data series, check all T_REC are available
 595              *
 596              */
 597             
 598             int getInputRS_aux(DRMS_RecordSet_t **inRS_ptr, char *inQuery, DRMS_RecordSet_t *harpRS)
 599             {
 600             	
 601             	int status = 0;
 602             	
 603             	*inRS_ptr = drms_open_records(drms_env, inQuery, &status);
 604             	if (status || (*inRS_ptr)->n == 0) return status;
 605             	
 606             	// Check if all T_rec are available, need to match both ways
 607             	int n = harpRS->n, n0 = (*inRS_ptr)->n;
 608 xudong 1.6  	
 609 xudong 1.1  	for (int i0 = 0; i0 < n0; i0++) {
 610             		DRMS_Record_t *inRec = (*inRS_ptr)->records[i0];
 611             		TIME trec0 = drms_getkey_time(inRec, "T_REC", &status);
 612             		TIME trec = 0;
 613             		for (int i = 0; i < n; i++) {
 614             			DRMS_Record_t *harpRec = harpRS->records[i];
 615             			trec = drms_getkey_time(harpRec, "T_REC", &status);
 616             			if (fabs(trec0 - trec) < 10) break;
 617             		}
 618             		if (fabs(trec0 - trec) >= 10) return 1;
 619             	}
 620             	
 621             	for (int i = 0; i < n; i++) {
 622             		DRMS_Record_t *harpRec = harpRS->records[i];
 623             		TIME trec = drms_getkey_time(harpRec, "T_REC", &status);
 624             		TIME trec0 = 0;
 625             		for (int i0 = 0; i0 < n0; i0++) {
 626             			DRMS_Record_t *inRec = (*inRS_ptr)->records[i0];
 627             			trec0 = drms_getkey_time(inRec, "T_REC", &status);
 628             			if (fabs(trec0 - trec) < 10) break;
 629             		}
 630 xudong 1.1  		if (fabs(trec0 - trec) >= 10) return 1;
 631             	}
 632             	
 633             	return 0;
 634             	
 635             }
 636             
 637 xudong 1.15 /*
 638 xudong 1.1   * Find record from record set with given T_rec
 639              *
 640              */
 641             
 642             int getInputRec_aux(DRMS_Record_t **inRec_ptr, DRMS_RecordSet_t *inRS, TIME trec)
 643             {
 644             	
 645             	int status = 0;
 646             	
 647             	int n = inRS->n;
 648             	for (int i = 0; i < n; i++) {
 649             		*inRec_ptr = inRS->records[i];
 650             		TIME trec0 = drms_getkey_time((*inRec_ptr), "T_REC", &status);
 651             		if (fabs(trec0 - trec) < 10) return 0;
 652             	}
 653             	
 654             	return 1;
 655             	
 656             }
 657             
 658             
 659 xudong 1.1  
 660             
 661             /*
 662              * Create CEA record: top level subroutine
 663              * Also compute all the space weather keywords here
 664              *
 665              */
 666             
 667 xudong 1.15 int createCeaRecord(DRMS_Record_t *mharpRec, DRMS_Record_t *bharpRec,
 668             					DRMS_Record_t *dopRec, DRMS_Record_t *contRec,
 669 xudong 1.1  					DRMS_Record_t *sharpRec, struct swIndex *swKeys_ptr)
 670             {
 671             	
 672             	int status = 0;
 673             	DRMS_Segment_t *inSeg;
 674             	DRMS_Array_t *inArray;
 675             	
 676             	struct mapInfo mInfo;
 677             	mInfo.proj = (enum projection) cyleqa;		// projection method
 678             	mInfo.xscale = XSCALE;
 679             	mInfo.yscale = YSCALE;
 680 xudong 1.17 	
 681 xudong 1.23     int ncol0, nrow0;		// oversampled map size
 682 xudong 1.1  	
 683             	// Get ephemeris
 684 xudong 1.6  	
 685 xudong 1.3  	if (getEphemeris(mharpRec, &(mInfo.ephem))) {
 686             		SHOW("CEA: get ephemeris error\n");
 687             		return 1;
 688             	}
 689 xudong 1.1  	
 690             	// Find position
 691             	
 692 xudong 1.3  	if (findPosition(mharpRec, &mInfo)) {
 693             		SHOW("CEA: find position error\n");
 694             		return 1;
 695             	}
 696 xudong 1.1  	
 697 xudong 1.17 	// ========================================
 698             	// Do this for all bitmaps, Aug 12 2013 XS
 699             	// ========================================
 700             	
 701 xudong 1.23     mInfo.nbin = 1;			// for bitmaps. suppress anti-aliasing
 702 xudong 1.17 	ncol0 = mInfo.ncol;
 703             	nrow0 = mInfo.nrow;
 704             	
 705             	mInfo.xi_out = (float *) (malloc(ncol0 * nrow0 * sizeof(float)));
 706             	mInfo.zeta_out = (float *) (malloc(ncol0 * nrow0 * sizeof(float)));
 707             	
 708             	findCoord(&mInfo);		// compute it here so it could be shared by the following 4 functions
 709             	
 710             	if (mapScaler(sharpRec, mharpRec, mharpRec, &mInfo, "bitmap")) {
 711             		SHOW("CEA: mapping bitmap error\n");
 712             		return 1;
 713             	}
 714             	printf("Bitmap mapping done.\n");
 715             	
 716 xudong 1.23     if (mapScaler(sharpRec, bharpRec, mharpRec, &mInfo, "conf_disambig")) {
 717 xudong 1.17 		SHOW("CEA: mapping conf_disambig error\n");
 718             		return 1;
 719             	}
 720             	printf("Conf disambig mapping done.\n");
 721             	
 722 xudong 1.23     free(mInfo.xi_out);
 723 xudong 1.17 	free(mInfo.zeta_out);
 724             	
 725             	// ========================================
 726             	// Do this again for floats, Aug 12 2013 XS
 727             	// ========================================
 728 xudong 1.1  	// Create xi_out, zeta_out array in mInfo:
 729             	// Coordinates to sample in original full disk image
 730             	
 731 xudong 1.17 	mInfo.nbin = NBIN;
 732 xudong 1.1  	ncol0 = mInfo.ncol * mInfo.nbin + (mInfo.nbin / 2) * 2;	// pad with nbin/2 on edge to avoid NAN
 733             	nrow0 = mInfo.nrow * mInfo.nbin + (mInfo.nbin / 2) * 2;
 734             	
 735             	mInfo.xi_out = (float *) (malloc(ncol0 * nrow0 * sizeof(float)));
 736             	mInfo.zeta_out = (float *) (malloc(ncol0 * nrow0 * sizeof(float)));
 737             	
 738             	findCoord(&mInfo);		// compute it here so it could be shared by the following 4 functions
 739             	
 740             	// Mapping single segment: Mharp, etc.
 741 xudong 1.15     
 742 xudong 1.3  	if (mapScaler(sharpRec, mharpRec, mharpRec, &mInfo, "magnetogram")) {
 743             		SHOW("CEA: mapping magnetogram error\n");
 744             		return 1;
 745             	}
 746 xudong 1.1  	printf("Magnetogram mapping done.\n");
 747 xudong 1.23     
 748 xudong 1.3  	if (mapScaler(sharpRec, dopRec, mharpRec, &mInfo, "Dopplergram")) {
 749             		SHOW("CEA: mapping dopplergram error\n");
 750             		return 1;
 751             	}
 752 xudong 1.1  	printf("Dopplergram mapping done.\n");
 753             	
 754 xudong 1.3  	if (mapScaler(sharpRec, contRec, mharpRec, &mInfo, "continuum")) {
 755             		SHOW("CEA: mapping continuum error\n");
 756             		return 1;
 757             	}
 758 xudong 1.1  	printf("Intensitygram mapping done.\n");
 759 xudong 1.6  	
 760 xudong 1.1  	// Mapping vector B
 761             	
 762 xudong 1.3  	if (mapVectorB(sharpRec, bharpRec, &mInfo)) {
 763             		SHOW("CEA: mapping vector B error\n");
 764             		return 1;
 765             	}
 766 xudong 1.1  	printf("Vector B mapping done.\n");
 767             	
 768             	// Mapping vector B errors
 769             	
 770 xudong 1.3  	if (mapVectorBErr(sharpRec, bharpRec, &mInfo)) {
 771             		SHOW("CEA: mapping vector B uncertainty error\n");
 772             		return 1;
 773             	}
 774 xudong 1.1  	printf("Vector B error done.\n");
 775             	
 776             	// Keywords & Links
 777             	
 778             	drms_copykey(sharpRec, mharpRec, "T_REC");
 779             	drms_copykey(sharpRec, mharpRec, "HARPNUM");
 780             	
 781 xudong 1.23     if (fullDisk) {
 782                     DRMS_Link_t *bLink = hcon_lookup_lower(&sharpRec->links, "B");
 783                     if (bLink) drms_link_set("B", sharpRec, bharpRec);
 784                 } else {
 785                     DRMS_Link_t *bHarpLink = hcon_lookup_lower(&sharpRec->links, "BHARP");
 786                     if (bHarpLink) drms_link_set("BHARP", sharpRec, bharpRec);
 787                 }
 788 xudong 1.1  	DRMS_Link_t *mHarpLink = hcon_lookup_lower(&sharpRec->links, "MHARP");
 789             	if (mHarpLink) drms_link_set("MHARP", sharpRec, mharpRec);
 790             	
 791 xudong 1.23     setKeys(sharpRec, mharpRec, bharpRec, &mInfo);            // Set all other keywords
 792 xudong 1.3  	drms_copykey(sharpRec, mharpRec, "QUALITY");		// copied from los records
 793 xudong 1.6  	
 794 xudong 1.1  	// Space weather
 795             	
 796             	computeSWIndex(swKeys_ptr, sharpRec, &mInfo);		// compute it!
 797             	printf("Space weather indices done.\n");
 798             	
 799             	setSWIndex(sharpRec, swKeys_ptr);	// Set space weather indices
 800 xudong 1.6  	
 801 xudong 1.1  	// Stats
 802             	
 803             	int nCEASegs = ARRLENGTH(CEASegs);
 804             	for (int iSeg = 0; iSeg < nCEASegs; iSeg++) {
 805             		DRMS_Segment_t *outSeg = drms_segment_lookupnum(sharpRec, iSeg);
 806             		DRMS_Array_t *outArray = drms_segment_read(outSeg, DRMS_TYPE_FLOAT, &status);
 807             		int stat = set_statistics(outSeg, outArray, 1);
 808 xudong 1.6  		//		printf("%d => %d\n", iSeg, stat);
 809 xudong 1.1  		drms_free_array(outArray);
 810             	}
 811             	
 812             	free(mInfo.xi_out);
 813             	free(mInfo.zeta_out);
 814             	return 0;
 815             	
 816             }
 817             
 818             
 819 xudong 1.15 /*
 820 xudong 1.1   * Mapping a single segment
 821              * Read in full disk image, utilize mapImage for mapping
 822              * then write the segment out, segName same in in/out Rec
 823              *
 824              */
 825             
 826             int mapScaler(DRMS_Record_t *sharpRec, DRMS_Record_t *inRec, DRMS_Record_t *harpRec,
 827             			  struct mapInfo *mInfo, char *segName)
 828             {
 829             	
 830             	int status = 0;
 831             	int nx = mInfo->ncol, ny = mInfo->nrow, nxny = nx * ny;
 832             	int dims[2] = {nx, ny};
 833 xudong 1.17 	int interpOpt = INTERP;		// Aug 12 XS, default, overridden below for bitmaps and conf_disambig
 834 xudong 1.1  	
 835             	// Input full disk array
 836             	
 837             	DRMS_Segment_t *inSeg = NULL;
 838             	inSeg = drms_segment_lookup(inRec, segName);
 839             	if (!inSeg) return 1;
 840             	
 841             	DRMS_Array_t *inArray = NULL;
 842             	inArray = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
 843             	if (!inArray) return 1;
 844             	
 845 xudong 1.23     if (!strcmp(segName, "conf_disambig") || !strcmp(segName, "bitmap")) {
 846                     // Moved out so it works for FD conf_disambig as well
 847                     // Jan 2 2014 XS
 848                     interpOpt = 3;		// Aug 12 XS, near neighbor
 849                 }
 850                 
 851 xudong 1.1  	float *inData;
 852             	int xsz = inArray->axis[0], ysz = inArray->axis[1];
 853             	if ((xsz != FOURK) || (ysz != FOURK)) {		// for bitmap, make tmp full disk
 854             		float *inData0 = (float *) inArray->data;
 855             		inData = (float *) (calloc(FOURK2, sizeof(float)));
 856             		int x0 = (int) drms_getkey_float(harpRec, "CRPIX1", &status) - 1;
 857             		int y0 = (int) drms_getkey_float(harpRec, "CRPIX2", &status) - 1;
 858             		int ind_map;
 859             		for (int row = 0; row < ysz; row++) {
 860             			for (int col = 0; col < xsz; col++) {
 861             				ind_map = (row + y0) * FOURK + (col + x0);
 862             				inData[ind_map] = inData0[row * xsz + col];
 863             			}
 864             		}
 865             		drms_free_array(inArray); inArray = NULL;
 866             	} else {
 867             		inData = (float *) inArray->data;
 868             	}
 869             	
 870             	// Mapping
 871             	
 872 xudong 1.1  	float *map = (float *) (malloc(nxny * sizeof(float)));
 873 xudong 1.17 	if (performSampling(map, inData, mInfo, interpOpt))		// Add interpOpt for different types, Aug 12 XS
 874 xudong 1.6  	{if (inArray) drms_free_array(inArray); free(map); return 1;}
 875 xudong 1.1  	
 876             	// Write out
 877             	
 878 xudong 1.15 	DRMS_Segment_t *outSeg = NULL;
 879 xudong 1.1  	outSeg = drms_segment_lookup(sharpRec, segName);
 880             	if (!outSeg) return 1;
 881             	
 882 xudong 1.15     //	DRMS_Type_t arrayType = outSeg->info->type;
 883 xudong 1.1  	DRMS_Array_t *outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, dims, map, &status);
 884             	if (status) {if (inArray) drms_free_array(inArray); free(map); return 1;}
 885             	
 886             	// convert to needed data type
 887             	
 888 xudong 1.15     //	drms_array_convert_inplace(outSeg->info->type, 0, 1, outArray);		// Jan 02 2013
 889 xudong 1.1  	
 890             	outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
 891 xudong 1.15     //	outArray->parent_segment = outSeg;
 892 xudong 1.8  	outArray->israw = 0;		// always compressed
 893 xudong 1.1  	outArray->bzero = outSeg->bzero;
 894             	outArray->bscale = outSeg->bscale;
 895 xudong 1.6  	
 896 xudong 1.1  	status = drms_segment_write(outSeg, outArray, 0);
 897             	if (status) return 0;
 898             	
 899             	if (inArray) drms_free_array(inArray);
 900 xudong 1.6  	if ((xsz != FOURK) || (ysz != FOURK)) free(inData);			// Dec 18 2012
 901 xudong 1.1  	if (outArray) drms_free_array(outArray);
 902             	return 0;
 903             	
 904             }
 905             
 906             
 907 xudong 1.15 /*
 908 xudong 1.1   * Mapping vector magnetogram
 909              *
 910              */
 911             
 912             int mapVectorB(DRMS_Record_t *sharpRec, DRMS_Record_t *bharpRec, struct mapInfo *mInfo)
 913             {
 914             	
 915             	int status = 0;
 916             	int nx = mInfo->ncol, ny = mInfo->nrow, nxny = nx * ny;
 917             	int dims[2] = {nx, ny};
 918             	
 919             	// Read in segments, filling factor assume to be 1
 920             	
 921             	float *bx_img = (float *) (malloc(FOURK2 * sizeof(float)));
 922             	float *by_img = (float *) (malloc(FOURK2 * sizeof(float)));
 923             	float *bz_img = (float *) (malloc(FOURK2 * sizeof(float)));
 924             	
 925             	if (readVectorB(bharpRec, bx_img, by_img, bz_img)) {
 926             		printf("Read full disk image error\n");
 927             		free(bx_img); free(by_img); free(bz_img);
 928             		return 1;
 929 xudong 1.1  	}
 930             	
 931             	// Mapping
 932             	
 933             	float *bx_map = NULL, *by_map = NULL, *bz_map = NULL;	// intermediate maps, in CCD bxyz representation
 934 xudong 1.6  	
 935 xudong 1.1  	bx_map = (float *) (malloc(nxny * sizeof(float)));
 936 xudong 1.17 	if (performSampling(bx_map, bx_img, mInfo, INTERP))
 937 xudong 1.6  	{free(bx_img); free(by_img); free(bz_img); free(bx_map); return 1;}
 938             	
 939 xudong 1.1  	by_map = (float *) (malloc(nxny * sizeof(float)));
 940 xudong 1.17 	if (performSampling(by_map, by_img, mInfo, INTERP))
 941 xudong 1.6  	{free(bx_img); free(by_img); free(bz_img); free(bz_map); return 1;}
 942             	
 943 xudong 1.1  	bz_map = (float *) (malloc(nxny * sizeof(float)));
 944 xudong 1.17 	if (performSampling(bz_map, bz_img, mInfo, INTERP))
 945 xudong 1.6  	{free(bx_img); free(by_img); free(bz_img); free(bz_map); return 1;}
 946 xudong 1.1  	
 947             	free(bx_img); free(by_img); free(bz_img);
 948             	
 949             	// Vector transform
 950             	
 951             	vectorTransform(bx_map, by_map, bz_map, mInfo);
 952             	
 953             	for (int i = 0; i < nxny; i++) by_map[i] *= -1;		// positive theta pointing south
 954             	
 955             	// Write out
 956             	
 957             	DRMS_Segment_t *outSeg;
 958             	DRMS_Array_t *outArray;
 959             	
 960             	float *data_prt[3] = {bx_map, by_map, bz_map};
 961             	char *segName[3] = {BP_SEG_CEA, BT_SEG_CEA, BR_SEG_CEA};
 962             	
 963             	for (int iSeg = 0; iSeg < 3; iSeg++) {
 964             		outSeg = drms_segment_lookup(sharpRec, segName[iSeg]);
 965             		outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, dims, data_prt[iSeg], &status);
 966             		outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
 967 xudong 1.15         //		outArray->parent_segment = outSeg;
 968 xudong 1.8  		outArray->israw = 0;
 969 xudong 1.1  		outArray->bzero = outSeg->bzero;
 970             		outArray->bscale = outSeg->bscale;
 971             		status = drms_segment_write(outSeg, outArray, 0);
 972             		if (status) return 1;
 973             		drms_free_array(outArray);
 974             	}
 975             	
 976             	//
 977             	
 978             	return 0;
 979             	
 980             }
 981             
 982             
 983 xudong 1.15 /*
 984 xudong 1.1   * Mapping vector magnetogram errors
 985              *
 986              */
 987             
 988             int mapVectorBErr(DRMS_Record_t *sharpRec, DRMS_Record_t *bharpRec, struct mapInfo *mInfo)
 989             {
 990             	
 991             	int status = 0;
 992             	
 993             	int nx = mInfo->ncol, ny = mInfo->nrow, nxny = nx * ny;
 994             	int dims[2] = {nx, ny};
 995             	
 996             	// Compute propogated errors, using nearest neighbour interpolation
 997             	
 998             	float *bx_err = (float *) (malloc(nxny * sizeof(float)));
 999             	float *by_err = (float *) (malloc(nxny * sizeof(float)));
1000             	float *bz_err = (float *) (malloc(nxny * sizeof(float)));
1001             	
1002             	if (getBErr(bx_err, by_err, bz_err, bharpRec, mInfo)) {
1003             		free(bx_err); free(by_err); free(bz_err);
1004             		return 1;
1005 xudong 1.1  	}
1006 xudong 1.6  	
1007 xudong 1.1  	// Write out
1008             	
1009             	DRMS_Segment_t *outSeg;
1010             	DRMS_Array_t *outArray;
1011             	
1012             	float *data_prt[3] = {bx_err, by_err, bz_err};
1013             	char *segName[3] = {BP_ERR_SEG_CEA, BT_ERR_SEG_CEA, BR_ERR_SEG_CEA};
1014             	
1015             	for (int iSeg = 0; iSeg < 3; iSeg++) {
1016             		outSeg = drms_segment_lookup(sharpRec, segName[iSeg]);
1017             		outArray = drms_array_create(DRMS_TYPE_FLOAT, 2, dims, data_prt[iSeg], &status);
1018             		outSeg->axis[0] = outArray->axis[0]; outSeg->axis[1] = outArray->axis[1];
1019 xudong 1.15         //		outArray->parent_segment = outSeg;
1020 xudong 1.8  		outArray->israw = 0;
1021 xudong 1.1  		outArray->bzero = outSeg->bzero;
1022             		outArray->bscale = outSeg->bscale;
1023             		status = drms_segment_write(outSeg, outArray, 0);
1024             		if (status) return 1;
1025             		drms_free_array(outArray);
1026             	}
1027             	
1028             	//
1029             	
1030             	return 0;
1031             	
1032             }
1033             
1034             
1035             
1036 xudong 1.15 /*
1037 xudong 1.1   * Determine reference point coordinate and patch size according to keywords
1038              * xc, yc are the coordinate of patch center, in degrees
1039              * ncol and nrow are the final size
1040              *
1041              */
1042             
1043             int findPosition(DRMS_Record_t *inRec, struct mapInfo *mInfo)
1044             {
1045             	
1046             	int status = 0;
1047             	int harpnum = drms_getkey_int(inRec, "HARPNUM", &status);
1048             	TIME trec = drms_getkey_time(inRec, "T_REC", &status);
1049             	float disk_lonc = drms_getkey_float(inRec, "CRLN_OBS", &status);
1050             	
1051             	/* Center coord */
1052             	
1053             	float minlon = drms_getkey_float(inRec, "LONDTMIN", &status); if (status) return 1;		// Stonyhurst lon
1054             	float maxlon = drms_getkey_float(inRec, "LONDTMAX", &status); if (status) return 1;
1055             	float minlat = drms_getkey_float(inRec, "LATDTMIN", &status); if (status) return 1;
1056             	float maxlat = drms_getkey_float(inRec, "LATDTMAX", &status); if (status) return 1;
1057             	
1058 xudong 1.1  	// A bug fixer for HARP (per M. Turmon)
1059             	// When AR is below threshold, "LONDTMIN", "LONDTMAX" will be wrong
1060             	// Also keywords such as "SIZE" will be NaN
1061             	// We compute minlon & minlat then by
1062             	// LONDTMIN(t) = LONDTMIN(t0) + (t - t0) * OMEGA_DT
1063             	
1064 xudong 1.15     //	float psize = drms_getkey_float(inRec, "SIZE", &status);
1065                 //	if (psize != psize) {
1066                 
1067                 if (minlon != minlon || maxlon != maxlon) {		// check lons instead of SIZE
1068 xudong 1.12 		TIME t0 = drms_getkey_time(inRec, "T_FRST1", &status); if (status) return 1;			// changed from T_FRST to T_FRST1, T_FRST may not exist
1069 xudong 1.1  		double omega = drms_getkey_double(inRec, "OMEGA_DT", &status); if (status) return 1;
1070             		char firstRecQuery[100], t0_str[100];
1071             		sprint_time(t0_str, t0, "TAI", 0);
1072             		snprintf(firstRecQuery, 100, "%s[%d][%s]", inRec->seriesinfo->seriesname, harpnum, t0_str);
1073             		DRMS_RecordSet_t *tmpRS = drms_open_records(drms_env, firstRecQuery, &status);
1074             		if (status || tmpRS->n != 1) return 1;
1075             		DRMS_Record_t *tmpRec = tmpRS->records[0];
1076             		double minlon0 = drms_getkey_double(tmpRec, "LONDTMIN", &status); if (status) return 1;
1077             		double maxlon0 = drms_getkey_double(tmpRec, "LONDTMAX", &status); if (status) return 1;
1078             		minlon = minlon0 + (trec - t0) * omega / SECINDAY;
1079             		maxlon = maxlon0 + (trec - t0) * omega / SECINDAY;
1080             		printf("%s, %f, %f\n", firstRecQuery, minlon, maxlon);
1081             	}
1082             	
1083             	mInfo->xc = (maxlon + minlon) / 2. + disk_lonc;
1084             	mInfo->yc = (maxlat + minlat) / 2.;
1085             	
1086             	/* Size */
1087             	
1088             	mInfo->ncol = round((maxlon - minlon) / mInfo->xscale);
1089             	mInfo->nrow = round((maxlat - minlat) / mInfo->yscale);
1090 xudong 1.1  	
1091             	return 0;
1092             	
1093             }
1094             
1095             
1096             /*
1097              * Fetch ephemeris info from a DRMS record
1098              * No error checking for now
1099              *
1100              */
1101             
1102             int getEphemeris(DRMS_Record_t *inRec, struct ephemeris *ephem)
1103             {
1104             	
1105             	int status = 0;
1106             	
1107             	float crota2 = drms_getkey_float(inRec, "CROTA2", &status);	// rotation
1108 xudong 1.15 	double sina = sin(crota2 * RADSINDEG);
1109 xudong 1.1  	double cosa = cos(crota2 * RADSINDEG);
1110             	
1111             	ephem->pa = - crota2 * RADSINDEG;
1112             	ephem->disk_latc = drms_getkey_float(inRec, "CRLT_OBS", &status) * RADSINDEG;
1113             	ephem->disk_lonc = drms_getkey_float(inRec, "CRLN_OBS", &status) * RADSINDEG;
1114             	
1115             	float crvalx = 0.0;
1116             	float crvaly = 0.0;
1117             	float crpix1 = drms_getkey_float(inRec, "IMCRPIX1", &status);
1118             	float crpix2 = drms_getkey_float(inRec, "IMCRPIX2", &status);
1119             	float cdelt = drms_getkey_float(inRec, "CDELT1", &status);  // in arcsec, assumimg dx=dy
1120             	ephem->disk_xc = PIX_X(0.0,0.0) - 1.0;		// Center of disk in pixel, starting at 0
1121             	ephem->disk_yc = PIX_Y(0.0,0.0) - 1.0;
1122             	
1123             	float dSun = drms_getkey_float(inRec, "DSUN_OBS", &status);
1124             	float rSun_ref = drms_getkey_float(inRec, "RSUN_REF", &status);
1125             	if (status) rSun_ref = 6.96e8;
1126             	
1127             	ephem->asd = asin(rSun_ref/dSun);
1128             	ephem->rSun = asin(rSun_ref / dSun) * RAD2ARCSEC / cdelt;
1129             	
1130 xudong 1.1  	return 0;
1131             	
1132             }
1133             
1134             
1135             /*
1136              * Compute the coordinates to be sampled on full disk image
1137              * mInfo->xi_out & mInfo->zeta_out
1138              * This is oversampled, its size is ncol0 & nrow0 as shown below
1139              *
1140              *
1141              */
1142             
1143             void findCoord(struct mapInfo *mInfo)
1144             {
1145             	
1146             	int ncol0 = mInfo->ncol * mInfo->nbin + (mInfo->nbin / 2) * 2;	// pad with nbin/2 on edge to avoid NAN
1147             	int nrow0 = mInfo->nrow * mInfo->nbin + (mInfo->nbin / 2) * 2;
1148             	
1149             	float xscale0 = mInfo->xscale / mInfo->nbin * RADSINDEG;		// oversampling resolution
1150             	float yscale0 = mInfo->yscale / mInfo->nbin * RADSINDEG;		// in rad
1151 xudong 1.1  	
1152             	double lonc = mInfo->xc * RADSINDEG;	// in rad
1153             	double latc = mInfo->yc * RADSINDEG;
1154             	
1155             	double disk_lonc = (mInfo->ephem).disk_lonc;
1156             	double disk_latc = (mInfo->ephem).disk_latc;
1157             	
1158             	double rSun = (mInfo->ephem).rSun;
1159             	double disk_xc = (mInfo->ephem).disk_xc / rSun;
1160             	double disk_yc = (mInfo->ephem).disk_yc / rSun;
1161             	double pa = (mInfo->ephem).pa;
1162             	
1163             	// Temp pointers
1164             	
1165             	float *xi_out = mInfo->xi_out;
1166             	float *zeta_out = mInfo->zeta_out;
1167             	
1168             	// start
1169             	
1170             	double x, y;		// map coord
1171             	double lat, lon;	// helio coord
1172 xudong 1.1  	double xi, zeta;	// image coord (for one point)
1173             	
1174             	int ind_map;
1175             	
1176             	for (int row0 = 0; row0 < nrow0; row0++) {
1177             		for (int col0 = 0; col0 < ncol0; col0++) {
1178             			
1179             			ind_map = row0 * ncol0 + col0;
1180             			
1181             			x = (col0 + 0.5 - ncol0/2.) * xscale0;		// in rad
1182             			y = (row0 + 0.5 - nrow0/2.) * yscale0;
1183             			
1184 xudong 1.15 			/* map grid [x, y] corresponds to the point [lon, lat] in the heliographic coordinates.
1185 xudong 1.1  			 * the [x, y] are in radians with respect of the center of the map [xcMap, ycMap].
1186             			 * projection methods could be Mercator, Lambert, and many others. [maplonc, mapLatc]
1187 xudong 1.15 			 * is the heliographic longitude and latitude of the map center. Both are in degree.
1188 xudong 1.1  			 */
1189             			
1190             			if (plane2sphere (x, y, latc, lonc, &lat, &lon, (int) mInfo->proj)) {
1191             				xi_out[ind_map] = -1;
1192             				zeta_out[ind_map] = -1;
1193             				continue;
1194             			}
1195             			
1196             			/* map the grid [lon, lat] in the heliographic coordinates to [xi, zeta], a point in the
1197             			 * image coordinates. The image properties, xCenter, yCenter, rSun, pa, ecc and chi are given.
1198             			 */
1199             			
1200 xudong 1.15 			if (sphere2img (lat, lon, disk_latc, disk_lonc, &xi, &zeta,
1201 xudong 1.1  							disk_xc, disk_yc, 1.0, pa, 0., 0., 0., 0.)) {
1202             				xi_out[ind_map] = -1;
1203             				zeta_out[ind_map] = -1;
1204             				continue;
1205             			}
1206             			
1207             			xi_out[ind_map] = xi * rSun;
1208             			zeta_out[ind_map] = zeta * rSun;
1209             			
1210             		}
1211             	}
1212             	
1213             }
1214             
1215             
1216 xudong 1.15 /*
1217 xudong 1.1   * Sampling function
1218              * oversampling by nbin, then binning using a Gaussian
1219              * save results in outData, always of float type
1220              *
1221              */
1222             
1223 xudong 1.17 int performSampling(float *outData, float *inData, struct mapInfo *mInfo, int interpOpt)
1224 xudong 1.1  {
1225             	
1226             	int status = 0;
1227 xudong 1.17 	int ind_map;
1228 xudong 1.1  	
1229             	int ncol0 = mInfo->ncol * mInfo->nbin + (mInfo->nbin / 2) * 2;	// pad with nbin/2 on edge to avoid NAN
1230             	int nrow0 = mInfo->nrow * mInfo->nbin + (mInfo->nbin / 2) * 2;
1231             	
1232 xudong 1.17 	// Changed Aug 12 2013, XS, for bitmaps
1233             	float *outData0;
1234             	if (interpOpt == 3 && mInfo->nbin == 1) {
1235 xudong 1.23         outData0 = outData;
1236 xudong 1.17 	} else {
1237 xudong 1.23         outData0 = (float *) (malloc(ncol0 * nrow0 * sizeof(float)));
1238 xudong 1.17 	}
1239 xudong 1.1  	
1240             	float *xi_out = mInfo->xi_out;
1241             	float *zeta_out = mInfo->zeta_out;
1242 xudong 1.6  	
1243 xudong 1.1  	// Interpolation
1244             	
1245             	struct fint_struct pars;
1246 xudong 1.17 	// Aug 12 2013, passed in as argument now
1247 xudong 1.1  	
1248             	switch (interpOpt) {
1249             		case 0:			// Wiener, 6 order, 1 constraint
1250             			init_finterpolate_wiener(&pars, 6, 1, 6, 2, 1, 1, NULL, dpath);
1251             			break;
1252             		case 1:			// Cubic convolution
1253             			init_finterpolate_cubic_conv(&pars, 1., 3.);
1254             			break;
1255             		case 2:			// Bilinear
1256             			init_finterpolate_linear(&pars, 1.);
1257             			break;
1258 xudong 1.17 		case 3:			// Near neighbor
1259 xudong 1.23             break;
1260 xudong 1.1  		default:
1261             			return 1;
1262             	}
1263             	
1264 xudong 1.17 	printf("interpOpt = %d, nbin = %d ", interpOpt, mInfo->nbin);
1265             	if (interpOpt == 3) {			// Aug 6 2013, Xudong
1266             	  	for (int row0 = 0; row0 < nrow0; row0++) {
1267 xudong 1.23             for (int col0 = 0; col0 < ncol0; col0++) {
1268                             ind_map = row0 * ncol0 + col0;
1269                             outData0[ind_map] = nnb(inData, FOURK, FOURK, xi_out[ind_map], zeta_out[ind_map]);
1270                         }
1271                     }
1272 xudong 1.17 	} else {
1273 xudong 1.23         finterpolate(&pars, inData, xi_out, zeta_out, outData0,
1274                                  FOURK, FOURK, FOURK, ncol0, nrow0, ncol0, DRMS_MISSING_FLOAT);
1275 xudong 1.17 	}
1276 xudong 1.1  	
1277             	// Rebinning, smoothing
1278             	
1279 xudong 1.17 	if (interpOpt == 3 && mInfo->nbin == 1) {
1280 xudong 1.23         return 0;
1281 xudong 1.17 	} else {
1282 xudong 1.23         frebin(outData0, outData, ncol0, nrow0, mInfo->nbin, 1);		// Gaussian
1283                     free(outData0);		// Dec 18 2012
1284 xudong 1.17 	}
1285 xudong 1.1  	
1286             	//
1287             	
1288             	return 0;
1289             	
1290             }
1291             
1292             
1293 xudong 1.15 /*
1294 xudong 1.1   * Performing local vector transformation
1295              *  xyz: z refers to vertical (radial) component, x EW (phi), y NS
1296              *
1297              */
1298             
1299             void vectorTransform(float *bx_map, float *by_map, float *bz_map, struct mapInfo *mInfo)
1300             {
1301             	
1302             	int ncol = mInfo->ncol;
1303             	int nrow = mInfo->nrow;
1304             	
1305             	float xscale = mInfo->xscale * RADSINDEG;		// in rad
1306             	float yscale = mInfo->yscale * RADSINDEG;
1307             	
1308             	double lonc = mInfo->xc * RADSINDEG;	// in rad
1309             	double latc = mInfo->yc * RADSINDEG;
1310             	
1311             	double disk_lonc = (mInfo->ephem).disk_lonc;
1312             	double disk_latc = (mInfo->ephem).disk_latc;
1313             	
1314             	double rSun = (mInfo->ephem).rSun;
1315 xudong 1.1  	double disk_xc = (mInfo->ephem).disk_xc / rSun;
1316             	double disk_yc = (mInfo->ephem).disk_yc / rSun;
1317             	double pa = (mInfo->ephem).pa;
1318             	
1319             	int ind_map;
1320             	double x, y;
1321             	double lat, lon;	// lat / lon for current point
1322             	
1323             	double bx_tmp, by_tmp, bz_tmp;
1324             	
1325             	//
1326             	
1327             	for (int row = 0; row < mInfo->nrow; row++) {
1328             		for (int col = 0; col < mInfo->ncol; col++) {
1329             			
1330             			ind_map = row * mInfo->ncol + col;
1331             			
1332             			x = (col + 0.5 - ncol / 2.) * xscale;
1333             			y = (row + 0.5 - nrow / 2.) * yscale;
1334             			
1335             			if (plane2sphere (x, y, latc, lonc, &lat, &lon, (int) mInfo->proj)) {
1336 xudong 1.1  				bx_map[ind_map] = DRMS_MISSING_FLOAT;
1337             				by_map[ind_map] = DRMS_MISSING_FLOAT;
1338             				bz_map[ind_map] = DRMS_MISSING_FLOAT;
1339             				continue;
1340             			}
1341             			
1342             			bx_tmp = by_tmp = bz_tmp = 0;
1343             			
1344             			img2helioVector (bx_map[ind_map], by_map[ind_map], bz_map[ind_map],
1345             							 &bx_tmp, &by_tmp, &bz_tmp,
1346             							 lon, lat, disk_lonc, disk_latc, pa);
1347             			
1348             			bx_map[ind_map] = bx_tmp;
1349             			by_map[ind_map] = by_tmp;
1350             			bz_map[ind_map] = bz_tmp;
1351             			
1352             		}
1353             	}
1354 xudong 1.6  	
1355 xudong 1.1  }
1356             
1357             
1358             
1359 xudong 1.15 /*
1360 xudong 1.1   * Map and propogate vector field errors
1361              *
1362              */
1363             
1364             int getBErr(float *bx_err, float *by_err, float *bz_err,
1365 xudong 1.6  			DRMS_Record_t *inRec, struct mapInfo *mInfo)
1366 xudong 1.1  {
1367             	
1368             	int status = 0;
1369             	
1370             	// Get variances and covariances, filling factor assume to be 1
1371             	
1372             	float *bT = (float *) (malloc(FOURK2 * sizeof(float)));	// field
1373             	float *bI = (float *) (malloc(FOURK2 * sizeof(float)));	// inclination
1374             	float *bA = (float *) (malloc(FOURK2 * sizeof(float)));	// azimuth
1375             	
1376             	float *errbT = (float *) (malloc(FOURK2 * sizeof(float)));
1377             	float *errbI = (float *) (malloc(FOURK2 * sizeof(float)));
1378             	float *errbA = (float *) (malloc(FOURK2 * sizeof(float)));
1379             	
1380             	float *errbTbI = (float *) (malloc(FOURK2 * sizeof(float)));
1381             	float *errbTbA = (float *) (malloc(FOURK2 * sizeof(float)));
1382             	float *errbIbA = (float *) (malloc(FOURK2 * sizeof(float)));
1383             	
1384 xudong 1.15 	if (readVectorBErr(inRec,
1385 xudong 1.1  					   bT, bI, bA,
1386 xudong 1.15 					   errbT, errbI, errbA,
1387 xudong 1.1  					   errbTbI, errbTbA, errbIbA)) {
1388             		printf("Read full disk variances & covariances error\n");
1389             		free(bT); free(bI); free(bA);
1390             		free(errbT); free(errbI); free(errbA);
1391             		free(errbTbI); free(errbTbA); free(errbIbA);
1392             		return 1;
1393             	}
1394             	
1395             	// Size
1396             	
1397             	int ncol = mInfo->ncol;
1398             	int nrow = mInfo->nrow;
1399             	
1400             	float xscale = mInfo->xscale * RADSINDEG;		// in rad
1401             	float yscale = mInfo->yscale * RADSINDEG;
1402             	
1403             	double lonc = mInfo->xc * RADSINDEG;	// in rad
1404             	double latc = mInfo->yc * RADSINDEG;
1405             	
1406             	double disk_lonc = (mInfo->ephem).disk_lonc;
1407             	double disk_latc = (mInfo->ephem).disk_latc;
1408 xudong 1.1  	
1409             	double rSun = (mInfo->ephem).rSun;
1410             	double disk_xc = (mInfo->ephem).disk_xc / rSun;
1411             	double disk_yc = (mInfo->ephem).disk_yc / rSun;
1412             	double pa = (mInfo->ephem).pa;
1413             	
1414             	// Start
1415             	
1416             	double x, y;          // map coord
1417             	double lat, lon;      // spherical coord
1418             	double xi, zeta;      // image coord, round to full pixel
1419             	
1420             	int ind_map, ind_img;
1421             	
1422             	double bpSigma2, btSigma2, brSigma2;		// variances after prop
1423 xudong 1.6  	
1424 xudong 1.1  	for (int row = 0; row < mInfo->nrow; row++) {
1425             		for (int col = 0; col < mInfo->ncol; col++) {
1426             			
1427             			ind_map = row * mInfo->ncol + col;
1428             			
1429             			x = (col + 0.5 - ncol / 2.) * xscale;
1430             			y = (row + 0.5 - nrow / 2.) * yscale;
1431             			
1432             			if (plane2sphere (x, y, latc, lonc, &lat, &lon, (int) mInfo->proj)) {
1433             				bx_err[ind_map] = DRMS_MISSING_FLOAT;
1434             				by_err[ind_map] = DRMS_MISSING_FLOAT;
1435             				bz_err[ind_map] = DRMS_MISSING_FLOAT;
1436             				continue;
1437             			}
1438             			
1439 xudong 1.15 			if (sphere2img (lat, lon, disk_latc, disk_lonc, &xi, &zeta,
1440 xudong 1.1  							disk_xc, disk_yc, 1.0, pa, 0., 0., 0., 0.)) {
1441             				bx_err[ind_map] = DRMS_MISSING_FLOAT;
1442 xudong 1.24 				by_err[ind_map] = DRMS_MISSING_FLOAT;
1443             				bz_err[ind_map] = DRMS_MISSING_FLOAT;       // Mar 7
1444 xudong 1.1  				continue;
1445             			}
1446             			
1447             			xi *= rSun; xi = round(xi);
1448             			zeta *= rSun; zeta = round(zeta);     // nearest neighbor
1449             			
1450             			ind_img = round(zeta * FOURK + xi);
1451             			
1452 xudong 1.15 			if (errorprop(bT, bA, bI,
1453             						  errbT, errbA, errbI, errbTbA, errbTbI, errbIbA,
1454             						  lon, lat, disk_lonc, disk_latc, pa, FOURK, FOURK, xi, zeta,
1455 xudong 1.1  						  &btSigma2, &bpSigma2, &brSigma2)) {
1456             				bx_err[ind_map] = DRMS_MISSING_FLOAT;
1457             				by_err[ind_map] = DRMS_MISSING_FLOAT;
1458             				bz_err[ind_map] = DRMS_MISSING_FLOAT;
1459             				continue;
1460             			}
1461             			
1462             			bx_err[ind_map] = sqrt(bpSigma2);
1463             			by_err[ind_map] = sqrt(btSigma2);
1464             			bz_err[ind_map] = sqrt(brSigma2);
1465             			
1466             		}
1467             	}
1468             	
1469             	//
1470             	
1471             	free(bT); free(bI); free(bA);
1472             	free(errbT); free(errbI); free(errbA);
1473             	free(errbTbI); free(errbTbA); free(errbIbA);
1474             	return 0;
1475             	
1476 xudong 1.1  }
1477             
1478             
1479             
1480             /*
1481              * Read full disk vector magnetograms
1482              * Fill factor is 1, use default disambiguity resolution
1483              *
1484              */
1485             
1486             int readVectorB(DRMS_Record_t *inRec, float *bx_img, float *by_img, float *bz_img)
1487             {
1488             	
1489             	int status = 0;
1490             	
1491             	DRMS_Segment_t *inSeg;
1492             	DRMS_Array_t *inArray_ambig;
1493 xudong 1.6  	DRMS_Array_t *inArray_bTotal, *inArray_bAzim, *inArray_bIncl;
1494 xudong 1.1  	
1495             	char *ambig;
1496             	float *bTotal, *bAzim, *bIncl;
1497             	
1498             	inSeg = drms_segment_lookup(inRec, "disambig");
1499             	inArray_ambig = drms_segment_read(inSeg, DRMS_TYPE_CHAR, &status);
1500             	if (status) return 1;
1501             	ambig = (char *)inArray_ambig->data;
1502             	
1503             	inSeg = drms_segment_lookup(inRec, "field");
1504             	inArray_bTotal = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
1505             	if (status) return 1;
1506             	bTotal = (float *)inArray_bTotal->data;
1507             	
1508             	inSeg = drms_segment_lookup(inRec, "azimuth");
1509             	inArray_bAzim = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
1510             	if (status) return 1;
1511             	bAzim = (float *)inArray_bAzim->data;
1512             	
1513             	inSeg = drms_segment_lookup(inRec, "inclination");
1514             	inArray_bIncl = drms_segment_read(inSeg, DRMS_TYPE_FLOAT, &status);
1515 xudong 1.1  	if (status) return 1;
1516             	bIncl = (float *)inArray_bIncl->data;
1517             	
1518             	// Convert CCD xyz
1519             	
1520             	int llx, lly;		// lower-left corner
1521             	int bmx, bmy;		// bitmap size
1522 xudong 1.23     
1523                 if (fullDisk) {
1524                     llx = lly = 0;
1525                     bmx = bmy = FOURK;
1526                 } else {
1527                     llx = (int)(drms_getkey_float(inRec, "CRPIX1", &status)) - 1;
1528                     lly = (int)(drms_getkey_float(inRec, "CRPIX2", &status)) - 1;
1529                     bmx = inArray_ambig->axis[0];
1530                     bmy = inArray_ambig->axis[1];
1531                 }
1532 xudong 1.1  	
1533             	int kx, ky, kOff;
1534             	int ix = 0, jy = 0, yOff = 0, iData = 0;
1535             	int xDim = FOURK, yDim = FOURK;
1536 xudong 1.23 	int amb = 0;
1537 xudong 1.1  	
1538             	for (jy = 0; jy < yDim; jy++)
1539             	{
1540             		ix = 0;
1541             		yOff = jy * xDim;
1542             		ky = jy - lly;
1543             		for (ix = 0; ix < xDim; ix++)
1544             		{
1545             			iData = yOff + ix;
1546             			kx = ix - llx;
1547             			
1548             			// zero azi pointing up, zero incl pointing out from sun
1549             			bx_img[iData] = - bTotal[iData] * sin(bIncl[iData] * RADSINDEG) * sin(bAzim[iData] * RADSINDEG);
1550             			by_img[iData] = bTotal[iData] * sin(bIncl[iData] * RADSINDEG) * cos(bAzim[iData] * RADSINDEG);
1551             			bz_img[iData] = bTotal[iData] * cos(bIncl[iData] * RADSINDEG);
1552                         
1553             			// Disambiguation
1554             			
1555             			if (kx < 0 || kx >= bmx || ky < 0 || ky >= bmy) {
1556             				continue;
1557             			} else {
1558 xudong 1.1  				kOff = ky * bmx + kx;
1559 xudong 1.23                 //				if (ambig[kOff] % 2) {		// 180
1560             				// Feb 12 2014, use bit #2 for full disk, lowest bit for patch
1561             				if (fullDisk) { amb = (ambig[kOff] / 4) % 2; } else { amb = ambig[kOff] % 2; }
1562             				if (amb) {				// Feb 12 2014, use bit #2
1563 xudong 1.1  					bx_img[iData] *= -1.; by_img[iData] *= -1.;
1564 xudong 1.15 				}
1565 xudong 1.1  			}
1566             		}
1567             	}
1568             	
1569             	// Clean up
1570             	
1571             	drms_free_array(inArray_ambig);
1572             	drms_free_array(inArray_bTotal);
1573             	drms_free_array(inArray_bAzim);
1574             	drms_free_array(inArray_bIncl);
1575             	
1576             	return 0;
1577             	
1578             }
1579             
1580             
1581             /*
1582              * Read variances and covariances of vector magnetograms
1583              *
1584              */
1585             
1586 xudong 1.15 int readVectorBErr(DRMS_Record_t *inRec,
1587 xudong 1.1  				   float *bT, float *bI, float *bA,
1588 xudong 1.15 				   float *errbT, float *errbI, float *errbA,
1589 xudong 1.1  				   float *errbTbI, float *errbTbA, float *errbIbA)
1590             {
1591             	
1592             	int status = 0;
1593             	
1594             	float *data_ptr[9];
1595             	char *segName[9] = {"field", "inclination", "azimuth",
1596 xudong 1.6  		"field_err", "inclination_err", "azimuth_err",
1597             		"field_inclination_err", "field_az_err", "inclin_azimuth_err"};
1598 xudong 1.1  	DRMS_Segment_t *inSegs[9];
1599             	DRMS_Array_t *inArrays[9];
1600             	
1601             	// Read full disk images
1602 xudong 1.23     // Do we need disambig? Dec 30 XS
1603 xudong 1.1  	
1604             	for (int iSeg = 0; iSeg < 9; iSeg++) {
1605             		
1606             		inSegs[iSeg] = drms_segment_lookup(inRec, segName[iSeg]);
1607             		inArrays[iSeg] = drms_segment_read(inSegs[iSeg], DRMS_TYPE_FLOAT, &status);
1608             		data_ptr[iSeg] = (float *) inArrays[iSeg]->data;
1609             		
1610             	}
1611             	
1612             	float *bT0 = data_ptr[0], *bI0 = data_ptr[1], *bA0 = data_ptr[2];
1613             	float *errbT0 = data_ptr[3], *errbI0 = data_ptr[4], *errbA0 = data_ptr[5];
1614             	float *errbTbI0 = data_ptr[6], *errbTbA0 = data_ptr[7], *errbIbA0 = data_ptr[8];
1615             	
1616 xudong 1.23 	// Add disambig, Feb 12 2014
1617             	
1618             	DRMS_Segment_t *inSeg;
1619                 DRMS_Array_t *inArray_ambig;
1620                 
1621                 if (amb4err) {              // Mar 4 2014
1622                 
1623                     inSeg = drms_segment_lookup(inRec, "disambig");
1624                     inArray_ambig = drms_segment_read(inSeg, DRMS_TYPE_CHAR, &status);
1625                     if (status) return 1;
1626                     char *ambig = (char *)inArray_ambig->data;
1627                     
1628                     int llx, lly;		// lower-left corner
1629                     int bmx, bmy;		// bitmap size
1630                     
1631                     if (fullDisk) {
1632                         llx = lly = 0;
1633                         bmx = bmy = FOURK;
1634                     } else {
1635                         llx = (int)(drms_getkey_float(inRec, "CRPIX1", &status)) - 1;
1636                         lly = (int)(drms_getkey_float(inRec, "CRPIX2", &status)) - 1;
1637 xudong 1.23             bmx = inArray_ambig->axis[0];
1638                         bmy = inArray_ambig->axis[1];
1639                     }
1640                     
1641                     int idx, idx_a;
1642                     int amb;
1643                     
1644                     for (int j = 0; j < bmy; j++) {
1645                         for (int i = 0; i < bmx; i++) {
1646                             idx_a = j * bmx + i;
1647                             idx = (j + lly) * FOURK + (i + llx);
1648                             // Feb 12 2014, use bit #2 for full disk, lowest bit for patch
1649                             if (fullDisk) { amb = (ambig[idx_a] / 4) % 2; } else { amb = ambig[idx_a] % 2; }
1650                             if (amb) { bA0[idx] += 180.; }
1651                         }
1652                     }
1653                     
1654                 }
1655                 
1656 xudong 1.1  	// Convert errors to variances, correlation coefficients to covariances
1657             	
1658             	for (int i = 0; i < FOURK2; i++) {
1659             		
1660             		if (fabs(errbI0[i]) > 180.) errbI0[i] = 180.;
1661             		if (fabs(errbA0[i]) > 180.) errbA0[i] = 180.;
1662             		
1663             		bT[i] = bT0[i];
1664 xudong 1.23 		bI[i] = bI0[i];		// in deg, coverted in errorprop
1665 xudong 1.1  		bA[i] = bA0[i];
1666             		
1667             		errbT[i] = errbT0[i] * errbT0[i];
1668             		errbI[i] = errbI0[i] * errbI0[i] * RADSINDEG * RADSINDEG;
1669             		errbA[i] = errbA0[i] * errbA0[i] * RADSINDEG * RADSINDEG;
1670             		
1671             		errbTbI[i] = errbTbI0[i] * errbT0[i] * errbI0[i] * RADSINDEG;
1672                     errbTbA[i] = errbTbA0[i] * errbT0[i] * errbA0[i] * RADSINDEG;
1673                     errbIbA[i] = errbIbA0[i] * errbI0[i] * errbA0[i] * RADSINDEG * RADSINDEG;
1674 xudong 1.6  		
1675 xudong 1.1  	}
1676             	
1677             	//
1678             	
1679             	for (int iSeg = 0; iSeg < 9; iSeg++) drms_free_array(inArrays[iSeg]);
1680 xudong 1.23 	if (amb4err) drms_free_array(inArray_ambig);		// Feb 12; Mar 04 2014
1681 xudong 1.6  	
1682 xudong 1.1  	return 0;
1683             	
1684             }
1685             
1686             
1687             /*
1688              * Create Cutout record: top level subroutine
1689              * Do the loops on segments and set the keywords here
1690              * Work is done in writeCutout routine below
1691 xudong 1.15  *
1692 xudong 1.1   */
1693             
1694 xudong 1.15 int createCutRecord(DRMS_Record_t *mharpRec, DRMS_Record_t *bharpRec,
1695             					DRMS_Record_t *dopRec, DRMS_Record_t *contRec,
1696 xudong 1.1  					DRMS_Record_t *sharpRec, struct swIndex *swKeys_ptr)
1697             {
1698             	
1699             	int status = 0;
1700             	
1701             	int iHarpSeg;
1702             	int nMharpSegs = ARRLENGTH(MharpSegs), nBharpSegs = ARRLENGTH(BharpSegs);
1703             	
1704             	// Cutout Mharp
1705             	
1706             	for (iHarpSeg = 0; iHarpSeg < nMharpSegs; iHarpSeg++) {
1707             		if (writeCutout(sharpRec, mharpRec, mharpRec, MharpSegs[iHarpSeg])) {
1708             			printf("Mharp cutout fails for %s\n", MharpSegs[iHarpSeg]);
1709             			break;
1710             		}
1711             	}
1712 xudong 1.3  	if (iHarpSeg != nMharpSegs) {
1713             		SHOW("Cutout: segment number unmatch\n");
1714             		return 1;		// if failed
1715             	}
1716 xudong 1.1  	printf("Magnetogram cutout done.\n");
1717             	
1718             	// Cutout Doppler
1719             	
1720             	if (writeCutout(sharpRec, dopRec, mharpRec, "Dopplergram")) {
1721             		printf("Doppler cutout failed\n");
1722             		return 1;
1723             	}
1724             	printf("Dopplergram cutout done.\n");
1725             	
1726             	// Cutout Continuum
1727             	
1728             	if (writeCutout(sharpRec, contRec, mharpRec, "continuum")) {
1729             		printf("Continuum cutout failed\n");
1730             		return 1;
1731             	}
1732             	printf("Intensitygram cutout done.\n");
1733             	
1734             	// Coutout Bharp
1735             	
1736             	for (iHarpSeg = 0; iHarpSeg < nBharpSegs; iHarpSeg++) {
1737 xudong 1.1  		if (writeCutout(sharpRec, bharpRec, mharpRec, BharpSegs[iHarpSeg])) {
1738             			printf("Bharp cutout fails for %s\n", BharpSegs[iHarpSeg]);
1739             			break;
1740             		}
1741             	}
1742             	if (iHarpSeg != nBharpSegs) return 1;		// if failed
1743             	printf("Vector B cutout done.\n");
1744             	
1745             	// Keywords & Links
1746             	
1747             	drms_copykey(sharpRec, mharpRec, "T_REC");
1748             	drms_copykey(sharpRec, mharpRec, "HARPNUM");
1749             	
1750             	DRMS_Link_t *mHarpLink = hcon_lookup_lower(&sharpRec->links, "MHARP");
1751             	if (mHarpLink) drms_link_set("MHARP", sharpRec, mharpRec);
1752             	DRMS_Link_t *bHarpLink = hcon_lookup_lower(&sharpRec->links, "BHARP");
1753             	if (bHarpLink) drms_link_set("BHARP", sharpRec, bharpRec);
1754             	
1755             	setSWIndex(sharpRec, swKeys_ptr);	// Set space weather indices
1756 xudong 1.23 	setKeys(sharpRec, mharpRec, bharpRec, NULL);              // Set all other keywords, NULL specifies cutout
1757 xudong 1.6  	
1758 xudong 1.1  	// Stats
1759 xudong 1.6  	
1760 xudong 1.1  	int nCutSegs = ARRLENGTH(CutSegs);
1761             	for (int iSeg = 0; iSeg < nCutSegs; iSeg++) {
1762             		DRMS_Segment_t *outSeg = drms_segment_lookupnum(sharpRec, iSeg);
1763             		DRMS_Array_t *outArray = drms_segment_read(outSeg, DRMS_TYPE_FLOAT, &status);
1764             		set_statistics(outSeg, outArray, 1);
1765             		drms_free_array(outArray);
1766             	}
1767 xudong 1.6  	
1768 xudong 1.1  	return 0;
1769             	
1770 xudong 1.15 }
1771 xudong 1.1  
1772             
1773 xudong 1.15 /*
1774 xudong 1.1   * Get cutout and write segment
1775              * Change DISAMB_AZI to apply disambiguation to azimuth
1776              *
1777              */
1778             
1779             int writeCutout(DRMS_Record_t *outRec, DRMS_Record_t *inRec, DRMS_Record_t *harpRec, char *SegName)
1780             {
1781             	
1782             	int status = 0;
1783             	
1784             	DRMS_Segment_t *inSeg = NULL, *outSeg = NULL;
1785             	DRMS_Array_t *cutoutArray = NULL;
1786             	//	DRMS_Type_t arrayType;
1787             	
1788             	int ll[2], ur[2], nx, ny, nxny;		// lower-left and upper right coords
1789             	
1790             	/* Info */
1791             	
1792             	inSeg = drms_segment_lookup(inRec, SegName);
1793             	if (!inSeg) return 1;
1794             	
1795 xudong 1.1  	nx = (int) drms_getkey_float(harpRec, "CRSIZE1", &status);
1796             	ny = (int) drms_getkey_float(harpRec, "CRSIZE2", &status);
1797             	nxny = nx * ny;
1798             	ll[0] = (int) drms_getkey_float(harpRec, "CRPIX1", &status) - 1; if (status) return 1;
1799             	ll[1] = (int) drms_getkey_float(harpRec, "CRPIX2", &status) - 1; if (status) return 1;
1800             	ur[0] = ll[0] + nx - 1; if (status) return 1;
1801             	ur[1] = ll[1] + ny - 1; if (status) return 1;
1802             	
1803             	if (inSeg->axis[0] == nx && inSeg->axis[1] == ny) {			// for bitmaps, infomaps, etc.
1804             		cutoutArray = drms_segment_read(inSeg, DRMS_TYPE_DOUBLE, &status);
1805             		if (status) return 1;
1806             	} else if (inSeg->axis[0] == FOURK && inSeg->axis[1] == FOURK) {		// for full disk ones
1807             		cutoutArray = drms_segment_readslice(inSeg, DRMS_TYPE_DOUBLE, ll, ur, &status);
1808             		if (status) return 1;
1809             	} else {
1810             		return 1;
1811             	}
1812 xudong 1.6  	
1813 xudong 1.23 	// Feb 12 2014, fool-proof, for patch, change everything to 0 or 7!!!
1814             	// This is a fix for disambiguation before Aug 2013
1815             	
1816             	if (!strcmp(SegName, "disambig") && !fullDisk) {
1817             		double *disamb = (double *) (cutoutArray->data);
1818             		for (int i = 0; i < nxny; i++) {
1819             			if (((int)disamb[i]) % 2) { disamb[i] = 7; } else { disamb[i] = 0; }
1820             		}
1821             	}
1822             	
1823 xudong 1.1  	/* Adding disambiguation resolution to cutout azimuth? */
1824 xudong 1.6  	
1825 xudong 1.1  #if DISAMB_AZI
1826 xudong 1.23 	int amb;
1827 xudong 1.1  	if (!strcmp(SegName, "azimuth")) {
1828 xudong 1.6  		DRMS_Segment_t *disambSeg = NULL;
1829             		disambSeg = drms_segment_lookup(inRec, "disambig");
1830 xudong 1.1  		if (!disambSeg) {drms_free_array(cutoutArray); return 1;}
1831             		DRMS_Array_t *disambArray;
1832 xudong 1.23         if (fullDisk) { // Jan 2 2014 XS
1833                         disambArray = drms_segment_readslice(disambSeg, DRMS_TYPE_CHAR, ll, ur, &status);
1834                         if (status) return 1;
1835                     } else {
1836                         if (disambSeg->axis[0] == nx && disambSeg->axis[1] == ny) {
1837                             disambArray = drms_segment_read(disambSeg, DRMS_TYPE_CHAR, &status);
1838                             if (status) {drms_free_array(cutoutArray); return 1;}
1839                         } else {
1840                             drms_free_array(cutoutArray);
1841                             return 1;
1842                         }
1843                     }
1844 xudong 1.1  		double *azimuth = (double *) cutoutArray->data;
1845             		char *disamb = (char *) disambArray->data;
1846             		for (int n = 0; n < nxny; n++) {
1847 xudong 1.23             //			if (disamb[n] % 2) azimuth[n] += 180.;      // Nov 12 2013 Fixed!!!
1848             			// Feb 12 2014, use bit #2 for full disk, lowest bit for patch
1849             			if (fullDisk) { amb = (disamb[n] / 4) % 2; } else { amb = disamb[n] % 2; }
1850             			if (amb) azimuth[n] += 180.;
1851 xudong 1.1  		}
1852             		drms_free_array(disambArray);
1853             	}
1854             #endif
1855 xudong 1.6  	
1856 xudong 1.1  	/* Write out */
1857             	
1858             	outSeg = drms_segment_lookup(outRec, SegName);
1859             	if (!outSeg) return 1;
1860 xudong 1.15     //	drms_array_convert_inplace(outSeg->info->type, 0, 1, cutoutArray);	// Jan 02 2013
1861 xudong 1.1  	outSeg->axis[0] = cutoutArray->axis[0];
1862             	outSeg->axis[1] = cutoutArray->axis[1];
1863 xudong 1.15     //	cutoutArray->parent_segment = outSeg;
1864 xudong 1.8  	cutoutArray->israw = 0;		// always compressed
1865 xudong 1.1      cutoutArray->bzero = outSeg->bzero;
1866                 cutoutArray->bscale = outSeg->bscale;		// Same as inArray's
1867             	status = drms_segment_write(outSeg, cutoutArray, 0);
1868             	drms_free_array(cutoutArray);
1869             	if (status) return 1;
1870             	
1871             	return 0;
1872             	
1873             }
1874             
1875             
1876 xudong 1.15 /*
1877 xudong 1.1   * Compute space weather indices, no error checking for now
1878              * Based on M. Bobra's swharp_vectorB.c
1879              * No error checking for now
1880              *
1881              */
1882             
1883             void computeSWIndex(struct swIndex *swKeys_ptr, DRMS_Record_t *inRec, struct mapInfo *mInfo)
1884             {
1885             	
1886             	int status = 0;
1887             	int nx = mInfo->ncol, ny = mInfo->nrow;
1888             	int nxny = nx * ny;
1889             	int dims[2] = {nx, ny};
1890 xudong 1.15     
1891 xudong 1.1  	// Get bx, by, bz, mask
1892             	
1893 xudong 1.6  	// Use HARP (Turmon) bitmap as a threshold on spaceweather quantities
1894 mbobra 1.5  	DRMS_Segment_t *bitmaskSeg = drms_segment_lookup(inRec, "bitmap");
1895             	DRMS_Array_t *bitmaskArray = drms_segment_read(bitmaskSeg, DRMS_TYPE_INT, &status);
1896             	int *bitmask = (int *) bitmaskArray->data;		// get the previously made mask array
1897 xudong 1.6  	
1898 xudong 1.15 	//Use conf_disambig map as a threshold on spaceweather quantities
1899             	DRMS_Segment_t *maskSeg = drms_segment_lookup(inRec, "conf_disambig");
1900 xudong 1.1  	DRMS_Array_t *maskArray = drms_segment_read(maskSeg, DRMS_TYPE_INT, &status);
1901             	int *mask = (int *) maskArray->data;		// get the previously made mask array
1902 xudong 1.6  	
1903 xudong 1.1  	DRMS_Segment_t *bxSeg = drms_segment_lookup(inRec, BP_SEG_CEA);
1904             	DRMS_Array_t *bxArray = drms_segment_read(bxSeg, DRMS_TYPE_FLOAT, &status);
1905             	float *bx = (float *) bxArray->data;		// bx
1906             	
1907             	DRMS_Segment_t *bySeg = drms_segment_lookup(inRec, BT_SEG_CEA);
1908             	DRMS_Array_t *byArray = drms_segment_read(bySeg, DRMS_TYPE_FLOAT, &status);
1909             	float *by = (float *) byArray->data;		// by
1910             	for (int i = 0; i < nxny; i++) by[i] *= -1;
1911             	
1912             	DRMS_Segment_t *bzSeg = drms_segment_lookup(inRec, BR_SEG_CEA);
1913             	DRMS_Array_t *bzArray = drms_segment_read(bzSeg, DRMS_TYPE_FLOAT, &status);
1914             	float *bz = (float *) bzArray->data;		// bz
1915 xudong 1.15     
1916 mbobra 1.29        //Use magnetogram map to compute R
1917                    DRMS_Segment_t *losSeg = drms_segment_lookup(inRec, "magnetogram");
1918                    DRMS_Array_t *losArray = drms_segment_read(losSeg, DRMS_TYPE_FLOAT, &status);
1919                    float *los = (float *) losArray->data;          // los
1920 xudong 1.23     
1921 mbobra 1.14 	DRMS_Segment_t *bz_errSeg = drms_segment_lookup(inRec, BR_ERR_SEG_CEA);
1922             	DRMS_Array_t *bz_errArray = drms_segment_read(bz_errSeg, DRMS_TYPE_FLOAT, &status);
1923             	float *bz_err = (float *) bz_errArray->data;		// bz_err
1924 xudong 1.15     
1925 mbobra 1.14 	DRMS_Segment_t *by_errSeg = drms_segment_lookup(inRec, BT_ERR_SEG_CEA);
1926             	DRMS_Array_t *by_errArray = drms_segment_read(by_errSeg, DRMS_TYPE_FLOAT, &status);
1927             	float *by_err = (float *) by_errArray->data;		// by_err
1928             	//for (int i = 0; i < nxny; i++) by_err[i] *= -1;
1929 xudong 1.15     
1930 mbobra 1.14 	DRMS_Segment_t *bx_errSeg = drms_segment_lookup(inRec, BP_ERR_SEG_CEA);
1931             	DRMS_Array_t *bx_errArray = drms_segment_read(bx_errSeg, DRMS_TYPE_FLOAT, &status);
1932             	float *bx_err = (float *) bx_errArray->data;		// bx_err
1933 xudong 1.1  	
1934             	// Get emphemeris
1935 mbobra 1.16 	float  cdelt1_orig = drms_getkey_float(inRec, "CDELT1",   &status);
1936 xudong 1.9  	float  dsun_obs    = drms_getkey_float(inRec, "DSUN_OBS",   &status);
1937 mbobra 1.16 	double rsun_ref    = drms_getkey_double(inRec, "RSUN_REF", &status);
1938             	double rsun_obs    = drms_getkey_double(inRec, "RSUN_OBS", &status);
1939             	float imcrpix1     = drms_getkey_float(inRec, "IMCRPIX1", &status);
1940             	float imcrpix2     = drms_getkey_float(inRec, "IMCRPIX2", &status);
1941             	float crpix1       = drms_getkey_float(inRec, "CRPIX1", &status);
1942             	float crpix2       = drms_getkey_float(inRec, "CRPIX2", &status);
1943 xudong 1.23     
1944 mbobra 1.28         // convert cdelt1_orig from degrees to arcsec
1945                     float cdelt1       = (atan((rsun_ref*cdelt1_orig*RADSINDEG)/(dsun_obs)))*(1/RADSINDEG)*(3600.);
1946             
1947 mbobra 1.29 	//if (nx1 > floor((nx-1)/scale + 1) )
1948             	//	DIE("X-dimension of output array in fsample() is too large.");
1949             	//if (ny1 > floor((ny-1)/scale + 1) )
1950             	//	DIE("Y-dimension of output array in fsample() is too large.");
1951 xudong 1.23     
1952 xudong 1.15 	// Temp arrays
1953 mbobra 1.14 	float *bh      = (float *) (malloc(nxny * sizeof(float)));
1954             	float *bt      = (float *) (malloc(nxny * sizeof(float)));
1955             	float *jz      = (float *) (malloc(nxny * sizeof(float)));
1956 xudong 1.9  	float *jz_smooth = (float *) (malloc(nxny * sizeof(float)));
1957 mbobra 1.14 	float *bpx     = (float *) (malloc(nxny * sizeof(float)));
1958             	float *bpy     = (float *) (malloc(nxny * sizeof(float)));
1959             	float *bpz     = (float *) (malloc(nxny * sizeof(float)));
1960             	float *derx    = (float *) (malloc(nxny * sizeof(float)));
1961             	float *dery    = (float *) (malloc(nxny * sizeof(float)));
1962 xudong 1.1  	float *derx_bt = (float *) (malloc(nxny * sizeof(float)));
1963             	float *dery_bt = (float *) (malloc(nxny * sizeof(float)));
1964             	float *derx_bh = (float *) (malloc(nxny * sizeof(float)));
1965             	float *dery_bh = (float *) (malloc(nxny * sizeof(float)));
1966             	float *derx_bz = (float *) (malloc(nxny * sizeof(float)));
1967             	float *dery_bz = (float *) (malloc(nxny * sizeof(float)));
1968 mbobra 1.14 	float *bt_err  = (float *) (malloc(nxny * sizeof(float)));
1969             	float *bh_err  = (float *) (malloc(nxny * sizeof(float)));
1970 mbobra 1.29         float *jz_err  = (float *) (malloc(nxny * sizeof(float)));
1971                     float *jz_err_squared = (float *) (malloc(nxny * sizeof(float)));
1972                     float *jz_err_squared_smooth = (float *) (malloc(nxny * sizeof(float)));
1973                     float *jz_rms_err = (float *) (malloc(nxny * sizeof(float)));
1974                  
1975                     // define some values for the R calculation 
1976                     int scale = round(2.0/cdelt1);
1977                     int nx1 = nx/scale;
1978                     int ny1 = ny/scale;
1979                     int nxp = nx1+40;
1980                     int nyp = ny1+40;
1981                     float *rim     = (float *)malloc(nx1*ny1*sizeof(float));
1982                     float *p1p0    = (float *)malloc(nx1*ny1*sizeof(float));
1983                     float *p1n0    = (float *)malloc(nx1*ny1*sizeof(float));
1984                     float *p1p     = (float *)malloc(nx1*ny1*sizeof(float));
1985                     float *p1n     = (float *)malloc(nx1*ny1*sizeof(float));
1986                     float *p1      = (float *)malloc(nx1*ny1*sizeof(float));
1987                     float *pmap    = (float *)malloc(nxp*nyp*sizeof(float));
1988                     float *p1pad   = (float *)malloc(nxp*nyp*sizeof(float));
1989                     float *pmapn   = (float *)malloc(nx1*ny1*sizeof(float));
1990 mbobra 1.30 
1991                     // define some arrays for the lorentz force calculation
1992                     float *fx = (float *) (malloc(nxny * sizeof(float)));
1993                     float *fy = (float *) (malloc(nxny * sizeof(float)));
1994                     float *fz = (float *) (malloc(nxny * sizeof(float)));
1995 xudong 1.23     
1996 mbobra 1.30 
1997 arta   1.22 	//spaceweather quantities computed
1998 xudong 1.15 	if (computeAbsFlux(bz_err, bz , dims, &(swKeys_ptr->absFlux), &(swKeys_ptr->mean_vf),  &(swKeys_ptr->mean_vf_err),
1999 mbobra 1.29                            &(swKeys_ptr->count_mask), mask, bitmask, cdelt1, rsun_ref, rsun_obs))
2000                     {
2001 xudong 1.1  		swKeys_ptr->absFlux = DRMS_MISSING_FLOAT;		// If fail, fill in NaN
2002             		swKeys_ptr->mean_vf = DRMS_MISSING_FLOAT;
2003 mbobra 1.29                 swKeys_ptr->mean_vf_err = DRMS_MISSING_FLOAT;
2004                             swKeys_ptr->count_mask  = DRMS_MISSING_INT;
2005 xudong 1.1  	}
2006 xudong 1.15     
2007 xudong 1.1  	for (int i = 0; i < nxny; i++) bpz[i] = bz[i];
2008 xudong 1.15 	greenpot(bpx, bpy, bpz, nx, ny);
2009 xudong 1.1  	
2010 mbobra 1.14 	computeBh(bx_err, by_err, bh_err, bx, by, bz, bh, dims, &(swKeys_ptr->mean_hf), mask, bitmask);
2011 xudong 1.15     
2012 mbobra 1.14 	if (computeGamma(bz_err, bh_err, bx, by, bz, bh, dims, &(swKeys_ptr->mean_gamma), &(swKeys_ptr->mean_gamma_err),mask, bitmask))
2013 xudong 1.15 	{
2014 mbobra 1.29                 swKeys_ptr->mean_gamma     =  DRMS_MISSING_FLOAT;
2015                             swKeys_ptr->mean_gamma_err =  DRMS_MISSING_FLOAT;
2016                     }
2017 xudong 1.1  	
2018 mbobra 1.14 	computeB_total(bx_err, by_err, bz_err, bt_err, bx, by, bz, bt, dims, mask, bitmask);
2019 xudong 1.1  	
2020 mbobra 1.29 	if (computeBtotalderivative(bt, dims, &(swKeys_ptr->mean_derivative_btotal), mask, bitmask, derx_bt, 
2021                                                 dery_bt, bt_err, &(swKeys_ptr->mean_derivative_btotal_err)))
2022                     {
2023 xudong 1.1  		swKeys_ptr->mean_derivative_btotal = DRMS_MISSING_FLOAT;
2024 mbobra 1.14 		swKeys_ptr->mean_derivative_btotal_err = DRMS_MISSING_FLOAT;
2025 mbobra 1.29         }
2026 xudong 1.1  	
2027 mbobra 1.29 	if (computeBhderivative(bh, bh_err, dims, &(swKeys_ptr->mean_derivative_bh), 
2028                                             &(swKeys_ptr->mean_derivative_bh_err), mask, bitmask, derx_bh, dery_bh))
2029                     {
2030 xudong 1.1  		swKeys_ptr->mean_derivative_bh = DRMS_MISSING_FLOAT;
2031 mbobra 1.29                 swKeys_ptr->mean_derivative_bh_err = DRMS_MISSING_FLOAT;
2032 mbobra 1.14 	}
2033 xudong 1.15     
2034 mbobra 1.29 	if (computeBzderivative(bz, bz_err, dims, &(swKeys_ptr->mean_derivative_bz), &(swKeys_ptr->mean_derivative_bz_err), 
2035                                             mask, bitmask, derx_bz, dery_bz))
2036                     {
2037 xudong 1.1  		swKeys_ptr->mean_derivative_bz = DRMS_MISSING_FLOAT; // If fail, fill in NaN
2038 mbobra 1.29                 swKeys_ptr->mean_derivative_bz_err = DRMS_MISSING_FLOAT;
2039                     }
2040 xudong 1.1  	
2041 xudong 1.15 	computeJz(bx_err, by_err, bx, by, dims, jz, jz_err, jz_err_squared, mask, bitmask, cdelt1, rsun_ref, rsun_obs,
2042                           derx, dery);
2043                 
2044                 
2045 mbobra 1.29         if(computeJzsmooth(bx, by, dims, jz, jz_smooth, jz_err, jz_rms_err, jz_err_squared_smooth, &(swKeys_ptr->mean_jz),
2046 xudong 1.15                        &(swKeys_ptr->mean_jz_err), &(swKeys_ptr->us_i), &(swKeys_ptr->us_i_err), mask, bitmask, cdelt1,
2047                                    rsun_ref, rsun_obs, derx, dery))
2048 mbobra 1.29         {
2049                             swKeys_ptr->mean_jz            = DRMS_MISSING_FLOAT;
2050 mbobra 1.14 		swKeys_ptr->us_i               = DRMS_MISSING_FLOAT;
2051 mbobra 1.29                 swKeys_ptr->mean_jz_err        = DRMS_MISSING_FLOAT;
2052                             swKeys_ptr->us_i_err           = DRMS_MISSING_FLOAT;
2053 xudong 1.1  	}
2054 xudong 1.15     
2055 mbobra 1.29 	if (computeAlpha(jz_err, bz_err, bz, dims, jz, jz_smooth, &(swKeys_ptr->mean_alpha), &(swKeys_ptr->mean_alpha_err), 
2056                                      mask, bitmask, cdelt1, rsun_ref, rsun_obs))
2057                     {
2058 mbobra 1.14 		swKeys_ptr->mean_alpha         = DRMS_MISSING_FLOAT;
2059 mbobra 1.29                 swKeys_ptr->mean_alpha_err     = DRMS_MISSING_FLOAT;
2060                     }
2061 mbobra 1.14 	
2062 mbobra 1.29 	if (computeHelicity(jz_err, jz_rms_err, bz_err, bz, dims, jz, &(swKeys_ptr->mean_ih), &(swKeys_ptr->mean_ih_err), 
2063                                         &(swKeys_ptr->total_us_ih), &(swKeys_ptr->total_abs_ih),
2064                                         &(swKeys_ptr->total_us_ih_err), &(swKeys_ptr->total_abs_ih_err), mask, bitmask, cdelt1, rsun_ref, rsun_obs))
2065                     {
2066 xudong 1.15 		swKeys_ptr->mean_ih            = DRMS_MISSING_FLOAT;
2067 mbobra 1.14 		swKeys_ptr->total_us_ih        = DRMS_MISSING_FLOAT;
2068               		swKeys_ptr->total_abs_ih       = DRMS_MISSING_FLOAT;
2069 mbobra 1.29                 swKeys_ptr->mean_ih_err        = DRMS_MISSING_FLOAT;
2070                             swKeys_ptr->total_us_ih_err    = DRMS_MISSING_FLOAT;
2071                             swKeys_ptr->total_abs_ih_err   = DRMS_MISSING_FLOAT;
2072 xudong 1.1  	}
2073 xudong 1.15     
2074             	if (computeSumAbsPerPolarity(jz_err, bz_err, bz, jz, dims, &(swKeys_ptr->totaljz), &(swKeys_ptr->totaljz_err),
2075 mbobra 1.29 				     mask, bitmask, cdelt1, rsun_ref, rsun_obs))
2076                     {  
2077 mbobra 1.14 		swKeys_ptr->totaljz            = DRMS_MISSING_FLOAT;
2078 mbobra 1.29                 swKeys_ptr->totaljz_err        = DRMS_MISSING_FLOAT;
2079 mbobra 1.14 	}
2080 xudong 1.6  	
2081 xudong 1.15 	if (computeFreeEnergy(bx_err, by_err, bx, by, bpx, bpy, dims,
2082 mbobra 1.29 			      &(swKeys_ptr->meanpot), &(swKeys_ptr->meanpot_err), &(swKeys_ptr->totpot), &(swKeys_ptr->totpot_err),
2083             			      mask, bitmask, cdelt1, rsun_ref, rsun_obs))
2084                     {
2085 mbobra 1.14 		swKeys_ptr->meanpot            = DRMS_MISSING_FLOAT; // If fail, fill in NaN
2086             		swKeys_ptr->totpot             = DRMS_MISSING_FLOAT;
2087 mbobra 1.29                 swKeys_ptr->meanpot_err        = DRMS_MISSING_FLOAT;
2088                             swKeys_ptr->totpot_err         = DRMS_MISSING_FLOAT;
2089 xudong 1.1  	}
2090 xudong 1.15     
2091 xudong 1.24     
2092 mbobra 1.18 	if (computeShearAngle(bx_err, by_err, bz_err, bx, by, bz, bpx, bpy, bpz, dims,
2093 mbobra 1.29 			      &(swKeys_ptr->meanshear_angle), &(swKeys_ptr->meanshear_angle_err), &(swKeys_ptr->area_w_shear_gt_45),
2094             			      mask, bitmask)) 
2095             	{
2096 mbobra 1.14 		swKeys_ptr->meanshear_angle    = DRMS_MISSING_FLOAT; // If fail, fill in NaN
2097 xudong 1.1  		swKeys_ptr->area_w_shear_gt_45 = DRMS_MISSING_FLOAT;
2098 mbobra 1.29                 swKeys_ptr->meanshear_angle_err= DRMS_MISSING_FLOAT;
2099 xudong 1.1  	}
2100 xudong 1.23     
2101             	if (computeR(bz_err, los , dims, &(swKeys_ptr->Rparam), cdelt1, rim, p1p0, p1n0,
2102 mbobra 1.29                      p1p, p1n, p1, pmap, nx1, ny1, scale, p1pad, nxp, nyp, pmapn))
2103 mbobra 1.25         {
2104 xudong 1.23 		swKeys_ptr->Rparam = DRMS_MISSING_FLOAT;		// If fail, fill in NaN
2105 mbobra 1.29         }
2106 mbobra 1.25 
2107 mbobra 1.30     
2108             	if (computeLorentz(bx, by, bz, fx, fy, fz, dims, &(swKeys_ptr->totfx), &(swKeys_ptr->totfy), &(swKeys_ptr->totfz), &(swKeys_ptr->totbsq),
2109                        &(swKeys_ptr->epsx), &(swKeys_ptr->epsy), &(swKeys_ptr->epsz), mask, bitmask, cdelt1, rsun_ref, rsun_obs))
2110                     {  
2111             		swKeys_ptr->totfx             = DRMS_MISSING_FLOAT;
2112                             swKeys_ptr->totfy             = DRMS_MISSING_FLOAT;
2113             		swKeys_ptr->totfz             = DRMS_MISSING_FLOAT;
2114             		swKeys_ptr->totbsq            = DRMS_MISSING_FLOAT;
2115                             swKeys_ptr->epsx              = DRMS_MISSING_FLOAT;
2116             		swKeys_ptr->epsy              = DRMS_MISSING_FLOAT;
2117                             swKeys_ptr->epsz              = DRMS_MISSING_FLOAT;
2118             
2119             	}
2120             
2121 xudong 1.6  	
2122 mbobra 1.14 	// Clean up the arrays
2123 xudong 1.1  	
2124 xudong 1.6  	drms_free_array(bitmaskArray);		// Dec 18 2012 Xudong
2125 xudong 1.1  	drms_free_array(maskArray);
2126 xudong 1.15 	drms_free_array(bxArray);
2127 xudong 1.1  	drms_free_array(byArray);
2128             	drms_free_array(bzArray);
2129 mbobra 1.29         drms_free_array(losArray);              // Mar 7
2130 mbobra 1.25         drms_free_array(bx_errArray);
2131 xudong 1.24 	drms_free_array(by_errArray);
2132             	drms_free_array(bz_errArray);
2133 xudong 1.1  	
2134 xudong 1.9  	free(bh); free(bt); free(jz); free(jz_smooth);
2135 xudong 1.1  	free(bpx); free(bpy); free(bpz);
2136 xudong 1.15 	free(derx); free(dery);
2137             	free(derx_bt); free(dery_bt);
2138             	free(derx_bz); free(dery_bz);
2139 xudong 1.1  	free(derx_bh); free(dery_bh);
2140 mbobra 1.14 	free(bt_err); free(bh_err);  free(jz_err);
2141 mbobra 1.25         free(jz_err_squared); free(jz_rms_err);
2142                     free(jz_err_squared_smooth);
2143 mbobra 1.30 
2144                     // free the arrays that are related to the r calculation     
2145 mbobra 1.25         free(rim);
2146                     free(p1p0);
2147                     free(p1n0);
2148                     free(p1p);
2149                     free(p1n);
2150                     free(p1);
2151                     free(pmap);
2152 mbobra 1.29         free(p1pad);
2153                     free(pmapn);
2154 mbobra 1.30 
2155                     // free the arrays that are related to the lorentz calculation
2156                     free(fx); free(fy); free(fz);
2157 xudong 1.1  }
2158             
2159 xudong 1.15 /*
2160 xudong 1.1   * Set space weather indices, no error checking for now
2161              *
2162              */
2163             
2164             void setSWIndex(DRMS_Record_t *outRec, struct swIndex *swKeys_ptr)
2165             {
2166 mbobra 1.25     drms_setkey_float(outRec, "USFLUX",  swKeys_ptr->mean_vf);
2167                 drms_setkey_float(outRec, "MEANGAM", swKeys_ptr->mean_gamma);
2168                 drms_setkey_float(outRec, "MEANGBT", swKeys_ptr->mean_derivative_btotal);
2169                 drms_setkey_float(outRec, "MEANGBH", swKeys_ptr->mean_derivative_bh);
2170                 drms_setkey_float(outRec, "MEANGBZ", swKeys_ptr->mean_derivative_bz);
2171                 drms_setkey_float(outRec, "MEANJZD", swKeys_ptr->mean_jz);
2172                 drms_setkey_float(outRec, "TOTUSJZ", swKeys_ptr->us_i);
2173                 drms_setkey_float(outRec, "MEANALP", swKeys_ptr->mean_alpha);
2174                 drms_setkey_float(outRec, "MEANJZH", swKeys_ptr->mean_ih);
2175                 drms_setkey_float(outRec, "TOTUSJH", swKeys_ptr->total_us_ih);
2176                 drms_setkey_float(outRec, "ABSNJZH", swKeys_ptr->total_abs_ih);
2177                 drms_setkey_float(outRec, "SAVNCPP", swKeys_ptr->totaljz);
2178                 drms_setkey_float(outRec, "MEANPOT", swKeys_ptr->meanpot);
2179                 drms_setkey_float(outRec, "TOTPOT",  swKeys_ptr->totpot);
2180                 drms_setkey_float(outRec, "MEANSHR", swKeys_ptr->meanshear_angle);
2181                 drms_setkey_float(outRec, "SHRGT45", swKeys_ptr->area_w_shear_gt_45);
2182 arta   1.22     drms_setkey_float(outRec, "CMASK",   swKeys_ptr->count_mask);
2183                 drms_setkey_float(outRec, "ERRBT",   swKeys_ptr->mean_derivative_btotal_err);
2184                 drms_setkey_float(outRec, "ERRVF",   swKeys_ptr->mean_vf_err);
2185                 drms_setkey_float(outRec, "ERRGAM",  swKeys_ptr->mean_gamma_err);
2186                 drms_setkey_float(outRec, "ERRBH",   swKeys_ptr->mean_derivative_bh_err);
2187                 drms_setkey_float(outRec, "ERRBZ",   swKeys_ptr->mean_derivative_bz_err);
2188                 drms_setkey_float(outRec, "ERRJZ",   swKeys_ptr->mean_jz_err);
2189                 drms_setkey_float(outRec, "ERRUSI",  swKeys_ptr->us_i_err);
2190                 drms_setkey_float(outRec, "ERRALP",  swKeys_ptr->mean_alpha_err);
2191                 drms_setkey_float(outRec, "ERRMIH",  swKeys_ptr->mean_ih_err);
2192                 drms_setkey_float(outRec, "ERRTUI",  swKeys_ptr->total_us_ih_err);
2193                 drms_setkey_float(outRec, "ERRTAI",  swKeys_ptr->total_abs_ih_err);
2194                 drms_setkey_float(outRec, "ERRJHT",  swKeys_ptr->totaljz_err);
2195                 drms_setkey_float(outRec, "ERRMPOT", swKeys_ptr->meanpot_err);
2196                 drms_setkey_float(outRec, "ERRTPOT", swKeys_ptr->totpot_err);
2197                 drms_setkey_float(outRec, "ERRMSHA", swKeys_ptr->meanshear_angle_err);
2198 xudong 1.23     drms_setkey_float(outRec, "R_VALUE", swKeys_ptr->Rparam);
2199 mbobra 1.30     drms_setkey_float(outRec, "TOTFX",   swKeys_ptr->totfx);
2200                 drms_setkey_float(outRec, "TOTFY",   swKeys_ptr->totfy);
2201                 drms_setkey_float(outRec, "TOTFZ",   swKeys_ptr->totfz);
2202                 drms_setkey_float(outRec, "TOTBSQ",  swKeys_ptr->totbsq);
2203                 drms_setkey_float(outRec, "EPSX",    swKeys_ptr->epsx);
2204                 drms_setkey_float(outRec, "EPSY",    swKeys_ptr->epsy);
2205                 drms_setkey_float(outRec, "EPSZ",    swKeys_ptr->epsz);
2206 xudong 1.1  };
2207             
2208 xudong 1.15 /*
2209 xudong 1.1   * Set all keywords, no error checking for now
2210              *
2211              */
2212             
2213 xudong 1.23 void setKeys(DRMS_Record_t *outRec, DRMS_Record_t *mharpRec, DRMS_Record_t *bharpRec, struct mapInfo *mInfo)
2214 xudong 1.1  {
2215 xudong 1.23     
2216             	copy_me_keys(bharpRec, outRec);
2217             	copy_patch_keys(mharpRec, outRec);      // Dec 30
2218             	copy_geo_keys(mharpRec, outRec);        // Dec 30
2219             	copy_ambig_keys(bharpRec, outRec);
2220                 
2221 xudong 1.15     int status = 0;
2222             	
2223 xudong 1.23 	// Change a few geometry keywords for CEA & cutout records
2224             	if (mInfo != NULL) {        // CEA
2225 xudong 1.15         
2226                     drms_setkey_float(outRec, "CRPIX1", mInfo->ncol/2. + 0.5);
2227             		drms_setkey_float(outRec, "CRPIX2", mInfo->nrow/2. + 0.5);
2228             		
2229             		drms_setkey_float(outRec, "CRVAL1", mInfo->xc);
2230             		drms_setkey_float(outRec, "CRVAL2", mInfo->yc);
2231             		drms_setkey_float(outRec, "CDELT1", mInfo->xscale);
2232             		drms_setkey_float(outRec, "CDELT2", mInfo->yscale);
2233             		drms_setkey_string(outRec, "CUNIT1", "degree");
2234             		drms_setkey_string(outRec, "CUNIT2", "degree");
2235             		
2236             		char key[64];
2237             		snprintf (key, 64, "CRLN-%s", wcsCode[(int) mInfo->proj]);
2238             		drms_setkey_string(outRec, "CTYPE1", key);
2239             		snprintf (key, 64, "CRLT-%s", wcsCode[(int) mInfo->proj]);
2240             		drms_setkey_string(outRec, "CTYPE2", key);
2241             		drms_setkey_float(outRec, "CROTA2", 0.0);
2242 xudong 1.23         
2243                     // Jan 2 2014 XS
2244                     int nSeg = ARRLENGTH(CEASegs);
2245                     for (int iSeg = 0; iSeg < nSeg; iSeg++) {
2246                         DRMS_Segment_t *outSeg = NULL;
2247                         outSeg = drms_segment_lookup(outRec, CEASegs[iSeg]);
2248                         if (!outSeg) continue;
2249                         // Set Bunit
2250                         char bunit_xxx[20];
2251                         sprintf(bunit_xxx, "BUNIT_%03d", iSeg);
2252                         //printf("%s, %s\n", bunit_xxx, CEABunits[iSeg]);
2253                         drms_setkey_string(outRec, bunit_xxx, CEABunits[iSeg]);
2254                     }
2255 xudong 1.15 		
2256 xudong 1.23 	} else {        // Cutout
2257 xudong 1.15         
2258 xudong 1.23         float disk_xc, disk_yc;
2259                     if (fullDisk) {
2260                         disk_xc = drms_getkey_float(bharpRec, "CRPIX1", &status);
2261                         disk_yc = drms_getkey_float(bharpRec, "CRPIX2", &status);
2262                     } else {
2263                         disk_xc = drms_getkey_float(mharpRec, "IMCRPIX1", &status);
2264                         disk_yc = drms_getkey_float(mharpRec, "IMCRPIX2", &status);
2265                     }
2266                     float x_ll = drms_getkey_float(mharpRec, "CRPIX1", &status);
2267                     float y_ll = drms_getkey_float(mharpRec, "CRPIX2", &status);
2268 xudong 1.15         // Defined as disk center's pixel address wrt lower-left of cutout
2269                     drms_setkey_float(outRec, "CRPIX1", disk_xc - x_ll + 1.);
2270             		drms_setkey_float(outRec, "CRPIX2", disk_yc - y_ll + 1.);
2271             		// Always 0.
2272             		drms_setkey_float(outRec, "CRVAL1", 0);
2273             		drms_setkey_float(outRec, "CRVAL2", 0);
2274 xudong 1.23         
2275                     // Jan 2 2014 XS
2276                     int nSeg = ARRLENGTH(CutSegs);
2277                     for (int iSeg = 0; iSeg < nSeg; iSeg++) {
2278                         DRMS_Segment_t *outSeg = NULL;
2279                         outSeg = drms_segment_lookup(outRec, CutSegs[iSeg]);
2280                         if (!outSeg) continue;
2281                         // Set Bunit
2282                         char bunit_xxx[20];
2283                         sprintf(bunit_xxx, "BUNIT_%03d", iSeg);
2284                         //printf("%s, %s\n", bunit_xxx, CutBunits[iSeg]);
2285                         drms_setkey_string(outRec, bunit_xxx, CutBunits[iSeg]);
2286                     }
2287                     
2288 xudong 1.15 		
2289             	}
2290 xudong 1.26 	
2291             	// Mar 19 XS
2292             	if (fullDisk) {
2293             		drms_setkey_int(outRec, "AMBPATCH", 0);
2294 xudong 1.27 		drms_setkey_int(outRec, "AMBWEAK", 2);
2295 xudong 1.26 	} else {
2296             		drms_setkey_int(outRec, "AMBPATCH", 1);
2297             	}
2298 xudong 1.23     
2299                 TIME val, trec, tnow, UNIX_epoch = -220924792.000; /* 1970.01.01_00:00:00_UTC */
2300                 tnow = (double)time(NULL);
2301                 tnow += UNIX_epoch;
2302             	
2303                 val = drms_getkey_time(bharpRec, "DATE", &status);
2304                 drms_setkey_time(outRec, "DATE_B", val);
2305                 drms_setkey_time(outRec, "DATE", tnow);
2306             	
2307                 // set cvs commit version into keyword HEADER
2308 mbobra 1.30     char *cvsinfo  = strdup("$Id: sharp.c,v 1.29 2014/06/02 19:47:10 mbobra Exp $");
2309 xudong 1.23     char *cvsinfo2 = sw_functions_version();
2310                 char cvsinfoall[2048];
2311                 strcat(cvsinfoall,cvsinfo);
2312                 strcat(cvsinfoall,"\n");
2313                 strcat(cvsinfoall,cvsinfo2);
2314                 status = drms_setkey_string(outRec, "CODEVER7", cvsinfoall);
2315 xudong 1.6  	
2316 xudong 1.1  };
2317             
2318             
2319             /* ############# Nearest neighbour interpolation ############### */
2320             
2321             float nnb (float *f, int nx, int ny, double x, double y)
2322             {
2323             	
2324             	if (x <= -0.5 || y <= -0.5 || x > nx - 0.5 || y > ny - 0.5)
2325             		return DRMS_MISSING_FLOAT;
2326             	int ilow = floor (x);
2327             	int jlow = floor (y);
2328             	int i = ((x - ilow) > 0.5) ? ilow + 1 : ilow;
2329             	int j = ((y - jlow) > 0.5) ? jlow + 1 : jlow;
2330             	return f[j * nx + i];
2331             	
2332             }
2333             
2334 mbobra 1.29 
2335 xudong 1.1  /* ################## Wrapper for Jesper's rebin code ################## */
2336             
2337             void frebin (float *image_in, float *image_out, int nx, int ny, int nbin, int gauss)
2338             {
2339             	
2340             	struct fresize_struct fresizes;
2341             	int nxout, nyout, xoff, yoff;
2342             	int nlead = nx;
2343             	
2344             	nxout = nx / nbin; nyout = ny / nbin;
2345             	if (gauss && nbin != 1)
2346             		init_fresize_gaussian(&fresizes, (nbin / 2), (nbin / 2 * 2), nbin);		// for nbin=3, sigma=1, half truncate width=2
2347             	else
2348             		init_fresize_bin(&fresizes, nbin);
2349             	xoff = nbin / 2 + nbin / 2;
2350             	yoff = nbin / 2 + nbin / 2;
2351             	fresize(&fresizes, image_in, image_out, nx, ny, nlead, nxout, nyout, nxout, xoff, yoff, DRMS_MISSING_FLOAT);
2352             	
2353             }

Karen Tian
Powered by
ViewCVS 0.9.4