encoding  utf-8
source Castleton_model.th

select Castleton_modelMP@castleton_model

export model -fmt survex -o outputs/castleton_modelth.3d

#export model -o outputs/castleton_level.lox

#export map -projection plan -fmt jepg -o outputs/castleton_model.jpg
#export model -fmt kml -o outputs/castleton_model.kml
#export map -proj plan -fmt kml -o outputs/castleton_model_plan.kml
#export map -projection plan -layout LayoutLandscapeA0 -o outputs/castleton_model.svg
#export map -projection plan -layout LayoutLandscapeA0 -o outputs/castleton_model.pdf
#export cave-list -location on -o Castletoncavesforinputintothconfig.html

layout LayoutLandscapeA0 
  #PAGE LAYOUT      
# page-setup <dimensions> <units> . set page dimensions in this order: paper-width,
#paper-height, page-width, page-height, left-margin and top-margin.
      
  page-setup 118.9 84.1 112.9 77.1 1.5 1.5 cm
  overlap 3 cm
  #size       110  75  cm
  size 105 80 cm ##105 80
  color map-fg altitude
base-scale 1 500
scale 1 1500
symbol-set BCRA
symbol-hide group cave-centreline
min-symbol-scale m #this hides all symbols smaller than m, therefore s and xs labels from above will be hidden
scale-bar 250 m
map-header 75 95 ne
map-header-bg on
##grid bottom ##AG hashed to try homemade grid
##grid-coords border ##AG hashed to try homemade grid
##grid-size 40 40 40 m ##AG hashed to try homemade grid
cs OSGB:SK
##grid-origin 14865.01 82575.16 193.75 m ##AG hashed to try homemade grid
doc-subject "Survey of Peak Speedwell System. 2024"
doc-author  "Wayne Sheldon, Alastair Gott"
#map-comment "Drawn by Wayne Sheldon and Alastair Gott. Copyright 2024 Technical Speleological Group"
map-comment "<bf>Drawn by Wayne Sheldon and Alastair Gott. Copyright 2024 Technical Speleological Group. <br>In Memory of the Late John S Beck.<rm><br><br>Surveyed By: Tim Allen, Steven Ancliffe, Dave Arveschoug, Nigel Ball<br>John S Beck, Allan Brentnall, Luke Brock, Simon Brooks, Mark Brown<br>David Botcherby, Luke Cafferty, John N Cordingley, Charley Cooley<br>John C Cooper, Paul Deakin, Katie Eavis, Robert Eavis, Alastair Gott<br>Adrian Gregory, John Gunn, Brian Hague, Olly Hall, John D Harrison<br>Tim Horne, Terry Jackson, Keith Joule (nee Bottoms), Jim Lister, Anthony Marsden<br>Louise McMahon, Mark McAuley, John Middlemiss, Rob Middleton, Ted Mullins<br>Dave A Nixon, Mark Noble, Pete O'Neill, Shaun L Puckering<br>John Randles, Alex Ried, Glyn Roberts, Roy Rodgers, Henry Rockcliffe<br>Luke Rowe, Matt Ryan, Leslie Salmon, Angus Sawyer, Richard Shaw, Wayne Sheldon<br>Dave Shearsmith, Robbie Shone, Guy Smith, Sam Stringer, Ann Soulsby<br>Joze Taylor, Laurence Thistlewood, Jeff Wade, Tim Webber, Clive Westlake<br>Bill Whitehouse, Edd Willatts, Phil Wolstenholme<br><br>Please note, length includes all caves and mines shown
#doc-keywords "Peak Speedwell System"

code metapost
### change the Grid defined by AG as Line:User:Grid, in Excelgrid.th to be a dotted line by robbing code from the metapost for map-connection and amending it to make the arrowhead small/insignificant to the naked eye and the dots spaced further apart
initsymbol("l_u_grid");
def l_u_grid (expr P) =
  thdrawoptions(dashed evenly scaled (20 * optical_zoom) withpen PenA);
  l_arrow_SKBB(P,1);
  thdrawoptions();
enddef;

