Package ivs :: Package catalogs :: Module trilegal
[hide private]
[frames] | no frames]

Source Code for Module ivs.catalogs.trilegal

  1  # -*- coding: utf-8 -*- 
  2  """ 
  3  Web query of TRILEGAL population synthesis code. 
  4  Author: Joris De Ridder 
  5  """ 
  6   
  7   
  8  from time import sleep, gmtime, strftime 
  9  from urllib import urlretrieve 
 10  from mechanize import Browser, urlopen 
 11   
12 -def trilegal(outputFileName, 13 longitude = 0, latitude = 0, 14 coordinateType = "galactic", 15 fieldArea = 1, 16 passband = 4, magnitudeLimit = 26, magnitudeResolution = 0.1, 17 IMFtype = 3, 18 includeBinaries = True, binaryFraction = 0.3, lowerBinaryMassRatio = 0.7, upperBinaryMassRatio = 1.0, 19 extinctionType = 2, extinctionValue = 0.0378, extinctionSigma = 0.0, 20 useThinDisc = False, 21 useThickDisc = False, 22 useBulge = True):
23 24 """ 25 Query the web interface of the TRILEGAL population synthesis code. 26 27 The TRILEGAL webform is automatically filled and submitted. The computations are done locally 28 on Girardi's computer. As soon as they are finished, the script retrieves the data file. 29 30 Example: 31 32 >>> trilegal("output.txt", longitude=3, latitude=14, coordinateType="galactic", fieldArea=1, magnitudeLimit=7, useThinDisc=True) 33 34 @param outputFileName: name of file wherein trilegal output will be saved 35 @type outputFileName: string 36 @param longitude: galactic longitude (degrees) or right ascension (hours) 37 @type longitude: integer 38 @param latitude: galactic latitude (degrees) or declination (degrees) 39 @type latitude: integer 40 @param coordinateType: either "galactic", or "equatorial" 41 @type coordinateType: string 42 @param fieldArea: total field area in square degrees (max. 10 deg^2) 43 @type fieldArea: float 44 @param passband: U,B,V,R,I,J,H,K = 1,2,3,4,5,6,7,8 for magnitude limit 45 @type passband: integer 46 @param magnitudeLimit: magnitude limit in specified passband 47 @type magnitudeLimit: float 48 @param magnitudeResolution: Distance modulus resolution of Galaxy components (mag) 49 @type magnitudeResolution: float 50 @param IMFtype: type of Initial Mass Function of single stars 51 1 = Salpeter with cutoff at 0.01, Msun, 52 2 = Chabrier exponential, 53 3 = Chabrier lognormal, 54 4 = Kroupa corrected for binaries, 55 5 = Kroupa not corrected for binaries 56 @type IMFtype: integer 57 @param includeBinaries: include binaries in the population (True or False) 58 @type includeBinaries: boolean 59 @param binaryFraction: fraction of binaries 60 @type binaryFraction: float 61 @param lowerBinaryMassRatio: lower limit of binary mass fraction 62 @type lowerBinaryMassRatio: float 63 @param upperBinaryMassRatio: upper limit of binary mass fraction 64 @type upperBinaryMassRatio: float 65 @param extinctionType: Type of extinction 66 0: no dust extinction 67 1: local calibration 68 2: calibration at infinity 69 @type extinctionType: integer 70 @param extinctionValue: for a local calibration this is dAv/dr in mag/pc 71 for the calibration at infinity this is Av at infinity in mag. 72 @type extinctionValue: float 73 @param extinctionSigma: 1-sigma extinction dispersion / total extinction (max. 0.3) 74 @type extinctionSigma: float 75 @param useThinDisk: if True use squared hyperbolic secant along z, if False don't include 76 @type useThinDisk: boolean 77 @param useThickDisk: if True use squared hyperbolic secant along z, if False don't include 78 @type useThickDisk: boolean 79 @param useBulge: if True use triaxal bulge, if False don't include 80 @type useBulge: boolean 81 @return None. A file is retrieved 82 """ 83 84 # The latest Trilegal web version 85 86 trilegalURL = "http://stev.oapd.inaf.it/cgi-bin/trilegal" 87 88 # Get the web form 89 90 timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) 91 print("{0}: Opening TRILEGAL web interface".format(timestamp)) 92 93 myBrowser = Browser() 94 try: 95 myBrowser.open(trilegalURL) 96 except: 97 timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) 98 print("{0}: Unable to open the TRILEGAL website".format(timestamp)) 99 return 100 101 myBrowser.select_form(nr=0) # there is only one form... 102 103 # Fill in the form. To know how the different fields in the form are 104 # named, we used 105 # >>> request = mechanize.Request(trilegalURL) 106 # >>> response = mechanize.urlopen(request) 107 # >>> forms = mechanize.ParseResponse(response, backwards_compat=False) 108 # >>> print forms[0] 109 110 timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) 111 print("{0}: Filling TRILEGAL web form".format(timestamp)) 112 113 if coordinateType == "galactic": 114 myBrowser["gal_coord"] = ["1"] 115 myBrowser["gc_l"] = str(longitude) 116 myBrowser["gc_b"] = str(latitude) 117 else: 118 myBrowser["gal_coord"] = ["2"] 119 myBrowser["eq_alpha"] = str(longitude) 120 myBrowser["eq_delta"] = str(latitude) 121 122 myBrowser["field"] = str(fieldArea) 123 myBrowser["icm_lim"] = str(passband) 124 myBrowser["mag_lim"] = str(magnitudeLimit) 125 myBrowser["mag_res"] = str(magnitudeResolution) 126 myBrowser["binary_kind"] = [str(int(includeBinaries))] 127 myBrowser["binary_frac"] = str(binaryFraction) 128 myBrowser["binary_mrinf"] = str(lowerBinaryMassRatio) 129 myBrowser["binary_mrsup"] = str(upperBinaryMassRatio) 130 131 myBrowser["extinction_kind"] = [str(extinctionType)] 132 if extinctionType == 1: 133 myBrowser["extinction_rho_sun"] = str(extinctionValue) 134 if extinctionType == 2: 135 myBrowser["extinction_infty"] = str(extinctionValue) 136 myBrowser["extinction_sigma"] = str(extinctionSigma) 137 138 if useThinDisc: 139 myBrowser["thindisk_kind"] = ["3"] 140 else: 141 myBrowser["thindisk_kind"] = ["0"] 142 143 if useThickDisc: 144 myBrowser["thickdisk_kind"] = ["3"] 145 else: 146 myBrowser["thickdisk_kind"] = ["0"] 147 148 if useBulge: 149 myBrowser["bulge_kind"] = ["2"] 150 else: 151 myBrowser["bulge_kind"] = ["0"] 152 153 # Submit the completed form 154 155 timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) 156 print("{0}: Submitting completed TRILEGAL web form".format(timestamp)) 157 158 nextWebPage = myBrowser.submit() 159 160 # Trilegal is now computing the result. Click on the special "Refresh" 161 # button until the webpage says that the computations are finished. 162 163 timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) 164 print ("{0}: Waiting until TRILEGAL computations are finished".format(timestamp)) 165 166 myBrowser.select_form(nr=0) # one form on the "be patient" web page 167 message = "Your job was finished" 168 while (message not in nextWebPage.read()): 169 nextWebPage = urlopen(myBrowser.click()) # click on the Refresh button 170 myBrowser.select_form(nr=0) # select form again, so that we can make a click again 171 sleep(5) # to not overload the website with refresh requests 172 173 # Get the url of the outputfile, and retrieve it. This can take a while. 174 175 timestamp = strftime("%a, %d %b %Y %H:%M:%S", gmtime()) 176 print("{0}: Retrieving TRILEGAL output file".format(timestamp)) 177 178 outputLink = myBrowser.links(url_regex="lgirardi/tmp/output").next() 179 urlretrieve(outputLink.absolute_url, outputFileName) 180 myBrowser.close() 181 182 # Save the parameters in an info file 183 184 parameterInfo = """ 185 coordinateType {0} 186 longitude {1} 187 latitude {2} 188 fieldArea {3} 189 passband {4} 190 magnitudeLimit {5} 191 magnitudeResolution {6} 192 IMFtype {7} 193 includeBinaries {8} 194 binaryFraction {9} 195 lowerBinaryMassRatio {10} 196 upperBinaryMassRatio {11} 197 extinctionType {12} 198 extinctionValue {13} 199 extinctionSigma {14} 200 """.format(coordinateType, 201 longitude, 202 latitude, 203 fieldArea, 204 passband, 205 magnitudeLimit, 206 magnitudeResolution, 207 IMFtype, 208 includeBinaries, 209 binaryFraction, 210 lowerBinaryMassRatio, 211 upperBinaryMassRatio, 212 extinctionType, 213 extinctionValue, 214 extinctionSigma) 215 216 infoFileName = "info_" + outputFileName 217 with open(infoFileName, 'w') as infoFile: 218 infoFile.write(parameterInfo)
219