<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Soil Moisture Active Passive (SMAP) Level 4 Data demo
In this demo we are downloading data using Planet OS Package-API which let's us use bigger amount of data with less time than raster API. 
We are showing Portugal and Spain droughts what might have been a reason for over 600 wildfires that happend during summer and autumn 2017. Note that dates in the demo has changed and therefore description might not be relevant
Step1:  <font color='red'>Please put your datahub API key into a file called APIKEY and place it to the notebook folder or assign your API key directly to the variable API_key!</font>
Step2:  Here we define the area we are intrested, time range from where we want to have the data, dataset key to use and variable name.
Step3:  This one here is generating working directory, we need it to know where we are going to save data. No worries, we will delete file after using it!
Step4:  Now we ara making a function for making images
Step5:  Here we are downloading data by using Package-API. If you are intrested how data is downloaded, find the file named package_api.py from notebook folder.
Step6:  Now we have data and we are reading it in using xarray
Step7:  Here we are making image by using function defined above. 
In this image we can see how dry Iberian peninsula (Portugal and Spain) was during wildfires in October. On 15th October strong winds from the  Hurricane Ophelia quickly spread flames along the Iberian coast. We can see that on that day Iberian peninsula soil moisture was comparable with African deserts.
Step8:  So let's see Portugal and Spain littlebit closer. For that we need to define the area and we will slice data from this area.
Step9:  Let's also import some data from later as well.
Step10:  Now we are making two images from the same area - Portugal and Spain. 
On the left image we can see soil moisture values on 15th October and on the right image we can see soil moisture on 21th October.
We can see that the land was getting littlebit wetter with a few days. It even helped firefighters to get wildfires under control.
Step11:  Finally, let's delete files we downloaded
<ASSISTANT_TASK:>
Python Code:
import time
import os
from package_api import download_data
import xarray as xr
from netCDF4 import Dataset, num2date
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import datetime
import warnings
warnings.filterwarnings("ignore",category=matplotlib.cbook.mplDeprecation)
Explanation: Soil Moisture Active Passive (SMAP) Level 4 Data demo
In this demo we are downloading data using Planet OS Package-API which let's us use bigger amount of data with less time than raster API. 
We are showing Portugal and Spain droughts what might have been a reason for over 600 wildfires that happend during summer and autumn 2017. Note that dates in the demo has changed and therefore description might not be relevant
End of explanation
API_key = open('APIKEY').read().strip()
Explanation: <font color='red'>Please put your datahub API key into a file called APIKEY and place it to the notebook folder or assign your API key directly to the variable API_key!</font>
End of explanation
def get_start_end(days):
    date = datetime.datetime.now() - datetime.timedelta(days=days)
    time_start = date.strftime('%Y-%m-%d') + 'T16:00:00'
    time_end = date.strftime('%Y-%m-%d') + 'T21:00:00'
    return time_start,time_end
latitude_south = 15.9; latitude_north = 69.5
longitude_west = -17.6; longitude_east = 38.6
area = 'europe'
days = 6
time_start,time_end = get_start_end(days)
dataset_key = 'nasa_smap_spl4smau'
variable = 'Analysis_Data__sm_surface_analysis'
Explanation: Here we define the area we are intrested, time range from where we want to have the data, dataset key to use and variable name.
End of explanation
folder = os.path.realpath('.') + '/'
Explanation: This one here is generating working directory, we need it to know where we are going to save data. No worries, we will delete file after using it!
End of explanation
def make_image(lon,lat,data,date,latitude_north, latitude_south,longitude_west, longitude_east,unit,**kwargs):
    m = Basemap(projection='merc', lat_0 = 55, lon_0 = -4,
         resolution = 'i', area_thresh = 0.05,
         llcrnrlon=longitude_west, llcrnrlat=latitude_south,
         urcrnrlon=longitude_east, urcrnrlat=latitude_north)
    lons,lats = np.meshgrid(lon,lat)
    lonmap,latmap = m(lons,lats)
    
    if len(kwargs) > 0:
        fig=plt.figure(figsize=(10,8))
        plt.subplot(221)
        m.drawcoastlines()
        m.drawcountries()
        c = m.pcolormesh(lonmap,latmap,data,vmin = 0.01,vmax = 0.35)
        plt.title(date)
        
        plt.subplot(222)
        m.drawcoastlines()
        m.drawcountries()
        plt.title(kwargs['date_later'])
        m.pcolormesh(lonmap,latmap,kwargs['data_later'],vmin = 0.01,vmax = 0.35)
        
        
        
    else:
        fig=plt.figure(figsize=(9,7))
        m.drawcoastlines()
        m.drawcountries()
        c = m.pcolormesh(lonmap,latmap,data,vmin = 0.01,vmax = 0.35)
        plt.title(date)
    
    cbar = plt.colorbar(c)
    cbar.set_label(unit)
    plt.show()