### define compass rose to be shaded compass rose as per Juraj Halama, https://therion.speleo.sk/wiki/metapost#north_arrows
def s_northarrow (expr rot) =
  scale_value = 0.5;
  decl := MagDecl; 
  T := identity;

  picture tmp_pic;
  tmp_pic = image (
      pickup pencircle scaled 1;
      thdraw fullcircle scaled 4cm;
      thdraw fullcircle scaled 2.75cm;
      pickup pencircle scaled .5;
      thdraw fullcircle scaled 3.75cm;
      for whereto=11.25 step 22.5 until 360:
        thdraw dir(whereto)*2.75/2cm--dir(whereto)*3.75/2cm;
      endfor;
% arrows
      path halfarrow;
      halfarrow = (+.3cm,.3cm)--(0,2cm)--(0,0)--cycle;
      for whereto = 45 step 90 until 315:
%          thfill halfarrow scaled .85 rotated whereto withcolor .30;
        pickup pencircle scaled 1;
        for halfarrowgrad = 1 step -.05 until 0:
          thdraw ((halfarrowgrad*.3cm, halfarrowgrad*.3cm)--(0,2cm)) scaled .85 rotatedaround ((0, 0),whereto) withcolor .75halfarrowgrad;
        endfor;  
        pickup pencircle scaled .5;
        thdraw halfarrow scaled .85 rotated whereto;;
        thfill halfarrow xscaled -1 scaled .85 rotated whereto withcolor white;
        thdraw halfarrow xscaled -1 scaled .85 rotated whereto;;
      endfor;  
      for whereto = 0 step 90 until 270:
%          thfill halfarrow scaled 1.15 rotated whereto withcolor .30;
        pickup pencircle scaled 1;
        for halfarrowgrad = 1 step -.05 until 0:
          thdraw ((halfarrowgrad*.3cm, halfarrowgrad*.3cm)--(0,2cm)) scaled 1.15 rotatedaround ((0, 0),whereto) withcolor .75halfarrowgrad;
        endfor;  
        pickup pencircle scaled .5;
        thdraw halfarrow scaled 1.15 rotated whereto;;
        thfill halfarrow xscaled -1 scaled 1.15 rotated whereto withcolor white;
        thdraw halfarrow xscaled -1 scaled 1.15 rotated whereto;;
      endfor;  
% central circles
      thfill fullcircle scaled .56cm withcolor 1white;
      pickup pencircle scaled .5;
      thdraw fullcircle scaled .56cm;
      thfill fullcircle scaled .3cm withcolor .30;
% characters
      label.bot(thTEX("\bf{}N") scaled 1.5, (0,2.9cm));
      label.lft(thTEX("\bf{}E") scaled 1.5, (2.9cm,0));
      label.rt (thTEX("\bf{}W") scaled 1.5, (-2.95cm,0));
      label.top(thTEX("\bf{}S") scaled 1.5, (0,-2.9cm));
% space among characters
      pickup pencircle scaled .5;
      thdraw (dir(45)*2cm)--(dir(45)*2.5cm);
      thdraw (dir(135)*2cm)--(dir(135)*2.5cm);
      thdraw (dir(225)*2cm)--(dir(225)*2.5cm);
      thdraw (dir(315)*2cm)--(dir(315)*2.5cm);
  );
  
  thdraw tmp_pic scaled scale_value rotatedaround (origin, - rot);
enddef;

