2D Map SAA (xfig example)

From Remeis-Wiki
Jump to navigation Jump to search

2D map SAA

require("isisscripts");

% load all the maps I want to plot:
% directory with electron and proton distributions
variable epdir = "/userdata/data/hell/saa_rhessi/karten/";
% directory with the magnetic field information
variable magdir = "/userdata/data/hell/saa_rhessi/magnet/";
% the image names
variable imgname = "/home/hell/Documents/saa_rhessi/map2003_75_570_cont";
variable imgname_e = "/home/hell/Documents/saa_rhessi/map2003_75_570_cont_e";
variable imgname_p = "/home/hell/Documents/saa_rhessi/map2003_75_570_cont_p";
% load the actual data
variable p = fits_read_img(epdir+"2003_75_a570.fits\[5]");
variable e = fits_read_img(epdir+"2003_75_a570.fits\[4]");
variable ep =  fits_read_img(epdir+"2003_75_a570.fits\[6]");
variable mag = fits_read_img(magdir+"bfield-570-2003.75.fits");

% define the values of the contour levels for the magnetic field
variable levels = int( [[19e3:27.5e3:2e3],[30e3:56e3:3e3]] );
% calculate the contours
variable cont = gcontour_compute(mag,levels);

% define a function to put the contour labels at a position in the map, 
% where they hopefully do not look stupid, i.e., not too close to the edges
% of the plot or too pointy slopes of the curve.
% Here I go for the point of the contour which is farthest south
define cont_label(xf,xar,yar,level) %push xar by -180, yar by -90 !
{
   variable lx = length(xar);
   if( lx < 20)
     {
        xf.plot(xar,yar;width=1);
        return;
     }
   variable mx = where_min(yar);
   mx = mx[length(mx)/2];
   
   % leave enough space for the label, i.e., not to close to the axes
   if(mx < 10 or mx > lx-11) mx = where_max(yar);
   if(mx < 10 or mx > lx-11) mx = int(lx/4.);
   variable er = [mx-13:mx+14];
   
   % rotate the label such that it is parallel to the line
   variable x0 = xar[mx][0];
   variable y0 = yar[mx][0];
   variable rot = atan( (yar[mx-13]-yar[mx+14])/(xar[mx-13]-xar[mx+14]) ) *180./PI ;
   if (isnan(rot) ) rot = 0;
   variable label = xfig_new_text( string(level); rotate=rot, size="\scriptsize"R);
   xf.add_object( label ,x0,y0,0,0 );

   % for better legibility, draw a white rectangle between the map and the label
   variable xx = [0,0.95,0.95,0];
   variable yy = [0,0,.205,.205];
   variable xcorner = xx*cos(rot*PI/180.) - yy*sin(rot*PI/180.);
   variable ycorner = xx*sin(rot*PI/180.) + yy*cos(rot*PI/180.);
   xf.add_object( xfig_new_polyline( xcorner,ycorner;
                                     color="white", fillcolor="white"),
                  x0,y0,0,-.05);
   
   % finally, also plot the contour
   xf.plot(xar,yar;width=1);
}