Explanation: Now we ara making a function for making images
End of explanation
try:
    package_key = download_data(folder,dataset_key,API_key,longitude_west,longitude_east,latitude_south,latitude_north,time_start,time_end,variable,area)
except:
    days = 7
    time_start,time_end = get_start_end(days)
    package_key = download_data(folder,dataset_key,API_key,longitude_west,longitude_east,latitude_south,latitude_north,time_start,time_end,variable,area)
Explanation: Here we are downloading data by using Package-API. If you are intrested how data is downloaded, find the file named package_api.py from notebook folder.
End of explanation
filename_europe = package_key + '.nc'
data = xr.open_dataset(filename_europe)
surface_soil_moisture_data = data.Analysis_Data__sm_surface_analysis
unit = surface_soil_moisture_data.units
surface_soil_moisture = data.Analysis_Data__sm_surface_analysis.values[0,:,:]
surface_soil_moisture = np.ma.masked_where(np.isnan(surface_soil_moisture),surface_soil_moisture)
latitude = data.lat; longitude = data.lon
lat = latitude.values
lon = longitude.values
date = str(data.time.values[0])[:-10]
Explanation: Now we have data and we are reading it in using xarray:
End of explanation
make_image(lon,lat,surface_soil_moisture,date,latitude_north, latitude_south,longitude_west, longitude_east,unit)
Explanation: Here we are making image by using function defined above. 
In this image we can see how dry Iberian peninsula (Portugal and Spain) was during wildfires in October. On 15th October strong winds from the  Hurricane Ophelia quickly spread flames along the Iberian coast. We can see that on that day Iberian peninsula soil moisture was comparable with African deserts.
End of explanation
iberia_west = -10; iberia_east = 3.3
iberia_south = 35; iberia_north = 45
lon_ib = longitude.sel(lon=slice(iberia_west,iberia_east)).values
lat_ib = latitude.sel(lat=slice(iberia_north,iberia_south)).values
soil_ib = surface_soil_moisture_data.sel(lat=slice(iberia_north,iberia_south),lon=slice(iberia_west,iberia_east)).values[0,:,:]
soil_ib = np.ma.masked_where(np.isnan(soil_ib),soil_ib)
Explanation: So let's see Portugal and Spain littlebit closer. For that we need to define the area and we will slice data from this area.
End of explanation
days2 = days -1
time_start, time_end = get_start_end(days2)
try:
    package_key_iberia = download_data(folder,dataset_key,API_key,iberia_west,iberia_east,iberia_south,iberia_north,time_start,time_end,variable,area)
except:
    days2 = days - 2
    time_start, time_end = get_start_end(days2)
    package_key_iberia = download_data(folder,dataset_key,API_key,iberia_west,iberia_east,iberia_south,iberia_north,time_start,time_end,variable,area)
filename_iberia = package_key_iberia + '.nc'
data_later = xr.open_dataset(filename_iberia)
soil_data_later = data_later.Analysis_Data__sm_surface_analysis
soil_later = data_later.Analysis_Data__sm_surface_analysis.values[0,:,:]
soil_later = np.ma.masked_where(np.isnan(soil_later),soil_later)
latitude_ib = data.lat; longitude_ib = data.lon
lat_ibl = latitude_ib.values
lon_ibl = longitude_ib.values
date_later = str(data_later.time.values[0])[:-10]
Explanation: Let's also import some data from later as well.
End of explanation
make_image(lon_ib,lat_ib,soil_ib,date,iberia_north, iberia_south,iberia_west, iberia_east, unit, data_later = soil_later,date_later = date_later)
Explanation: Now we are making two images from the same area - Portugal and Spain. 
On the left image we can see soil moisture values on 15th October and on the right image we can see soil moisture on 21th October.
We can see that the land was getting littlebit wetter with a few days. It even helped firefighters to get wildfires under control.
End of explanation
if os.path.exists(filename_europe):
    os.remove(filename_europe)
if os.path.exists(filename_iberia):
    os.remove(filename_iberia)
Explanation: Finally, let's delete files we downloaded:
End of explanation
<END_TASK>