Beispiel #1
0
def plot_grid_box(startY, endY, lat_boxsize, lon_boxsize, lat_min, lat_max,
                  lon_min, lon_max, lats, lons, variablename, error_variable,
                  clim_filename, dirname):
    ### get box info
    nx, ny, box_lat_start, box_lat_end, box_lon_start, box_lon_end = data_info.getboxinfo(
        lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min, lon_max, lats,
        lons)

    cmap = plt.get_cmap("jet")
    cmap.set_under("black")
    cmap.set_over("DarkViolet")
    clevs = np.linspace(270, 310, 21)

    parallels = np.arange(-90, 90 + 5, 5)
    meridians = np.arange(0, 360 + 5, 5)

    fig = plt.figure(figsize=(6, 4), dpi=300)

    map = Basemap(projection='cyl',
                  resolution='i',
                  llcrnrlon=lon_min,
                  llcrnrlat=lat_min,
                  urcrnrlon=lon_max,
                  urcrnrlat=lat_max)

    map.drawcoastlines(linewidth=0.25)
    map.drawcountries(linewidth=0.25)
    map.fillcontinents(color='0.8')
    map.drawparallels(parallels, labels=[1, 0, 0, 0], fontsize=6)
    map.drawmeridians(meridians, labels=[1, 0, 0, 1], fontsize=6)

    for i in range(1, ny + 1):
        for j in range(1, nx + 1):
            x, y = map([
                lon_min + (i - 1) * lon_boxsize, lon_min + i * lon_boxsize,
                lon_min + i * lon_boxsize, lon_min +
                (i - 1) * lon_boxsize, lon_min + (i - 1) * lon_boxsize
            ], [
                lat_min + j * lat_boxsize, lat_min + j * lat_boxsize, lat_min +
                (j - 1) * lat_boxsize, lat_min +
                (j - 1) * lat_boxsize, lat_min + j * lat_boxsize
            ])

            map.plot(x, y, color='r', linewidth=2)

            plt.text((min(x) + max(x)) * 0.5, (min(y) + max(y)) * 0.5,
                     str(j) + 'x' + str(i),
                     fontsize=14,
                     fontweight='bold',
                     ha='center',
                     va='center',
                     color='r')

    plt.title('Data Region Division (' + str(lat_boxsize) + 'x' +
              str(lon_boxsize) + ' degree)',
              fontsize=10,
              fontweight='bold')

    plt.show()
Beispiel #2
0
def plot_line_box_climatology(lat_boxsize, lon_boxsize, lat_min, lat_max,
                              lon_min, lon_max, lats, lons, variablename,
                              clim_filename, outfilename):

    ### get box info
    nx, ny, box_lat_start, box_lat_end, box_lon_start, box_lon_end = data_info.getboxinfo(
        lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min, lon_max, lats,
        lons)

    # Read in climatology
    ncin = Dataset(outfilename, 'r')
    daynumber = ncin.variables['daynumber'][:]
    sst_box_average = ncin.variables['sst_box_average'][:]
    ncin.close()

    ### setup plot
    fig = plt.figure(figsize=(1.5 * ny, 1.2 * nx))
    fig.suptitle('Climatology in Each Grid Box (' + str(lat_boxsize) + 'x' +
                 str(lon_boxsize) + ' degree)',
                 fontsize=16,
                 fontweight='bold')
    plt.subplots_adjust(bottom=0.05, left=.03, right=.99, top=.95, hspace=.02)

    cplot = 1
    for j in range(nx - 1, -1, -1):
        for i in range(0, ny):
            ax = plt.subplot(nx, ny, cplot)
            ax.grid(True)
            plt.plot(daynumber, sst_box_average[:, j, i])
            plt.xlim(np.min(daynumber), np.max(daynumber))
            plt.ylim(np.nanmin(sst_box_average), np.nanmax(sst_box_average))
            if i == 0:
                plt.ylabel(variablename, fontsize='medium')
            if j == 0:
                plt.xlabel('Day Number', fontsize='medium')
            if i > 0:
                ax.set_yticklabels([])
            if j != 0:
                ax.set_xticklabels([])

            plt.text(
                (np.min(daynumber) + np.max(daynumber)) * 0.5,
                (np.nanmin(sst_box_average) + np.nanmax(sst_box_average)) *
                0.5,
                str(j + 1) + 'x' + str(i + 1),
                fontsize=10,
                fontweight='bold',
                ha='center',
                va='center',
                color='r')
            cplot = cplot + 1

    plt.show()