% function to draw the map, the contours and the worldcoastlines, add x- and
% y-axes and a color scale
define draw_map_cont(image,cont,lev,filename, scaletitle,minv,maxv, scale)
{
   require("isisscripts");
   require("xfig");
   require("png");
   variable width = 15;
   variable height = 10;
   
   % define a rainbow color map, starting at white over purple-blue-green-yellow-orange to red.
   % array with 256 colors
   variable color =
    [
       0xFFFFFF, 0xD3D3FF, 0xC9C9FF, 0xC4C4FF, 0xBABAFF, 0xB5B5FF, 0xB0B0FF, 0xABABFF,
       0xA6A6FF, 0xA1A1FF, 0x9E9EFF, 0x9C9CFF, 0x9797FF, 0x9494FF, 0x9292FF, 0x8D8DFF,
       0x8A8AFF, 0x8888FF, 0x8383FF, 0x8080FF, 0x7E7EFF, 0x7979FF, 0x7676FF, 0x7474FF,
       0x6F6FFF, 0x6C6CFF, 0x6A6AFF, 0x6565FF, 0x6262FF, 0x6060FF, 0x5B5BFF, 0x5858FF,
       0x5656FF, 0x5151FF, 0x4E4EFF, 0x4C4CFF, 0x4747FF, 0x4444FF, 0x4242FF, 0x3D3DFF,
       0x3838FF, 0x3333FF, 0x2E2EFF, 0x2929FF, 0x2424FF, 0x1F1FFF,
       0x1A1AFF, 0x1515FF, 0x1010FF, 0x0B0BFF, 0x0606FF, 0x0000FF, 0x0006FF, 0x000BFF,
       0x0010FF, 0x0015FF, 0x001AFF, 0x001FFF, 0x0024FF, 0x0029FF, 0x002EFF, 0x0033FF,
       0x0038FF, 0x003DFF, 0x0042FF, 0x0047FF, 0x004CFF, 0x0051FF, 0x0056FF, 0x005BFF,
       0x0060FF, 0x0065FF, 0x006AFF, 0x006FFF, 0x0074FF, 0x0079FF, 0x007EFF, 0x0083FF,
       0x0088FF, 0x008DFF, 0x0092FF, 0x0097FF, 0x009CFF, 0x00A1FF, 0x00A6FF, 0x00ABFF,
       0x00B0FF, 0x00B5FF, 0x00BAFF, 0x00BFFF, 0x00C4FF, 0x00C9FF, 0x00CEFF, 0x00D3FF,
       0x00D8FF, 0x00DDFF, 0x00E2FF, 0x00E7FF, 0x00ECFF, 0x00F1FF, 0x00F6FF, 0x00FBFF,
       0x00FFFF, 0x00FFF6, 0x00FFF1, 0x00FFEC, 0x00FFE7, 0x00FFE2, 0x00FFDD, 0x00FFD8,
       0x00FFD3, 0x00FFCE, 0x00FFC9, 0x00FFC4, 0x00FFBF, 0x00FFBA, 0x00FFB5, 0x00FFB0,
       0x00FFAB, 0x00FFA6, 0x00FFA1, 0x00FF9C, 0x00FF97, 0x00FF92, 0x00FF8D, 0x00FF8D,
       0x00FF88, 0x00FF83, 0x00FF7E, 0x00FF79, 0x00FF74, 0x00FF6F, 0x00FF6A, 0x00FF65,
       0x00FF60, 0x00FF5B, 0x00FF56, 0x00FF51, 0x00FF4C, 0x00FF47, 0x00FF42, 0x00FF3D,
       0x00FF38, 0x00FF33, 0x00FF2E, 0x00FF29, 0x00FF24, 0x00FF1F, 0x00FF1A, 0x00FF15,
       0x00FF10, 0x00FF0B, 0x00FF06, 0x00FF00, 0x06FF00, 0x0BFF00, 0x10FF00, 0x15FF00,
       0x1AFF00, 0x1FFF00, 0x24FF00, 0x29FF00, 0x2EFF00, 0x33FF00, 0x38FF00, 0x3DFF00,
       0x42FF00, 0x47FF00, 0x4CFF00, 0x51FF00, 0x56FF00, 0x5BFF00, 0x60FF00, 0x65FF00,
      0x6AFF00, 0x6FFF00, 0x74FF00, 0x79FF00, 0x7EFF00, 0x83FF00, 0x88FF00, 0x8DFF00,
       0x92FF00, 0x97FF00, 0x9CFF00, 0xA1FF00, 0xA6FF00, 0xABFF00, 0xB0FF00, 0xB5FF00,
       0xBAFF00, 0xBFFF00, 0xC4FF00, 0xC9FF00, 0xCEFF00, 0xD3FF00, 0xD8FF00, 0xDDFF00,
       0xE2FF00, 0xE7FF00, 0xECFF00, 0xF1FF00, 0xF6FF00, 0xFBFF00, 0xFFFF00, 0xFFFB00,
       0xFFF600, 0xFFF100, 0xFFEC00, 0xFFE700, 0xFFE200, 0xFFDD00, 0xFFD800, 0xFFD300,
       0xFFCE00, 0xFFC900, 0xFFC400, 0xFFBF00, 0xFFBA00, 0xFFB500, 0xFFB000, 0xFFAB00,
       0xFFA600, 0xFFA100, 0xFF9C00, 0xFF9700, 0xFF9200, 0xFF8D00, 0xFF8800, 0xFF8300,
       0xFF7E00, 0xFF7900, 0xFF7400, 0xFF6F00, 0xFF6A00, 0xFF6500, 0xFF6000, 0xFF5B00,
       0xFF5600, 0xFF5100, 0xFF4C00, 0xFF4700, 0xFF4200, 0xFF3D00, 0xFF3800, 0xFF3300,
       0xFF2E00, 0xFF2900, 0xFF2400, 0xFF1F00, 0xFF1A00, 0xFF1500, 0xFF1000, 0xFF0B00,
       0xFF0600, 0xFF0000
     ];
   png_add_colormap ("rainbow_white", color);

   variable imagergb, scalearr, scalergb;
   variable m1 = xfig_plot_new (width, height);
   variable m2 = xfig_plot_new (0.5,height);

   % number of bins in either direction of the image
   variable nblat = length(image[*,0]);
   variable nblon = length(image[0,*]);
   
   % scaling (logarithmic or linear) of the color map
   if (scale == "log")
     {
        % convert the values in the image to colors
        imagergb = png_gray_to_rgb (log10(image),"rainbow_white";gmin = log10(minv), gmax = log10(maxv)) ;
        % save the image as png with png_write_flipped as the corner (0,0) of the image is top left, 
        % but my world map scaling starts at the bottom left corner of the array (or something like that)
        png_write_flipped (filename+".png",imagergb[[0:nblat-1],[0:nblon-1]]) ;

        % create the key for the color scale
        scalearr = _reshape([log10(minv):log10(maxv):#256],[256,1]);
        scalergb = png_gray_to_rgb (scalearr, "rainbow_white");
        png_write_flipped ("scale.png", scalergb);

        m2.world (0, 1, minv, maxv; logy);
     }
   else if (scale == "lin")
     {
        imagergb = png_gray_to_rgb (image,"rainbow_white"; gmin = minv, gmax = maxv) ;
        png_write_flipped (filename+".png",imagergb[[0:nblat-1],[0:nblon-1]]) ;

        scalearr = _reshape([minv:maxv:#256],[256,1]);
        scalergb = png_gray_to_rgb (scalearr, "rainbow_white");
        png_write_flipped ("scale.png", scalergb);

        m2.world (0, 1, minv, maxv);
        %m2.world (minv, maxv, 0, 1);
     }
   else
     {
        ()=printf("Invalid scale: use \"log\" or \"lin\" \n");
        return;
     }

   % define the world coordinates for the image
   m1.world(-180,180, -90,90 ; xlin , ylin) ;
   m1.xlabel("Longitude") ;
   m1.ylabel("Latitude") ;
   % load the png image to frame with the coordinate system
   m1.plot_png(filename+".png") ;

   % define horizontal lines lines to indicate the tropes and the polar regions
   variable fineness = 3600;
   variable tropics = struct{lon=[-180:179:#fineness], equat = ones(fineness)*0.,
                             cancer = ones(fineness)*23.43, capricorn = ones(fineness)*(-23.43),
                             arctic_north = ones(fineness)*66.55, arctic_south = ones(fineness)*(-66.55)};

   xfig_new_color("grey",0x808080);
   variable cl;
   % plot the world coast line
   foreach cl(WorldCoastLine())
     {
        m1.plot(cl.x, cl.y; width = 1, color="grey");
     }
   m1.plot(tropics.lon, tropics.equat ; width = 1, color="grey" );
   m1.plot(tropics.lon, tropics.cancer ; width = 1, color="grey" );
   m1.plot(tropics.lon, tropics.capricorn ; width = 1, color="grey" );
   m1.plot(tropics.lon, tropics.arctic_north ; width = 1, color="grey");
   m1.plot(tropics.lon, tropics.arctic_south ; width = 1, color="grey" );

   % draw the contour lines and their labels
   variable i,j;
   variable lx,mx,xar,yar,er,x0,y0,rot;
   _for i (0,length(cont)-1,1)
     {
        _for j (0,length(cont[i].x_list)-1,1)
          {
             cont_label(m1,cont[i].x_list[j]-180, cont[i].y_list[j]-90, lev[i]);
          }
     }

   m2.xaxis (;off);
   m2.yaxis (;off);
   m2.y2axis (;on);
   m2.x2axis (;off);
   m2.y2label(scaletitle);
   m2.plot_png("scale.png");

   % combine the map and the key to one image
   xfig_new_hbox_compound (m1, m2, 0.1).render(filename+".eps");
   % get rid of the redundant png file
   () = remove (filename + ".png");
}

% use the above function to plot as many SAA/magentic field maps as you wish:
% electron to proton ratio
draw_map_cont(ep, cont,levels, imgname, "electrons per proton",0.5,50.,"log");
% electrons
draw_map_cont(e, cont,levels, imgname_e, "electrons",0.05,4000.,"log");
%protons
draw_map_cont(p, cont,levels, imgname_p, "protons",0.05,4000.,"log");