Ejemplo n.º 1
0
def ferreModelLibrary(lib='GK',pca=True,sixd=True,unf=False,dr=None,
                      convertToBin=True,spider=False):
    """
    NAME:
       ferreModelLibrary
    PURPOSE:
       download a FERRE model library
    INPUT:
       lib= ('GK') spectral library
       dr= return the path corresponding to this data release
       pca= (True) if True, download the PCA compressed library
       sixd= (True) if True, download the 6D library (w/o vmicro)
       unf= (False) if True, download the binary library (otherwise ascii)
       convertToBin= (True) if True and not unf, convert the ascii file to binary using ferre's ascii2bin (which has to be on the path)
       spider= (False) if True, run wget as a spider (doesn't download)
    OUTPUT:
       (none; just downloads; also downloads the corresponding .hdr)
    HISTORY:
       2015-01-21 - Written - Bovy (IAS)
    """
    if dr is None: dr= path._default_dr()
    # First make sure the file doesn't exist
    filePath= path.ferreModelLibraryPath(lib=lib,dr=dr,pca=pca,
                                         sixd=sixd,unf=unf)
    if not os.path.exists(filePath):
        # Create the file path
        downloadPath= filePath.replace(os.path.join(path._APOGEE_DATA,
                                                    _dr_string(dr)),
                                       _base_url(dr=dr))
        _download_file(downloadPath,filePath,dr,verbose=True,spider=spider)
        if convertToBin and not spider:
            sys.stdout.write('\r'+"Converting ascii model library to binary (can take a few minutes) ...\r")
            sys.stdout.flush()
            try:
                p= subprocess.Popen(['ascii2bin'],stdout=subprocess.PIPE,
                                    stdin=subprocess.PIPE,
                                    stderr=subprocess.PIPE,
                                    cwd=os.path.dirname(filePath))
                p.stdin.write((os.path.basename(filePath)+'\n')\
                                  .encode('utf-8'))
                p.stdin.write(b'unf\n')
                stdout, stderr= p.communicate()
            except subprocess.CalledProcessError:
                print("Conversion of %s to binary failed ..." % (os.path.basename(filePath)))
            sys.stdout.write('\r'+_ERASESTR+'\r')
            sys.stdout.flush()
    # Also download the header
    if unf:
        headerFilePath= filePath.replace('.unf','.hdr')
    else:
        headerFilePath= filePath.replace('.dat','.hdr')
    if os.path.exists(headerFilePath): return None
    headerDownloadPath= headerFilePath.replace(os.path.join(path._APOGEE_DATA,
                                                            _dr_string(dr)),
                                               _base_url(dr=dr))
    _download_file(headerDownloadPath,headerFilePath,dr,verbose=True,
                   spider=spider)
    return None
Ejemplo n.º 2
0
def ferreModelLibrary(lib='GK',pca=True,sixd=True,unf=False,dr=None,
                      convertToBin=True,spider=False):
    """
    NAME:
       ferreModelLibrary
    PURPOSE:
       download a FERRE model library
    INPUT:
       lib= ('GK') spectral library
       dr= return the path corresponding to this data release
       pca= (True) if True, download the PCA compressed library
       sixd= (True) if True, download the 6D library (w/o vmicro)
       unf= (False) if True, download the binary library (otherwise ascii)
       convertToBin= (True) if True and not unf, convert the ascii file to binary using ferre's ascii2bin (which has to be on the path)
       spider= (False) if True, run wget as a spider (doesn't download)
    OUTPUT:
       (none; just downloads; also downloads the corresponding .hdr)
    HISTORY:
       2015-01-21 - Written - Bovy (IAS)
    """
    if dr is None: dr= path._default_dr()
    # First make sure the file doesn't exist
    filePath= path.ferreModelLibraryPath(lib=lib,dr=dr,pca=pca,
                                         sixd=sixd,unf=unf)
    if not os.path.exists(filePath):
        # Create the file path    
        downloadPath= filePath.replace(os.path.join(path._APOGEE_DATA,
                                                    _dr_string(dr)),
                                       _base_url(dr=dr))
        _download_file(downloadPath,filePath,dr,verbose=True,spider=spider)
        if convertToBin and not spider:
            sys.stdout.write('\r'+"Converting ascii model library to binary (can take a few minutes) ...\r")
            sys.stdout.flush()
            try:
                p= subprocess.Popen(['ascii2bin'],stdout=subprocess.PIPE,
                                    stdin=subprocess.PIPE,
                                    stderr=subprocess.PIPE,
                                    cwd=os.path.dirname(filePath))
                p.stdin.write((os.path.basename(filePath)+'\n')\
                                  .encode('utf-8'))
                p.stdin.write(b'unf\n')
                stdout, stderr= p.communicate()
            except subprocess.CalledProcessError:
                print("Conversion of %s to binary failed ..." % (os.path.basename(filePath)))
            sys.stdout.write('\r'+_ERASESTR+'\r')
            sys.stdout.flush()        
    # Also download the header
    if unf:
        headerFilePath= filePath.replace('.unf','.hdr')
    else:
        headerFilePath= filePath.replace('.dat','.hdr')
    if os.path.exists(headerFilePath): return None
    headerDownloadPath= headerFilePath.replace(os.path.join(path._APOGEE_DATA,
                                                            _dr_string(dr)),
                                               _base_url(dr=dr))
    _download_file(headerDownloadPath,headerFilePath,dr,verbose=True,
                   spider=spider)
    return None
