from mpl_toolkits.basemap import Basemap, cm, shiftgrid, interp from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata import scipy.stats from matplotlib.dates import DateFormatter import datetime country = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/Ctry_halfdeg.nc', 'r') #print iizumi coun = country.variables['MASK_Country'][:, :] #area=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf.nc','r') #gridarea = area.variables['cell_area'][:,:] #gridlon = area.variables['lon'][:] #gridarea,gridlon = shiftgrid(180.5,gridarea,gridlon,start=False) def annualyield(year, couna, counb): bb = year - 2006 region1 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/RCP85_crop_150901.nc', 'r') maitrop = region1.variables['maize_trop'][bb, :, :] maitemp = region1.variables['maize_temp'][bb, :, :] maitropi = region1.variables['maize_trop_irrig'][bb, :, :]
def draw_velocity_map(nc_file_name): nc = NetCDFFile(nc_file_name, 'r+') lat = nc.variables['nav_lat_grid_U'][:] lon = nc.variables['nav_lon_grid_U'][:] vel_dir = "samples/bad/vel/" nc_name = nc_file_name.split("/")[4].split(".")[0] velocity = np.zeros((400, 1100), dtype=np.float32) for file_name in os.listdir(vel_dir): if nc_name in file_name: square = img.load_square_from_file(vel_dir + file_name.split(".")[0]) print(file_name) print(str(np.min(square)) + "; " + str(np.max(square))) square_index = data.extract_square_index(vel_dir + file_name.split(".")[0]) x = int(square_index.split("_")[0]) y = int(square_index.split("_")[1]) print(str(x) + " " + str(y)) velocity[y:y + 100, x:x + 100] = square nc.close() lat_left_bottom = lat[-1][-1] lon_left_bottom = lon[-1][-1] lat_right_top = lat[0][0] lon_right_top = lon[0][0] lat_center = 90 # 110, 119 lon_center = 110 m = Basemap(projection='stere', lon_0=lon_center, lat_0=lat_center, resolution='l', llcrnrlat=lat_left_bottom, llcrnrlon=lon_left_bottom, urcrnrlat=lat_right_top, urcrnrlon=lon_right_top) m.pcolormesh(lon, lat, velocity, latlon=True, cmap='RdYlBu_r', vmax=0.6) m.drawcoastlines() m.drawcountries() m.fillcontinents(color='#cc9966', lake_color='#99ffff') # plt.rcParams.update({'font.size': 22}) ax = plt.gca() # ax.tick_params(labelsize=10) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) plt.colorbar(cax=cax, label="Sea current velocity") # plt.title("Anomalies detection results for: " + "20 March, 2013") model = load_model("samples/current_model/model.h5") valid_squares = [[*list(range(1, 10))]] with open("samples/valid_samples.csv", 'r', newline='') as csvfile: samples = [] reader = csv.reader(csvfile, delimiter=',') for row in reader: if "samples/bad/vel/" + nc_name in row[0]: print(row) samples.append(row) square_index = data.extract_square_index(row[0]) x = int(square_index.split("_")[0]) y = int(square_index.split("_")[1]) if (x >= 100) and (x < 1000) and (y < 300): sample = np.zeros((1, 100, 100, 1), dtype=np.float32) square = np.expand_dims(img.load_square_from_file(row[0]), axis=2) sample[0] = square result = model.predict(sample) result_x, result_y = m(lon[y + 50][x + 50], lat[y + 50][x + 50]) ax.text(result_x, result_y, "%0.3f" % result[0][0], ha='center', size=7, color="yellow", path_effects=[ PathEffects.withStroke(linewidth=3, foreground='black') ]) if row[1] == "1": print("outlier!") lat_poly = np.array([ lat[y][x], lat[y][x + 99], lat[y + 99][x + 99], lat[y + 99][x] ]) lon_poly = np.array([ lon[y][x], lon[y][x + 99], lon[y + 99][x + 99], lon[y + 99][x] ]) mapx, mapy = m(lon_poly, lat_poly) points = np.zeros((4, 2), dtype=np.float32) for j in range(0, 4): points[j][0] = mapx[j] points[j][1] = mapy[j] if result[0][0] > 0.5: poly = Polygon(points, color='green', fill=False, linewidth=3) ax.add_patch(poly) else: poly = Polygon(points, color='red', fill=False, linewidth=3) ax.add_patch(poly) else: if result[0][0] > 0.5: lat_poly = np.array([ lat[y][x], lat[y][x + 99], lat[y + 99][x + 99], lat[y + 99][x] ]) lon_poly = np.array([ lon[y][x], lon[y][x + 99], lon[y + 99][x + 99], lon[y + 99][x] ]) mapx, mapy = m(lon_poly, lat_poly) points = np.zeros((4, 2), dtype=np.float32) for j in range(0, 4): points[j][0] = mapx[j] points[j][1] = mapy[j] poly = Polygon(points, color='red', fill=False, linewidth=3) ax.add_patch(poly) print(result) # plt.show() red = Patch(color='red', label='Error') green = Patch(color='green', label='Correct') plt.legend(loc='lower right', fontsize='medium', bbox_to_anchor=(1, 1), handles=[green, red]) plt.savefig("test" + "_bad_result.png", dpi=500)
def draw_map(nc_file_name): nc = NetCDFFile(nc_file_name, 'r+') lat = nc.variables['nav_lat_grid_U'][:] lon = nc.variables['nav_lon_grid_U'][:] nc_name = nc_file_name.split("/")[4].split(".")[0] velocity = np.zeros((400, 1100), dtype=np.float32) vel_dir = "samples/bad/vel/" for file_name in os.listdir(vel_dir): if nc_name in file_name: square = img.load_square_from_file(vel_dir + file_name.split(".")[0]) print(file_name) print(str(np.min(square)) + "; " + str(np.max(square))) square_index = data.extract_square_index(vel_dir + file_name.split(".")[0]) x = int(square_index.split("_")[0]) y = int(square_index.split("_")[1]) print(str(x) + " " + str(y)) velocity[y:y + 100, x:x + 100] = square nc.close() lat_left_bottom = lat[-1][-1] lon_left_bottom = lon[-1][-1] lat_right_top = lat[0][0] lon_right_top = lon[0][0] lat_center = 90 # 110, 119 lon_center = 110 m = Basemap(projection='stere', lon_0=lon_center, lat_0=lat_center, resolution='l', llcrnrlat=lat_left_bottom, llcrnrlon=lon_left_bottom, urcrnrlat=lat_right_top, urcrnrlon=lon_right_top) m.pcolormesh(lon, lat, velocity, latlon=True, cmap='RdYlBu_r', vmax=0.6) m.drawcoastlines() m.drawcountries() m.fillcontinents(color='#cc9966', lake_color='#99ffff') ax = plt.gca() # ax.tick_params(labelsize=10) divider = make_axes_locatable(ax) cax = divider.append_axes("right", size="5%", pad=0.05) plt.colorbar(cax=cax, label="Sea current velocity") # ax = plt.gca() # divider = make_axes_locatable(ax) # cax = divider.append_axes("right", size="5%", pad=0.05) # plt.colorbar().set_label(label='Sea current velocity', size=15) # with open("samples/valid_samples.csv", 'r', newline='') as csvfile: # samples = [] # # reader = csv.reader(csvfile, delimiter=',') # for row in reader: # if "samples/bad/vel/" + nc_name in row[0]: # print(row) # samples.append(row) # square_index = data.extract_square_index(row[0]) # # x = int(square_index.split("_")[0]) # y = int(square_index.split("_")[1]) # # if (x >= 100) and (x < 1000) and (y < 300): # # sample = np.zeros((1, 100, 100, 1), dtype=np.float32) # square = np.expand_dims(img.load_square_from_file(row[0]), axis=2) # sample[0] = square # lat_poly = np.array([lat[y][x], lat[y][x + 99], lat[y + 99][x + 99], lat[y + 99][x]]) # lon_poly = np.array([lon[y][x], lon[y][x + 99], lon[y + 99][x + 99], lon[y + 99][x]]) # mapx, mapy = m(lon_poly, lat_poly) # points = np.zeros((4, 2), dtype=np.float32) # for j in range(0, 4): # points[j][0] = mapx[j] # points[j][1] = mapy[j] # poly = Polygon(points, color='black', fill=False, linewidth=3) # ax.add_patch(poly) # plt.savefig("levels" + "_!!!.png", dpi=500) plt.show()
from mpl_toolkits.basemap import Basemap, cm, shiftgrid,interp,maskoceans from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import numpy.ma as ma import matplotlib.colors as colors mask=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/Ctry_halfdeg.nc','r') cou1 = mask.variables['MASK_Country'][:,:] cou1= ma.masked_where(cou1<=0.0,cou1) region=NetCDFFile('/project/projectdirs/m1602/datasets4.full/surfdata_05x05.nc','r') ind = region.variables['REGION_MASK_CRU_NCEP'][:,:] #area=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf.nc','r') #gridarea = area.variables['cell_area'] isam=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/rice_cheyenne/his_cru/ric_irr_fert/output/ric_irr_fert.nc','r') lonisam1=isam.variables['lon'][:] #ric_i_f=isam.variables['totalyield'][96:103,:,:] #riceb=N.average(ric_i_f,axis=0) spam=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/spamsoy_isam.nc','r') spamy=spam.variables['soyy_total'][:,:] spampro=spam.variables['soyp_total'][:,:] spamarea=spam.variables['soya_total'][:,:] #riceb,lona11=shiftgrid(180.5,riceb,lonisam1,start=False)
print('this one is for chlorophyll 1 in 2006') from netCDF4 import Dataset as NetCDFFile import numpy as np from datetime import datetime as dt mydirectory = '/users/asmit101/data/stuff/2006_mod8_run6a_NAM' nc = NetCDFFile(mydirectory + '/surfchl_whole_year.nc') print(nc.variables.keys()) chlorophyll1 = nc.variables['chlorophyll1'][:] time = nc.variables['ocean_time'][:] timearray = [] for i in time: timearray.append(dt.fromtimestamp(i)) for j in timearray: print(j) print(chlorophyll1.ndim) chlorophyll1array = [] chlorophyll1 = chlorophyll1.mean(axis=tuple(range(2, 4))) for i in chlorophyll1: chlorophyll1array.append(float(i)) print(chlorophyll1array) print(timearray)
filename5a = '/data/scihub-users/giyoung/PWRF_V3.6.1/RUNS/MAC_WRF/56_DeMott_WATSAT_10xHM_noThresh_eta70_MYNN/wrfout_d01_2015-11-27_00:00:00' ################################### # Extract domain number: ################################### domainno_start = filename1.find('/wrfout_') + 8 domainno_end = filename1.find('_2015',domainno_start) domainno1 = filename1[domainno_start:domainno_end] del domainno_end, domainno_start ################################### # LOAD NETCDF FILE ################################### nc1 = NetCDFFile(filename1, 'r') nc2 = NetCDFFile(filename2, 'r') nc3 = NetCDFFile(filename3, 'r') nc4 = NetCDFFile(filename4, 'r') nc5 = NetCDFFile(filename5, 'r') nc1a = NetCDFFile(filename1a, 'r') nc2a = NetCDFFile(filename2a, 'r') nc3a = NetCDFFile(filename3a, 'r') nc4a = NetCDFFile(filename4a, 'r') nc5a = NetCDFFile(filename5a, 'r') ################################### # DEFINE TEMPERATURE BINNING ###################################
mx = maximum_filter(mat, size=window, mode=mode) # (mat == mx) true if pixel is equal to the local max # (mat == mn) true if pixel is equal to the local in # Return the indices of the maxima, minima return np.nonzero(mat == mn), np.nonzero(mat == mx) if len(sys.argv) < 2: print 'enter date to plot (YYYYMMDDHH) on command line' raise SystemExit # get date from command line. YYYYMMDDHH = sys.argv[1] # open OpenDAP dataset. try: data=NetCDFFile("http://nomad1.ncep.noaa.gov:9090/dods/gdas/rotating/gdas"+YYYYMMDDHH+".grib2") except: data=NetCDFFile("http://nomad2.ncep.noaa.gov:9090/dods/gdas/rotating/gdas"+YYYYMMDDHH+".grib2") # read lats,lons. lats = data.variables['lat'][:] lons1 = data.variables['lon'][:] nlats = len(lats) nlons = len(lons1) # read prmsl, convert to hPa (mb). prmsl = 0.01*data.variables['prmslmsl'][0] # the window parameter controls the number of highs and lows detected. # (higher value, fewer highs and lows) local_min, local_max = extrema(prmsl, mode='wrap', window=25) # create Basemap instance. m =\
x, y = map(lons * 180. / np.pi, lats * 180. / np.pi) # contour data over the map. cs = map.contour(x, y, wave + mean, 15, linewidths=1.5) plt.title('contour lines over filled continent background') plt.show() from mpl_toolkits.basemap import Basemap, cm # requires netcdf4-python (netcdf4-python.googlecode.com) from netCDF4 import Dataset as NetCDFFile import numpy as np import matplotlib.pyplot as plt # plot rainfall from NWS using special precipitation # colormap used by the NWS, and included in basemap. nc = NetCDFFile('../../../examples/nws_precip_conus_20061222.nc') # data from http://water.weather.gov/precip/ prcpvar = nc.variables['amountofprecip'] data = 0.01 * prcpvar[:] latcorners = nc.variables['lat'][:] loncorners = -nc.variables['lon'][:] lon_0 = -nc.variables['true_lon'].getValue() lat_0 = nc.variables['true_lat'].getValue() # create figure and axes instances fig = plt.figure(figsize=(8, 8)) ax = fig.add_axes([0.1, 0.1, 0.8, 0.8]) # create polar stereographic Basemap instance. m = Basemap(projection='stere', lon_0=lon_0, lat_0=90., lat_ts=lat_0,
metavar="NUMBER") options, args = parser.parse_args() if not options.filename: options.filename = 'landice_grid.nc' print('No file specified. Attempting to use landice_grid.nc') if not options.afile: sys.exit( "Error: A restart file from test B5 is required to set up this test. Specify with -b" ) # copy the restart file to be the new input file shutil.copyfile(options.afile, options.filename) # Open the file, get needed dimensions gridfile = NetCDFFile(options.filename, 'r+') StrLen = len(gridfile.dimensions['StrLen']) gridfile.variables['xtime'][0, :] = netCDF4.stringtoarr( '0000-01-01_00:00:00'.ljust(StrLen), StrLen) gridfile.variables['simulationStartTime'][:] = netCDF4.stringtoarr( '0000-01-01_00:00:00'.ljust(StrLen), StrLen) # modify melt inputs gridfile.variables['externalWaterInput'][ 0, :] = gridfile.variables['externalWaterInput'][ 0, :] * 1.0e-12 # Make value at moulin locations tiny but positive # value for basalMeltInput doesn't matter, because it will be overwritten in the code. gridfile.close() print('Successfully added initial conditions to: ' + options.filename)
from mpl_toolkits.basemap import Basemap, cm, shiftgrid, interp from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata import matplotlib.colors as colors area1 = NetCDFFile( '/project/projectdirs/m1602/datasets4.full/surfdata_05x05.nc', 'r') mask = area1.variables['REGION_MASK_CRU_NCEP'][:, :] area = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf_isam.nc', 'r') gridarea1 = area.variables['cell_area'][:, :] gridlon = area.variables['lon'][:] gridlat = area.variables['lat'][:] #print gridlon nclu = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/m3yield_isam.nc', 'r') ncvar_maize = nclu.variables['maizey'][0, :, :] marea = nclu.variables['maize_area'][0, :, :] region1 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/HistoricalGLM_crop_150901.nc', 'r') maitrop = region1.variables['maize_trop'][95:105, :, :] maitemp = region1.variables['maize_temp'][95:105, :, :] maitropi = region1.variables['maize_trop_irrig'][95:105, :, :] maitempi = region1.variables['maize_temp_irrig'][95:105, :, :]
from mpl_toolkits.basemap import Basemap, cm, shiftgrid, interp, maskoceans from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import numpy.ma as ma from statsmodels.stats.weightstats import DescrStatsW import matplotlib.colors as colors region = NetCDFFile( '/global/project/projectdirs/m1602/datasets4.full/arbit_init_state_05x05.nc', 'r') ind = region.variables['REGION_MASK'][:, :] lonisam1 = region.variables['lon'][:] ind, lona11 = shiftgrid(180.5, ind, lonisam1, start=False) area = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf.nc', 'r') gridarea = area.variables['cell_area'][:, :] nclu = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/maize_AreaYieldProduction.nc', 'r') ncvar_m = nclu.variables['maizeData'][0, 0, :, :] ncvar_y = nclu.variables['maizeData'][0, 1, :, :] ncvar_a = nclu.variables['maizeData'][0, 4, :, :] ncvar_p = nclu.variables['maizeData'][0, 5, :, :] nclu1 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/soybean_AreaYieldProduction.nc', 'r') ncvar_ms = nclu1.variables['soybeanData'][0, 0, :, :] ncvar_ys = nclu1.variables['soybeanData'][0, 1, :, :]
def yieldout(year): bb=year-1900 region1=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/HistoricalGLM_crop_150901.nc','r') maitrop = region1.variables['maize_trop'][bb-1,:,:] maitemp = region1.variables['maize_temp'][bb-1,:,:] maitropi=region1.variables['maize_trop_irrig'][bb-1,:,:] maitempi=region1.variables['maize_temp_irrig'][bb-1,:,:] gridarea = region1.variables['area'][:,:] maitrop=ma.masked_where(maitrop<=0,maitrop) maitrop=ma.filled(maitrop, fill_value=0.) maitemp=ma.masked_where(maitemp<=0,maitemp) maitemp=ma.filled(maitemp, fill_value=0.) maitropi=ma.masked_where(maitropi<=0,maitropi) maitropi=ma.filled(maitropi, fill_value=0.) maitempi=ma.masked_where(maitempi<=0,maitempi) maitempi=ma.filled(maitempi, fill_value=0.) maizetor=maitrop+maitemp maizetoi=maitropi+maitempi maizetrop=maitrop+maitropi maizetemp=maitemp+maitempi maizeto = maitrop+maitemp+maitropi+maitempi ff=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/HistoricalFertilizer.nc','r') fert_maitrop = ff.variables['maize_trop_fert'][bb-1,:,:] fert_maitemp = ff.variables['maize_temp_fert'][bb-1,:,:] clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45historical/maizetrop_historical_co2_rf_fert_0.5x0.5.nc','r') # clmtropf = clm.variables['yield'][bb,:,:] clmtropfer=clm.variables['fertilizer'][bb-1,:,:] clm1=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45historical/maizetemp_historical_co2_rf_fert_0.5x0.5.nc','r') # clmtempf = clm1.variables['yield'][bb,:,:] clmtempfer=clm1.variables['fertilizer'][bb-1,:,:] isam=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamhistorical/heat/maizetrop_historical_co2_rf_fert_0.5x0.5.nc','r') clmtropf = isam.variables['totalyield'][bb,:,:] isam1=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamhistorical/heat/maizetemp_historical_co2_rf_fert_0.5x0.5.nc','r') clmtempf = isam1.variables['totalyield'][bb,:,:] clm2=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamhistorical/heat/maizetrop_historical_co2_irrig_fert_0.5x0.5.nc','r') clmtropfi = clm2.variables['totalyield'][bb,:,:] clm3=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamhistorical/heat/maizetemp_historical_co2_irrig_fert_0.5x0.5.nc','r') clmtempfi = clm3.variables['totalyield'][bb,:,:] clma=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamhistorical/heat/maizetrop_historical_co2_rf_nofert_0.5x0.5.nc','r') clmtropfno = clma.variables['totalyield'][bb,:,:] clm1a=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamhistorical/heat/maizetemp_historical_co2_rf_nofert_0.5x0.5.nc','r') clmtempfno = clm1a.variables['totalyield'][bb,:,:] clm2a=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamhistorical/heat/maizetrop_historical_co2_irrig_nofert_0.5x0.5.nc','r') clmtropfnoi = clm2a.variables['totalyield'][bb,:,:] clm3a=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamhistorical/heat/maizetemp_historical_co2_irrig_nofert_0.5x0.5.nc','r') clmtempfnoi = clm3a.variables['totalyield'][bb,:,:] lonisam=clm3a.variables['lon'][:] clmtempfnoi,lonisam1 = shiftgrid(180.5,clmtempfnoi,lonisam,start=False) clmtropfnoi,lonisam1 = shiftgrid(180.5,clmtropfnoi,lonisam,start=False) clmtempfno,lonisam1 = shiftgrid(180.5,clmtempfno,lonisam,start=False) clmtropfno,lonisam1 = shiftgrid(180.5,clmtropfno,lonisam,start=False) clmtempf,lonisam1 = shiftgrid(180.5,clmtempf,lonisam,start=False) clmtropf,lonisam1 = shiftgrid(180.5,clmtropf,lonisam,start=False) clmtempfi,lonisam1 = shiftgrid(180.5,clmtempfi,lonisam,start=False) clmtropfi,lonisam1 = shiftgrid(180.5,clmtropfi,lonisam,start=False) #print lonisam1 clmtropfer=N.flipud(clmtropfer) clmtempfer=N.flipud(clmtempfer) clmtropf= ma.masked_where(maitrop<=0,clmtropf) clmtempf= ma.masked_where(maitemp<=0,clmtempf) clmtropf=ma.filled(clmtropf, fill_value=0.) clmtempf=ma.filled(clmtempf, fill_value=0.) clmtropfi= ma.masked_where(maitropi<=0,clmtropfi) clmtempfi= ma.masked_where(maitempi<=0,clmtempfi) clmtropfi=ma.filled(clmtropfi, fill_value=0.) clmtempfi=ma.filled(clmtempfi, fill_value=0.) clmtropfno= ma.masked_where(maitrop<=0,clmtropfno) clmtempfno= ma.masked_where(maitemp<=0,clmtempfno) clmtropfno=ma.filled(clmtropfno, fill_value=0.) clmtempfno=ma.filled(clmtempfno, fill_value=0.) clmtropfnoi= ma.masked_where(maitropi<=0,clmtropfnoi) clmtempfnoi= ma.masked_where(maitempi<=0,clmtempfnoi) clmtropfnoi=ma.filled(clmtropfnoi, fill_value=0.) clmtempfnoi=ma.filled(clmtempfnoi, fill_value=0.) fertfractiontrop= N.zeros((360, 720)) nofertfractiontrop= N.zeros((360, 720)) fertfractiontemp= N.zeros((360, 720)) nofertfractiontemp= N.zeros((360, 720)) for x in range(0,360): for y in range(0,720): if clmtropfer[x,y] > 0.0: fertfractiontrop[x,y] = min(1.0,fert_maitrop[x,y]/clmtropfer[x,y]) nofertfractiontrop[x,y] = 1.0 - fertfractiontrop[x,y] else: fertfractiontrop[x,y]= 0.0 nofertfractiontrop[x,y] = 1.0 for x in range(0,360): for y in range(0,720): if clmtempfer[x,y] > 0.0: fertfractiontemp[x,y] = min(1.0,fert_maitemp[x,y]/clmtempfer[x,y]) nofertfractiontemp[x,y] = 1.0 - fertfractiontemp[x,y] else: fertfractiontemp[x,y]= 0.0 nofertfractiontemp[x,y] = 1.0 clmtropfnew= N.zeros((360, 720)) clmtempfnew= N.zeros((360, 720)) clmtropfinew= N.zeros((360, 720)) clmtempfinew= N.zeros((360, 720)) for x in range(0,360): for y in range(0,720): clmtropfnew[x,y] = (nofertfractiontrop[x,y]*clmtropfno[x,y])+(fertfractiontrop[x,y]*clmtropf[x,y]) clmtempfnew[x,y] = (nofertfractiontemp[x,y]*clmtempfno[x,y])+(fertfractiontemp[x,y]*clmtempf[x,y]) clmtropfinew[x,y] = (nofertfractiontrop[x,y]*clmtropfnoi[x,y])+(fertfractiontrop[x,y]*clmtropfi[x,y]) clmtempfinew[x,y] = (nofertfractiontemp[x,y]*clmtempfnoi[x,y])+(fertfractiontemp[x,y]*clmtempfi[x,y]) yield_clmtf=clmtropf+clmtempf yield_clmtf = ma.masked_where(yield_clmtf<=0,yield_clmtf) #yield_clmtf = ma.masked_where(maizetor<=0,yield_clmtf ) yield_clmtf=ma.filled(yield_clmtf, fill_value=0.) yield_clmtfi=clmtropfi+clmtempfi yield_clmtfi = ma.masked_where(yield_clmtfi<=0,yield_clmtfi) #yield_clmtfi = ma.masked_where(maizetoi<=0,yield_clmtfi) yield_clmtfi=ma.filled(yield_clmtfi, fill_value=0.) yield_clmtfnew=clmtropfnew+clmtempfnew yield_clmtfnew = ma.masked_where(yield_clmtfnew<=0,yield_clmtfnew) #yield_clmtf = ma.masked_where(maizetor<=0,yield_clmtf ) yield_clmtfnew=ma.filled(yield_clmtfnew, fill_value=0.) yield_clmtfinew=clmtropfinew+clmtempfinew yield_clmtfinew = ma.masked_where(yield_clmtfinew<=0,yield_clmtfinew) #yield_clmtfi = ma.masked_where(maizetoi<=0,yield_clmtfi) yield_clmtfinew=ma.filled(yield_clmtfinew, fill_value=0.) area=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf.nc','r') gridarea = area.variables['cell_area'][:,:] gridlon = area.variables['lon'][:] gridlat=area.variables['lat'][:] gridarea,gridlon = shiftgrid(180.5,gridarea,gridlon,start=False) lon2,lat2 = N.meshgrid(gridlon,gridlat) map = Basemap(projection ='cyl', llcrnrlat=-65, urcrnrlat=90,llcrnrlon=-180, urcrnrlon=180, resolution='c') x,y = map(lon2,lat2) yield_clmtf=maskoceans(x,y,yield_clmtf) yield_clmtf = ma.masked_where(maizeto<=0,yield_clmtf) yield_clmtfi=maskoceans(x,y,yield_clmtfi) yield_clmtfi = ma.masked_where(maizeto<=0,yield_clmtfi) clmy=((yield_clmtf*maizetor*gridarea)+(yield_clmtfi*maizetoi*gridarea))/((maizetoi*gridarea)+(maizetor*gridarea)) yield_clmtfnew=maskoceans(x,y,yield_clmtfnew) yield_clmtfnew = ma.masked_where(maizeto<=0,yield_clmtfnew) yield_clmtfinew=maskoceans(x,y,yield_clmtfinew) yield_clmtfinew = ma.masked_where(maizeto<=0,yield_clmtfinew) clmynew=((yield_clmtfnew*maizetor*gridarea)+(yield_clmtfinew*maizetoi*gridarea))/((maizetoi*gridarea)+(maizetor*gridarea)) return clmy
yield_clmtfinew=maskoceans(x,y,yield_clmtfinew) yield_clmtfinew = ma.masked_where(maizeto<=0,yield_clmtfinew) clmynew=((yield_clmtfnew*maizetor*gridarea)+(yield_clmtfinew*maizetoi*gridarea))/((maizetoi*gridarea)+(maizetor*gridarea)) return clmy yief= N.zeros((105, 360, 720)) years = range(1901, 2006) for i, yeara in enumerate(years): yie=yieldout(yeara) yief[i, :, :] = yie area=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf.nc','r') gridarea = area.variables['cell_area'][:,:] gridlon = area.variables['lon'][:] gridlat=area.variables['lat'][:] gridarea,gridlon = shiftgrid(180.5,gridarea,gridlon,start=False) #print gridlon ncfile=NetCDFFile('isamhis_maiscaleiyield_heat.nc','w',format='NETCDF3_64BIT_OFFSET') ncfile.createDimension('lat', 360) ncfile.createDimension('lon', 720) ncfile.createDimension('time', 105) times = ncfile.createVariable('time', 'f8', ('time',)) latitudes = ncfile.createVariable('lat', 'f8', ('lat',)) longitudes = ncfile.createVariable('lon', 'f8', ('lon',))
def annualyield(year, couna, counb): bb = year - 2006 region1 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/RCP85_crop_150901.nc', 'r') maitrop = region1.variables['maize_trop'][bb, :, :] maitemp = region1.variables['maize_temp'][bb, :, :] maitropi = region1.variables['maize_trop_irrig'][bb, :, :] maitempi = region1.variables['maize_temp_irrig'][bb, :, :] gridarea = region1.variables['area'][:, :] maitrop = ma.masked_where(maitrop <= 0, maitrop) maitrop = ma.filled(maitrop, fill_value=0.) maitemp = ma.masked_where(maitemp <= 0, maitemp) maitemp = ma.filled(maitemp, fill_value=0.) maitropi = ma.masked_where(maitropi <= 0, maitropi) maitropi = ma.filled(maitropi, fill_value=0.) maitempi = ma.masked_where(maitempi <= 0, maitempi) maitempi = ma.filled(maitempi, fill_value=0.) maizetro = maitrop + maitropi maizetem = maitemp + maitempi maizetor = maitrop + maitemp maizetoi = maitropi + maitempi maizeto = maitrop + maitemp + maitropi + maitempi clm = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetrop_rcp85_constco2_rf_nofert_0.5x0.5.nc', 'r') clmtropf = clm.variables['yield'][bb, :, :] clm1 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetemp_rcp85_constco2_rf_nofert_0.5x0.5.nc', 'r') clmtempf = clm1.variables['yield'][bb, :, :] clm2 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetrop_rcp85_co2_rf_nofert_0.5x0.5.nc', 'r') clmtropfi = clm2.variables['yield'][bb, :, :] clm3 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetemp_rcp85_co2_rf_nofert_0.5x0.5.nc', 'r') clmtempfi = clm3.variables['yield'][bb, :, :] clmtropf = N.flipud(clmtropf) clmtempf = N.flipud(clmtempf) clmtropfi = N.flipud(clmtropfi) clmtempfi = N.flipud(clmtempfi) clmtropf = ma.masked_where(maizetro <= 0, clmtropf) clmtempf = ma.masked_where(maizetem <= 0, clmtempf) clmtropf = ma.filled(clmtropf, fill_value=0.) clmtempf = ma.filled(clmtempf, fill_value=0.) clmtropfi = ma.masked_where(maizetro <= 0, clmtropfi) clmtempfi = ma.masked_where(maizetem <= 0, clmtempfi) clmtropfi = ma.filled(clmtropfi, fill_value=0.) clmtempfi = ma.filled(clmtempfi, fill_value=0.) yield_clmtf = clmtropf + clmtempf yield_clmtf = ma.masked_where(yield_clmtf <= 0, yield_clmtf) yield_clmtf = ma.masked_where(maizeto <= 0, yield_clmtf) yield_clmtf = ma.filled(yield_clmtf, fill_value=0.) yield_clmtfi = clmtropfi + clmtempfi yield_clmtfi = ma.masked_where(yield_clmtfi <= 0, yield_clmtfi) yield_clmtfi = ma.masked_where(maizeto <= 0, yield_clmtfi) yield_clmtfi = ma.filled(yield_clmtfi, fill_value=0.) clmn = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetrop_rcp85_co2_rf_fert_0.5x0.5.nc', 'r') clmtropfn = clmn.variables['yield'][bb, :, :] clm1n = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetemp_rcp85_co2_rf_fert_0.5x0.5.nc', 'r') clmtempfn = clm1n.variables['yield'][bb, :, :] clm2n = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetrop_rcp85_co2_irrig_fert_0.5x0.5.nc', 'r') clmtropfin = clm2n.variables['yield'][bb, :, :] clm3n = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetemp_rcp85_co2_irrig_fert_0.5x0.5.nc', 'r') clmtempfin = clm3n.variables['yield'][bb, :, :] clmtropfn = N.flipud(clmtropfn) clmtempfn = N.flipud(clmtempfn) clmtropfin = N.flipud(clmtropfin) clmtempfin = N.flipud(clmtempfin) clmtropfn = ma.masked_where(maizetro <= 0, clmtropfn) clmtempfn = ma.masked_where(maizetem <= 0, clmtempfn) clmtropfn = ma.filled(clmtropfn, fill_value=0.) clmtempfn = ma.filled(clmtempfn, fill_value=0.) clmtropfin = ma.masked_where(maizetro <= 0, clmtropfin) clmtempfin = ma.masked_where(maizetem <= 0, clmtempfin) clmtropfin = ma.filled(clmtropfin, fill_value=0.) clmtempfin = ma.filled(clmtempfin, fill_value=0.) yield_clmtfn = clmtropfn + clmtempfn yield_clmtfn = ma.masked_where(yield_clmtfn <= 0, yield_clmtfn) yield_clmtfn = ma.masked_where(maizeto <= 0, yield_clmtfn) yield_clmtfn = ma.filled(yield_clmtfn, fill_value=0.) yield_clmtfin = clmtropfin + clmtempfin yield_clmtfin = ma.masked_where(yield_clmtfin <= 0, yield_clmtfin) yield_clmtfin = ma.masked_where(maizeto <= 0, yield_clmtfin) yield_clmtfin = ma.filled(yield_clmtfin, fill_value=0.) base = NetCDFFile( "/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamrcp85/heat/maizetemp_rcp85_constco2_rf_nofert_0.5x0.5.nc", mode='r') base2 = NetCDFFile( "/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamrcp85/heat/maizetemp_rcp85_co2_rf_nofert_0.5x0.5.nc", mode='r') lona1 = base.variables["lon"][:] lata1 = base.variables["lat"][:] yieldf = base.variables["totalyield"][bb, :, :] yieldfa = base2.variables["totalyield"][bb, :, :] basei = NetCDFFile( "/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamrcp85/heat/maizetemp_rcp85_co2_rf_fert_0.5x0.5.nc", mode='r') base2i = NetCDFFile( "/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamrcp85/heat/maizetemp_rcp85_co2_irrig_fert_0.5x0.5.nc", mode='r') yieldfi = basei.variables["totalyield"][bb, :, :] yieldfai = base2i.variables["totalyield"][bb, :, :] baseif = NetCDFFile( "/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamrcp85/heat/maizetrop_rcp85_co2_rf_fert_0.5x0.5.nc", mode='r') base2if = NetCDFFile( "/scratch2/scratchdirs/tslin2/isam/cheyenne/yieldout/isamrcp85/heat/maizetrop_rcp85_co2_irrig_fert_0.5x0.5.nc", mode='r') yieldfitr = baseif.variables["totalyield"][bb, :, :] yieldfaitr = base2if.variables["totalyield"][bb, :, :] yielda = yieldf yield_new, lona11 = shiftgrid(180.5, yielda, lona1, start=False) yieldb = yieldfa yield_new1, lona11 = shiftgrid(180.5, yieldb, lona1, start=False) yieldai = yieldfi yield_newi, lona11 = shiftgrid(180.5, yieldai, lona1, start=False) yieldbi = yieldfai yield_new1i, lona11 = shiftgrid(180.5, yieldbi, lona1, start=False) yieldaitr = yieldfitr yield_newitr, lona11 = shiftgrid(180.5, yieldaitr, lona1, start=False) yieldbitr = yieldfaitr yield_new1itr, lona11 = shiftgrid(180.5, yieldbitr, lona1, start=False) yield_new[N.isnan(yield_new)] = -9999 yield_new = ma.masked_where(yield_new <= 0, yield_new) yield_new = ma.masked_where(maizeto <= 0, yield_new) yield_new = ma.filled(yield_new, fill_value=0.) yield_new1[N.isnan(yield_new1)] = -9999 yield_new1 = ma.masked_where(yield_new1 <= 0, yield_new1) yield_new1 = ma.masked_where(maizeto <= 0, yield_new1) yield_new1 = ma.filled(yield_new1, fill_value=0.) yield_newi[N.isnan(yield_newi)] = -9999 yield_newi = ma.masked_where(yield_newi <= 0, yield_newi) yield_newi = ma.masked_where(maizetem <= 0, yield_newi) yield_newi = ma.filled(yield_newi, fill_value=0.) yield_new1i[N.isnan(yield_new1i)] = -9999 yield_new1i = ma.masked_where(yield_new1i <= 0, yield_new1i) yield_new1i = ma.masked_where(maizetem <= 0, yield_new1i) yield_new1i = ma.filled(yield_new1i, fill_value=0.) yield_newitr[N.isnan(yield_newitr)] = -9999 yield_newitr = ma.masked_where(yield_newitr <= 0, yield_newitr) yield_newitr = ma.masked_where(maizetro <= 0, yield_newitr) yield_newitr = ma.filled(yield_newitr, fill_value=0.) yield_new1itr[N.isnan(yield_new1i)] = -9999 yield_new1itr = ma.masked_where(yield_new1itr <= 0, yield_new1itr) yield_new1itr = ma.masked_where(maizetro <= 0, yield_new1itr) yield_new1itr = ma.filled(yield_new1itr, fill_value=0.) yield_newi = yield_newi + yield_newitr yield_new1i = yield_new1i + yield_new1itr yieldagf = 0. yieldg = 0. harea = 0. a = 0 yieldgid = 0. yieldgia = 0. yieldgib = 0. yieldgic = 0. yieldgca = 0. yieldgcb = 0. yieldgcc = 0. yieldgcd = 0. yieldagfa = 0. yieldagfb = 0. yieldagfc = 0. yieldagfd = 0. yieldagfa1 = 0. yieldagfb1 = 0. yieldagfc1 = 0. yieldagfd1 = 0. yieldagfa = yield_new yieldagfb = yield_new1 yieldagfc = yield_newi yieldagfd = yield_new1i yieldagfa1 = yield_clmtf yieldagfb1 = yield_clmtfi yieldagfc1 = yield_clmtfn yieldagfd1 = yield_clmtfin yieldagfa = ma.masked_where(yieldagfa <= 0, yieldagfa) yieldagfb = ma.masked_where(yieldagfb <= 0, yieldagfb) yieldagfc = ma.masked_where(yieldagfc <= 0, yieldagfc) yieldagfd = ma.masked_where(yieldagfd <= 0, yieldagfd) yieldagfa1 = ma.masked_where(yieldagfa1 <= 0, yieldagfa1) yieldagfb1 = ma.masked_where(yieldagfb1 <= 0, yieldagfb1) yieldagfc1 = ma.masked_where(yieldagfc1 <= 0, yieldagfc1) yieldagfd1 = ma.masked_where(yieldagfd1 <= 0, yieldagfd1) for xx in range(0, 360): for yy in range(0, 720): if coun[xx, yy] >= couna and coun[xx, yy] <= counb: maizeto[xx, yy] = maizeto[xx, yy] else: maizeto[xx, yy] = 0 maizeto = ma.masked_where(maizeto <= 0, maizeto) return yieldagfa, yieldagfa1, yieldagfb, yieldagfb1, yieldagfc, yieldagfc1, yieldagfd, yieldagfd1, maizeto
from mpl_toolkits.basemap import Basemap, cm, shiftgrid, interp from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata import matplotlib.colors as colors from matplotlib.dates import DateFormatter import datetime dat = NetCDFFile( '/scratch2/scratchdirs/tslin2/isam/maisoy_cheyenne/code/mai1901_2015region.nc', 'r') dat1 = NetCDFFile( '/scratch2/scratchdirs/tslin2/isam/maisoy_cheyenne/code/mairegion_rcp85.nc', 'r') dat2 = NetCDFFile( '/scratch2/scratchdirs/tslin2/isam/maisoy_cheyenne/code/mairegion_rcp45.nc', 'r') for i in range(1, 6): locals()['e{0}p'.format(i)] = dat.variables['e{0}p'.format(i)][:, :] locals()['e{0}y'.format(i)] = dat.variables['e{0}y'.format(i)][:, :] locals()['e{0}g'.format(i)] = dat.variables['e{0}g'.format(i)][:, :] locals()['e{0}p85'.format(i)] = dat1.variables['e{0}p'.format(i)][:, :] locals()['e{0}y85'.format(i)] = dat1.variables['e{0}y'.format(i)][:, :] locals()['e{0}g85'.format(i)] = dat1.variables['e{0}g'.format(i)][:, :] locals()['e{0}p45'.format(i)] = dat2.variables['e{0}p'.format(i)][:, :]
from mpl_toolkits.basemap import Basemap, cm, shiftgrid, interp, maskoceans from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata import matplotlib.colors as colors from scipy.stats import ttest_ind iizumi = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/iizumi/iizumi.2013JAN29.maize.1982-2006.30min.nc4', 'r') #print iizumi iyield = iizumi.variables['yield50'][18, :, :] iarea = iizumi.variables['area'][18, :, :] la = iizumi.variables['lat'][:] lo = iizumi.variables['lon'][:] iyield = N.flipud(iyield) iarea = N.flipud(iarea) region1 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/HistoricalGLM_crop_150901.nc', 'r') maitrop = region1.variables['maize_trop'][99, :, :] maitemp = region1.variables['maize_temp'][99, :, :] maitropi = region1.variables['maize_trop_irrig'][99, :, :] maitempi = region1.variables['maize_temp_irrig'][99, :, :] gridarea = region1.variables['area'][:, :] maitrop = ma.masked_where(maitrop <= 0, maitrop) maitrop = ma.filled(maitrop, fill_value=0.)
from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata import matplotlib.colors as colors from statsmodels.stats.weightstats import DescrStatsW from scipy.stats import ttest_ind from matplotlib.markers import TICKDOWN import datetime from matplotlib.dates import DateFormatter from scipy import stats area = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf_isam.nc', 'r') gridarea = area.variables['cell_area'][:, :] gridlon = area.variables['lon'][:] gridlat = area.variables['lat'][:] gridarea, gridlon = shiftgrid(180.5, gridarea, gridlon, start=False) #print gridlon nclu = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/maize_AreaYieldProduction.nc', 'r') ncvar_maize = nclu.variables['maizeData'][:] latnc = nclu.variables['latitude'][:] znc = nclu.variables['level'][:] lonnc = nclu.variables['longitude'][:] timenc = nclu.variables['time'][:] latnc = N.flipud(latnc)
files.append(files_c[f]) #--- Make plot for each vars and zooms. for var in vars: file_plot = rundir + '/' + model + '_' + var + '_apr1_' + \ yyyy_s + '_' + yyyy_e + '.nc' syscom = 'ncra' for f in range(len(files)): syscom = syscom + ' ' + files[f] syscom = syscom + ' ' + file_plot print 'syscom = ', syscom os.system(syscom) print 'Loading ', var, ' from ', file_plot nc = NetCDFFile(file_plot, 'r') ntimes = len(nc.dimensions['Time']) vardat = {} dat_tmp = nc.variables[var][:,:,:] dat_tmp = np.transpose(dat_tmp) vardat[var] = dat_tmp nc.close() titlein = model.upper() + ', ' + var_lab[var] + ', ' + \ 'Apr 1st, ' + yyyy_s + ' - ' + yyyy_e plotdat = vardat[var][:,:,0] for z in range(len(zooms)): plotfname = rundir + '/' + model + '_' + sdt + '_' + \ edt + '_apr1_' + var + '_' + \
from mpl_toolkits.basemap import Basemap, cm, shiftgrid, interp from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata import matplotlib.colors as colors area = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf_isam.nc', 'r') gridarea = area.variables['cell_area'][:, :] gridlon = area.variables['lon'][:] gridlat = area.variables['lat'][:] gridarea, gridlon = shiftgrid(180.5, gridarea, gridlon, start=False) #print gridlon nclu = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/soybean_AreaYieldProduction.nc', 'r') ncvar_maize = nclu.variables['soybeanData'][:] latnc = nclu.variables['latitude'][:] znc = nclu.variables['level'][:] lonnc = nclu.variables['longitude'][:] timenc = nclu.variables['time'][:] latnc = N.flipud(latnc) ncvar_maizef = N.zeros((2160, 4320)) ncvar_maizef = ncvar_maize[0, 1, :, :] ncvar_maize1 = ncvar_maize[0, 4, :, :] ncvar_mask = N.zeros((2160, 4320))
#-----------------INPUT VARIABLES----------------- no_of_years = 55 season = int(sys.argv[1]) myjob = int(sys.argv[2]) #Get season name #-----------------CALLING FUNCTION 1----------------- season_name = seasonname() #-----------------END CALLING FUNCTION 1----------------- variable_name = 'psl' datadir = '/home/scottyiu/Desktop/work/data/model/50yrs/seasonal/2D/psl/' data = [] results = [] mydiff = np.zeros((145, 192)) el_nino_strength = [-3.0, -2.25, -1.5, -0.75, 0.0, 0.75, 1.50, 2.25, 3.0] #We go by order of El Nino strength data.append((NetCDFFile(datadir + variable_name + '_sea_xlnjj_' + season_name + '_timmean.nc'))) data.append((NetCDFFile(datadir + variable_name + '_sea_xlnji_' + season_name + '_timmean.nc'))) data.append((NetCDFFile(datadir + variable_name + '_sea_xlnjh_' + season_name + '_timmean.nc'))) data.append((NetCDFFile(datadir + variable_name + '_sea_xlnjg_' + season_name + '_timmean.nc'))) data.append((NetCDFFile(datadir + variable_name + '_sea_xlnja_' + season_name + '_timmean.nc'))) data.append((NetCDFFile(datadir + variable_name + '_sea_xlnjc_' + season_name + '_timmean.nc'))) data.append((NetCDFFile(datadir + variable_name + '_sea_xlnjd_' + season_name + '_timmean.nc'))) data.append((NetCDFFile(datadir + variable_name + '_sea_xlnje_' + season_name + '_timmean.nc'))) data.append((NetCDFFile(datadir + variable_name + '_sea_xlnjf_' + season_name +
filename5 = '/data/scihub-users/giyoung/PWRF_V3.6.1/RUNS/MAC_WRF/56_DeMott_WATSAT_10xHM_noThresh_eta70_MYNN/wrfout_d02_2015-11-27_00:00:00' ################################### # Extract domain number: ################################### domainno_start = filename1.find('/wrfout_') + 8 domainno_end = filename1.find('_2015',domainno_start) domainno1 = filename1[domainno_start:domainno_end] del domainno_end, domainno_start ################################### # LOAD NETCDF FILE ################################### nc1 = NetCDFFile(filename1, 'r') nc2 = NetCDFFile(filename2, 'r') nc3 = NetCDFFile(filename3, 'r') nc4 = NetCDFFile(filename4, 'r') nc5 = NetCDFFile(filename5, 'r') ################################### # DEFINE TEMPERATURE BINNING ################################### min218 = 264 max218 = 271 T3D = np.arange(np.round(min218),np.round(max218),0.2) prams = params(T3D)
#!/usr/bin/python # apply hierarchical clustering to output cusize based on the 'Interaction Potential' from netCDF4 import Dataset as NetCDFFile from matplotlib import pyplot as plt from scipy.cluster.hierarchy import dendrogram, linkage, fcluster, ward import numpy as np from scipy.spatial import distance from haversine import haversine_V cusize = NetCDFFile('/home/vanlaar/HDCP2data/TA_dom4/cusize_output_time41.nc') # Import data from netcdf file: cloudlon = cusize.variables['cloud_lon'] cloudlat = cusize.variables['cloud_lat'] nclouds_cusize = cusize.variables['nclouds'] cloud_bin = cusize.variables['cloud_bin'] size = cusize.variables['size'] nclouds = int(nclouds_cusize[0]) # Adjust data format for later use: cloud_lon = cloudlon[0, 0:nclouds] cloud_lat = cloudlat[0, 0:nclouds] cloud_size = cloud_bin[0, :nclouds] * size[0] cloudcentres = np.vstack((cloud_lon, cloud_lat, cloud_size)).T # Compute distances for all pairs based on White et al 2018: Y = distance.pdist(cloudcentres, haversine_V) # Compute linkage matrix:
from mpl_toolkits.basemap import Basemap, cm, shiftgrid, interp from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata region1 = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/HistoricalGLM_crop_150901.nc', 'r') landfrac = region1.variables['landfrac'][:] lonisam1 = region1.variables['lon'][:] maitrop = region1.variables['soy_trop'][95:105, :, :] maitemp = region1.variables['soy_temp'][95:105, :, :] maitropi = region1.variables['soy_trop_irrig'][95:105, :, :] maitempi = region1.variables['soy_temp_irrig'][95:105, :, :] maitrop = ma.masked_where(maitrop <= 0, maitrop) maitrop = ma.filled(maitrop, fill_value=0.) maitemp = ma.masked_where(maitemp <= 0, maitemp) maitemp = ma.filled(maitemp, fill_value=0.) maitropi = ma.masked_where(maitropi <= 0, maitropi) maitropi = ma.filled(maitropi, fill_value=0.) maitempi = ma.masked_where(maitempi <= 0, maitempi) maitempi = ma.filled(maitempi, fill_value=0.) maizetro = maitrop + maitropi maizetem = maitemp + maitempi maizetor = maitrop + maitemp maizetoi = maitropi + maitempi
from mpl_toolkits.basemap import Basemap, cm, shiftgrid,interp from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata from scipy.stats import ttest_ind region1=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/RCP85_crop_150901.nc','r') maitrop = region1.variables['soy_trop'][4,:,:] maitemp = region1.variables['soy_temp'][4,:,:] maitropi = region1.variables['soy_trop_irrig'][4,:,:] maitempi = region1.variables['soy_temp_irrig'][4,:,:] maitrop= ma.masked_where(maitrop<=0,maitrop) maitropi= ma.masked_where(maitropi<=0,maitropi) maitemp= ma.masked_where(maitemp<=0,maitemp) maitempi= ma.masked_where(maitempi<=0,maitempi) maitrop=ma.filled(maitrop, fill_value=0.) maitropi=ma.filled(maitropi,fill_value=0.) maitemp=ma.filled(maitemp, fill_value=0.) maitempi=ma.filled(maitempi, fill_value=0.) maizeto = maitrop+maitemp maizetoi = maitropi+maitempi maitoatemp=maitemp+maitempi maitoatrop=maitrop+maitropi maizetotal = maizeto+maizetoi clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/soytrop_rcp85_constco2_rf_nofert_0.5x0.5.nc','r') #clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/maizetrop_rcp45_co2_rf_nofert_0.5x0.5.nc','r') #clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/maizetrop_rcp45_co2_rf_fert_0.5x0.5.nc','r') #clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp45/maizetrop_rcp45_co2_irrig_fert_0.5x0.5.nc','r')
from mpl_toolkits.basemap import Basemap, cm, shiftgrid, interp from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata import matplotlib.colors as colors from statsmodels.stats.weightstats import DescrStatsW from scipy.stats import ttest_ind from matplotlib.markers import TICKDOWN import matplotlib.patches as mpatches area = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf_isam.nc', 'r') gridarea = area.variables['cell_area'][:, :] gridlon = area.variables['lon'][:] gridlat = area.variables['lat'][:] gridarea, gridlon1 = shiftgrid(180.5, gridarea, gridlon, start=False) #print gridlon nclu = NetCDFFile( '/scratch2/scratchdirs/tslin2/plot/globalcrop/data/m3yield_isam.nc', 'r') ncvar_maize = nclu.variables['maize_total'][0, :, :] ncvar_soy = nclu.variables['soy_total'][0, :, :] areamaize = nclu.variables['maize_area'][0, :, :] areasoy = nclu.variables['soy_area'][0, :, :] latnc = nclu.variables['lat'][:] lonnc = nclu.variables['lon'][:] ncvar_maize, gridlon1 = shiftgrid(180.5, ncvar_maize, gridlon, start=False)
return clmy,areall,clmall areaa= N.zeros((115, 360, 720)) yiep= N.zeros((115, 360, 720)) yief= N.zeros((115, 360, 720)) years = range(1901, 2016) for i, yeara in enumerate(years): yie=yieldout(yeara) yief[i, :, :] = yie[0] areaa[i,:,:]=yie[1] yiep[i,:,:]=yie[2] area=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf_isam.nc','r') gridarea = area.variables['cell_area'][:,:] gridlon = area.variables['lon'][:] gridlat=area.variables['lat'][:] gridarea,gridlon = shiftgrid(180.5,gridarea,gridlon,start=False) #print gridlon gg= N.zeros((115, 360, 720)) for i in range(0,115): gg[i,:,:]=gridarea[:,:] yiey=yiep/(gg*0.0001) ncfile=NetCDFFile('isamhiscru_soy_luh2.nc','w',format='NETCDF3_64BIT_OFFSET') ncfile.createDimension('lat', 360) ncfile.createDimension('lon', 720) ncfile.createDimension('time', 115)
def draw_velocity_map_with_level(nc_file_name, level): nc = NetCDFFile(nc_file_name, 'r+') lat = nc.variables['nav_lat_grid_U'][:] lon = nc.variables['nav_lon_grid_U'][:] velocity = np.zeros((400, 1100), dtype=np.float32) time = 0 x_vel = nc.variables['vozocrtx'][:][time][level] y_vel = nc.variables['vomecrty'][:][time][level] velocity = data.calculate_velocity_magnitude_matrix(x_vel, y_vel) lat_left_bottom = lat[-1][-1] lon_left_bottom = lon[-1][-1] lat_right_top = lat[0][0] lon_right_top = lon[0][0] lat_center = 90 # 110, 119 lon_center = 110 m = Basemap(projection='stere', lon_0=lon_center, lat_0=lat_center, resolution='l', llcrnrlat=lat_left_bottom, llcrnrlon=lon_left_bottom, urcrnrlat=lat_right_top, urcrnrlon=lon_right_top) m.pcolormesh(lon, lat, velocity, latlon=True, cmap='jet', vmax=0.6) m.drawcoastlines() m.drawcountries() m.fillcontinents(color='#cc9966', lake_color='#99ffff') plt.colorbar() plt.title(nc_file_name) model = load_model("samples/model.h5") for y in range(0, 400, 100): for x in range(0, 1100, 100): sample = np.zeros((1, 100, 100, 1), dtype=np.float32) sample[0] = np.expand_dims(velocity[y:y + 100, x:x + 100], axis=2) # print(sample) result = model.predict(sample) result_x, result_y = m(lon[y + 50][x + 50], lat[y + 50][x + 50]) max_x, max_y = m(lon[y + 70][x + 50], lat[y + 70, x + 50]) plt.text(result_x, result_y, str(result[0][0]), ha='center', size=10, color="yellow", bbox=dict(facecolor='black', alpha=0.5, edgecolor='black')) plt.text(max_x, max_y, np.max(sample[0]), ha='center', size=10, color="yellow") if result[0][0] > 0.5: lat_poly = np.array([ lat[y][x], lat[y][x + 99], lat[y + 99][x + 99], lat[y + 99][x] ]) lon_poly = np.array([ lon[y][x], lon[y][x + 99], lon[y + 99][x + 99], lon[y + 99][x] ]) mapx, mapy = m(lon_poly, lat_poly) points = np.zeros((4, 2), dtype=np.float32) for j in range(0, 4): points[j][0] = mapx[j] points[j][1] = mapy[j] poly = Polygon(points, facecolor='green', alpha=0.4) plt.gca().add_patch(poly) plt.show()
def yieldout(year): bb=year-1900 bb1=year-850 region1=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/luh2area_850_2015_corrcrop.nc','r') maitrop1 = region1.variables['fsoy_rf'][bb1,:,:] maitropi1=region1.variables['fsoy_irr'][bb1,:,:] maitrop1=ma.masked_where(maitrop1<=0,maitrop1) maitrop1=ma.filled(maitrop1, fill_value=0.) maitropi1=ma.masked_where(maitropi1<=0,maitropi1) maitropi1=ma.filled(maitropi1, fill_value=0.) lonisam=region1.variables['lon'][:] maitrop,lonisam1 = shiftgrid(180.5,maitrop1,lonisam,start=False) maitropi,lonisam1 = shiftgrid(180.5,maitropi1,lonisam,start=False) maizetor=maitrop maizetoi=maitropi maizetrop=maitrop+maitropi maizeto = maitrop+maitropi isam=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/maisoy_cheyenne/his_cru/soy_fert/output/soy_fert.nc','r') clmtropf = isam.variables['totalyield'][bb-1,:,:] clm2=NetCDFFile('/scratch2/scratchdirs/tslin2/isam/maisoy_cheyenne/his_cru/soy_irr_fert/output/soy_irr_fert.nc','r') clmtropfi = clm2.variables['totalyield'][bb-1,:,:] lonisam=clm2.variables['lon'][:] clmtropf,lonisam1 = shiftgrid(180.5,clmtropf,lonisam,start=False) clmtropfi,lonisam1 = shiftgrid(180.5,clmtropfi,lonisam,start=False) print lonisam1 clmtropf= ma.masked_where(maitrop<=0,clmtropf) clmtropf=ma.filled(clmtropf, fill_value=0.) clmtropfi= ma.masked_where(maitropi<=0,clmtropfi) clmtropfi=ma.filled(clmtropfi, fill_value=0.) yield_clmtf=clmtropf yield_clmtf = ma.masked_where(yield_clmtf<=0,yield_clmtf) yield_clmtf=ma.filled(yield_clmtf, fill_value=0.) yield_clmtfi=clmtropfi yield_clmtfi = ma.masked_where(yield_clmtfi<=0,yield_clmtfi) yield_clmtfi=ma.filled(yield_clmtfi, fill_value=0.) area=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/gridareahalf_isam.nc','r') gridarea = area.variables['cell_area'][:,:] gridlon = area.variables['lon'][:] gridlat=area.variables['lat'][:] gridarea,gridlon = shiftgrid(180.5,gridarea,gridlon,start=False) lon2,lat2 = N.meshgrid(gridlon,gridlat) map = Basemap(projection ='cyl', llcrnrlat=-65, urcrnrlat=90,llcrnrlon=-180, urcrnrlon=180, resolution='c') x,y = map(lon2,lat2) yield_clmtf=maskoceans(x,y,yield_clmtf) yield_clmtf = ma.masked_where(maizeto<=0,yield_clmtf) yield_clmtfi=maskoceans(x,y,yield_clmtfi) yield_clmtfi = ma.masked_where(maizeto<=0,yield_clmtfi) clmy=((yield_clmtf*maizetor)+(yield_clmtfi*maizetoi))/((maizetoi)+(maizetor)) areall=(maizetoi)+(maizetor) clmall=((yield_clmtf*maizetor)+(yield_clmtfi*maizetoi)) return clmy,areall,clmall
11.05.2016 """ from netCDF4 import Dataset as NetCDFFile from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import numpy as np # Fontsize font = 15 wrf_path = '/global/work/blasterdalen/WRF_Dec/wrfout_d03_2014-12-10_00:00:00' geo_path = '/home/blasterdalen/Ymse/geo_em.d03.nc' nc_wrf = NetCDFFile(wrf_path, mode='r') nc_geo = NetCDFFile(geo_path, mode='r') # Extracting 3-D variables (time, south-north, west-east) from WRF time = 72 # time of interest lon = nc_wrf.variables['XLONG'][time,:,:] lat = nc_wrf.variables['XLAT'][time,:,:] # 4-D variables (time, eta, lon, lat) eta = 4 # index of 4 gives (normally) height 60 meter a.g.l. u = nc_wrf.variables['U'][time,eta,0:300,0:300] v = nc_wrf.variables['V'][time,eta,0:300,0:300] # Extracting top. height from geo-file lon_geo = nc_geo.variables['XLONG_M'][0,:,:] lat_geo = nc_geo.variables['XLAT_M'][0,:,:] hgt = nc_geo.variables['HGT_M'][0,:,:]
from mpl_toolkits.basemap import Basemap, cm, shiftgrid,interp from netCDF4 import Dataset as NetCDFFile import numpy as N import matplotlib.pyplot as plt import glob import numpy.ma as ma from scipy.interpolate import griddata from scipy.stats import ttest_ind region1=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/RCP85_crop_150901.nc','r') maitrop = region1.variables['maize_trop'][4,:,:] maitemp = region1.variables['maize_temp'][4,:,:] maitropi = region1.variables['maize_trop_irrig'][4,:,:] maitempi = region1.variables['maize_temp_irrig'][4,:,:] maitrop= ma.masked_where(maitrop<=0,maitrop) maitropi= ma.masked_where(maitropi<=0,maitropi) maitemp= ma.masked_where(maitemp<=0,maitemp) maitempi= ma.masked_where(maitempi<=0,maitempi) maitrop=ma.filled(maitrop, fill_value=0.) maitropi=ma.filled(maitropi,fill_value=0.) maitemp=ma.filled(maitemp, fill_value=0.) maitempi=ma.filled(maitempi, fill_value=0.) maizeto = maitrop+maitemp maizetoi = maitropi+maitempi maitoatemp=maitemp+maitempi maitoatrop=maitrop+maitropi maizetotal = maizeto+maizetoi #clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetrop_rcp85_constco2_rf_nofert_0.5x0.5.nc','r') #clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetrop_rcp85_co2_rf_nofert_0.5x0.5.nc','r') #clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetrop_rcp85_co2_rf_fert_0.5x0.5.nc','r') clm=NetCDFFile('/scratch2/scratchdirs/tslin2/plot/globalcrop/data/clm/clm45rcp85/maizetrop_rcp85_co2_irrig_fert_0.5x0.5.nc','r')