###define scalebar to be scalebar 2b as per B. Hallinger from https://therion.speleo.sk/wiki/metapost#north_arrows
  def s_scalebar (expr l, units, txt) =
  % l = value of scale-bar length
  % units = ??
  % txt = string representing units
    begingroup
      interim warningcheck:=0;
      tmpl:=l / Scale * cm * units / 2;
      % tmpl = half plotted length of scale bar from central top insertion point  
      tmpx:=l / Scale * cm * units / 5;
      tmph:=5bp; % bar height
    endgroup;
    pickup PenC;
    draw (-tmpl,0)--(tmpl,0)--(tmpl,-tmph)--(-tmpl,-tmph)--cycle;
    p:=(0,0)--(tmpx,0)--(tmpx,-tmph)--(0,-tmph)--cycle;
    for i:=-0.5 step 2 until 2:   % start drawing at the third block (leave space for smaller divisions)
      fill p shifted (i * tmpx,0);
    endfor;
    
    % Draw first part with subdivided blocks
    p:=(0,0)--(tmpx/5,0)--(tmpx/5,-tmph)--(0,-tmph)--cycle;  % define width of segment (tmpx is length of a normal bar segment)
    for i:=-2.5 step 2/5 until -0.75:                        % Startpos, segments, count-index
      fill p shifted (i * tmpx,0) withcolor black;
    endfor;
    
    % Label of scale: Scalebar top, values below
    begingroup
      interim labeloffset:=3.5bp;
      for i:=0 step (l/5) until (l-1):
        tmpx:=tmpl * (i * 2 / l - 1);
        label.bot(thTEX(decimal (i)),(tmpx,-tmph));
      endfor;
      label.bot(thTEX(decimal (l) & "\thinspace" & txt),(tmpl,-tmph));
      label.top(thTEX("Scale 1 : " & decimal round(Scale*100)),(0,0));
    endgroup;
    
  enddef;

### define water to be light blue
 def a_water (expr p) =
 T:=identity;
 thfill p withcolor (0.48, 0.84, 1.0);
 enddef;
 
 ### define sump to be dark blue
 def a_sump (expr p) =
 T:=identity;
 thfill p withcolor (0.05, 0.0, 0.7);
 enddef;
 endcode
 
  code metapost
  fonts_setup(12,16,18,24, 28); 
  #font size 0.01 is invisible, but text still adds kilobytes to file size  
  #these fonts look OK scaled half size,but 8,9 a bit small 
    
code tex-map
\legendcontent={%
  \hsize=\legendwidth
  \ifnortharrow\vbox to 0pt{\line{\hfil\northarrow}\vss}\fi
  \edef\tmp{\the\cavename} \ifx\tmp\empty \else
    {\size[26]\the\cavename} \vskip1cm
  \fi
  \ifscalebar\scalebar\vskip1cm\fi
  {\rightskip=0pt plus 3em\parskip=3bp
    \edef\tmp{\the\comment} \ifx\tmp\empty \else
    {\size[12]\the\comment} \par\medskip
    \fi
    \everypar{\hangindent=2em\hangafter=1}
    \edef\tmp{\the\cavelength} \ifx\tmp\empty \else
      {\size[12]\si\the\cavelengthtitle: \ss\the\cavelength\par}
    \fi
    \edef\tmp{\the\cavedepth} \ifx\tmp\empty \else
      {\size[12]\si\the\cavedepthtitle: \ss\the\cavedepth\par}
    \fi
    \edef\tmp{\the\exploteam} \ifx\tmp\empty \else
      {\size[12]\si\the\explotitle:
\ss\the\exploteam\quad\si\the\explodate\par}
    \fi
    \edef\tmp{\the\topoteam} \ifx\tmp\empty \else
      {\size[12]\si\the\topotitle: \ss\the\topoteam\quad\si\the\topodate\par}
    \fi
    \edef\tmp{\the\cartoteam} \ifx\tmp\empty \else
      {\size[12]\si\the\cartotitle:
\ss\the\cartoteam\quad\si\the\cartodate\par}
    \fi
    \edef\tmp{\the\copyrights} \ifx\tmp\empty \else
      {\size[12]\ss\the\copyrights\par}
    \fi
  }
  \formattedlegend
}
  
endlayout LayoutLandscapeA0

  layout custom_grid
##change default cross hair grid to grid
    code metapost
      def s_hgrid (expr xpos, ypos, xsize, ysize) =
        pickup PenD;
	if ( xpos >=  -xsize ) and ( ypos >= -ysize ):
          draw ( -xsize/2, 0) -- ( xsize/2, 0);
	  draw ( 0, -ysize/2) -- ( 0, ysize/2);
	fi
      enddef;
    endcode
  endlayout