from scipy.io.idl import readsav
from netCDF4 import Dataset, num2date
from mpl_toolkits.basemap import Basemap
from astropy.io import fits
import numpy as np
import healpy as hp
import scipy as sp
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
import os


def mapping(nside, pixelnum, plotfile, data, nest_or_ring='ring'):
    # file pixels may be organized as ring or nest
    if nest_or_ring is 'ring':
        ra, dec = hp.pixelfunc.pix2ang(int(nside), pixelnum, nest=False, lonlat=True)
    if nest_or_ring is 'nest':
        ra, dec = hp.pixelfunc.pix2ang(int(nside), pixelnum, nest=True, lonlat=True)
    ra[np.where(ra > 180)] -= 360
    # plot of Galactic gas with coordinate projection
    min_ra = np.min(ra)
    max_ra = np.max(ra)
    mean_ra = np.mean(ra)
    min_dec = np.min(dec)
    max_dec = np.max(dec)
    mean_dec = np.mean(dec)

    fig = plt.figure()
    ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
    # llc and urc are x & y ranges, and are specific to a location.
    # The latitude and longitude settings are part of basemap.
    m = Basemap(projection='ortho', lon_0=mean_ra, lat_0=mean_dec, #llcrnrlon=min_ra, llcrnrlat=min_dec, urcrnrlon=max_ra, urcrnry=max_dec, resolution='h')
    x, y = m(ra, dec)
    # draw parallels and meridians. Labels are 1/0 as [Top,bottom,right,left]
    # m.drawparallels(np.arange(-90., 120., 10.), labels=[1, 0, 0, 0])
    # m.drawmeridians(np.arange(0., 420., 10.), labels=[0, 0, 0, 1])
    # creates a scatter plot of the selected data on a globe.
    m.scatter(x, y, 3, marker='o', linewidths=.1, c=data, cmap=plt.cm.coolwarm)
    m.colorbar()
    print "saved map to " + m
    plt.savefig(m)
