From kimmo.lehtinen at nls.fi Tue Jun 13 04:42:13 2017 From: kimmo.lehtinen at nls.fi (Lehtinen Kimmo (MML)) Date: Tue, 13 Jun 2017 08:42:13 +0000 Subject: [AstroPy] Format of a FITS-LDAC file Message-ID: <1497343333424.50788@nls.fi> Hi I need to make a FITS-LDAC file for the SCAMP program. Normally this file is made with the Sextractor program but this time I am calculating the star positions outside Sextractor, within Python. I am using a FITS-LDAC file made with Sextractor as a template so that I do not need to make eveything from scratch. Let's open the FITS-LDAC template: >>> from astropy.io import fits >>> hdulist=fits.open('LDAC-template.fits') >>> hdulist.info() Filename: LDAC-template.fits No. Name Type Cards Dimensions Format 0 PRIMARY PrimaryHDU 4 () 1 LDAC_IMHEAD BinTableHDU 12 1R x 1C [17760A] 2 LDAC_OBJECTS BinTableHDU 57 1762R x 13C [1J, 1E, 1E, 1J, 1D, 1D, 1E, 1E, 1E, 1I, 1I, 1J, 1E] So there are three HDU objects. The hdulist[2] includes the positions and fluxes of the stars. I am able to replace hdulist[2] with a binary table HDU object made with the BinTableHDU.from_columns function. The problem is hdulist[1] which includes the astrometry of the image. The content of hdulist[1]: >>> hdulist[1].header XTENSION= 'BINTABLE' / THIS IS A BINARY TABLE (FROM THE LDACTOOLS) BITPIX = 8 / NAXIS = 2 / NAXIS1 = 17760 / BYTES PER ROW NAXIS2 = 1 / NUMBER OF ROWS PCOUNT = 0 / RANDOM PARAMETER COUNT GCOUNT = 1 / GROUP COUNT TFIELDS = 1 / FIELDS PER ROWS EXTNAME = 'LDAC_IMHEAD' / TABLE NAME TTYPE1 = 'Field Header Card' TFORM1 = '17760A ' TDIM1 = '(80, 222)' >>> hdulist[1].data FITS_rec([ (chararray([ 'SIMPLE = T /, 'BITPIX = -32 / Number of bits per data pixel', 'NAXIS = 2 / Number of data axes', 'NAXIS1 = 8736 /', ... many fits keywords... 'SEXDWSCL= 0.000000000000E+00 / Detection-weight re-scaling factor', 'END'], dtype='|S80'))], dtype=[('Field Header Card', 'S80', (222,))]) I need to replace hdulist[1].data with a header that I read from a separate FITS-file. The header includes astrometry made with www.astrometry.net. Lets open that file: >>> wcs = fits.open('wcs.fits') >>> wcs[0].header SIMPLE = T / Standard FITS file BITPIX = 8 / ASCII or bytes array NAXIS = 0 / Minimal header EXTEND = T / There may be FITS ext WCSAXES = 2 / no comment CTYPE1 = 'RA---TAN-SIP' / TAN (gnomic) projection + SIP distortions CTYPE2 = 'DEC--TAN-SIP' / TAN (gnomic) projection + SIP distortions EQUINOX = 2000.0 / Equatorial coordinates definition (yr) LONPOLE = 180.0 / no comment LATPOLE = 0.0 / no comment CRVAL1 = 304.317023912 / RA of reference point CRVAL2 = 45.3045531672 / DEC of reference point CRPIX1 = 4123.62069546 / X reference pixel CRPIX2 = 3118.81777021 / Y reference pixel CUNIT1 = 'deg ' / X pixel scale units CUNIT2 = 'deg ' / Y pixel scale units CD1_1 = 0.000187351090429 / Transformation matrix CD1_2 = 3.58456619667E-06 / no comment CD2_1 = 3.68116182084E-06 / no comment CD2_2 = -0.000187399195345 / no comment ... many fits keywords ... ** The problem is: how do I replace record array hdulist[1].data with another array made from wcs[0].header or how to remove/update keywords in hdulist[1].data ? ** I am confused about the format of hdulist[1].data, is it some kind of record array ? Thanks for your help, Kimmo Lehtinen -------------- next part -------------- An HTML attachment was scrubbed... URL: From evert.rol at gmail.com Tue Jun 13 06:08:21 2017 From: evert.rol at gmail.com (Evert Rol) Date: Tue, 13 Jun 2017 11:08:21 +0100 Subject: [AstroPy] Format of a FITS-LDAC file In-Reply-To: <1497343333424.50788@nls.fi> References: <1497343333424.50788@nls.fi> Message-ID: <7EF096FB-5641-4E4B-85F4-734B955544D8@gmail.com> Your assumption is correct: the data attribute of hdulist[1] is a (binary) table; a record array in numpy, essentially. It just contains the original header. I'm not familiar with LDAC (and it appears strange to me that one puts a header into the data section of a HDU when an actual header is available; perhaps someone else can clarify that), but judging from what you have listed, you'd probably want to add or replace the essential WCS keywords (from wcs[0].header) into that table. Given that the contents of hdulist[1].data is just an array of the full 80-chars header lines, you may need to perform some manual string searching to find the relevant keywords (str.startswith(...) is probably a convenient method to use), while simply stepping through the individual entries of the data array. I see that your wcs solution uses SIP. Note that, as far as I'm aware, SExtractor, and probably SCAMP and other related utilities, don't deal with these nth order corrections. Perhaps that has changed, but I've always had to update SExtractor astrometry output after the fact, to correct for SIP corrections (and similarly, I have avoided using SCAMP). Evert > Hi > > I need to make a FITS-LDAC file for the SCAMP program. > Normally this file is made with the Sextractor program but this time I am > calculating the star positions outside Sextractor, within Python. > > I am using a FITS-LDAC file made with Sextractor as a template so that I do not > need to make eveything from scratch. Let's open the FITS-LDAC template: > > >>> from astropy.io import fits > >>> hdulist=fits.open('LDAC-template.fits') > >>> hdulist.info() > Filename: LDAC-template.fits > No. Name Type Cards Dimensions Format > 0 PRIMARY PrimaryHDU 4 () > 1 LDAC_IMHEAD BinTableHDU 12 1R x 1C [17760A] > 2 LDAC_OBJECTS BinTableHDU 57 1762R x 13C [1J, 1E, 1E, 1J, 1D, 1D, 1E, 1E, 1E, 1I, 1I, 1J, 1E] > > So there are three HDU objects. > The hdulist[2] includes the positions and fluxes of the stars. I am able to replace > hdulist[2] with a binary table HDU object made with the BinTableHDU.from_columns > function. > > The problem is hdulist[1] which includes the astrometry of the image. > The content of hdulist[1]: > > >>> hdulist[1].header > XTENSION= 'BINTABLE' / THIS IS A BINARY TABLE (FROM THE LDACTOOLS) > BITPIX = 8 / > NAXIS = 2 / > NAXIS1 = 17760 / BYTES PER ROW > NAXIS2 = 1 / NUMBER OF ROWS > PCOUNT = 0 / RANDOM PARAMETER COUNT > GCOUNT = 1 / GROUP COUNT > TFIELDS = 1 / FIELDS PER ROWS > EXTNAME = 'LDAC_IMHEAD' / TABLE NAME > TTYPE1 = 'Field Header Card' > TFORM1 = '17760A ' > TDIM1 = '(80, 222)' > > > >>> hdulist[1].data > FITS_rec([ (chararray([ 'SIMPLE = T /, > 'BITPIX = -32 / Number of bits per data pixel', > 'NAXIS = 2 / Number of data axes', > 'NAXIS1 = 8736 /', > ... many fits keywords... > 'SEXDWSCL= 0.000000000000E+00 / Detection-weight re-scaling factor', > 'END'], > dtype='|S80'))], > dtype=[('Field Header Card', 'S80', (222,))]) > > > > I need to replace hdulist[1].data with a header that I read from a separate > FITS-file. The header includes astrometry made with www.astrometry.net. > Lets open that file: > > >>> wcs = fits.open('wcs.fits') > >>> wcs[0].header > SIMPLE = T / Standard FITS file > BITPIX = 8 / ASCII or bytes array > NAXIS = 0 / Minimal header > EXTEND = T / There may be FITS ext > WCSAXES = 2 / no comment > CTYPE1 = 'RA---TAN-SIP' / TAN (gnomic) projection + SIP distortions > CTYPE2 = 'DEC--TAN-SIP' / TAN (gnomic) projection + SIP distortions > EQUINOX = 2000.0 / Equatorial coordinates definition (yr) > LONPOLE = 180.0 / no comment > LATPOLE = 0.0 / no comment > CRVAL1 = 304.317023912 / RA of reference point > CRVAL2 = 45.3045531672 / DEC of reference point > CRPIX1 = 4123.62069546 / X reference pixel > CRPIX2 = 3118.81777021 / Y reference pixel > CUNIT1 = 'deg ' / X pixel scale units > CUNIT2 = 'deg ' / Y pixel scale units > CD1_1 = 0.000187351090429 / Transformation matrix > CD1_2 = 3.58456619667E-06 / no comment > CD2_1 = 3.68116182084E-06 / no comment > CD2_2 = -0.000187399195345 / no comment > ... many fits keywords ... > > > ** The problem is: how do I replace record array hdulist[1].data with another > array made from wcs[0].header or how to remove/update keywords in hdulist[1].data ? ** > > I am confused about the format of hdulist[1].data, is it some kind of record array ? > > Thanks for your help, Kimmo Lehtinen > > > _______________________________________________ > AstroPy mailing list > AstroPy at python.org > https://mail.python.org/mailman/listinfo/astropy From simon at sconseil.fr Tue Jun 13 06:33:16 2017 From: simon at sconseil.fr (Simon Conseil) Date: Tue, 13 Jun 2017 12:33:16 +0200 Subject: [AstroPy] Format of a FITS-LDAC file In-Reply-To: <1497343333424.50788@nls.fi> References: <1497343333424.50788@nls.fi> Message-ID: Hi, There is an open issue (https://github.com/astropy/astropy/issues/5193) which links to another conversation (https://groups.google.com/forum/#!topic/astropy-dev/dugAPaIrm6I) which apparently lead to a new Python package by Fred Moolekamp: https://github.com/fred3m/astromatic_wrapper http://astromatic-wrapper.readthedocs.io/en/latest/working_with.html#fits-ldac-files Simon Le 13/06/2017 ? 10:42, Lehtinen Kimmo (MML) a ?crit : > > Hi > > I need to make a FITS-LDAC file for the SCAMP program. > Normally this file is made with the Sextractor program but this time I am > calculating the star positions outside Sextractor, within Python. > > I am using a FITS-LDAC file made with Sextractor as a template so that > I do not > need to make eveything from scratch. Let's open the FITS-LDAC template: > > >>> from astropy.io import fits > >>> hdulist=fits.open('LDAC-template.fits') > >>> hdulist.info() > Filename: LDAC-template.fits > No. Name Type Cards Dimensions Format > 0 PRIMARY PrimaryHDU 4 () > 1 LDAC_IMHEAD BinTableHDU 12 1R x 1C [17760A] > 2 LDAC_OBJECTS BinTableHDU 57 1762R x 13C [1J, 1E, 1E, 1J, > 1D, 1D, 1E, 1E, 1E, 1I, 1I, 1J, 1E] > > So there are three HDU objects. > The hdulist[2] includes the positions and fluxes of the stars. I am > able to replace > hdulist[2] with a binary table HDU object made with the > BinTableHDU.from_columns > function. > > The problem is hdulist[1] which includes the astrometry of the image. > The content of hdulist[1]: > > >>> hdulist[1].header > XTENSION= 'BINTABLE' / THIS IS A BINARY TABLE (FROM THE > LDACTOOLS) > BITPIX = 8 / > NAXIS = 2 / > NAXIS1 = 17760 / BYTES PER ROW > NAXIS2 = 1 / NUMBER OF ROWS > PCOUNT = 0 / RANDOM PARAMETER COUNT > GCOUNT = 1 / GROUP COUNT > TFIELDS = 1 / FIELDS PER ROWS > EXTNAME = 'LDAC_IMHEAD' / TABLE NAME > TTYPE1 = 'Field Header Card' > TFORM1 = '17760A ' > TDIM1 = '(80, 222)' > > > >>> hdulist[1].data > FITS_rec([ (chararray([ 'SIMPLE = T /, > 'BITPIX = -32 / Number of bits per data pixel', > 'NAXIS = 2 / Number of data axes', > 'NAXIS1 = 8736 /', > ... many fits keywords... > 'SEXDWSCL= 0.000000000000E+00 / Detection-weight re-scaling > factor', > 'END'], > dtype='|S80'))], > dtype=[('Field Header Card', 'S80', (222,))]) > > > > I need to replace hdulist[1].data with a header that I read from a > separate > FITS-file. The header includes astrometry made with www.astrometry.net. > Lets open that file: > > >>> wcs = fits.open('wcs.fits') > >>> wcs[0].header > SIMPLE = T / Standard FITS file > BITPIX = 8 / ASCII or bytes array > NAXIS = 0 / Minimal header > EXTEND = T / There may be FITS ext > WCSAXES = 2 / no comment > CTYPE1 = 'RA---TAN-SIP' / TAN (gnomic) projection + SIP distortions > CTYPE2 = 'DEC--TAN-SIP' / TAN (gnomic) projection + SIP distortions > EQUINOX = 2000.0 / Equatorial coordinates definition (yr) > LONPOLE = 180.0 / no comment > LATPOLE = 0.0 / no comment > CRVAL1 = 304.317023912 / RA of reference point > CRVAL2 = 45.3045531672 / DEC of reference point > CRPIX1 = 4123.62069546 / X reference pixel > CRPIX2 = 3118.81777021 / Y reference pixel > CUNIT1 = 'deg ' / X pixel scale units > CUNIT2 = 'deg ' / Y pixel scale units > CD1_1 = 0.000187351090429 / Transformation matrix > CD1_2 = 3.58456619667E-06 / no comment > CD2_1 = 3.68116182084E-06 / no comment > CD2_2 = -0.000187399195345 / no comment > ... many fits keywords ... > > > ** The problem is: how do I replace record array hdulist[1].data with > another > array made from wcs[0].header or how to remove/update keywords in > hdulist[1].data ? ** > > I am confused about the format of hdulist[1].data, is it some kind of > record array ? > > Thanks for your help, Kimmo Lehtinen > > > > > _______________________________________________ > AstroPy mailing list > AstroPy at python.org > https://mail.python.org/mailman/listinfo/astropy -------------- next part -------------- An HTML attachment was scrubbed... URL: From shupe at ipac.caltech.edu Tue Jun 13 13:47:45 2017 From: shupe at ipac.caltech.edu (David Shupe) Date: Tue, 13 Jun 2017 10:47:45 -0700 Subject: [AstroPy] Format of a FITS-LDAC file In-Reply-To: <1497343333424.50788@nls.fi> References: <1497343333424.50788@nls.fi> Message-ID: <04547BE7-355E-4AF4-B516-619602EE3FFB@ipac.caltech.edu> A solution I?ve used is to provide an ascii header file to SCAMP. The astrometry solution represented by the WCS keywords in the ascii header will override that in the LDAC_IMHEAD extension of the FITS-LDAC catalog. As others pointed out, SCAMP does not recognize the SIP keywords. There are some modules in https://github.com/stargaser/sip_tpv/ that can be used to convert your astropy header object with SIP keywords to the TPV format. Then you can use the .totextfile method of your header to write the ascii header for SCAMP to use. David > On Jun 13, 2017, at 1:42 AM, Lehtinen Kimmo (MML) wrote: > > Hi > > I need to make a FITS-LDAC file for the SCAMP program. > Normally this file is made with the Sextractor program but this time I am > calculating the star positions outside Sextractor, within Python. > > I am using a FITS-LDAC file made with Sextractor as a template so that I do not > need to make eveything from scratch. Let's open the FITS-LDAC template: > > >>> from astropy.io import fits > >>> hdulist=fits.open('LDAC-template.fits') > >>> hdulist.info () > Filename: LDAC-template.fits > No. Name Type Cards Dimensions Format > 0 PRIMARY PrimaryHDU 4 () > 1 LDAC_IMHEAD BinTableHDU 12 1R x 1C [17760A] > 2 LDAC_OBJECTS BinTableHDU 57 1762R x 13C [1J, 1E, 1E, 1J, 1D, 1D, 1E, 1E, 1E, 1I, 1I, 1J, 1E] > > So there are three HDU objects. > The hdulist[2] includes the positions and fluxes of the stars. I am able to replace > hdulist[2] with a binary table HDU object made with the BinTableHDU.from_columns > function. > > The problem is hdulist[1] which includes the astrometry of the image. > The content of hdulist[1]: > > >>> hdulist[1].header > XTENSION= 'BINTABLE' / THIS IS A BINARY TABLE (FROM THE LDACTOOLS) > BITPIX = 8 / > NAXIS = 2 / > NAXIS1 = 17760 / BYTES PER ROW > NAXIS2 = 1 / NUMBER OF ROWS > PCOUNT = 0 / RANDOM PARAMETER COUNT > GCOUNT = 1 / GROUP COUNT > TFIELDS = 1 / FIELDS PER ROWS > EXTNAME = 'LDAC_IMHEAD' / TABLE NAME > TTYPE1 = 'Field Header Card' > TFORM1 = '17760A ' > TDIM1 = '(80, 222)' > > > >>> hdulist[1].data > FITS_rec([ (chararray([ 'SIMPLE = T /, > 'BITPIX = -32 / Number of bits per data pixel', > 'NAXIS = 2 / Number of data axes', > 'NAXIS1 = 8736 /', > ... many fits keywords... > 'SEXDWSCL= 0.000000000000E+00 / Detection-weight re-scaling factor', > 'END'], > dtype='|S80'))], > dtype=[('Field Header Card', 'S80', (222,))]) > > > > I need to replace hdulist[1].data with a header that I read from a separate > FITS-file. The header includes astrometry made with www.astrometry.net . > Lets open that file: > > >>> wcs = fits.open('wcs.fits') > >>> wcs[0].header > SIMPLE = T / Standard FITS file > BITPIX = 8 / ASCII or bytes array > NAXIS = 0 / Minimal header > EXTEND = T / There may be FITS ext > WCSAXES = 2 / no comment > CTYPE1 = 'RA---TAN-SIP' / TAN (gnomic) projection + SIP distortions > CTYPE2 = 'DEC--TAN-SIP' / TAN (gnomic) projection + SIP distortions > EQUINOX = 2000.0 / Equatorial coordinates definition (yr) > LONPOLE = 180.0 / no comment > LATPOLE = 0.0 / no comment > CRVAL1 = 304.317023912 / RA of reference point > CRVAL2 = 45.3045531672 / DEC of reference point > CRPIX1 = 4123.62069546 / X reference pixel > CRPIX2 = 3118.81777021 / Y reference pixel > CUNIT1 = 'deg ' / X pixel scale units > CUNIT2 = 'deg ' / Y pixel scale units > CD1_1 = 0.000187351090429 / Transformation matrix > CD1_2 = 3.58456619667E-06 / no comment > CD2_1 = 3.68116182084E-06 / no comment > CD2_2 = -0.000187399195345 / no comment > ... many fits keywords ... > > > ** The problem is: how do I replace record array hdulist[1].data with another > array made from wcs[0].header or how to remove/update keywords in hdulist[1].data ? ** > > I am confused about the format of hdulist[1].data, is it some kind of record array ? > > Thanks for your help, Kimmo Lehtinen > > > _______________________________________________ > AstroPy mailing list > AstroPy at python.org > https://mail.python.org/mailman/listinfo/astropy -------------- next part -------------- An HTML attachment was scrubbed... URL: From rgg282 at nyu.edu Tue Jun 20 06:00:25 2017 From: rgg282 at nyu.edu (Ruth Gebremedhin) Date: Tue, 20 Jun 2017 14:00:25 +0400 Subject: [AstroPy] help Message-ID: Hello, My name is Ruth and I am a student at NYU Abu Dhabi doing astrophysical research for summer. Sorry to bother you but I have a question on how fits saves columns that have list objects of different lengths in each cell. There is no problem when I make an astropy table containing a column that stores the list [ [1,2] , [3,4] ] in its first cell and the list [5,6,7] in its second cell. But when I output a fits table out of this astropy table, fits just displays the first element of each cell. If I expand each cell the table appears distorted. In addtion, when I read in the table I wrote out, it appears distorted. I have attached screenshots of my code and the resulting astropy and fits tables. I would appreciate any help. Best wishes, Ruth -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: wholecode.PNG Type: image/png Size: 26758 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: wholecode2.PNG Type: image/png Size: 19652 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: codesinp2.PNG Type: image/png Size: 4137 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: codesnip3.PNG Type: image/png Size: 3735 bytes Desc: not available URL: From rgg282 at nyu.edu Thu Jun 22 06:23:46 2017 From: rgg282 at nyu.edu (Ruth Gebremedhin) Date: Thu, 22 Jun 2017 14:23:46 +0400 Subject: [AstroPy] help In-Reply-To: References: Message-ID: Hello, Sorry to bother you again but I was just wondering if you received my email and if you are able to help me. Sincerely, Ruth On Tue, Jun 20, 2017 at 2:00 PM, Ruth Gebremedhin wrote: > Hello, > > My name is Ruth and I am a student at NYU Abu Dhabi doing astrophysical > research for summer. > > Sorry to bother you but I have a question on how fits saves columns that > have list objects of different lengths in each cell. There is no problem > when I make an astropy table containing a column that stores the list [ > [1,2] , [3,4] ] in its first cell and the list [5,6,7] in its second cell. > But when I output a fits table out of this astropy table, fits just > displays the first element of each cell. If I expand each cell the table > appears distorted. In addtion, when I read in the table I wrote out, it > appears distorted. > > I have attached screenshots of my code and the resulting astropy and fits > tables. I would appreciate any help. > > Best wishes, > > Ruth > -------------- next part -------------- An HTML attachment was scrubbed... URL: From gabrielperren at gmail.com Thu Jun 22 08:17:24 2017 From: gabrielperren at gmail.com (Gabriel Perren) Date: Thu, 22 Jun 2017 12:17:24 +0000 Subject: [AstroPy] help In-Reply-To: References: Message-ID: Hello Ruth, I'd say you should try posting your question at https://stackoverflow.com using the tag 'astropy'. Give as many details as possible, even a working block of code if possible. Regards, Gabriel El jue., 22 de jun. de 2017 a la(s) 07:24, Ruth Gebremedhin escribi?: > Hello, > > Sorry to bother you again but I was just wondering if you received my > email and if you are able to help me. > > Sincerely, > > Ruth > > On Tue, Jun 20, 2017 at 2:00 PM, Ruth Gebremedhin wrote: > >> Hello, >> >> My name is Ruth and I am a student at NYU Abu Dhabi doing astrophysical >> research for summer. >> >> Sorry to bother you but I have a question on how fits saves columns that >> have list objects of different lengths in each cell. There is no problem >> when I make an astropy table containing a column that stores the list [ >> [1,2] , [3,4] ] in its first cell and the list [5,6,7] in its second cell. >> But when I output a fits table out of this astropy table, fits just >> displays the first element of each cell. If I expand each cell the table >> appears distorted. In addtion, when I read in the table I wrote out, it >> appears distorted. >> >> I have attached screenshots of my code and the resulting astropy and fits >> tables. I would appreciate any help. >> >> Best wishes, >> >> Ruth >> > > _______________________________________________ > AstroPy mailing list > AstroPy at python.org > https://mail.python.org/mailman/listinfo/astropy > -------------- next part -------------- An HTML attachment was scrubbed... URL: From russellosterlund at gmail.com Mon Jun 26 18:37:33 2017 From: russellosterlund at gmail.com (Russell Osterlund) Date: Mon, 26 Jun 2017 18:37:33 -0400 Subject: [AstroPy] Design Pattern Message-ID: Hi. When I execute the following: print( 'v:', np.sqrt( ( G * u.M_sun.to( u.kilogram ) ) / u.au.to( u.meter ) ) / 1000 ) this is the output: v: 29.788833564362875 m(3/2) / (kg(1/2) s) But when I change the code to this: print( 'v:', np.sqrt( ( ( G * u.M_sun ) / u.au ) ).to( u.km / u.second ) ) I see this: v: 29.788833564362875 km / s The differing output suggests that the correct design pattern is to work with the constants as-is and only convert using the "to" method when required, e.g., np.sqrt(), or displaying output. Is this correct? Thanks in advance. Russ Osterlund -------------- next part -------------- An HTML attachment was scrubbed... URL: From derek at astro.physik.uni-goettingen.de Mon Jun 26 19:39:46 2017 From: derek at astro.physik.uni-goettingen.de (Derek Homeier) Date: Tue, 27 Jun 2017 01:39:46 +0200 Subject: [AstroPy] Design Pattern In-Reply-To: References: Message-ID: On 27 Jun 2017, at 12:37 am, Russell Osterlund wrote: > > When I execute the following: > > print( 'v:', np.sqrt( ( G * u.M_sun.to( u.kilogram ) ) / u.au.to( u.meter ) ) / 1000 ) > > this is the output: > > v: 29.788833564362875 m(3/2) / (kg(1/2) s) > > But when I change the code to this: > > print( 'v:', np.sqrt( ( ( G * u.M_sun ) / u.au ) ).to( u.km / u.second ) ) > > I see this: > > v: 29.788833564362875 km / s > > The differing output suggests that the correct design pattern is to work with the constants as-is and only convert using the "to" method when required, e.g., np.sqrt(), or displaying output. Is this correct? > The difference is that the constant G is a Quantity as opposed to the units M_sun, au. When using constants and Quantities throughout, both ways should work: import astropy.constants as apc import astropy.units as apu print('v={0:}'.format(np.sqrt(apc.G.to('km3/(kg s2)')*apc.M_sun.to('kg')/(1*apu.au).to('km')))) v=29.788833564362875 km / s print(?v = {0:.3f}'.format(np.sqrt(apc.G*apc.M_sun/(1*apu.au)).to('km/s'))) v = 29.789 km / s May not seem too intuitive, and I can?t actually tell you the reasoning why it is behaving this way, but as the unit is just an attribute of a Quantity, there probably is one? HTH, Derek From russellosterlund at gmail.com Tue Jun 27 09:54:53 2017 From: russellosterlund at gmail.com (Russell Osterlund) Date: Tue, 27 Jun 2017 09:54:53 -0400 Subject: [AstroPy] Design Pattern In-Reply-To: References: Message-ID: Thank you! Your 1st example clarifies things - In order to make my first attempt work correctly, I need to call the "to" method with the correct units on the constant G! All the best. Russ Osterlund On Mon, Jun 26, 2017 at 7:39 PM, Derek Homeier < derek at astro.physik.uni-goettingen.de> wrote: > On 27 Jun 2017, at 12:37 am, Russell Osterlund > wrote: > > > > When I execute the following: > > > > print( 'v:', np.sqrt( ( G * u.M_sun.to( u.kilogram ) ) / u.au.to( > u.meter ) ) / 1000 ) > > > > this is the output: > > > > v: 29.788833564362875 m(3/2) / (kg(1/2) s) > > > > But when I change the code to this: > > > > print( 'v:', np.sqrt( ( ( G * u.M_sun ) / u.au ) ).to( u.km / u.second > ) ) > > > > I see this: > > > > v: 29.788833564362875 km / s > > > > The differing output suggests that the correct design pattern is to work > with the constants as-is and only convert using the "to" method when > required, e.g., np.sqrt(), or displaying output. Is this correct? > > > The difference is that the constant G is a Quantity as opposed to the > units M_sun, au. > When using constants and Quantities throughout, both ways should work: > > import astropy.constants as apc > import astropy.units as apu > > print('v={0:}'.format(np.sqrt(apc.G.to('km3/(kg s2)')*apc.M_sun.to > ('kg')/(1*apu.au).to('km')))) > v=29.788833564362875 km / s > > print(?v = {0:.3f}'.format(np.sqrt(apc.G*apc.M_sun/(1*apu.au)).to('km/ > s'))) > v = 29.789 km / s > > May not seem too intuitive, and I can?t actually tell you the reasoning > why it is behaving > this way, but as the unit is just an attribute of a Quantity, there > probably is one? > > HTH, > Derek > > _______________________________________________ > AstroPy mailing list > AstroPy at python.org > https://mail.python.org/mailman/listinfo/astropy > -------------- next part -------------- An HTML attachment was scrubbed... URL: