Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

#!/usr/local/bin/python 

# encoding: utf-8 

""" 

fitstools 

=============== 

:Summary: 

    Some helpful tools to work with FITS files 

 

:Author: 

    David Young 

 

:Date Created: 

    March 19, 2013 

 

:dryx syntax: 

    - ``xxx`` = come back here and do some more work 

    - ``_someObject`` = a 'private' object that should only be changed for debugging 

 

:Notes: 

    - If you have any questions requiring this script please email me: d.r.young@qub.ac.uk 

""" 

################# GLOBAL IMPORTS #################### 

import sys 

import os 

 

###################################################### 

# MAIN LOOP - USED FOR DEBUGGING OR WHEN SCRIPTING   # 

###################################################### 

 

 

def main(): 

    """Used for debugging 

 

    Key Arguments: 

        - 

        - dbConn -- mysql database connection 

        - log -- logger 

 

    Return: 

        - None 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    ## THIRD PARTY ## 

    ## LOCAL APPLICATION ## 

    import pesstoMarshallPythonPath as pp 

    pp.set_python_path() 

    import pmCommonUtils as p 

    import dryxPython.commonutils as cu 

 

    ################ > SETUP ################## 

    ## SETUP DB CONNECTION AND A LOGGER 

    dbConn, log = p.settings() 

    ## START LOGGING ## 

    startTime = cu.get_now_sql_datetime() 

    log.info('--- STARTING TO RUN THE fitstools AT %s' % (startTime,)) 

 

    ################ > VARIABLE SETTINGS ###### 

    ################ >ACTION(S) ############### 

 

    dbConn.commit() 

    dbConn.close() 

    ## FINISH LOGGING ## 

    endTime = cu.get_now_sql_datetime() 

    runningTime = cu.calculate_time_difference(startTime, endTime) 

    log.info('-- FINISHED ATTEMPT TO RUN THE fitstools AT %s (RUNTIME: %s) --' % 

             (endTime, runningTime,)) 

    return 

 

################################################################### 

# CLASSES                                                         # 

################################################################### 

 

################################################################### 

# PUBLIC FUNCTIONS                                                # 

################################################################### 

## LAST MODIFIED : March 19, 2013 

## CREATED : March 19, 2013 

## AUTHOR : DRYX 

def convert_fits_header_to_dictionary( 

        log, 

        pathToFitsFile, 

        headerExtension=0): 

    """Convert a FITS file header keywords / values into a python dictionary 

 

    **Key Arguments:** 

        - ``log`` -- logger 

        - ``pathToFitsFile`` -- path to the fits file 

        - ``headerExtension`` -- the header extension to look in 

 

    **Return:** 

        - ``headerDictionary`` -- the header converted to a dictionary suitable for mysql ingest 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    ## THIRD PARTY ## 

    ## LOCAL APPLICATION ## 

 

    ################ > VARIABLE SETTINGS ###### 

    ################ >ACTION(S) ################ 

    log.info('starting the ``convert_fits_header_to_dictionary`` function') 

    if not isinstance(pathToFitsFile, str): 

        raise TypeError("pathToFitsFile argument needs to be a string") 

 

    try: 

        with open(pathToFitsFile): 

            pass 

            fileExists = True 

    except IOError: 

       fileExists = False 

       raise IOError("the fits file %s does not exist" % (pathToFitsFile,)) 

 

    headerDictionary = {} 

    try: 

        fitsHeader = get_fits_header(log, pathToFitsFile, headerExtension=headerExtension) 

        cardList = fitsHeader.ascardlist() 

        print cardList 

    except: 

        headerDictionary["corrupted"] = [True, "file is corrupted"] 

        return headerDictionary 

 

    for cl in cardList: 

        if len(cl.key) > 0: 

            # log.debug('cl: %s' % (cl,)) 

            headerDictionary[cl.key] = [cl.value, cl.comment] 

 

    log.info('finished the ``convert_fits_header_to_dictionary`` function') 

 

    return headerDictionary 

 

## LAST MODIFIED : March 19, 2013 

## CREATED : March 19, 2013 

## AUTHOR : DRYX 