Ejemplo n.º 3
0
def write_input_nml(dir,
                    pfile,
                    offile,
                    ffile=None,
                    erfile=None,
                    opfile=None,
                    ndim=6,
                    nov=0,
                    indv=None,
                    synthfile=None,
                    filterfile=None,
                    inter=3,
                    errbar=1,
                    indini=0,
                    init=0,
                    f_format=1,
                    f_access=1):
    """
    NAME:
       write_input_nml
    PURPOSE:
       write a FERRE input.nml file
    INPUT:
       dir - directory where the input.nml file will be written to
       pfile - name of the input parameter file
       offile - name of the output best-fitting model file
       ffile= name of the input parameter file
       erfile= name of the flux errors file
       opfile= name of the output parameter file
       ndim= (6) number of dimensions/parameters
       nov= (0) number of parameters to search (0=interpolation)
       synthfile= (default ferreModelLibraryPath in apogee.tools.path) file name of the model grid's header
       filterfile= (None) name of the file with weights to apply to the chi^2 (as weight x diff/err^2); typically an elemental abundance window
       inter= (3) order of the interpolation
       errbar= (1) method for calculating the error bars
       indini= (0) how to initialize the search
       init= (0) if 0, initialize the search at the parameters in the pfile
       f_format= (1) file format (0=ascii, 1=unf)
       f_access= (1) 0: load whole library, 1: use direct access (for small numbers of interpolations)
    OUTPUT:
       (none; just writes the file)
    HISTORY:
       2015-01-22 - Written - Bovy (IAS)
    """
    if indv is None:
        indv= range(1,nov+1)
    if synthfile is None:
        import apogee.tools.path as appath
        synthfile= appath.ferreModelLibraryPath(header=True)
    with open(os.path.join(dir,'input.nml'),'w') as outfile:
        outfile.write('&LISTA\n')
        outfile.write('NDIM = %i\n' % ndim)
        outfile.write('NOV = %i\n' % nov)
        indvstr= 'INDV ='
        for ii in indv:
            indvstr+= ' %i' % ii
        outfile.write(indvstr+'\n')
        outfile.write("SYNTHFILE(1) = '%s'\n" % synthfile)
        outfile.write("PFILE = '%s'\n" % pfile)
        if not ffile is None:
            outfile.write("FFILE = '%s'\n" % ffile)
        if not erfile is None:
            outfile.write("ERFILE = '%s'\n" % erfile)
        if not opfile is None:
            outfile.write("OPFILE = '%s'\n" % opfile)
        outfile.write("OFFILE = '%s'\n" % offile)
        if not filterfile is None:
            outfile.write("FILTERFILE = '%s'\n" % filterfile)           
        outfile.write('INTER = %i\n' % inter)
        outfile.write('ERRBAR = %i\n' % errbar)
        indinistr= 'INDINI ='
        if isinstance(indini,int):
            indini= numpy.zeros(nov,dtype='int')+indini
        for ii in range(nov):
            indinistr+= ' %i' % indini[ii]
        outfile.write(indinistr+'\n')
        if init == 0:
            outfile.write('NRUNS = 1\n')
        else:
            outfile.write('NRUNS = %i\n' % numpy.prod(indini))
        outfile.write('INIT = %i\n' % init)
        outfile.write('F_FORMAT = %i\n' % f_format)
        outfile.write('F_ACCESS = %i\n' % f_access)
        outfile.write('/\n')
    return None