def save_mag_to_file(mag, filename, rhofile):
    """Save the values in rho- or mag-format.
    """
    if rhofile:
        # bring data in shape
        null = np.zeros(len(mag))
        if mag.shape[1] == 3:
            null = np.column_stack((null, null, null, null))
        result = np.column_stack((mag, null))

        # save datapoints
        with open(filename, 'w') as fid:
            fid.write('{0}\n'.format(mag.shape[0]))
        with open(filename, 'ab') as fid:
            np.savetxt(fid, np.array(result), fmt='%f')

    else:
        # bring data in shape
        with open('inv/rho00.mag', 'r') as fid:
            coor = np.loadtxt(fid, skiprows=1, usecols=[0, 1])
        # calculated back to log
        if mag.shape[1] == 3:
            logx = [math.log(d, 10) for d in mag[:, 0]]
            logy = [math.log(d, 10) for d in mag[:, 1]]
            logz = [math.log(d, 10) for d in mag[:, 2]]
            mag_log = np.column_stack((logx, logy, logz))
        else:
            mag_log = [math.log(d, 10) for d in mag]
        content = np.column_stack((coor[:, 0], coor[:, 1], mag_log))

        # save datapoints
        with open(filename, 'w') as fid:
            fid.write('{0}\n'.format(content.shape[0]))
        with open(filename, 'ab') as fid:
            np.savetxt(fid, np.array(content), fmt='%f')
def save_mag_to_file ( mag , filename , rhofile ) : if rhofile : null = np . zeros ( len ( mag ) ) if mag . shape [ 1 ] == 3 : null = np . column_stack ( ( null , null , null , null ) ) result = np . column_stack ( ( mag , null ) ) with open ( filename , 'w' ) as fid : fid . write ( '{0}\n' . format ( mag . shape [ 0 ] ) ) with open ( filename , 'ab' ) as fid : np . savetxt ( fid , np . array ( result ) , fmt = '%f' ) else : with open ( 'inv/rho00.mag' , 'r' ) as fid : coor = np . loadtxt ( fid , skiprows = 1 , usecols = [ 0 , 1 ] ) if mag . shape [ 1 ] == 3 : logx = [ math . log ( d , 10 ) for d in mag [ : , 0 ] ] logy = [ math . log ( d , 10 ) for d in mag [ : , 1 ] ] logz = [ math . log ( d , 10 ) for d in mag [ : , 2 ] ] mag_log = np . column_stack ( ( logx , logy , logz ) ) else : mag_log = [ math . log ( d , 10 ) for d in mag ] content = np . column_stack ( ( coor [ : , 0 ] , coor [ : , 1 ] , mag_log ) ) with open ( filename , 'w' ) as fid : fid . write ( '{0}\n' . format ( content . shape [ 0 ] ) ) with open ( filename , 'ab' ) as fid : np . savetxt ( fid , np . array ( content ) , fmt = '%f' )
Save the values in rho- or mag-format.