text: # coding: utf-8
  """
  Name: netcdfmagics.py
  
  Read netcdf to plot data
  
  Usage: This is the module.
  
  Author: Ryosuke Tomita
  Date: 2021/05/25
  """
  import os
  from Magics.macro import *
  
  
  class PlotMap:
      """plot netcdf file data to japanmap using magics"""
      def __init__(self, titleName, outName, landColor="on"):
          self.title = mtext(
              text_lines=[titleName],
              text_font_size=0.8,
              text_colour="charcoal",
              text_font_style='bold',
              text_font='Times new Roman',
          )
  
          self.output = output(
              output_formats=['png'],
              output_name=(outName),
              output_name_first_page_number="off",
              output_width=800)
  
          self.center = page(
              layout='automatic',
              page_x_length=27., #If picture is overhanging, use more smaller value.
              page_y_length=20.,
              page_id_line='off',  # erase ECMWF logo↲
              page_x_position=0.,
              page_y_position=0.,
          )
          self.area = mmap(
              subpage_map_projection="cylindrical",
              subpage_upper_right_longitude=180, #150
              subpage_upper_right_latitude=55, #50
              subpage_lower_left_latitude=20, #25
              subpage_lower_left_longitude=120, #120
          )
  
          self.coast = mcoast(
              map_coastline_land_shade=landColor,  # colorの邪魔にならないように
              map_coastline_land_shade_colour="cream",
              map_grid_line_style="dash",
              map_grid_colour="charcoal",
              map_grid_thickness=2.0,
              map_grid_latitude_increment=10,
              map_grid_longitude_increment=10,
              map_label="on",
              map_coastline_colour="black",
              map_coastline_thickness=2.7,
          )
  
          self.plot_content = [""] * 6
          self.plot_content[0] = "self.center,self.title,self.output,self.area,"
  
      def shadePlot(self, shadeFile, physicalParam, legendTitle, colorMax, colorMin):
          self.shade = mnetcdf(
              netcdf_filename=shadeFile,
              netcdf_value_variable=physicalParam,
              netcdf_field_scaling_factor=1,
              netcdf_y_variable="lon",
              netcdf_x_variable="lat",
          )
  
          self.shading = mcont(
              contour="on",
              contour_shade="on",
              contour_shade_method="area_fill",
              contour_automatic_setting="off",
              contour_highlight="off",
              contour_shade_max_level_colour="red",
              contour_shade_min_level_colour="cyan",
              contour_label="off",
              contour_max_level=colorMax,
              contour_min_level=colorMin,
              legend="on")
  
          self.legend = mlegend(
              legend_display_type="continuous",
              legend_automatic_position="right",
              legend_text_colour="black",
              legend_text_font_size=0.5,
              legend_title="on",
              legend_title_text=legendTitle,
              legend_title_font_size=0.8,
              legend_title_orientation="horizontal",
              legend_label_frequency=100,)
  
          self.plot_content[1] = "self.shade,self.shading,"
  
      def contourPlot(self, contourFile, physicalParam, legendTitle, contInterval):
          self.contour = mnetcdf(
              netcdf_filename=contourFile,
              netcdf_value_variable=physicalParam,
              netcdf_field_scaling_factor=1,
              netcdf_y_variable="lon",
              netcdf_x_variable="lat",
          )
  
          self.contouring = mcont(
              contour="on",
              contour_shade="off",
              contour_automatic_setting="off",
              contour_label="on",
              contour_label_height=0.7,
              contour_level_selection_type="interval",
              contour_line_thickness=2,
              contour_line_colour="black",
              contour_highlight_colour="black",
              contour_interval=contInterval,
              legend='on',
              legend_text_font_size="0.7",
              legend_text_colour="black",
              legend_text_font_style="italic",
              legend_text_font='Times new Roman',
              contour_legend_text=legendTitle,
          )
  
          self.plot_content[3] = "self.contour,self.contouring,"
  
      def vectorPlot(self, vectorFile, vectInterval):
          self.vector = mnetcdf(
              netcdf_filename=vectorFile,
              netcdf_field_scaling_factor=0.1,
              netcdf_x_component_variable="u10",
              netcdf_y_component_variable="v10",
              #netcdf_latitude_variable       = "lon",
              #netcdf_longitude_variable      = "lat",
          )
          self.wind_arrows = mwind(
              legend='on',
              wind_field_type='arrows',
              wind_arrow_thickness=1,
              wind_thinning_factor=vectInterval,
              wind_arrow_calm_below=0.0,  # min
              wind_arrow_colour='black',
              wind_arrow_head_ratio=0.5,
              wind_arrow_head_shape=1,)
  
          self.plot_content[4] = "self.vector,self.wind_arrows,"
  
      def plotAll(self, outDir, PWD):
          os.chdir(outDir)
  
          self.plot_content[2] = "self.coast,"
          if self.plot_content[1]:
              self.plot_content[5] = "self.legend,"
          plot_contentStr = "".join(self.plot_content)
          plot(eval(plot_contentStr))
          os.chdir(PWD)
  