The VisAO Camera
visaoimutils.h
Go to the documentation of this file.
1 /************************************************************
2  * visaoimutils.h
3  *
4  * Author: Jared R. Males (jrmales@email.arizona.edu)
5  *
6  * Declarations for various image utility functions.
7  *
8  * Developed as part of the Magellan Adaptive Optics system.
9  ************************************************************/
10 
11 /** \file visaoimutils.h
12  * \author Jared R. Males
13  * \brief Declarations for various image utility functions.
14  *
15  */
16 
17 #ifndef __VISAOIMUTILS_H__
18 #define __VISAOIMUTILS_H__
19 
20 #include <stdio.h>
21 #include <fitsio.h>
22 #include <sys/time.h>
23 #include <time.h>
24 
25 #include "libvisao.h"
26 #include "sharedim_stack.h"
27 
28 #include "statusboard.h"
29 
30 #define INT_2BYTES short
31 
32 typedef struct
33 {
34  std::string origin;
35  std::string telescop;
36  std::string instrume;
37 
38  timeval dateobs;
39  timeval timestamp_dma;
40 
41  long frameNo;
42 
53 
55 
57 
58 void init_visao_imheader(visao_imheader * imhead);
59 
60 
61 ///Writes the standard visao unique filename for a timeval to the buffer.
62 /** YYYYMODDHHMMSSUUUUUU where
63  * YYYY = the 4 digit year
64  * MO = the 2 digit month
65  * DD = the 2 digit day
66  * HH = the 2 digit hour
67  * MM = the 2 digit minut
68  * SS = the 2 digit second
69  * UUUUUU = the 6 digit microsecond
70  * \param buffer pointer to be written to, must be allocated to at least 21
71  * \param tv is the time to use to construct the filename.
72  * \retval 0 on success.
73  * \retval -1 on failure in gmtime_r.
74  */
75 int get_visao_filename(char * buffer, struct timeval *tv);
76 
77 ///Writes the standard visao unique filename for a timespec to the buffer.
78 /** YYYYMODDHHMMSSUUUUUU where
79  * YYYY = the 4 digit year
80  * MO = the 2 digit month
81  * DD = the 2 digit day
82  * HH = the 2 digit hour
83  * MM = the 2 digit minut
84  * SS = the 2 digit second
85  * UUUUUU = the 6 digit microsecond
86  * \param buffer pointer to be written to, must be allocated to at least 21
87  * \param ts is the time to use to construct the filename.
88  * \retval 0 on success.
89  * \retval -1 on failure in gmtime_r.
90  */
91 int get_visao_filename(char * buffer, struct timespec *ts);
92 
93 
94 template <class dataT> int visao_fits_create_img( fitsfile *fptr, int naxis, long *naxes, int *status);
95 template <class dataT> int visao_fits_write_subset(fitsfile *fptr, long *fpixel, long *lpixel,
96  dataT *array, int *status);
97 
98 ///Write a fits file.
99 template <class dataT> int write_visao_fits(const char *foutname, int nx, int ny, dataT *im, visao_imheader *head, bool aohead, bool visaohead);
100 
101 ///Write a fits file, using the sharedim structure.
102 template <class dataT> int write_visao_fits(const char * name_base, sharedim<dataT> *sim, visao_imheader * head, bool aohead=true, bool visaohead=true);
103 
104 int write_visao_raw(const char * name_base, sharedimS *sim, visao_imheader * head, bool aohead=true, bool visaohead=true);
105 
106 
107 int write_visao_fits_aosys_header(fitsfile * outfptr, visao_imheader * head);
108 int write_visao_fits_visao_header(fitsfile * outfptr, visao_imheader * head);
109 
110 
111 template <class dataT> int write_visao_fits(const char *foutname, int nx, int ny, dataT *im, visao_imheader * head, bool aohead, bool visaohead)
112 {
113  fitsfile *outfptr; /* FITS file pointers defined in fitsio.h */
114  int status = 0, i; /* status must always be initialized = 0 */
115  long naxes[2], lpix[2],fpix[2];//, imlong[1024*1024];
116  char hfld[81], tmp[30], datestr[80];
117  struct tm *dateobs_tm;
118  int timeref;
119 
120  //fits_get_system_date(&day, &month, &year, &status );
121  fits_get_system_time(datestr, &timeref, &status);
122 
123  //im = (INT_2BYTES *) malloc(nx*ny*2);
124 
125  /* Create the output file */
126  if ( !fits_create_file(&outfptr, foutname, &status) )
127  {
128  naxes[0] = nx;
129  naxes[1] = ny;
130 
131  if(!visao_fits_create_img<dataT>(outfptr,2, naxes, &status))
132  {
133  fpix[0] = 1;
134  fpix[1] = 1;
135  lpix[0] = nx;
136  lpix[1] = ny;
137  visao_fits_write_subset<dataT>(outfptr,fpix, lpix, im, &status);
138  if(!status)
139  {
140  for(i=0;i<81;i++) hfld[i] = ' ';
141  fits_update_key(outfptr, TSTRING, "DATE", (void *)datestr,"Date this file was written YYYY-mm-dd", &status);
142  if(head)
143  {
144  fits_update_key(outfptr, TSTRING, "ORIGIN", (void *)head->origin.c_str(),0, &status);
145  fits_update_key(outfptr, TSTRING, "TELESCOP", (void *)head->telescop.c_str(),0, &status);
146  fits_update_key(outfptr, TSTRING, "INSTRUME", (void *)head->instrume.c_str(),0, &status);
147 
148  dateobs_tm = gmtime(&head->dateobs.tv_sec);
149  strftime(tmp, 30, "%Y-%m-%dT%H:%M:%S", dateobs_tm);
150  sprintf(datestr, "%s.%06u", tmp, (unsigned) head->dateobs.tv_usec);
151  //yyyy-mm-ddTHH:MM:SS[.sss]
152  fits_update_key(outfptr, TSTRING, "DATE-OBS", (void *)datestr, "Date of obs. YYYY-mm-ddTHH:MM:SS", &status);
153 
154  if(head->frameNo > -1) fits_update_key(outfptr, TLONG, "FRAMENO", (void *)&head->frameNo, "Camera frame counter", &status);
155 
156  if(aohead) status = write_visao_fits_aosys_header(outfptr, head);
157  if(visaohead) status = write_visao_fits_visao_header(outfptr, head);
158  }
159  }
160  }
161 
162  fits_close_file(outfptr, &status);
163  }
164 
165  /* if error occured, print out error message */
166  if (status) fits_report_error(stderr, status);
167  return(status);
168 
169 }
170 
171 template <class dataT> int write_visao_fits(const char * name_base, sharedim<dataT> *sim, visao_imheader * head, bool aohead, bool visaohead)
172 {
173  std::string fname;
174  char fext[21];
175 
176  fname = name_base;
177  get_visao_filename(fext, &sim->frame_time);
178 
179  fname += fext;
180  fname += ".fits";
181 
182  if(head)
183  {
184  head->dateobs.tv_sec = sim->frame_time.tv_sec;
185  head->dateobs.tv_usec = sim->frame_time.tv_usec;
186  head->timestamp_dma.tv_sec = sim->frame_time_dma.tv_sec;
187  head->timestamp_dma.tv_usec = sim->frame_time_dma.tv_usec;
188 
189  head->frameNo = sim->frameNo;
190  }
191 
192  return write_visao_fits<dataT>(fname.c_str(), sim->nx, sim->ny, sim->imdata, head, aohead, visaohead);
193 
194 }
195 
196 int get_fits_im(unsigned char *im, long hduno, long *fpix, long *lpix, fitsfile **fptr, const char *fname);
197 
198 int get_fits_im(float *im, long hduno, long *fpix, long *lpix, fitsfile **fptr, const char *fname);
199 
200 int get_fits_im(double *im, long hduno, long *fpix, long *lpix, fitsfile **fptr, const char *fname);
201 
202 #endif //__VISAOIMUTILS_H__
203 
int write_visao_fits(const char *foutname, int nx, int ny, dataT *im, visao_imheader *head, bool aohead, bool visaohead)
Write a fits file.
Definition: visaoimutils.h:111
Declarations for the VisAO status information structures.
int get_visao_filename(char *buffer, struct timeval *tv)
Writes the standard visao unique filename for a timeval to the buffer.
VisAO software utilitites, declarations.
Declarations for the shared image circular buffer.