Beispiel #3
0
def climatologybox_chlor(lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min, lon_max, lats, lons, variablename, clim_filename, outfilename):

  nstep = 1
  nd = 366

  ### get box info
  nx, ny, box_lat_start, box_lat_end, box_lon_start, box_lon_end = data_info.getboxinfo(lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min, lon_max, lats, lons)

  # Read in climatology
  print(clim_filename)
  ncin = Dataset(clim_filename, 'r')
  sst_clim = ncin.variables['chlor_climatology'][:]
  ncin.close()

  ### create box average of parameters
  sst_avg=np.empty((nd, nx, ny))

  for k in range(0, nd):
       for ii in range(0, nx):
          for jj in range(0, ny):
             atemp = sst_clim[int(box_lat_start[ii]):int(box_lat_end[ii]):nstep, int(box_lon_start[jj]):int(box_lon_end[jj]):nstep, k-1]
             atemp = atemp.flatten()
             b = (atemp < -0.0)
             if np.count_nonzero(b) > 0:
               atemp[b]=np.nan
             sst_avg[k,ii,jj] = np.nanmean(atemp)

  fid = Dataset(outfilename,'w')
  # Define the dimensions
  nx = fid.createDimension('nx', nx) # Unlimited
  ny = fid.createDimension('ny', ny) # Unlimited
  day = fid.createDimension('daynumber', 366) # Unlimited

  nc_var = fid.createVariable('daynumber', 'i4',('daynumber'),zlib=True)
  fid.variables['daynumber'][:] = range(1, 367)
  fid.variables['daynumber'].standard_name='day number'
  fid.variables['daynumber'].units='day number'
  fid.variables['daynumber'].axis='T'
  fid.variables['daynumber'].comment="Starting on Jan 1 of earch year with day number 1 until the end of the year with either day number 365 or 366 depending on the leap year status."

  nc_var = fid.createVariable('chlor_box_average', 'f8',('daynumber', 'nx', 'ny'),zlib=True)
  fid.variables['chlor_box_average'][:] = sst_avg[:,:,:]
  fid.variables['chlor_box_average'].standard_name='Data Average in the Box'
  fid.variables['chlor_box_average'].units='kelvin'

  fid.close()
