import os, ee, datetime, csv, time
import pandas as pd
import numpy as np
ee.Initialize()



DATA = ee.ImageCollection('MODIS/006/MOD13Q1').select("NDVI").filterDate('2002-01-01', '2003-03-31')

#Get the time series at these points.
points = [ee.Geometry.Point(-85.16516, 30.850000000000001)];
collection = ee.FeatureCollection(points);

#Extract the values by running reduceRegions over each image in the image collection.
def myfunction(i):
        return i.reduceRegions(collection, 'first')
values = DATA.map(myfunction).flatten()

#Turn the result into a feature collection and export it. 
taskParams = {
    'driveFolder' : '',
    'driveFileNamePrefix': 'NDVITEST',
    'fileFormat' : 'CSV'
};
  
MyTry=ee.batch.Export.table(values, 'NDVIfile', taskParams)
MyTry.start()
state = MyTry.status()['state']
while state in ['READY', 'RUNNING']:
  print state + '...'
  time.sleep(1)
  state = MyTry.status()['state']
print 'Done.', MyTry.status()





df = pd.read_csv(r'C:\Users\YARON\Google Drive\NDVITEST.csv')
print df


df = df.drop(['.geo'], axis=1)

NDVI = lambda x: x*0.0001
convert_TO_DATE = lambda x: x[0:4]+"/"+x[5:7]+"/"+x[8:10]
df['time']=df['system:index'].apply(convert_TO_DATE)



df = df.drop(['system:index'], axis=1)
df.to_csv(r'D:\NDVIa.csv', index=False, quoting=csv.QUOTE_NONE)
df0 = pd.read_csv(r'D:\NDVIa.csv', index_col='time', parse_dates=True)
print df0.index
df0.rename(columns={'first': 'NDVI'}, inplace=True)
df0.plot()