def get_fits_header( 

        log, 

        pathToFits, 

        headerExtension=0): 

    """Return the HDU for the given fits file 

 

    **Key Arguments:** 

        - ``log`` -- logger 

        - ``pathToFits`` -- the absolute path to the fits file 

        - ``headerExtension`` -- the header extension to look in 

 

    **Return:** 

        - ``hdu`` -- the FITS header requested 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    ## THIRD PARTY ## 

    import pyfits as pf 

    ## LOCAL APPLICATION ## 

 

    ################ > VARIABLE SETTINGS ###### 

 

    ################ >ACTION(S) ################ 

    try: 

        hdu = pf.getheader(pathToFits, headerExtension) 

    # FIX THE CARDS (SHOULD BE NO LONGER THAN 80 CHARACTERS) 

    except: 

        try: 

            _correct_extended_fits_keywords(pathToFits) 

        except: 

            sys.exit('image ' + str( 

                pathToFits) + ' is corrupted, delete it and start again') 

            hdu = pf.getheader(pathToFits, headerExtension) 

 

    return hdu 

 

################################################################### 

# PRIVATE (HELPER) FUNCTIONS                                      # 

################################################################### 

## LAST MODIFIED : March 19, 2013 

## CREATED : March 19, 2013 

## AUTHOR : Stefano Valenti & DRYX 

 

 

def _correct_extended_fits_keywords(log, pathToFits): 

    """Values within the fits header should be no longer than 80 characters 

 

    **Key Arguments:** 

        - ``log`` -- logger 

        - ``pathToFits`` -- path the the FITS file 

 

    **Return:** 

        - None 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    import re 

    ## THIRD PARTY ## 

    from pyfits import open as popen 

    from numpy import asarray 

    ## LOCAL APPLICATION ## 

 

    ################ > VARIABLE SETTINGS ###### 

 

    ################ >ACTION(S) ################ 

    hdulist = popen(pathToFits) 

    a = hdulist[0]._verify('fix') 

    _header = hdulist[0].header 

    for i in range(len(a)): 

        if not a[i]: 

            a[i] = [''] 

    ww = asarray([i for i in range(len(a)) if (re.sub(' ', '', a[i][0]) != '')]) 

    if len(ww) > 0: 

        newheader = [] 

        headername = [] 

        for j in _header.items(): 

            headername.append(j[0]) 

            newheader.append(j[1]) 

        hdulist.close() 

        imm = popen(pathToFits, mode='update') 

        _header = imm[0].header 

        for i in ww: 

            if headername[i]: 

                try: 

                    _header.update(headername[i], newheader[i]) 

                except: 

                    _header.update(headername[i], 'xxxx') 

        imm.flush() 

        imm.close() 

 

    return 

 

 

## LAST MODIFIED : May 28, 2013 

## CREATED : May 28, 2013 

## AUTHOR : DRYX 

 

def remove_keyword_from_file( 

        log, 

        pathToFitsFile, 

        keyword): 

    """Remove a given keyword card from a fits file 

 

    **Return:** 

        - None 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    ## THIRD PARTY ## 

    import pyfits as pf 

    ## LOCAL APPLICATION ## 

 

    ################ >ACTION(S) ################ 

    hduList = pf.open(pathToFitsFile) 

    fitsData = hduList[0].data 

    primHeader = hduList[0].header 

    hduList.close() 

    os.remove(pathToFitsFile) 

    primCardList = primHeader.ascardlist() 

 

    thisCardList = [] 

    for key in primCardList.keys(): 

        if key != keyword: 

            thisCardList.append(primCardList[key]) 

 

    primHeader = pf.Header(cards=thisCardList) 

    primHdu = pf.PrimaryHDU(header=primHeader, data=fitsData) 

    thisHduList = pf.HDUList([primHdu,]) 

 

    thisHduList.writeto(pathToFitsFile, checksum=True) 

 

    log.debug('primHeader %s' % (primHeader,)) 

 

    return 

 

 

## LAST MODIFIED : May 28, 2013 

## CREATED : May 28, 2013 

## AUTHOR : DRYX 

 

def add_or_replace_keyword_to_fits( 

        log, 

        pathToFitsFile, 

        keywordName, 

        keywordValue, 

        keywordComment): 

    """Add or replace a given keyword card from a fits file 

 

    **Return:** 

        - None 

    """ 

    ################ > IMPORTS ################ 

    ## STANDARD LIB ## 

    ## THIRD PARTY ## 

    import pyfits as pf 

    ## LOCAL APPLICATION ## 

 

    ################ >ACTION(S) ################ 

    hduList = pf.open(pathToFitsFile) 

    fitsData = hduList[0].data 

    primHeader = hduList[0].header 

    hduList.close() 

    os.remove(pathToFitsFile) 

    primCardList = primHeader.ascardlist() 

 

    if keywordName in primCardList.keys(): 

        primCardList[keywordName].comment = keywordComment 

        primCardList[keywordName].value = keywordValue 

    else: 

        card = pf.Card(keywordName, keywordValue, keywordComment) 

        primCardList.append(card) 

 

    primHeader = pf.Header(cards=primCardList) 

    primHdu = pf.PrimaryHDU(header=primHeader, data=fitsData) 

    thisHduList = pf.HDUList([primHdu,]) 

 

    thisHduList.writeto(pathToFitsFile, checksum=True) 

 

    log.debug('primHeader %s' % (primHeader,)) 

 

    return 

 

 

if __name__ == '__main__': 

    main() 

 

 

################################################################### 

# TEMPLATE FUNCTIONS                                              # 

###################################################################