Beispiel #4
0
def plot_clim_box_contour(lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min,
                          lon_max, lats, lons, variablename, error_variable,
                          clim_filename):
    ### get box info
    nx, ny, box_lat_start, box_lat_end, box_lon_start, box_lon_end = data_info.getboxinfo(
        lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min, lon_max, lats,
        lons)

    #*** read climatology file
    ncin = Dataset(clim_filename, 'r')
    data = ncin.variables['sst_climatology'][:]
    lons = ncin.variables['lon'][:]
    lats = ncin.variables['lat'][:]
    ncin.close()

    parallels = np.arange(-90, 90 + 5, 5)
    meridians = np.arange(0, 360 + 5, 5)

    cmap = plt.get_cmap("jet")
    cmap.set_under("black")
    cmap.set_over("DarkViolet")
    clevs = np.linspace(270, 310, 21)

    fig = plt.figure(figsize=(6, 4), dpi=300)

    map = Basemap(projection='cyl',
                  resolution='i',
                  llcrnrlon=lon_min,
                  llcrnrlat=lat_min,
                  urcrnrlon=lon_max,
                  urcrnrlat=lat_max)

    map.drawcoastlines(linewidth=0.25)
    map.drawcountries(linewidth=0.25)
    map.fillcontinents(color='0.8')
    map.drawparallels(parallels, labels=[1, 0, 0, 0], fontsize=6)
    map.drawmeridians(meridians, labels=[1, 0, 0, 1], fontsize=6)

    x, y = map(*np.meshgrid(lons, lats))
    cs = map.contourf(x,
                      y,
                      data[:, :, 0].squeeze(),
                      clevs,
                      cmap=cmap,
                      extend='both')

    cb = map.colorbar(cs, 'right', size='2%', pad='0.5%')
    cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=6)
    cb.set_label('SST (K)', fontsize=7, fontweight="bold")
    cb.set_ticks(range(270, 315, 5))

    for i in range(1, ny + 1):
        for j in range(1, nx + 1):

            x, y = map([
                lon_min + (i - 1) * lon_boxsize, lon_min + i * lon_boxsize,
                lon_min + i * lon_boxsize, lon_min +
                (i - 1) * lon_boxsize, lon_min + (i - 1) * lon_boxsize
            ], [
                lat_min + j * lat_boxsize, lat_min + j * lat_boxsize, lat_min +
                (j - 1) * lat_boxsize, lat_min +
                (j - 1) * lat_boxsize, lat_min + j * lat_boxsize
            ])

            map.plot(x, y, color='r', linewidth=1)

            x, y = map([
                lon_min + (i - 1) * lon_boxsize, lon_min + i * lon_boxsize,
                lon_min + i * lon_boxsize, lon_min +
                (i - 1) * lon_boxsize, lon_min + (i - 1) * lon_boxsize
            ], [
                lat_min + j * lat_boxsize, lat_min + j * lat_boxsize, lat_min +
                (j - 1) * lat_boxsize, lat_min +
                (j - 1) * lat_boxsize, lat_min + j * lat_boxsize
            ])

            map.plot(x, y, color='r', linewidth=2)

            plt.text((min(x) + max(x)) * 0.5, (min(y) + max(y)) * 0.5,
                     str(j) + 'x' + str(i),
                     fontsize=14,
                     fontweight='bold',
                     ha='center',
                     va='center',
                     color='r')

    plt.title('Data Region Division (5 degree)',
              fontsize=10,
              fontweight='bold')

    plt.show()
Beispiel #5
0
def plot_line_box_parameter_merged(pname, dataset_name, startY, endY,
                                   lat_boxsize, lon_boxsize, lat_min, lat_max,
                                   lon_min, lon_max, lats, lons, variablename,
                                   error_variable, clim_filename, dirname,
                                   output_dir):

    print("\nPlease wait, reading files ...\n")

    ### get box info
    nx, ny, box_lat_start, box_lat_end, box_lon_start, box_lon_end = data_info.getboxinfo(
        lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min, lon_max, lats,
        lons)

    ### Read in data
    for ii in range(0, nx):
        ### setup plot
        fig = plt.figure(ii + 1, figsize=(0.5 * (endY - startY), 1.5 * ny))
        fig.suptitle(pname + ' in Each Grid Box (' + str(lat_boxsize) + 'x' +
                     str(lon_boxsize) + ' degree)',
                     fontsize=16,
                     fontweight='bold')
        plt.subplots_adjust(bottom=0.05,
                            left=.06,
                            right=.99,
                            top=.95,
                            hspace=.02)
        for jj in range(0, ny):
            ax = plt.subplot(ny, 1, jj + 1)
            yearticks = np.arange(startY, endY + 1)
            plt.xlim([startY, endY + 1])
            plt.ylabel(pname, fontsize='medium')
            plt.xticks(yearticks, fontsize=8, rotation=90)
            plt.yticks(fontsize=8)
            if jj == ny - 1:
                plt.xlabel('Year', fontsize='medium')
            if jj < ny - 1:
                ax.set_xticklabels([])

            tpos = 0.0
            for i in range(startY, endY + 1):
                boxfilename = output_dir + '/' + str(
                    i) + '/' + variablename + '_' + str(
                        i) + '_' + dataset_name + '_' + str(
                            lat_boxsize) + '_degree_box_' + str(
                                jj + 1) + 'x' + str(ii + 1) + '.nc'
                ncin = Dataset(boxfilename, 'r')
                if pname == 'Average':
                    data_pname = ncin.variables['sst_box_average'][:]
                elif pname == 'Anomaly':
                    data_pname = ncin.variables['sst_box_anomaly'][:]
                    plt.plot([startY, endY + 1], [0, 0],
                             color='red',
                             linewidth=1)
                elif pname == 'STD':
                    data_pname = ncin.variables['sst_box_std'][:]
                atime = ncin.variables['time'][:]
                plt.plot(i + atime / float(np.max(atime)), data_pname)
                if np.nanmax(data_pname) == np.nanmax(data_pname):
                    tpos = np.nanmax(data_pname)

            plt.text(endY,
                     tpos,
                     str(jj + 1) + 'x' + str(ii + 1),
                     fontsize=10,
                     fontweight='bold',
                     ha='center',
                     va='center',
                     color='r')

    plt.show()
