/* * writerec_fits.c ~rick/src/jsoc * * Export a data record from SUMS/DRMS to a FITS file... * * Responsible: Rick Bogart RBogart@solar.stanford.edu * * Usage: * writerec_fits [-v] [msg= ] * * Parameters: (type default description) * series str GroundTestFrame data series name * start Time none start time for records to be selected * stop Time none stop time for records to be selected * out str none name of output directory * -m Flag - merge records into single file * -r Flag - leave camera records raw * * Bugs: * Incomplete * Input data type is assumed short * There is no checking for valid output data scaling, the keyvalues are * assumed present * The SUMS_handle returned is actually the DSDS dataset name * The SUMS handle is not used, rather a fixed raw image is read * * Revision history is at end of file. */ #include "mylibs.c" /* required .h inclusion */ #include /* optional module identifier */ char module_name[] = "writerec_fits"; char version_id[] = "0.8"; /* arguments list */ DefaultParams_t default_params[] = { {"series", "GroundTestFrame"}, {"start", ""}, {"stop", ""}, {"out", ""}, {NULL, NULL} /* required */ }; /* arguments list */ DefaultParams_t optional_params[] = { {"msg", "Hello, world!"}, {NULL, NULL} }; int process_run_parameters (char **series, TIME *start, TIME *stop, char **outdir, int *merge, int *rawcam, int *verbose, int *query) { extern CmdParams_t cmdparams; int status; char *start_str = cmdparams_get_str (&cmdparams, "start", &status); char *stop_str = cmdparams_get_str (&cmdparams, "stop", &status); char *out = cmdparams_get_str (&cmdparams, "out", &status); *verbose = cmdparams_exists (&cmdparams, "V"); *query = cmdparams_exists (&cmdparams, "q"); int needvals = (strlen (start_str) == 0); needvals += (strlen (stop_str) == 0); needvals += (strlen (out) == 0); if (*query || needvals) { *series = cmdparams_get_str (&cmdparams, "series", &status); printf ("%s version %s: Required parameters:\n", module_name, version_id); printf (" series : %s\n", *series); printf (" start : no default\n"); printf (" stop : no default\n"); printf (" out : no default\n"); return (1); } if (*verbose) printf ("%s version %s:\n\n", module_name, version_id); *series = cmdparams_get_str (&cmdparams, "series", &status); *start = cmdparams_get_time (&cmdparams, "start", &status); *stop = cmdparams_get_time (&cmdparams, "stop", &status); *merge = cmdparams_exists (&cmdparams, "m"); *rawcam = cmdparams_exists (&cmdparams, "r"); /* strip trailing / from directory name */ while (out[strlen (out) - 1] == '/') out[strlen (out) - 1] = '\0'; *outdir = out; return (0); } char *construct_descriptor (char *series, TIME start, TIME stop) { static char series_descriptor[2100]; char tests[2048], test[256]; /* "GroundTestFrame[? FrameSequenceNumber > 7500 ?]"; "GroundTestFrame[? filename=i_060130_225848.fit ?]"; "GroundTestFrame[? 1=1 ?]"; */ tests[0] = '\0'; sprintf (test, "ObservationTime >= %f", start); strcat (tests, test); sprintf (test, " and ObservationTime <= %f", stop); strcat (tests, test); sprintf (series_descriptor, "%s[? %s ?]", series, tests); return series_descriptor; } DR *dr_from_DRMS (DRMS_Record_t *record, int *status) { /* Creates a DR memory struct from the key info in the DRMS */ static DR *dr; float scale, offset; char keyname[64]; dr = dr_create (); dr_set_rank (dr, drms_getkey_int (record, "rank", status)); if (dr->rank) { int *dimensions = dr_malloc_length (dr->rank); int n = 0; while (n < dr->rank) { sprintf (keyname, "length%d", n); dimensions[n++] = drms_getkey_int (record, keyname, status); } dr->length = dimensions; } /* this is a hack; datatype does not appear to be in the series description */ dr_set_datatype (dr, DR_SHORT); dr_set_scaling (dr, 0, 1.0, 0.0); scale = drms_getkey_float (record, "bscale", status); offset = drms_getkey_float (record, "bzero", status); /* Need to check whether these actually exist */ dr_set_scaling (dr, 0, scale, offset); dr_create_data (dr); return dr; } char *sums_handle_from_DRMS (DRMS_Record_t *record, int *status) { static char *SUMS_name; SUMS_name = drms_getkey_string (record, "DSDS_Name", status); return (SUMS_name); } void stream_data_from_SUMS (DR *dr, char *sums_handle, char *DRMS_filename, /* part of hack */ int *status) { FILE *in = fopen ("/home/rick/data/hmi/raw/060130_225848.dat", "r"); int nbytes; FILE *wdf; char cmd[4096], wdname[512], filename[1024], tfile[] = "/tmp/wdn.writerec"; /* a ridiculous hack to use the DSDS handles and peq interactively! */ sprintf (cmd, "peq -wA %s | grep ^in_0_wd | awk '{print $3}' > %s", sums_handle, tfile); system (cmd); wdf = fopen (tfile, "r"); fscanf (wdf, "%s", wdname); sprintf (filename, "%s/%s", wdname, DRMS_filename); sprintf (cmd, "dtr in=%s out=/scr/rick/tmp/foo.raw ofmt=raw", filename); system (cmd); in = fopen ("/scr/rick/tmp/foo.raw", "r"); nbytes = dr_data_length (dr) * dr_numbytes (dr); if (fread (dr->data, nbytes, 1, in) == 0) { *status = READ_FAILURE; dr_free_data (dr); } else dr->data_avail = 1; fclose (in); } void strip_camera_image (DR *img) { int col, row, n; float *sv, *sv0; float *fv = (float *)dr_data (img); int rank = dr_rank (img); int cols = img->length[0]; int rows = img->length[1]; sv = sv0 = (float *)malloc (4096 * 4096 * sizeof (float)); /* skip first (and last) 20 rows */ fv += 20 * cols; n = 0; for (row = 20; row < 4180; row++) { /* skip 64 central rows */ if (row >= 2068 && row <= 2131) { fv += cols; continue; } /* skip first and last 51 columns of each row */ fv += 51; for (col = 51; col < 4149; col++) { /* skip 2 central columns of each row */ if (col == 2099 || col == 2100) { fv++; continue; } *sv++ = *fv++; } fv += 51; } dr_free_data (img); dr_set_rank (img, rank); img->length[0] = img->length[1] = 4096; dr_set_data (img, sv0); } int DoIt (void) { DRMS_Keyword_t *keyword; DRMS_RecordSet_t *record_set; DR *record; FILE *out; TIME start, stop; int *dimensions; int verbose, query, merge, want_raw_cam; int rank; int n, status; char *series_name, *set_descriptor, *output_directory, *sums_handle; char filename[256]; /* get the runtime calling parameters */ if (process_run_parameters (&series_name, &start, &stop, &output_directory, &merge, &want_raw_cam, &verbose, &query)) return 0; /* check that we are connected to a DRMS server */ if (drms_env == 0) { fprintf (stderr, "ERROR: must be connected to DRMS\n"); return (1); } /* contruct the series descriptor to build the record list */ set_descriptor = construct_descriptor (series_name, start, stop); /* query the DRMS for the corresponding record set */ record_set = drms_open_records (drms_env, set_descriptor, &status); if (!record_set) { fprintf (stderr, "ERROR: could not find set:\n %s\n", set_descriptor); return (1); } if (verbose) { char start_str[64], stop_str[64]; sprint_time (start_str, start, "UT", 0); sprint_time (stop_str, stop, "UT", 0); printf ("found %d records in time range %s - %s\n", record_set->n, start_str, stop_str); if (merge) printf ("Warning: merge option not yet implemented\n"); } /* process the records in the selected set individually */ for (n = 0; n < record_set->n; n++) { /* create DR memory struct from key info in DRMS */ record = dr_from_DRMS (record_set->records[n], &status); /* get SUMS data pointer from DRMS (this is currently a hack) */ sums_handle = sums_handle_from_DRMS (record_set->records[n], &status); /* get data segment from SUMS (this is currently a hack) */ stream_data_from_SUMS (record, sums_handle, drms_getkey_string (record_set->records[n], "FILENAME", &status), /* hack */ &status); /* now have a DR struct with its data segment */ /* work with it! */ dr_data_convert (record, DR_FLOAT); if (!want_raw_cam) strip_camera_image (record); /* open appropriately named output file */ sprintf (filename, "%s/%s", output_directory, drms_getkey_string (record_set->records[n], "FILENAME", &status)); if (!(out = fopen (filename, "w"))) { fprintf (stderr, " error: unable to open file %s for write\n", filename); dr_free (&record); continue; } /* write DR struct to output */ if (status = dr_write_fits (record, out)) { fprintf (stderr, " error: unable to write FITS file %s\n", filename); fprintf (stderr, " status = %d\n", status); continue; } printf ("%d\t", n); drms_keyword_printval (drms_keyword_lookup (record_set->records[n], "ObservationTime")); printf ("\t%s", sums_handle); printf ("\n"); fclose (out); dr_free_data (record); dr_free (&record); } drms_free_records (record_set); return (0); } /* * Revision History * * 06.02.08 Rick Bogart created this file */