Beispiel #6
0
def indexProcessing(dataset_name, startY, endY, lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min, lon_max, lats, lons, variablename, error_variable, clim_filename, dirname, output_dir):
  
  nstep = 1
  nd = 366

  ### get box info
  nx, ny, box_lat_start, box_lat_end, box_lon_start, box_lon_end = data_info.getboxinfo(lat_boxsize, lon_boxsize, lat_min, lat_max, lon_min, lon_max, lats, lons)

  ### Read in climatology
  ncin = Dataset(clim_filename, 'r')
  clim = ncin.variables['sst_box_average'][:]
  ncin.close()

  ### setup size for tempory matrix
  nlat0 = math.ceil((box_lat_end[0]-box_lat_start[0])/nstep)
  nlon0 = math.ceil((box_lon_end[0]-box_lon_start[0])/nstep)

  for i in range(startY, endY+1):
    print("Processing Year = " + str(i)+", please wait ...")
    k = 0

    sst_year=np.empty((nd, nlat0, nlon0, nx, ny))
    sst_avg=np.empty((nd, nx, ny))
    sst_std=np.empty((nd, nx, ny))
    sst_anomaly=np.empty((nd, nx, ny))
    error_year=np.empty((nd, nlat0, nlon0, nx, ny))
    mask_year=np.empty((nd, nlat0, nlon0, nx, ny))

    sst_year[:] = np.nan
    sst_avg[:] = np.nan
    sst_std[:] = np.nan
    sst_anomaly[:] = np.nan
    error_year[:] = np.nan
    mask_year[:] = np.nan

    atime=np.empty(nd)

    for j in range(1, nd+1):
     for filename in glob.glob(dirname+'/'+str(i)+'/'+"{0:0>3}".format(j)+'/*.nc'):
       ncfile = filename.rsplit( "/")[ -1 ]
       ncin = Dataset(filename, 'r')
       sst = ncin.variables[variablename][:]
       error = ncin.variables[error_variable][:]
       mask = ncin.variables['mask'][:]
       for ii in range(0, nx):
          for jj in range(0, ny):
             sst_year[k, 0:len(range(int(box_lat_start[ii]),int(box_lat_end[ii]),nstep)), 0:len(range(int(box_lon_start[jj]),int(box_lon_end[jj]),nstep)), ii,jj] = sst[0,int(box_lat_start[ii]):int(box_lat_end[ii]):nstep, int(box_lon_start[jj]):int(box_lon_end[jj]):nstep]
             error_year[k, 0:len(range(int(box_lat_start[ii]),int(box_lat_end[ii]),nstep)), 0:len(range(int(box_lon_start[jj]),int(box_lon_end[jj]),nstep)), ii,jj] = error[0,int(box_lat_start[ii]):int(box_lat_end[ii]):nstep, int(box_lon_start[jj]):int(box_lon_end[jj]):nstep]
             mask_year[k, 0:len(range(int(box_lat_start[ii]),int(box_lat_end[ii]),nstep)), 0:len(range(int(box_lon_start[jj]),int(box_lon_end[jj]),nstep)), ii,jj] = mask[0,int(box_lat_start[ii]):int(box_lat_end[ii]):nstep, int(box_lon_start[jj]):int(box_lon_end[jj]):nstep]
             btemp = mask_year[k, :,:, ii,jj]
             atemp = sst_year[k, :,:, ii,jj]
             atemp = atemp.flatten()
             btemp = btemp.flatten()
             b = (atemp < -250.0) | (atemp > 350.0) | (btemp > 1)
             if np.count_nonzero(b) > 0:
               atemp[b]=np.nan
             sst_avg[k,ii,jj] = np.nanmean(atemp)
             sst_std[k,ii,jj] = np.nanstd(atemp)
             if j == 366:
               sst_anomaly[k,ii,jj] = sst_avg[k,ii,jj] - clim[364,ii,jj]
             else:
               sst_anomaly[k,ii,jj] = sst_avg[k,ii,jj] - clim[j-1,ii,jj]
       ncin.close()
       atime[k] = j
       k = k + 1

    ### create output directory
    data_info.createdir(output_dir+'/'+str(i))

    for ii in range(0, nx):
     for jj in range(0, ny):
        outfilename = output_dir + '/'+str(i)+'/'+variablename+'_'+str(i)+'_'+dataset_name+'_'+str(lat_boxsize)+'_degree_box_'+str(jj+1)+'x'+str(ii+1)+'.nc'
        fid = Dataset(outfilename,'w')
        # Define the dimensions
        nlat = fid.createDimension('lat', math.ceil((box_lat_end[0]-box_lat_start[0])/nstep)) # Unlimited
        nlon = fid.createDimension('lon', math.ceil((box_lon_end[0]-box_lon_start[0])/nstep)) # Unlimited
        time = fid.createDimension('time', k) # Unlimited

        nc_var = fid.createVariable('lat', 'f8',('lat'),zlib=True)
        fid.variables['lat'][:] = lats[int(box_lat_start[ii]):int(box_lat_end[ii]):nstep]
        fid.variables['lat'].standard_name='latitude'
        fid.variables['lat'].long_name='latitude'
        fid.variables['lat'].axis='Y'
        fid.variables['lat'].units='degrees_north'
        fid.variables['lat'].comment='Uniform grid with centers from '+str(lats[int(box_lat_start[ii])])+' to '+str(lats[int(box_lat_end[ii])])+ ' degrees.'

        nc_var = fid.createVariable('lon', 'f8',('lon'),zlib=True)
        fid.variables['lon'][:] = lons[int(box_lon_start[jj]):int(box_lon_end[jj]):nstep]
        fid.variables['lon'].standard_name='longitude'
        fid.variables['lon'].long_name='longitude'
        fid.variables['lon'].units='degrees_east'
        fid.variables['lon'].axis='X'
        fid.variables['lon'].comment='Uniform grid with centers from '+str(lons[int(box_lon_start[jj])])+' to '+str(lons[int(box_lon_end[jj])])+' degrees.'

        nc_var = fid.createVariable('time', 'i4',('time'),zlib=True)
        fid.variables['time'][:] = atime[0:k]
        fid.variables['time'].standard_name='time'
        fid.variables['time'].long_name='day number'
        fid.variables['time'].units='day number'
        fid.variables['time'].axis='T'
        fid.variables['time'].comment="Starting on Jan 1 of earch year with day number 1 until the end of the year with either day number 365 or 366 depending on the leap year status."

        nc_var = fid.createVariable('sst_box_average', 'f8',('time'),zlib=True)
        fid.variables['sst_box_average'][:] = sst_avg[0:k,ii,jj]
        fid.variables['sst_box_average'].standard_name='SST Average in the Box'
        fid.variables['sst_box_average'].units='kelvin'

        nc_var = fid.createVariable('sst_box_anomaly', 'f8',('time'),zlib=True)
        fid.variables['sst_box_anomaly'][:] = sst_anomaly[0:k,ii,jj]
        fid.variables['sst_box_anomaly'].standard_name='SST Anomaly in the Box'
        fid.variables['sst_box_anomaly'].units='kelvin'

        nc_var = fid.createVariable('sst_box_std', 'f8',('time'),zlib=True)
        fid.variables['sst_box_std'][:] = sst_std[0:k,ii,jj]
        fid.variables['sst_box_std'].standard_name='SST Standard Deviation in the Box'
        fid.variables['sst_box_std'].units='kelvin'

        nc_var = fid.createVariable('analysed_sst', 'f8',('time', 'lat', 'lon'),zlib=True,fill_value=-32768)
        fid.variables['analysed_sst'][:] = sst_year[0:k,:,:,ii,jj]
        fid.variables['analysed_sst'].units='kelvin'
        #fid.variables['analysed_sst']._FillValue=-32768
        fid.variables['analysed_sst'].comment='SST defined at all grid points but no physical meaning is ascribed to values over land'
        fid.variables['analysed_sst'].source='https://podaac.jpl.nasa.gov/dataset/CMC0.2deg-CMC-L4-GLOB-v2.0'

        nc_var = fid.createVariable('analysis_error', 'f8',('time', 'lat', 'lon'),zlib=True,fill_value=-32768)
        fid.variables['analysis_error'][:] = error_year[0:k,:,:,ii,jj]
        fid.variables['analysis_error'].units='kelvin'
        fid.variables['analysis_error'].comment='Error defined at all grid points but no meaning is ascribed to values over land'
        fid.variables['analysis_error'].source='https://podaac.jpl.nasa.gov/dataset/CMC0.2deg-CMC-L4-GLOB-v2.0'

        nc_var = fid.createVariable('mask', 'i',('time', 'lat', 'lon'),zlib=True,fill_value=-1)
        fid.variables['mask'][:] = mask_year[0:k,:,:,ii,jj]
        fid.variables['mask'].valid_min = 0
        fid.variables['mask'].valid_max = 31
        fid.variables['mask'].flag_masks = '1b, 2b, 4b, 8b, 16b'
        fid.variables['mask'].flag_meanings = "water land optional_lake_surface sea_ice optional_river_surface"
        fid.variables['mask'].comment='Mask can be used to further filter the data'
        fid.variables['mask'].source='https://podaac.jpl.nasa.gov/dataset/CMC0.2deg-CMC-L4-GLOB-v2.0'

        fid.product_version     = "Version 1.0"
        #fid.spatial_resolution  = str(dlat*nstep)+" degree"
        fid.Conventions         = "CF-1.6"
        #fid.title               = "CMC 0.2 global "+str(dlat*nstep)+" deg daily sea surface temperature analysis using Multi-Resolution Variational Analysis (MRVA) method for interpolation"
        fid.institution         = "Jet Propulsion Laboratory"
        fid.summary             = "Applies the method of statistical interpolation to assimilate observations from in situ and satellite sources using, as the background, the analysis valid 24 hours prior assuming persistence of the anomalies."
        fid.source              = 'https://podaac.jpl.nasa.gov/dataset/CMC0.2deg-CMC-L4-GLOB-v2.0'
        fid.creator_name        = "Ed Armstrong, Yibo Jiang"
        fid.creator_email       = "[email protected]; [email protected]"
        fid.creator_url         = "http;//podaac.jpl.nasa.gov"
        fid.project             = "Phenology of North Atlantic Ocean"
        fid.acknowledgment      = "This project was supported in part by a grant from"
        fid.processing_level    = "L4"
        #fid.date_created        = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
        fid.gds_version_id      = "2.0r5"
        fid.naming_authority    = "org.ghrsst"
        #fid.geospatial_lat_resolution    = str(dlat*nstep)+"f"
        fid.geospatial_lat_units         = "degrees_north"
        #fid.geospatial_lon_resolution    = str(dlat*nstep)+"f"
        fid.geospatial_lon_units         = "degrees_east"
        fid.westernmost_longitude        = str( lons[int(box_lon_start[jj])] )
        fid.easternmost_longitude        = str( lons[int(box_lon_end[jj])] )
        fid.southernmost_latitude        = str( lats[int(box_lat_start[ii])] )
        fid.northernmost_latitude        = str( lats[int(box_lat_end[ii])] )
        fid.cdm_data_type       = "Grid"

        fid.close()